UNPKG

dastal

Version:

Data Structures & Algorithms implementations

96 lines (95 loc) 3.39 kB
import { CompareFn } from 'src'; import { AVLTreeNode } from './avlTreeNode'; import { Edge } from './binaryTreeUtils'; import { SortedTree } from './sortedTree'; import { LinkedNode } from 'src/list'; /** * An AVL tree is a self-balancing binary search tree ([source](https://en.wikipedia.org/wiki/AVL_tree)). * * It is named after inventors Georgy Adelson-Velsky and Evgenii Landis and was the first such * data structure to be invented. In an AVL tree, the heights of the two child * subtrees of any node differ by at most one; if at any time they differ by more * than one, rebalancing is done to restore this property. * * Lookup, insertion, and deletion all take O(log(n)) time in both the average and worst cases, * where n is the number of nodes in the tree prior to the operation. Insertions and deletions * may require the tree to be rebalanced by one or more tree rotations. * * AVL trees are often compared with red–black trees as both take O(log(n)) * time for the basic operations. For lookup-intensive applications, AVL trees are * faster than red–black trees because they are more strictly balanced. * Similar to red–black trees, AVL trees are height-balanced. */ export declare class AVLTree<T> implements SortedTree<T> { /** * The function to determine the order of elements. */ protected compare: CompareFn<T>; /** * Indicates how to handle duplicates: * - < 0 : Add to left subtree * - = 0 : Do now allow duplicates * - > 0 : Add to right subtree */ protected dupeWeight: number; /** * The number of elements in the list. */ protected length: number; /** * The root of the tree. */ protected root: AVLTreeNode<T>; /** * Instantiate a tree. * * @param compareFn - The function to determine the order of elements. * @param elements - A set of elements to initialize the tree with. */ constructor(compareFn: CompareFn<T>, elements?: Iterable<T>); /** * Instantiate a tree. * * @param compareFn - The function to determine the order of elements. * @param allowDuplicates - Whether to allow duplicates * @param elements - A set of elements to initialize the tree with. */ constructor(compareFn: CompareFn<T>, allowDuplicates: boolean, elements?: Iterable<T>); add(element: T): this; clear(): void; comparator(): CompareFn<T>; delete(element: T): boolean; has(element: T): boolean; max(): T | undefined; min(): T | undefined; pop(): T | undefined; shift(): T | undefined; get size(): number; sorted(): Iterable<T>; /** * Receive an iterator through the list. * * **Note:** Unexpected behavior can occur if the collection is modified during iteration. * * @returns An iterator through the list */ [Symbol.iterator](): Iterator<T>; update(curElement: T, newElement: T): boolean; protected build(obj: Iterable<T>): void; } /** * @internal */ export declare function balance<T>(node: AVLTreeNode<T>): AVLTreeNode<T>; /** * @internal */ export declare function remove<T>(stack: LinkedNode<Edge<AVLTreeNode<T>>>): boolean; /** * @internal */ export declare function rotateL<T>(P: AVLTreeNode<T>): AVLTreeNode<T>; /** * @internal */ export declare function rotateR<T>(P: AVLTreeNode<T>): AVLTreeNode<T>;