data-structure-typed 1.47.5 → 1.47.6
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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/cjs/data-structures/heap/heap.js +52 -34
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/min-heap.js +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/cjs/data-structures/queue/deque.js +3 -0
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/cjs/data-structures/queue/queue.js +3 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/data-structures/stack/stack.js +10 -2
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/cjs/types/common.d.ts +2 -0
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/mjs/data-structures/heap/heap.js +53 -35
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/mjs/data-structures/queue/deque.js +3 -0
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/mjs/data-structures/queue/queue.js +3 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/data-structures/stack/stack.js +10 -2
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/mjs/types/common.d.ts +2 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +307 -229
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/heap/heap.ts +57 -39
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +100 -0
- package/test/integration/conversion.test.ts +0 -0
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, BTNKey } from '../../types';
|
|
9
|
-
import { CP, IterationType } from '../../types';
|
|
8
|
+
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, BTNKey, Comparator } from '../../types';
|
|
9
|
+
import { CP, IterableEntriesOrKeys, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
@@ -34,18 +34,18 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
|
|
|
34
34
|
set right(v: N | undefined);
|
|
35
35
|
}
|
|
36
36
|
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>, TREE extends BST<V, N, TREE> = BST<V, N, BSTNested<V, N>>> extends BinaryTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
37
|
-
options: BSTOptions;
|
|
38
37
|
/**
|
|
39
38
|
* The constructor function initializes a binary search tree with an optional comparator function.
|
|
40
39
|
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
41
40
|
* for the binary search tree.
|
|
42
41
|
*/
|
|
43
|
-
constructor(options?: BSTOptions);
|
|
42
|
+
constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<BSTOptions>);
|
|
44
43
|
protected _root?: N;
|
|
45
44
|
/**
|
|
46
45
|
* Get the root node of the binary tree.
|
|
47
46
|
*/
|
|
48
47
|
get root(): N | undefined;
|
|
48
|
+
comparator: Comparator<BTNKey>;
|
|
49
49
|
/**
|
|
50
50
|
* The function creates a new binary search tree node with the given key and value.
|
|
51
51
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
@@ -55,11 +55,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
55
55
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
56
56
|
*/
|
|
57
57
|
createNode(key: BTNKey, value?: V): N;
|
|
58
|
-
createTree(options?: BSTOptions): TREE;
|
|
59
|
-
/**
|
|
60
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
61
|
-
* Space Complexity: O(1) - Constant space is used.
|
|
62
|
-
*/
|
|
58
|
+
createTree(options?: Partial<BSTOptions>): TREE;
|
|
63
59
|
/**
|
|
64
60
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
65
61
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -74,8 +70,8 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
74
70
|
*/
|
|
75
71
|
add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
|
|
76
72
|
/**
|
|
77
|
-
* Time Complexity: O(
|
|
78
|
-
* Space Complexity: O(
|
|
73
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
74
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
79
75
|
*/
|
|
80
76
|
/**
|
|
81
77
|
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
@@ -97,10 +93,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
97
93
|
* current instance of the binary search tree
|
|
98
94
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
99
95
|
*/
|
|
100
|
-
addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType
|
|
96
|
+
addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
101
97
|
/**
|
|
102
|
-
* Time Complexity: O(log n) -
|
|
103
|
-
* Space Complexity: O(
|
|
98
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
99
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
104
100
|
*/
|
|
105
101
|
/**
|
|
106
102
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -117,10 +113,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
117
113
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
118
114
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
119
115
|
*/
|
|
120
|
-
lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType
|
|
116
|
+
lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): BTNKey;
|
|
121
117
|
/**
|
|
122
118
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
123
|
-
* Space Complexity: O(
|
|
119
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
124
120
|
*/
|
|
125
121
|
/**
|
|
126
122
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -137,6 +133,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
137
133
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
138
134
|
*/
|
|
139
135
|
getNodeByKey(key: BTNKey, iterationType?: IterationType): N | undefined;
|
|
136
|
+
/**
|
|
137
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
138
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
139
|
+
*/
|
|
140
140
|
/**
|
|
141
141
|
* The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
|
|
142
142
|
* otherwise it returns the key itself.
|
|
@@ -147,10 +147,6 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
147
147
|
* @returns either a node object (N) or undefined.
|
|
148
148
|
*/
|
|
149
149
|
ensureNotKey(key: BTNKey | N | undefined, iterationType?: IterationType): N | undefined;
|
|
150
|
-
/**
|
|
151
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
152
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
153
|
-
*/
|
|
154
150
|
/**
|
|
155
151
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
156
152
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -174,7 +170,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
174
170
|
* performed on the binary tree. It can have two possible values:
|
|
175
171
|
* @returns The method returns an array of nodes (`N[]`).
|
|
176
172
|
*/
|
|
177
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType
|
|
173
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): N[];
|
|
178
174
|
/**
|
|
179
175
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
180
176
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -200,19 +196,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
200
196
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
201
197
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
202
198
|
*/
|
|
203
|
-
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType
|
|
204
|
-
/**
|
|
205
|
-
* Balancing Adjustment:
|
|
206
|
-
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
207
|
-
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
208
|
-
*
|
|
209
|
-
* Use Cases and Efficiency:
|
|
210
|
-
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
211
|
-
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
212
|
-
*/
|
|
199
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
213
200
|
/**
|
|
214
|
-
* Time Complexity: O(n) -
|
|
215
|
-
* Space Complexity: O(n) -
|
|
201
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
202
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
216
203
|
*/
|
|
217
204
|
/**
|
|
218
205
|
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
@@ -225,10 +212,19 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
225
212
|
* values:
|
|
226
213
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
227
214
|
*/
|
|
228
|
-
perfectlyBalance(iterationType?: IterationType
|
|
215
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
229
216
|
/**
|
|
230
|
-
*
|
|
231
|
-
*
|
|
217
|
+
* Balancing Adjustment:
|
|
218
|
+
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
219
|
+
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
220
|
+
*
|
|
221
|
+
* Use Cases and Efficiency:
|
|
222
|
+
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
223
|
+
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
224
|
+
*/
|
|
225
|
+
/**
|
|
226
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
227
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
232
228
|
*/
|
|
233
229
|
/**
|
|
234
230
|
* Time Complexity: O(n) - Visiting each node once.
|
|
@@ -239,7 +235,12 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
239
235
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
240
236
|
* @returns a boolean value.
|
|
241
237
|
*/
|
|
242
|
-
isAVLBalanced(iterationType?: IterationType
|
|
238
|
+
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
239
|
+
/**
|
|
240
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
241
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
242
|
+
*/
|
|
243
|
+
init(elements: IterableEntriesOrKeys<V>): void;
|
|
243
244
|
protected _setRoot(v: N | undefined): void;
|
|
244
245
|
/**
|
|
245
246
|
* The function compares two values using a comparator function and returns whether the first value
|
|
@@ -45,21 +45,22 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
export class BST extends BinaryTree {
|
|
48
|
-
options;
|
|
49
48
|
/**
|
|
50
49
|
* The constructor function initializes a binary search tree with an optional comparator function.
|
|
51
50
|
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
52
51
|
* for the binary search tree.
|
|
53
52
|
*/
|
|
54
|
-
constructor(options) {
|
|
55
|
-
super(options);
|
|
53
|
+
constructor(elements, options) {
|
|
54
|
+
super([], options);
|
|
56
55
|
if (options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
const { comparator } = options;
|
|
57
|
+
if (comparator) {
|
|
58
|
+
this.comparator = comparator;
|
|
59
|
+
}
|
|
61
60
|
}
|
|
62
61
|
this._root = undefined;
|
|
62
|
+
if (elements)
|
|
63
|
+
this.init(elements);
|
|
63
64
|
}
|
|
64
65
|
_root;
|
|
65
66
|
/**
|
|
@@ -68,6 +69,7 @@ export class BST extends BinaryTree {
|
|
|
68
69
|
get root() {
|
|
69
70
|
return this._root;
|
|
70
71
|
}
|
|
72
|
+
comparator = (a, b) => a - b;
|
|
71
73
|
/**
|
|
72
74
|
* The function creates a new binary search tree node with the given key and value.
|
|
73
75
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
@@ -80,12 +82,11 @@ export class BST extends BinaryTree {
|
|
|
80
82
|
return new BSTNode(key, value);
|
|
81
83
|
}
|
|
82
84
|
createTree(options) {
|
|
83
|
-
return new BST(
|
|
85
|
+
return new BST([], {
|
|
86
|
+
iterationType: this.iterationType,
|
|
87
|
+
comparator: this.comparator, ...options
|
|
88
|
+
});
|
|
84
89
|
}
|
|
85
|
-
/**
|
|
86
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
87
|
-
* Space Complexity: O(1) - Constant space is used.
|
|
88
|
-
*/
|
|
89
90
|
/**
|
|
90
91
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
91
92
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -176,8 +177,8 @@ export class BST extends BinaryTree {
|
|
|
176
177
|
return inserted;
|
|
177
178
|
}
|
|
178
179
|
/**
|
|
179
|
-
* Time Complexity: O(
|
|
180
|
-
* Space Complexity: O(
|
|
180
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
181
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
181
182
|
*/
|
|
182
183
|
/**
|
|
183
184
|
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
@@ -199,7 +200,7 @@ export class BST extends BinaryTree {
|
|
|
199
200
|
* current instance of the binary search tree
|
|
200
201
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
201
202
|
*/
|
|
202
|
-
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.
|
|
203
|
+
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
203
204
|
// TODO this addMany function is inefficient, it should be optimized
|
|
204
205
|
function hasNoUndefined(arr) {
|
|
205
206
|
return arr.indexOf(undefined) === -1;
|
|
@@ -269,8 +270,8 @@ export class BST extends BinaryTree {
|
|
|
269
270
|
return inserted;
|
|
270
271
|
}
|
|
271
272
|
/**
|
|
272
|
-
* Time Complexity: O(log n) -
|
|
273
|
-
* Space Complexity: O(
|
|
273
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
274
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
274
275
|
*/
|
|
275
276
|
/**
|
|
276
277
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -287,7 +288,7 @@ export class BST extends BinaryTree {
|
|
|
287
288
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
288
289
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
289
290
|
*/
|
|
290
|
-
lastKey(beginRoot = this.root, iterationType = this.
|
|
291
|
+
lastKey(beginRoot = this.root, iterationType = this.iterationType) {
|
|
291
292
|
if (this._compare(0, 1) === CP.lt)
|
|
292
293
|
return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
293
294
|
else if (this._compare(0, 1) === CP.gt)
|
|
@@ -297,7 +298,7 @@ export class BST extends BinaryTree {
|
|
|
297
298
|
}
|
|
298
299
|
/**
|
|
299
300
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
300
|
-
* Space Complexity: O(
|
|
301
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
301
302
|
*/
|
|
302
303
|
/**
|
|
303
304
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -344,6 +345,10 @@ export class BST extends BinaryTree {
|
|
|
344
345
|
}
|
|
345
346
|
}
|
|
346
347
|
}
|
|
348
|
+
/**
|
|
349
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
350
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
351
|
+
*/
|
|
347
352
|
/**
|
|
348
353
|
* The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
|
|
349
354
|
* otherwise it returns the key itself.
|
|
@@ -356,10 +361,6 @@ export class BST extends BinaryTree {
|
|
|
356
361
|
ensureNotKey(key, iterationType = IterationType.ITERATIVE) {
|
|
357
362
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
358
363
|
}
|
|
359
|
-
/**
|
|
360
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
361
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
362
|
-
*/
|
|
363
364
|
/**
|
|
364
365
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
365
366
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -383,7 +384,7 @@ export class BST extends BinaryTree {
|
|
|
383
384
|
* performed on the binary tree. It can have two possible values:
|
|
384
385
|
* @returns The method returns an array of nodes (`N[]`).
|
|
385
386
|
*/
|
|
386
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.
|
|
387
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
387
388
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
388
389
|
if (!beginRoot)
|
|
389
390
|
return [];
|
|
@@ -464,7 +465,7 @@ export class BST extends BinaryTree {
|
|
|
464
465
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
465
466
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
466
467
|
*/
|
|
467
|
-
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.
|
|
468
|
+
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
468
469
|
targetNode = this.ensureNotKey(targetNode);
|
|
469
470
|
const ans = [];
|
|
470
471
|
if (!targetNode)
|
|
@@ -505,17 +506,8 @@ export class BST extends BinaryTree {
|
|
|
505
506
|
}
|
|
506
507
|
}
|
|
507
508
|
/**
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
511
|
-
*
|
|
512
|
-
* Use Cases and Efficiency:
|
|
513
|
-
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
514
|
-
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
515
|
-
*/
|
|
516
|
-
/**
|
|
517
|
-
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
518
|
-
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
509
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
510
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
519
511
|
*/
|
|
520
512
|
/**
|
|
521
513
|
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
@@ -528,7 +520,7 @@ export class BST extends BinaryTree {
|
|
|
528
520
|
* values:
|
|
529
521
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
530
522
|
*/
|
|
531
|
-
perfectlyBalance(iterationType = this.
|
|
523
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
532
524
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
533
525
|
this.clear();
|
|
534
526
|
if (sorted.length < 1)
|
|
@@ -566,8 +558,17 @@ export class BST extends BinaryTree {
|
|
|
566
558
|
}
|
|
567
559
|
}
|
|
568
560
|
/**
|
|
569
|
-
*
|
|
570
|
-
*
|
|
561
|
+
* Balancing Adjustment:
|
|
562
|
+
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
563
|
+
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
564
|
+
*
|
|
565
|
+
* Use Cases and Efficiency:
|
|
566
|
+
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
567
|
+
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
568
|
+
*/
|
|
569
|
+
/**
|
|
570
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
571
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
571
572
|
*/
|
|
572
573
|
/**
|
|
573
574
|
* Time Complexity: O(n) - Visiting each node once.
|
|
@@ -578,7 +579,7 @@ export class BST extends BinaryTree {
|
|
|
578
579
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
579
580
|
* @returns a boolean value.
|
|
580
581
|
*/
|
|
581
|
-
isAVLBalanced(iterationType = this.
|
|
582
|
+
isAVLBalanced(iterationType = this.iterationType) {
|
|
582
583
|
if (!this.root)
|
|
583
584
|
return true;
|
|
584
585
|
let balanced = true;
|
|
@@ -623,6 +624,23 @@ export class BST extends BinaryTree {
|
|
|
623
624
|
}
|
|
624
625
|
return balanced;
|
|
625
626
|
}
|
|
627
|
+
/**
|
|
628
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
629
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
630
|
+
*/
|
|
631
|
+
init(elements) {
|
|
632
|
+
if (elements) {
|
|
633
|
+
for (const entryOrKey of elements) {
|
|
634
|
+
if (Array.isArray(entryOrKey)) {
|
|
635
|
+
const [key, value] = entryOrKey;
|
|
636
|
+
this.add(key, value);
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
this.add(entryOrKey);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
626
644
|
_setRoot(v) {
|
|
627
645
|
if (v) {
|
|
628
646
|
v.parent = undefined;
|
|
@@ -638,7 +656,7 @@ export class BST extends BinaryTree {
|
|
|
638
656
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
639
657
|
*/
|
|
640
658
|
_compare(a, b) {
|
|
641
|
-
const compared = this.
|
|
659
|
+
const compared = this.comparator(a, b);
|
|
642
660
|
if (compared > 0)
|
|
643
661
|
return CP.gt;
|
|
644
662
|
else if (compared < 0)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BiTreeDeleteResult, BTNCallback, BTNKey, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
8
|
+
import { BiTreeDeleteResult, BTNCallback, BTNKey, IterableEntriesOrKeys, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
@@ -21,23 +21,18 @@ export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N>
|
|
|
21
21
|
*/
|
|
22
22
|
export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>, TREE extends RedBlackTree<V, N, TREE> = RedBlackTree<V, N, RedBlackTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
23
23
|
Sentinel: N;
|
|
24
|
-
options: RBTreeOptions;
|
|
25
24
|
/**
|
|
26
25
|
* The constructor function initializes a Red-Black Tree with an optional set of options.
|
|
27
26
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
28
27
|
* passed to the constructor. It is used to configure the RBTree object with specific options.
|
|
29
28
|
*/
|
|
30
|
-
constructor(options?: RBTreeOptions);
|
|
29
|
+
constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<RBTreeOptions>);
|
|
31
30
|
protected _root: N;
|
|
32
31
|
get root(): N;
|
|
33
32
|
protected _size: number;
|
|
34
33
|
get size(): number;
|
|
35
34
|
createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
|
|
36
35
|
createTree(options?: RBTreeOptions): TREE;
|
|
37
|
-
/**
|
|
38
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
39
|
-
* Space Complexity: O(1)
|
|
40
|
-
*/
|
|
41
36
|
/**
|
|
42
37
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
43
38
|
* Space Complexity: O(1)
|
|
@@ -70,6 +65,10 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
70
65
|
* @returns an array of `BiTreeDeleteResult<N>`.
|
|
71
66
|
*/
|
|
72
67
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BiTreeDeleteResult<N>[];
|
|
68
|
+
/**
|
|
69
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
70
|
+
* Space Complexity: O(1)
|
|
71
|
+
*/
|
|
73
72
|
isRealNode(node: N | undefined): node is N;
|
|
74
73
|
getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
75
74
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
@@ -101,7 +100,12 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
101
100
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
102
101
|
*/
|
|
103
102
|
getPredecessor(x: N): N;
|
|
103
|
+
/**
|
|
104
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
105
|
+
* Space Complexity: O(1)
|
|
106
|
+
*/
|
|
104
107
|
clear(): void;
|
|
108
|
+
init(elements: IterableEntriesOrKeys<V>): void;
|
|
105
109
|
protected _setRoot(v: N): void;
|
|
106
110
|
/**
|
|
107
111
|
* Time Complexity: O(1)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { RBTNColor } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { BinaryTreeNode } from './binary-tree';
|
|
11
11
|
export class RedBlackTreeNode extends BSTNode {
|
|
@@ -24,21 +24,16 @@ export class RedBlackTreeNode extends BSTNode {
|
|
|
24
24
|
*/
|
|
25
25
|
export class RedBlackTree extends BST {
|
|
26
26
|
Sentinel = new RedBlackTreeNode(NaN);
|
|
27
|
-
options;
|
|
28
27
|
/**
|
|
29
28
|
* The constructor function initializes a Red-Black Tree with an optional set of options.
|
|
30
29
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
31
30
|
* passed to the constructor. It is used to configure the RBTree object with specific options.
|
|
32
31
|
*/
|
|
33
|
-
constructor(options) {
|
|
34
|
-
super(options);
|
|
35
|
-
if (options) {
|
|
36
|
-
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options };
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
40
|
-
}
|
|
32
|
+
constructor(elements, options) {
|
|
33
|
+
super([], options);
|
|
41
34
|
this._root = this.Sentinel;
|
|
35
|
+
if (elements)
|
|
36
|
+
this.init(elements);
|
|
42
37
|
}
|
|
43
38
|
_root;
|
|
44
39
|
get root() {
|
|
@@ -52,12 +47,11 @@ export class RedBlackTree extends BST {
|
|
|
52
47
|
return new RedBlackTreeNode(key, value, color);
|
|
53
48
|
}
|
|
54
49
|
createTree(options) {
|
|
55
|
-
return new RedBlackTree(
|
|
50
|
+
return new RedBlackTree([], {
|
|
51
|
+
iterationType: this.iterationType,
|
|
52
|
+
comparator: this.comparator, ...options
|
|
53
|
+
});
|
|
56
54
|
}
|
|
57
|
-
/**
|
|
58
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
59
|
-
* Space Complexity: O(1)
|
|
60
|
-
*/
|
|
61
55
|
/**
|
|
62
56
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
63
57
|
* Space Complexity: O(1)
|
|
@@ -203,13 +197,13 @@ export class RedBlackTree extends BST {
|
|
|
203
197
|
// TODO
|
|
204
198
|
return ans;
|
|
205
199
|
}
|
|
206
|
-
isRealNode(node) {
|
|
207
|
-
return node !== this.Sentinel && node !== undefined;
|
|
208
|
-
}
|
|
209
200
|
/**
|
|
210
201
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
211
202
|
* Space Complexity: O(1)
|
|
212
203
|
*/
|
|
204
|
+
isRealNode(node) {
|
|
205
|
+
return node !== this.Sentinel && node !== undefined;
|
|
206
|
+
}
|
|
213
207
|
/**
|
|
214
208
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
215
209
|
* Space Complexity: O(1)
|
|
@@ -231,7 +225,7 @@ export class RedBlackTree extends BST {
|
|
|
231
225
|
* `getNodes` method, which is called within the `getNode` method.
|
|
232
226
|
* @returns a value of type `N`, `null`, or `undefined`.
|
|
233
227
|
*/
|
|
234
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
228
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
235
229
|
if (identifier instanceof BinaryTreeNode)
|
|
236
230
|
callback = (node => node);
|
|
237
231
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -284,10 +278,27 @@ export class RedBlackTree extends BST {
|
|
|
284
278
|
}
|
|
285
279
|
return y;
|
|
286
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
283
|
+
* Space Complexity: O(1)
|
|
284
|
+
*/
|
|
287
285
|
clear() {
|
|
288
286
|
this._root = this.Sentinel;
|
|
289
287
|
this._size = 0;
|
|
290
288
|
}
|
|
289
|
+
init(elements) {
|
|
290
|
+
if (elements) {
|
|
291
|
+
for (const entryOrKey of elements) {
|
|
292
|
+
if (Array.isArray(entryOrKey)) {
|
|
293
|
+
const [key, value] = entryOrKey;
|
|
294
|
+
this.add(key, value);
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
this.add(entryOrKey);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
291
302
|
_setRoot(v) {
|
|
292
303
|
if (v) {
|
|
293
304
|
v.parent = undefined;
|