deno.land / std@0.224.0 / data_structures / _binary_search_node.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
export type Direction = "left" | "right";
export class BinarySearchNode<T> { left: BinarySearchNode<T> | null; right: BinarySearchNode<T> | null; constructor(public parent: BinarySearchNode<T> | null, public value: T) { this.left = null; this.right = null; }
static from<T>(node: BinarySearchNode<T>): BinarySearchNode<T> { const copy: BinarySearchNode<T> = new BinarySearchNode( node.parent, node.value, ); copy.left = node.left; copy.right = node.right; return copy; }
directionFromParent(): Direction | null { return this.parent === null ? null : this === this.parent.left ? "left" : this === this.parent.right ? "right" : null; }
findMinNode(): BinarySearchNode<T> { let minNode: BinarySearchNode<T> | null = this.left; while (minNode?.left) minNode = minNode.left; return minNode ?? this; }
findMaxNode(): BinarySearchNode<T> { let maxNode: BinarySearchNode<T> | null = this.right; while (maxNode?.right) maxNode = maxNode.right; return maxNode ?? this; }
findSuccessorNode(): BinarySearchNode<T> | null { if (this.right !== null) return this.right.findMinNode(); let parent: BinarySearchNode<T> | null = this.parent; let direction: Direction | null = this.directionFromParent(); while (parent && direction === "right") { direction = parent.directionFromParent(); parent = parent.parent; } return parent; }}
Version Info