UNPKG

@deno/kv

Version:

A Deno KV client library optimized for Node.js.

75 lines (74 loc) 2.21 kB
"use strict"; // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. Object.defineProperty(exports, "__esModule", { value: true }); exports.BinarySearchNode = void 0; 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; } } exports.BinarySearchNode = BinarySearchNode;