avl-tree-typed 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +3859 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +3852 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +15 -3
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/avl-tree-typed.js +3852 -0
- package/dist/umd/avl-tree-typed.js.map +1 -0
- package/dist/umd/avl-tree-typed.min.js +13 -0
- package/dist/umd/avl-tree-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -15
- package/dist/index.js +0 -31
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -23
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -1,12 +1,413 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { BinaryTreeOptions, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types';
|
|
9
|
+
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
import { Range } from '../../common';
|
|
12
|
+
/**
|
|
13
|
+
* Represents a Node in a Binary Search Tree.
|
|
14
|
+
*
|
|
15
|
+
* @template K - The type of the key.
|
|
16
|
+
* @template V - The type of the value.
|
|
17
|
+
*/
|
|
18
|
+
export declare class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
19
|
+
parent?: BSTNode<K, V>;
|
|
20
|
+
/**
|
|
21
|
+
* Creates an instance of BSTNode.
|
|
22
|
+
* @remarks Time O(1), Space O(1)
|
|
23
|
+
*
|
|
24
|
+
* @param key - The key of the node.
|
|
25
|
+
* @param [value] - The value associated with the key.
|
|
26
|
+
*/
|
|
27
|
+
constructor(key: K, value?: V);
|
|
28
|
+
_left?: BSTNode<K, V> | null | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Gets the left child of the node.
|
|
31
|
+
* @remarks Time O(1), Space O(1)
|
|
32
|
+
*
|
|
33
|
+
* @returns The left child.
|
|
34
|
+
*/
|
|
35
|
+
get left(): BSTNode<K, V> | null | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Sets the left child of the node and updates its parent reference.
|
|
38
|
+
* @remarks Time O(1), Space O(1)
|
|
39
|
+
*
|
|
40
|
+
* @param v - The node to set as the left child.
|
|
41
|
+
*/
|
|
42
|
+
set left(v: BSTNode<K, V> | null | undefined);
|
|
43
|
+
_right?: BSTNode<K, V> | null | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Gets the right child of the node.
|
|
46
|
+
* @remarks Time O(1), Space O(1)
|
|
47
|
+
*
|
|
48
|
+
* @returns The right child.
|
|
49
|
+
*/
|
|
50
|
+
get right(): BSTNode<K, V> | null | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Sets the right child of the node and updates its parent reference.
|
|
53
|
+
* @remarks Time O(1), Space O(1)
|
|
54
|
+
*
|
|
55
|
+
* @param v - The node to set as the right child.
|
|
56
|
+
*/
|
|
57
|
+
set right(v: BSTNode<K, V> | null | undefined);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Represents a Binary Search Tree (BST).
|
|
61
|
+
* Keys are ordered, allowing for faster search operations compared to a standard Binary Tree.
|
|
62
|
+
* @template K - The type of the key.
|
|
63
|
+
* @template V - The type of the value.
|
|
64
|
+
* @template R - The type of the raw data object (if using `toEntryFn`).
|
|
65
|
+
*
|
|
66
|
+
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
67
|
+
* 2. Unique Keys: No duplicate keys in a standard BST.
|
|
68
|
+
* 3. Efficient Search: Enables quick search, minimum, and maximum operations.
|
|
69
|
+
* 4. Inorder Traversal: Yields nodes in ascending order.
|
|
70
|
+
* 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
|
|
71
|
+
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
72
|
+
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
73
|
+
* @example
|
|
74
|
+
* // Merge 3 sorted datasets
|
|
75
|
+
* const dataset1 = new BST<number, string>([
|
|
76
|
+
* [1, 'A'],
|
|
77
|
+
* [7, 'G']
|
|
78
|
+
* ]);
|
|
79
|
+
* const dataset2 = [
|
|
80
|
+
* [2, 'B'],
|
|
81
|
+
* [6, 'F']
|
|
82
|
+
* ];
|
|
83
|
+
* const dataset3 = new BST<number, string>([
|
|
84
|
+
* [3, 'C'],
|
|
85
|
+
* [5, 'E'],
|
|
86
|
+
* [4, 'D']
|
|
87
|
+
* ]);
|
|
88
|
+
*
|
|
89
|
+
* // Merge datasets into a single BinarySearchTree
|
|
90
|
+
* const merged = new BST<number, string>(dataset1);
|
|
91
|
+
* merged.addMany(dataset2);
|
|
92
|
+
* merged.merge(dataset3);
|
|
93
|
+
*
|
|
94
|
+
* // Verify merged dataset is in sorted order
|
|
95
|
+
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
|
96
|
+
* @example
|
|
97
|
+
* // Find elements in a range
|
|
98
|
+
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
99
|
+
* console.log(bst.search(new Range(5, 10))); // [5, 7, 10]
|
|
100
|
+
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['5', '7', '10', '12']
|
|
101
|
+
* console.log(bst.search(new Range(4, 12, true, false))); // [5, 7, 10]
|
|
102
|
+
* console.log(bst.rangeSearch([15, 20])); // [15, 18]
|
|
103
|
+
* console.log(bst.search(new Range(15, 20, false))); // [18]
|
|
104
|
+
* @example
|
|
105
|
+
* // Find lowest common ancestor
|
|
106
|
+
* const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
107
|
+
*
|
|
108
|
+
* // LCA helper function
|
|
109
|
+
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
110
|
+
* const path1 = bst.getPathToRoot(num1);
|
|
111
|
+
* const path2 = bst.getPathToRoot(num2);
|
|
112
|
+
* // Find the first common ancestor
|
|
113
|
+
* return findFirstCommon(path1, path2);
|
|
114
|
+
* };
|
|
115
|
+
*
|
|
116
|
+
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
|
|
117
|
+
* for (const num of arr1) {
|
|
118
|
+
* if (arr2.indexOf(num) !== -1) {
|
|
119
|
+
* return num;
|
|
120
|
+
* }
|
|
121
|
+
* }
|
|
122
|
+
* return undefined;
|
|
123
|
+
* }
|
|
124
|
+
*
|
|
125
|
+
* // Assertions
|
|
126
|
+
* console.log(findLCA(3, 10)); // 7
|
|
127
|
+
* console.log(findLCA(5, 35)); // 15
|
|
128
|
+
* console.log(findLCA(20, 30)); // 25
|
|
129
|
+
*/
|
|
130
|
+
export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
131
|
+
/**
|
|
132
|
+
* Creates an instance of BST.
|
|
133
|
+
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
134
|
+
*
|
|
135
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
|
|
136
|
+
* @param [options] - Configuration options for the BST, including comparator.
|
|
137
|
+
*/
|
|
138
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>);
|
|
139
|
+
protected _root?: BSTNode<K, V>;
|
|
140
|
+
/**
|
|
141
|
+
* Gets the root node of the tree.
|
|
142
|
+
* @remarks Time O(1)
|
|
143
|
+
*
|
|
144
|
+
* @returns The root node.
|
|
145
|
+
*/
|
|
146
|
+
get root(): OptNode<BSTNode<K, V>>;
|
|
147
|
+
protected _isReverse: boolean;
|
|
148
|
+
/**
|
|
149
|
+
* Gets whether the tree's comparison logic is reversed.
|
|
150
|
+
* @remarks Time O(1)
|
|
151
|
+
*
|
|
152
|
+
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
153
|
+
*/
|
|
154
|
+
get isReverse(): boolean;
|
|
155
|
+
/**
|
|
156
|
+
* The default comparator function.
|
|
157
|
+
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
158
|
+
*/
|
|
159
|
+
protected _comparator: Comparator<K>;
|
|
160
|
+
/**
|
|
161
|
+
* Gets the comparator function used by the tree.
|
|
162
|
+
* @remarks Time O(1)
|
|
163
|
+
*
|
|
164
|
+
* @returns The comparator function.
|
|
165
|
+
*/
|
|
166
|
+
get comparator(): Comparator<K>;
|
|
167
|
+
protected _specifyComparable?: (key: K) => Comparable;
|
|
168
|
+
/**
|
|
169
|
+
* Gets the function used to extract a comparable value from a complex key.
|
|
170
|
+
* @remarks Time O(1)
|
|
171
|
+
*
|
|
172
|
+
* @returns The key-to-comparable conversion function.
|
|
173
|
+
*/
|
|
174
|
+
get specifyComparable(): ((key: K) => Comparable) | undefined;
|
|
175
|
+
/**
|
|
176
|
+
* (Protected) Creates a new BST node.
|
|
177
|
+
* @remarks Time O(1), Space O(1)
|
|
178
|
+
*
|
|
179
|
+
* @param key - The key for the new node.
|
|
180
|
+
* @param [value] - The value for the new node (used if not in Map mode).
|
|
181
|
+
* @returns The newly created BSTNode.
|
|
182
|
+
*/
|
|
183
|
+
createNode(key: K, value?: V): BSTNode<K, V>;
|
|
184
|
+
/**
|
|
185
|
+
* Ensures the input is a node. If it's a key or entry, it searches for the node.
|
|
186
|
+
* @remarks Time O(log N) (height of the tree), O(N) worst-case.
|
|
187
|
+
*
|
|
188
|
+
* @param keyNodeOrEntry - The item to resolve to a node.
|
|
189
|
+
* @param [iterationType=this.iterationType] - The traversal method to use if searching.
|
|
190
|
+
* @returns The resolved node, or undefined if not found.
|
|
191
|
+
*/
|
|
192
|
+
ensureNode(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): OptNode<BSTNode<K, V>>;
|
|
193
|
+
/**
|
|
194
|
+
* Checks if the given item is a `BSTNode` instance.
|
|
195
|
+
* @remarks Time O(1), Space O(1)
|
|
196
|
+
*
|
|
197
|
+
* @param keyNodeOrEntry - The item to check.
|
|
198
|
+
* @returns True if it's a BSTNode, false otherwise.
|
|
199
|
+
*/
|
|
200
|
+
isNode(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BSTNode<K, V>;
|
|
201
|
+
/**
|
|
202
|
+
* Checks if the given key is valid (comparable).
|
|
203
|
+
* @remarks Time O(1)
|
|
204
|
+
*
|
|
205
|
+
* @param key - The key to validate.
|
|
206
|
+
* @returns True if the key is valid, false otherwise.
|
|
207
|
+
*/
|
|
208
|
+
isValidKey(key: any): key is K;
|
|
209
|
+
/**
|
|
210
|
+
* Performs a Depth-First Search (DFS) traversal.
|
|
211
|
+
* @remarks Time O(N), visits every node. Space O(log N) for the call/explicit stack. O(N) worst-case.
|
|
212
|
+
*
|
|
213
|
+
* @template C - The type of the callback function.
|
|
214
|
+
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
215
|
+
* @param [pattern='IN'] - The traversal order ('IN', 'PRE', 'POST').
|
|
216
|
+
* @param [onlyOne=false] - If true, stops after the first callback.
|
|
217
|
+
* @param [startNode=this._root] - The node to start from.
|
|
218
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
219
|
+
* @returns An array of callback results.
|
|
220
|
+
*/
|
|
221
|
+
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
222
|
+
/**
|
|
223
|
+
* Performs a Breadth-First Search (BFS) or Level-Order traversal.
|
|
224
|
+
* @remarks Time O(N), visits every node. Space O(N) in the worst case for the queue.
|
|
225
|
+
*
|
|
226
|
+
* @template C - The type of the callback function.
|
|
227
|
+
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
228
|
+
* @param [startNode=this._root] - The node to start from.
|
|
229
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
230
|
+
* @returns An array of callback results.
|
|
231
|
+
*/
|
|
232
|
+
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
233
|
+
/**
|
|
234
|
+
* Returns a 2D array of nodes, grouped by level.
|
|
235
|
+
* @remarks Time O(N), visits every node. Space O(N) for the result array and the queue/stack.
|
|
236
|
+
*
|
|
237
|
+
* @template C - The type of the callback function.
|
|
238
|
+
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
239
|
+
* @param [startNode=this._root] - The node to start from.
|
|
240
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
241
|
+
* @returns A 2D array of callback results.
|
|
242
|
+
*/
|
|
243
|
+
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[][];
|
|
244
|
+
/**
|
|
245
|
+
* Gets the first node matching a predicate.
|
|
246
|
+
* @remarks Time O(log N) if searching by key, O(N) if searching by predicate. Space O(log N) or O(N).
|
|
247
|
+
*
|
|
248
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
249
|
+
* @param [startNode=this._root] - The node to start the search from.
|
|
250
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
251
|
+
* @returns The first matching node, or undefined if not found.
|
|
252
|
+
*/
|
|
253
|
+
getNode(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, startNode?: BSTNOptKeyOrNode<K, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>;
|
|
254
|
+
/**
|
|
255
|
+
* Searches the tree for nodes matching a predicate, key, or range.
|
|
256
|
+
* @remarks This is an optimized search for a BST. If searching by key or range, it prunes branches.
|
|
257
|
+
* Time O(H + M) for key/range search (H=height, M=matches). O(N) for predicate search.
|
|
258
|
+
* Space O(log N) for the stack.
|
|
259
|
+
*
|
|
260
|
+
* @template C - The type of the callback function.
|
|
261
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, predicate, or range to search for.
|
|
262
|
+
* @param [onlyOne=false] - If true, stops after finding the first match.
|
|
263
|
+
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
264
|
+
* @param [startNode=this._root] - The node to start the search from.
|
|
265
|
+
* @param [iterationType=this.iterationType] - Whether to use 'RECURSIVE' or 'ITERATIVE' search.
|
|
266
|
+
* @returns An array of results from the callback function for each matching node.
|
|
267
|
+
*/
|
|
268
|
+
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
269
|
+
/**
|
|
270
|
+
* Performs an optimized search for nodes within a given key range.
|
|
271
|
+
* @remarks Time O(H + M), where H is tree height and M is the number of matches.
|
|
272
|
+
*
|
|
273
|
+
* @template C - The type of the callback function.
|
|
274
|
+
* @param range - A `Range` object or a `[low, high]` tuple.
|
|
275
|
+
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
276
|
+
* @param [startNode=this._root] - The node to start the search from.
|
|
277
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
278
|
+
* @returns An array of callback results.
|
|
279
|
+
*/
|
|
280
|
+
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
281
|
+
/**
|
|
282
|
+
* Adds a new node to the BST based on key comparison.
|
|
283
|
+
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
284
|
+
*
|
|
285
|
+
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
286
|
+
* @param [value] - The value, if providing just a key.
|
|
287
|
+
* @returns True if the addition was successful, false otherwise.
|
|
288
|
+
*/
|
|
289
|
+
add(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
290
|
+
/**
|
|
291
|
+
* Adds multiple items to the tree.
|
|
292
|
+
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced add).
|
|
293
|
+
* If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
|
|
294
|
+
* Space O(N) for sorting and recursion/iteration stack.
|
|
295
|
+
*
|
|
296
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add.
|
|
297
|
+
* @param [values] - An optional parallel iterable of values.
|
|
298
|
+
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
299
|
+
* @param [iterationType=this.iterationType] - The traversal method for balanced add (recursive or iterative).
|
|
300
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
301
|
+
*/
|
|
302
|
+
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
303
|
+
/**
|
|
304
|
+
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
305
|
+
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
306
|
+
*
|
|
307
|
+
* @template C - The type of the callback function.
|
|
308
|
+
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on matching nodes.
|
|
309
|
+
* @param [lesserOrGreater=-1] - -1 for lesser, 1 for greater, 0 for equal.
|
|
310
|
+
* @param [targetNode=this._root] - The node to compare against.
|
|
311
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
312
|
+
* @returns An array of callback results.
|
|
313
|
+
*/
|
|
314
|
+
lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, lesserOrGreater?: CP, targetNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
315
|
+
/**
|
|
316
|
+
* Rebuilds the tree to be perfectly balanced.
|
|
317
|
+
* @remarks Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
318
|
+
*
|
|
319
|
+
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
320
|
+
* @returns True if successful, false if the tree was empty.
|
|
321
|
+
*/
|
|
322
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
323
|
+
/**
|
|
324
|
+
* Checks if the tree meets the AVL balance condition (height difference <= 1).
|
|
325
|
+
* @remarks Time O(N), as it must visit every node to compute height. Space O(log N) for recursion or O(N) for iterative map.
|
|
326
|
+
*
|
|
327
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
328
|
+
* @returns True if the tree is AVL balanced, false otherwise.
|
|
329
|
+
*/
|
|
330
|
+
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
331
|
+
/**
|
|
332
|
+
* Creates a new BST by mapping each [key, value] pair to a new entry.
|
|
333
|
+
* @remarks Time O(N * H), where N is nodes in this tree, and H is height of the new tree during insertion.
|
|
334
|
+
* Space O(N) for the new tree.
|
|
335
|
+
*
|
|
336
|
+
* @template MK - New key type.
|
|
337
|
+
* @template MV - New value type.
|
|
338
|
+
* @template MR - New raw type.
|
|
339
|
+
* @param callback - A function to map each [key, value] pair.
|
|
340
|
+
* @param [options] - Options for the new BST.
|
|
341
|
+
* @param [thisArg] - `this` context for the callback.
|
|
342
|
+
* @returns A new, mapped BST.
|
|
343
|
+
*/
|
|
344
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): BST<MK, MV, MR>;
|
|
345
|
+
/**
|
|
346
|
+
* Deletes the first node found that satisfies the predicate.
|
|
347
|
+
* @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
|
|
348
|
+
*
|
|
349
|
+
* @param predicate - A function to test each [key, value] pair.
|
|
350
|
+
* @returns True if a node was deleted, false otherwise.
|
|
351
|
+
*/
|
|
352
|
+
deleteWhere(predicate: (key: K, value: V | undefined, index: number, tree: this) => boolean): boolean;
|
|
353
|
+
/**
|
|
354
|
+
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
355
|
+
* @remarks Time O(1)
|
|
356
|
+
*
|
|
357
|
+
* @template TK, TV, TR - Generic types for the new instance.
|
|
358
|
+
* @param [options] - Options for the new BST.
|
|
359
|
+
* @returns A new, empty BST.
|
|
360
|
+
*/
|
|
361
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this;
|
|
362
|
+
/**
|
|
363
|
+
* (Protected) Creates a new instance of the same BST constructor, potentially with different generic types.
|
|
364
|
+
* @remarks Time O(N log N) or O(N^2) (from constructor) due to processing the iterable.
|
|
365
|
+
*
|
|
366
|
+
* @template TK, TV, TR - Generic types for the new instance.
|
|
367
|
+
* @param [iter=[]] - An iterable to populate the new BST.
|
|
368
|
+
* @param [options] - Options for the new BST.
|
|
369
|
+
* @returns A new BST.
|
|
370
|
+
*/
|
|
371
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<BSTOptions<TK, TV, TR>>): BST<TK, TV, TR>;
|
|
372
|
+
/**
|
|
373
|
+
* (Protected) Snapshots the current BST's configuration options.
|
|
374
|
+
* @remarks Time O(1)
|
|
375
|
+
*
|
|
376
|
+
* @template TK, TV, TR - Generic types for the options.
|
|
377
|
+
* @returns The options object.
|
|
378
|
+
*/
|
|
379
|
+
protected _snapshotOptions<TK = K, TV = V, TR = R>(): BSTOptions<TK, TV, TR>;
|
|
380
|
+
/**
|
|
381
|
+
* (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
382
|
+
* @remarks Time O(1)
|
|
383
|
+
*
|
|
384
|
+
* @param keyNodeOrEntry - The input item.
|
|
385
|
+
* @param [value] - An optional value (used if input is just a key).
|
|
386
|
+
* @returns A tuple of [node, value].
|
|
387
|
+
*/
|
|
388
|
+
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): [OptNode<BSTNode<K, V>>, V | undefined];
|
|
389
|
+
/**
|
|
390
|
+
* (Protected) Sets the root node and clears its parent reference.
|
|
391
|
+
* @remarks Time O(1)
|
|
392
|
+
*
|
|
393
|
+
* @param v - The node to set as root.
|
|
394
|
+
*/
|
|
395
|
+
protected _setRoot(v: OptNode<BSTNode<K, V>>): void;
|
|
396
|
+
/**
|
|
397
|
+
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
398
|
+
* @remarks Time O(1) (or O(C) if `specifyComparable` is used).
|
|
399
|
+
*
|
|
400
|
+
* @param a - The first key.
|
|
401
|
+
* @param b - The second key.
|
|
402
|
+
* @returns A number (1, -1, or 0) representing the comparison.
|
|
403
|
+
*/
|
|
404
|
+
protected _compare(a: K, b: K): number;
|
|
405
|
+
/**
|
|
406
|
+
* (Private) Deletes a node by its key.
|
|
407
|
+
* @remarks Standard BST deletion algorithm. Time O(log N), O(N) worst-case. Space O(1).
|
|
408
|
+
*
|
|
409
|
+
* @param key - The key of the node to delete.
|
|
410
|
+
* @returns True if the node was found and deleted, false otherwise.
|
|
411
|
+
*/
|
|
412
|
+
private _deleteByKey;
|
|
413
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export * from './binary-tree';
|
|
2
2
|
export * from './bst';
|
|
3
|
-
export * from './
|
|
3
|
+
export * from './binary-indexed-tree';
|
|
4
4
|
export * from './segment-tree';
|
|
5
|
-
export * from './avl-tree
|
|
5
|
+
export * from './avl-tree';
|
|
6
6
|
export * from './red-black-tree';
|
|
7
|
+
export * from './avl-tree-multi-map';
|
|
7
8
|
export * from './tree-multi-map';
|
|
8
9
|
export * from './tree-counter';
|
|
9
10
|
export * from './avl-tree-counter';
|
|
@@ -1,3 +1,208 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { BinaryTreeDeleteResult, BinaryTreeOptions, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
9
|
+
import { BST, BSTNode } from './bst';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
12
|
+
parent?: RedBlackTreeNode<K, V>;
|
|
13
|
+
/**
|
|
14
|
+
* Create a Red-Black Tree and optionally bulk-insert items.
|
|
15
|
+
* @remarks Time O(n log n), Space O(n)
|
|
16
|
+
* @param key - See parameter type for details.
|
|
17
|
+
* @param [value]- See parameter type for details.
|
|
18
|
+
* @param color - See parameter type for details.
|
|
19
|
+
* @returns New RedBlackTree instance.
|
|
20
|
+
*/
|
|
21
|
+
constructor(key: K, value?: V, color?: RBTNColor);
|
|
22
|
+
_left?: RedBlackTreeNode<K, V> | null | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Get the left child pointer.
|
|
25
|
+
* @remarks Time O(1), Space O(1)
|
|
26
|
+
* @returns Left child node, or null/undefined.
|
|
27
|
+
*/
|
|
28
|
+
get left(): RedBlackTreeNode<K, V> | null | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Set the left child and update its parent pointer.
|
|
31
|
+
* @remarks Time O(1), Space O(1)
|
|
32
|
+
* @param v - New left node, or null/undefined.
|
|
33
|
+
* @returns void
|
|
34
|
+
*/
|
|
35
|
+
set left(v: RedBlackTreeNode<K, V> | null | undefined);
|
|
36
|
+
_right?: RedBlackTreeNode<K, V> | null | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Get the right child pointer.
|
|
39
|
+
* @remarks Time O(1), Space O(1)
|
|
40
|
+
* @returns Right child node, or null/undefined.
|
|
41
|
+
*/
|
|
42
|
+
get right(): RedBlackTreeNode<K, V> | null | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Set the right child and update its parent pointer.
|
|
45
|
+
* @remarks Time O(1), Space O(1)
|
|
46
|
+
* @param v - New right node, or null/undefined.
|
|
47
|
+
* @returns void
|
|
48
|
+
*/
|
|
49
|
+
set right(v: RedBlackTreeNode<K, V> | null | undefined);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Represents a Red-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
|
|
53
|
+
* @remarks Time O(1), Space O(1)
|
|
54
|
+
* @template K
|
|
55
|
+
* @template V
|
|
56
|
+
* @template R
|
|
57
|
+
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
|
|
58
|
+
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
59
|
+
* @example
|
|
60
|
+
* // using Red-Black Tree as a price-based index for stock data
|
|
61
|
+
* // Define the structure of individual stock records
|
|
62
|
+
* interface StockRecord {
|
|
63
|
+
* price: number; // Stock price (key for indexing)
|
|
64
|
+
* symbol: string; // Stock ticker symbol
|
|
65
|
+
* volume: number; // Trade volume
|
|
66
|
+
* }
|
|
67
|
+
*
|
|
68
|
+
* // Simulate stock market data as it might come from an external feed
|
|
69
|
+
* const marketStockData: StockRecord[] = [
|
|
70
|
+
* { price: 142.5, symbol: 'AAPL', volume: 1000000 },
|
|
71
|
+
* { price: 335.2, symbol: 'MSFT', volume: 800000 },
|
|
72
|
+
* { price: 3285.04, symbol: 'AMZN', volume: 500000 },
|
|
73
|
+
* { price: 267.98, symbol: 'META', volume: 750000 },
|
|
74
|
+
* { price: 234.57, symbol: 'GOOGL', volume: 900000 }
|
|
75
|
+
* ];
|
|
76
|
+
*
|
|
77
|
+
* // Extend the stock record type to include metadata for database usage
|
|
78
|
+
* type StockTableRecord = StockRecord & { lastUpdated: Date };
|
|
79
|
+
*
|
|
80
|
+
* // Create a Red-Black Tree to index stock records by price
|
|
81
|
+
* // Simulates a database index with stock price as the key for quick lookups
|
|
82
|
+
* const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
|
|
83
|
+
* toEntryFn: stockRecord => [
|
|
84
|
+
* stockRecord.price, // Use stock price as the key
|
|
85
|
+
* {
|
|
86
|
+
* ...stockRecord,
|
|
87
|
+
* lastUpdated: new Date() // Add a timestamp for when the record was indexed
|
|
88
|
+
* }
|
|
89
|
+
* ]
|
|
90
|
+
* });
|
|
91
|
+
*
|
|
92
|
+
* // Query the stock with the highest price
|
|
93
|
+
* const highestPricedStock = priceIndex.getRightMost();
|
|
94
|
+
* console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
|
|
95
|
+
*
|
|
96
|
+
* // Query stocks within a specific price range (200 to 400)
|
|
97
|
+
* const stocksInRange = priceIndex.rangeSearch(
|
|
98
|
+
* [200, 400], // Price range
|
|
99
|
+
* node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
|
|
100
|
+
* );
|
|
101
|
+
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
|
|
102
|
+
*/
|
|
103
|
+
export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
104
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>);
|
|
105
|
+
protected _root: RedBlackTreeNode<K, V> | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* Get the current root node.
|
|
108
|
+
* @remarks Time O(1), Space O(1)
|
|
109
|
+
* @returns Root node, or undefined.
|
|
110
|
+
*/
|
|
111
|
+
get root(): RedBlackTreeNode<K, V> | undefined;
|
|
112
|
+
/**
|
|
113
|
+
* Create a red-black node for the given key/value (value ignored in map mode).
|
|
114
|
+
* @remarks Time O(1), Space O(1)
|
|
115
|
+
* @param key - See parameter type for details.
|
|
116
|
+
* @param [value] - See parameter type for details.
|
|
117
|
+
* @param color - See parameter type for details.
|
|
118
|
+
* @returns A new RedBlackTreeNode instance.
|
|
119
|
+
*/
|
|
120
|
+
createNode(key: K, value?: V, color?: RBTNColor): RedBlackTreeNode<K, V>;
|
|
121
|
+
/**
|
|
122
|
+
* Type guard: check whether the input is a RedBlackTreeNode.
|
|
123
|
+
* @remarks Time O(1), Space O(1)
|
|
124
|
+
* @param keyNodeOrEntry - See parameter type for details.
|
|
125
|
+
* @returns True if the value is a RedBlackTreeNode.
|
|
126
|
+
*/
|
|
127
|
+
isNode(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is RedBlackTreeNode<K, V>;
|
|
128
|
+
/**
|
|
129
|
+
* Remove all nodes and clear the key→value store (if in map mode).
|
|
130
|
+
* @remarks Time O(n), Space O(1)
|
|
131
|
+
* @returns void
|
|
132
|
+
*/
|
|
133
|
+
clear(): void;
|
|
134
|
+
/**
|
|
135
|
+
* Insert or replace an entry using BST order and red-black fix-up.
|
|
136
|
+
* @remarks Time O(log n), Space O(1)
|
|
137
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
|
|
138
|
+
* @param [value]- See parameter type for details.
|
|
139
|
+
* @returns True if inserted or updated; false if ignored.
|
|
140
|
+
*/
|
|
141
|
+
add(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Delete a node by key/node/entry and rebalance as needed.
|
|
144
|
+
* @remarks Time O(log n), Space O(1)
|
|
145
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node to delete.
|
|
146
|
+
* @returns Array with deletion metadata (removed node, rebalancing hint if any).
|
|
147
|
+
*/
|
|
148
|
+
delete(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[];
|
|
149
|
+
/**
|
|
150
|
+
* Transform entries into a like-kind red-black tree with possibly different key/value types.
|
|
151
|
+
* @remarks Time O(n), Space O(n)
|
|
152
|
+
* @template MK
|
|
153
|
+
* @template MV
|
|
154
|
+
* @template MR
|
|
155
|
+
* @param callback - Mapping function from (key, value, index, tree) to a new [key, value].
|
|
156
|
+
* @param [options] - See parameter type for details.
|
|
157
|
+
* @param [thisArg] - See parameter type for details.
|
|
158
|
+
* @returns A new RedBlackTree with mapped entries.
|
|
159
|
+
*/
|
|
160
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
161
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
|
|
162
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
|
|
163
|
+
protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void;
|
|
164
|
+
protected _replaceNode(oldNode: RedBlackTreeNode<K, V>, newNode: RedBlackTreeNode<K, V>): RedBlackTreeNode<K, V>;
|
|
165
|
+
/**
|
|
166
|
+
* (Protected) Standard BST insert followed by red-black fix-up.
|
|
167
|
+
* @remarks Time O(log n), Space O(1)
|
|
168
|
+
* @param node - Node to insert.
|
|
169
|
+
* @returns Status string: 'CREATED' or 'UPDATED'.
|
|
170
|
+
*/
|
|
171
|
+
protected _insert(node: RedBlackTreeNode<K, V>): CRUD;
|
|
172
|
+
/**
|
|
173
|
+
* (Protected) Transplant a subtree in place of another during deletion.
|
|
174
|
+
* @remarks Time O(1), Space O(1)
|
|
175
|
+
* @param u - Node to replace.
|
|
176
|
+
* @param v - Replacement subtree root (may be undefined).
|
|
177
|
+
* @returns void
|
|
178
|
+
*/
|
|
179
|
+
protected _transplant(u: RedBlackTreeNode<K, V>, v: RedBlackTreeNode<K, V> | undefined): void;
|
|
180
|
+
/**
|
|
181
|
+
* (Protected) Restore red-black properties after insertion (recolor/rotate).
|
|
182
|
+
* @remarks Time O(log n), Space O(1)
|
|
183
|
+
* @param z - Recently inserted node.
|
|
184
|
+
* @returns void
|
|
185
|
+
*/
|
|
186
|
+
protected _insertFixup(z: RedBlackTreeNode<K, V> | undefined): void;
|
|
187
|
+
/**
|
|
188
|
+
* (Protected) Restore red-black properties after deletion (recolor/rotate).
|
|
189
|
+
* @remarks Time O(log n), Space O(1)
|
|
190
|
+
* @param node - Child that replaced the deleted node (may be undefined).
|
|
191
|
+
* @returns void
|
|
192
|
+
*/
|
|
193
|
+
protected _deleteFixup(node: RedBlackTreeNode<K, V> | undefined): void;
|
|
194
|
+
/**
|
|
195
|
+
* (Protected) Perform a left rotation around x.
|
|
196
|
+
* @remarks Time O(1), Space O(1)
|
|
197
|
+
* @param x - Pivot node to rotate around.
|
|
198
|
+
* @returns void
|
|
199
|
+
*/
|
|
200
|
+
protected _leftRotate(x: RedBlackTreeNode<K, V> | undefined): void;
|
|
201
|
+
/**
|
|
202
|
+
* (Protected) Perform a right rotation around y.
|
|
203
|
+
* @remarks Time O(1), Space O(1)
|
|
204
|
+
* @param y - Pivot node to rotate around.
|
|
205
|
+
* @returns void
|
|
206
|
+
*/
|
|
207
|
+
protected _rightRotate(y: RedBlackTreeNode<K, V> | undefined): void;
|
|
208
|
+
}
|