UNPKG

@deno/kv

Version:

A Deno KV client library optimized for Node.js.

71 lines (70 loc) 2.06 kB
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. export class BinarySearchNode { constructor(parent, value) { Object.defineProperty(this, "parent", { enumerable: true, configurable: true, writable: true, value: parent }); Object.defineProperty(this, "value", { enumerable: true, configurable: true, writable: true, value: value }); Object.defineProperty(this, "left", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "right", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.left = null; this.right = null; } static from(node) { const copy = new BinarySearchNode(node.parent, node.value); copy.left = node.left; copy.right = node.right; return copy; } directionFromParent() { return this.parent === null ? null : this === this.parent.left ? "left" : this === this.parent.right ? "right" : null; } findMinNode() { let minNode = this.left; while (minNode?.left) minNode = minNode.left; return minNode ?? this; } findMaxNode() { let maxNode = this.right; while (maxNode?.right) maxNode = maxNode.right; return maxNode ?? this; } findSuccessorNode() { if (this.right !== null) return this.right.findMinNode(); let parent = this.parent; let direction = this.directionFromParent(); while (parent && direction === "right") { direction = parent.directionFromParent(); parent = parent.parent; } return parent; } }