The type of the key.
The type of the value.
The type of the raw data object (if using toEntryFn).
Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.@example // Find elements in a range // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding. type Datum = { timestamp: Date; temperature: number }; // Fixed dataset of CPU temperature readings const cpuData: Datum[] = [ { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 }, { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 }, { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 }, { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 }, { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 }, { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 }, { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 }, { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 }, { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 }, { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 }, { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 }, { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 }, { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 }, { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 }, { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 }, { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 }, { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 }, { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 }, { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 }, { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 }, { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 }, { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 }, { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 }, { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 }, { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 }, { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 }, { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 }, { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 }, { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 }, { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 } ];
// Create an AVL tree to store CPU temperature data const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, { toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature] });
// Query a specific time range (e.g., from 00:05 to 00:15) const rangeStart = new Date('2024-12-02T00:05:00'); const rangeEnd = new Date('2024-12-02T00:15:00'); const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({ minute: node ? node.key.getMinutes() : 0, temperature: cpuTemperatureTree.get(node ? node.key : undefined) }));
console.log(rangeResults); // [ // { minute: 5, temperature: 59.4 }, // { minute: 6, temperature: 60.1 }, // { minute: 7, temperature: 61.3 }, // { minute: 8, temperature: 62 }, // { minute: 9, temperature: 63.5 }, // { minute: 10, temperature: 64 }, // { minute: 11, temperature: 62.8 }, // { minute: 12, temperature: 61.5 }, // { minute: 13, temperature: 60.2 }, // { minute: 14, temperature: 59.8 }, // { minute: 15, temperature: 58.6 } // ]
Gets whether the tree allows duplicate keys.
True if duplicates are allowed, false otherwise.
Gets whether the tree is in Map mode.
True if in Map mode, false otherwise.
Gets whether the tree's comparison logic is reversed.
True if the tree is reversed (e.g., a max-heap logic).
Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
The NIL node.
Gets the number of nodes in the tree.
The size of the tree.
Protected_balance(Protected) Calculates the balance factor (height(right) - height(left)).
The node to check.
The balance factor (positive if right-heavy, negative if left-heavy).
Protected_balanceLL(Protected) Performs a Left-Left (LL) rotation (a single right rotation).
The unbalanced node (root of the unbalanced subtree).
Protected_balanceLR(Protected) Performs a Left-Right (LR) double rotation.
The unbalanced node (root of the unbalanced subtree).
Protected_balanceProtected_balanceRL(Protected) Performs a Right-Left (RL) double rotation.
The unbalanced node (root of the unbalanced subtree).
Protected_balanceRR(Protected) Performs a Right-Right (RR) rotation (a single left rotation).
The unbalanced node (root of the unbalanced subtree).
Protected_clearProtected_clearProtected_clone(Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
The new, empty tree instance to populate.
Protected_compareProtected_createProtected_create(Protected) Creates a new AVL tree node.
The newly created AVLTreeNode.
Protected_DEFAULT_(Protected) Default callback function, returns the node's key.
The node.
The node's key or undefined.
Protected_dfsCallback type.
Function to call on nodes.
Optionalpattern: DFSOrderPatternTraversal order.
OptionalonlyOne: booleanStop after first match.
OptionalstartNode: Starting node.
OptionaliterationType: IterationTypeTraversal method.
OptionalincludeNull: booleanInclude nulls.
OptionalshouldVisitLeft: ((node: undefined | null | BinaryTreeNode<K, V>) => boolean)Predicate to traverse left.
OptionalshouldVisitRight: ((node: undefined | null | BinaryTreeNode<K, V>) => boolean)Predicate to traverse right.
OptionalshouldVisitRoot: ((node: undefined | null | BinaryTreeNode<K, V>) => boolean)Predicate to visit root.
OptionalshouldProcessRoot: ((node: undefined | null | BinaryTreeNode<K, V>) => boolean)Predicate to process root.
Array of callback results.
Protected_display(Protected) Recursive helper for toVisual.
The current node.
Print options.
Layout information for this subtree.
Protected_ensure(Protected) Converts a key, node, entry, or predicate into a standardized predicate function.
The item to convert.
A predicate function.
Protected_extractProtected_get(Protected) Gets the iterator for the tree (default in-order).
Optionalnode: null | BinaryTreeNode<K, V> = ...The node to start iteration from.
An iterator for [key, value] pairs.
Protected_is(Protected) Checks if an item is a predicate function.
The item to check.
True if it's a function.
Protected_keyProtected_replace(Protected) Replaces a node, ensuring height is copied.
The node to be replaced.
The node to insert.
The newNode.
Protected_setProtected_setProtected_snapshotProtected_swap(Protected) Swaps properties of two nodes, including height.
The source node.
The destination node.
The destNode (now holding srcNode's properties).
Protected_update(Protected) Recalculates and updates the height of a node based on its children's heights.
The node to update.
Adds multiple items to the tree.
An iterable of items to add.
Optionalvalues: Iterable<undefined | V, any, any>An optional parallel iterable of values.
OptionalisBalanceAdd: boolean = trueIf true, builds a balanced tree from the items.
OptionaliterationType: IterationType = ...The traversal method for balanced add (recursive or iterative).
An array of booleans indicating the success of each individual add operation.
Deletes a node from the AVL tree and re-balances the tree.
An array containing deletion results.
Performs a Depth-First Search (DFS) traversal.
Optionalcallback: C = ...Function to call on each node.
Optionalpattern: DFSOrderPattern = 'IN'The traversal order ('IN', 'PRE', 'POST').
OptionalonlyOne: boolean = falseIf true, stops after the first callback.
OptionalstartNode: The node to start from.
OptionaliterationType: IterationType = ...The traversal method.
An array of callback results.
Gets the value associated with a key.
The key, node, or entry to get the value for.
OptionalstartNode: The node to start searching from (if not in Map mode).
OptionaliterationType: IterationType = ...The traversal method (if not in Map mode).
The associated value, or undefined.
Finds the leftmost node in a subtree (the node with the smallest key in a BST).
The type of the callback function.
The callback result for the leftmost node.
Gets the first node matching a predicate.
The key, node, entry, or predicate function to search for.
OptionalstartNode: BSTNOptKeyOrNode<K, BSTNode<K, V>> = ...The node to start the search from.
OptionaliterationType: IterationType = ...The traversal method.
The first matching node, or undefined if not found.
Gets all nodes matching a predicate.
The key, node, entry, or predicate function to search for.
OptionalonlyOne: booleanIf true, stops after finding the first match.
OptionalstartNode: The node to start the search from.
OptionaliterationType: IterationTypeThe traversal method.
An array of matching nodes.
Gets the Morris traversal predecessor (rightmost node in the left subtree, or node itself).
The node to find the predecessor for.
The Morris predecessor.
Finds the rightmost node in a subtree (the node with the largest key in a BST).
The type of the callback function.
The callback result for the rightmost node.
Gets the in-order successor of a node in a BST.
Optionalx: null | K | BinaryTreeNode<K, V>The node to find the successor of.
The successor node, or null/undefined if none exists.
Checks if a node matching the predicate exists in the tree.
OptionalkeyNodeEntryOrPredicate: The key, node, entry, or predicate to check for.
OptionalstartNode: The node to start the search from.
OptionaliterationType: IterationTypeThe traversal method.
True if a matching node exists, false otherwise.
Whether there exists an entry with the given value.
Value to test.
true if found; otherwise false.
Checks if the given item is an AVLTreeNode instance.
True if it's an AVLTreeNode, false otherwise.
Checks if the given item is a Range object.
The item to check.
True if it's a Range, false otherwise.
Checks if the given item is a "real" node (i.e., not null, undefined, or NIL).
True if it's a real node, false otherwise.
Checks if the given item is either a "real" node or null.
True if it's a real node or null, false otherwise.
Traverses the tree and returns nodes that are lesser or greater than a target node.
Optionalcallback: C = ...Function to call on matching nodes.
OptionallesserOrGreater: CP = -11 for lesser, 1 for greater, 0 for equal.
OptionaltargetNode: The node to compare against.
OptionaliterationType: IterationType = ...The traversal method.
An array of callback results.
Merges another tree into this one by adding all its nodes.
The tree to merge.
Performs an optimized search for nodes within a given key range.
A Range object or a [low, high] tuple.
Optionalcallback: C = ...A function to call on matching nodes.
OptionalstartNode: The node to start the search from.
OptionaliterationType: IterationType = ...The traversal method.
An array of callback results.
Searches the tree for nodes matching a predicate, key, or range.
The key, node, entry, predicate, or range to search for.
OptionalonlyOne: boolean = falseIf true, stops after finding the first match.
Optionalcallback: C = ...A function to call on matching nodes.
OptionalstartNode: The node to start the search from.
OptionaliterationType: IterationType = ...Whether to use 'RECURSIVE' or 'ITERATIVE' search.
An array of results from the callback function for each matching node.
Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree. This tree extends BST and performs rotations on add/delete to maintain balance.