min-heap-typed 1.50.1 → 1.50.3
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/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -12,14 +12,16 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
|
12
12
|
this._right = undefined;
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* The function returns the value of the `_left` property.
|
|
16
|
+
* @returns The `_left` property of the current object is being returned.
|
|
16
17
|
*/
|
|
17
18
|
get left() {
|
|
18
19
|
return this._left;
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
21
|
-
*
|
|
22
|
-
* @param {
|
|
22
|
+
* The function sets the left child of a node and updates the parent reference of the child.
|
|
23
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an
|
|
24
|
+
* instance of the `NODE` class or `undefined`.
|
|
23
25
|
*/
|
|
24
26
|
set left(v) {
|
|
25
27
|
if (v) {
|
|
@@ -28,14 +30,17 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
|
28
30
|
this._left = v;
|
|
29
31
|
}
|
|
30
32
|
/**
|
|
31
|
-
*
|
|
33
|
+
* The function returns the right node of a binary tree or undefined if there is no right node.
|
|
34
|
+
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
35
|
+
* `undefined`.
|
|
32
36
|
*/
|
|
33
37
|
get right() {
|
|
34
38
|
return this._right;
|
|
35
39
|
}
|
|
36
40
|
/**
|
|
37
|
-
*
|
|
38
|
-
* @param {
|
|
41
|
+
* The function sets the right child of a node and updates the parent reference of the child.
|
|
42
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a
|
|
43
|
+
* `NODE` object or `undefined`.
|
|
39
44
|
*/
|
|
40
45
|
set right(v) {
|
|
41
46
|
if (v) {
|
|
@@ -56,10 +61,10 @@ exports.BSTNode = BSTNode;
|
|
|
56
61
|
*/
|
|
57
62
|
class BST extends binary_tree_1.BinaryTree {
|
|
58
63
|
/**
|
|
59
|
-
* This is the constructor function for a
|
|
60
|
-
*
|
|
61
|
-
* @param
|
|
62
|
-
* binary search tree.
|
|
64
|
+
* This is the constructor function for a TypeScript class that initializes a binary search tree with
|
|
65
|
+
* optional keys or nodes or entries and options.
|
|
66
|
+
* @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
|
|
67
|
+
* to initialize the binary search tree with the provided keys, nodes, or entries.
|
|
63
68
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
64
69
|
* configuration options for the binary search tree. It can have the following properties:
|
|
65
70
|
*/
|
|
@@ -75,19 +80,27 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
75
80
|
if (keysOrNodesOrEntries)
|
|
76
81
|
this.addMany(keysOrNodesOrEntries);
|
|
77
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* The function returns the root node of a tree structure.
|
|
85
|
+
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
86
|
+
*/
|
|
78
87
|
get root() {
|
|
79
88
|
return this._root;
|
|
80
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* The function returns the value of the _variant property.
|
|
92
|
+
* @returns The value of the `_variant` property.
|
|
93
|
+
*/
|
|
81
94
|
get variant() {
|
|
82
95
|
return this._variant;
|
|
83
96
|
}
|
|
84
97
|
/**
|
|
85
|
-
* The function creates a new
|
|
86
|
-
* @param {K} key - The key parameter is the
|
|
87
|
-
*
|
|
88
|
-
* @param [value] - The parameter
|
|
89
|
-
*
|
|
90
|
-
* @returns a new instance of the BSTNode class
|
|
98
|
+
* The function creates a new BSTNode with the given key and value and returns it.
|
|
99
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
100
|
+
* being created.
|
|
101
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
102
|
+
* value associated with the key in the node being created.
|
|
103
|
+
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
|
|
91
104
|
*/
|
|
92
105
|
createNode(key, value) {
|
|
93
106
|
return new BSTNode(key, value);
|
|
@@ -95,22 +108,23 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
95
108
|
/**
|
|
96
109
|
* The function creates a new binary search tree with the specified options.
|
|
97
110
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
98
|
-
* behavior of the `createTree` method. It
|
|
99
|
-
*
|
|
100
|
-
* @returns a new instance of the BST class with the
|
|
111
|
+
* behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
|
|
112
|
+
* partial object of type `BSTOptions<K>`.
|
|
113
|
+
* @returns a new instance of the BST class, with the provided options merged with the default
|
|
114
|
+
* options. The returned value is casted as TREE.
|
|
101
115
|
*/
|
|
102
116
|
createTree(options) {
|
|
103
117
|
return new BST([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
104
118
|
}
|
|
105
119
|
/**
|
|
106
|
-
* The function `
|
|
120
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
107
121
|
* otherwise it returns undefined.
|
|
108
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
122
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
109
123
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
110
|
-
* `
|
|
111
|
-
* @returns a node of type
|
|
124
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
125
|
+
* @returns a node of type NODE or undefined.
|
|
112
126
|
*/
|
|
113
|
-
|
|
127
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
114
128
|
let node;
|
|
115
129
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
116
130
|
return;
|
|
@@ -138,7 +152,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
138
152
|
/**
|
|
139
153
|
* Time Complexity: O(log n)
|
|
140
154
|
* Space Complexity: O(log n)
|
|
141
|
-
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
142
155
|
*/
|
|
143
156
|
/**
|
|
144
157
|
* Time Complexity: O(log n)
|
|
@@ -146,11 +159,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
146
159
|
*
|
|
147
160
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
148
161
|
* otherwise it returns the key itself.
|
|
149
|
-
* @param {K |
|
|
162
|
+
* @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
|
|
150
163
|
* `undefined`.
|
|
151
164
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
152
165
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
153
|
-
* @returns either a node object (
|
|
166
|
+
* @returns either a node object (NODE) or undefined.
|
|
154
167
|
*/
|
|
155
168
|
ensureNode(keyOrNodeOrEntry, iterationType = types_1.IterationType.ITERATIVE) {
|
|
156
169
|
let res;
|
|
@@ -169,7 +182,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
169
182
|
}
|
|
170
183
|
/**
|
|
171
184
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
172
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,
|
|
185
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
173
186
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
174
187
|
*/
|
|
175
188
|
isNode(keyOrNodeOrEntry) {
|
|
@@ -178,7 +191,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
178
191
|
/**
|
|
179
192
|
* Time Complexity: O(log n)
|
|
180
193
|
* Space Complexity: O(1)
|
|
181
|
-
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
182
194
|
*/
|
|
183
195
|
/**
|
|
184
196
|
* Time Complexity: O(log n)
|
|
@@ -193,7 +205,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
193
205
|
* node was not added.
|
|
194
206
|
*/
|
|
195
207
|
add(keyOrNodeOrEntry, value) {
|
|
196
|
-
const newNode = this.
|
|
208
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
197
209
|
if (newNode === undefined)
|
|
198
210
|
return false;
|
|
199
211
|
if (this.root === undefined) {
|
|
@@ -217,7 +229,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
217
229
|
else if (this._compare(current.key, newNode.key) === types_1.CP.gt) {
|
|
218
230
|
if (current.left === undefined) {
|
|
219
231
|
current.left = newNode;
|
|
220
|
-
newNode.parent = current;
|
|
221
232
|
this._size++;
|
|
222
233
|
return true;
|
|
223
234
|
}
|
|
@@ -226,7 +237,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
226
237
|
else {
|
|
227
238
|
if (current.right === undefined) {
|
|
228
239
|
current.right = newNode;
|
|
229
|
-
newNode.parent = current;
|
|
230
240
|
this._size++;
|
|
231
241
|
return true;
|
|
232
242
|
}
|
|
@@ -237,12 +247,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
237
247
|
}
|
|
238
248
|
/**
|
|
239
249
|
* Time Complexity: O(k log n)
|
|
240
|
-
* Space Complexity: O(k)
|
|
241
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
250
|
+
* Space Complexity: O(k + log n)
|
|
242
251
|
*/
|
|
243
252
|
/**
|
|
244
253
|
* Time Complexity: O(k log n)
|
|
245
|
-
* Space Complexity: O(k)
|
|
254
|
+
* Space Complexity: O(k + log n)
|
|
246
255
|
*
|
|
247
256
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
248
257
|
* balancing the tree after each addition.
|
|
@@ -258,7 +267,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
258
267
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
259
268
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
260
269
|
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
261
|
-
* @returns The function `addMany` returns an array of nodes (`
|
|
270
|
+
* @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
|
|
262
271
|
*/
|
|
263
272
|
addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
264
273
|
const inserted = [];
|
|
@@ -349,7 +358,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
349
358
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
350
359
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
351
360
|
* values:
|
|
352
|
-
* @returns The function `getNodeByKey` returns a node (`
|
|
361
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
353
362
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
354
363
|
*/
|
|
355
364
|
getNodeByKey(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
@@ -385,32 +394,31 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
385
394
|
}
|
|
386
395
|
/**
|
|
387
396
|
* Time Complexity: O(log n)
|
|
388
|
-
* Space Complexity: O(log n)
|
|
389
|
-
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
397
|
+
* Space Complexity: O(k + log n)
|
|
390
398
|
* /
|
|
391
399
|
|
|
392
400
|
/**
|
|
393
401
|
* Time Complexity: O(log n)
|
|
394
|
-
* Space Complexity: O(log n)
|
|
402
|
+
* Space Complexity: O(k + log n)
|
|
395
403
|
*
|
|
396
404
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
397
405
|
* recursive or iterative approach.
|
|
398
406
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
399
407
|
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
400
408
|
* callback function `C`.
|
|
401
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
409
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
|
|
402
410
|
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
403
|
-
* function type that extends the `BTNCallback<
|
|
411
|
+
* function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
|
|
404
412
|
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
405
413
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
406
414
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
407
415
|
* searching for all nodes that match the identifier and return an array containing
|
|
408
|
-
* @param {K |
|
|
416
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
409
417
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
410
418
|
* traversal will start from the root of the tree.
|
|
411
419
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
412
420
|
* performed on the binary tree. It can have two possible values:
|
|
413
|
-
* @returns The method returns an array of nodes (`
|
|
421
|
+
* @returns The method returns an array of nodes (`NODE[]`).
|
|
414
422
|
*/
|
|
415
423
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
416
424
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -468,25 +476,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
468
476
|
}
|
|
469
477
|
return ans;
|
|
470
478
|
}
|
|
471
|
-
// /**
|
|
472
|
-
// * The function overrides the subTreeTraverse method and returns the result of calling the super
|
|
473
|
-
// * method with the provided arguments.
|
|
474
|
-
// * @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
475
|
-
// * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
|
|
476
|
-
// * the tree. The return type of the callback function can be any type.
|
|
477
|
-
// * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
|
|
478
|
-
// * can be either a key, a node, or an entry.
|
|
479
|
-
// * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
480
|
-
// * be performed during the traversal of the subtree. It can have one of the following values:
|
|
481
|
-
// * @returns The method is returning an array of the return type of the callback function.
|
|
482
|
-
// */
|
|
483
|
-
// override subTreeTraverse<C extends BTNCallback<N>>(
|
|
484
|
-
// callback: C = this._defaultOneParamCallback as C,
|
|
485
|
-
// beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
486
|
-
// iterationType = this.iterationType
|
|
487
|
-
// ): ReturnType<C>[] {
|
|
488
|
-
// return super.subTreeTraverse(callback, beginRoot, iterationType, false);
|
|
489
|
-
// }
|
|
490
479
|
/**
|
|
491
480
|
* Time complexity: O(n)
|
|
492
481
|
* Space complexity: O(n)
|
|
@@ -548,7 +537,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
548
537
|
* The function overrides the listLevels method and returns an array of arrays containing the return
|
|
549
538
|
* type of the callback function for each level of the tree.
|
|
550
539
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
551
|
-
* `BTNCallback<
|
|
540
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
|
|
552
541
|
* during the level listing process.
|
|
553
542
|
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
|
|
554
543
|
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
|
|
@@ -563,18 +552,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
563
552
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
564
553
|
}
|
|
565
554
|
/**
|
|
566
|
-
* Time Complexity: O(
|
|
567
|
-
* Space Complexity: O(
|
|
568
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
555
|
+
* Time Complexity: O(log n)
|
|
556
|
+
* Space Complexity: O(1)
|
|
569
557
|
*/
|
|
570
558
|
/**
|
|
571
|
-
* Time Complexity: O(
|
|
572
|
-
* Space Complexity: O(
|
|
559
|
+
* Time Complexity: O(log n)
|
|
560
|
+
* Space Complexity: O(1)
|
|
573
561
|
*
|
|
574
562
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
575
563
|
* leftmost node if the comparison result is greater than.
|
|
576
|
-
* @param {K |
|
|
577
|
-
* type `K`, `
|
|
564
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
565
|
+
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
578
566
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
579
567
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
580
568
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
@@ -601,7 +589,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
601
589
|
/**
|
|
602
590
|
* Time Complexity: O(log n)
|
|
603
591
|
* Space Complexity: O(log n)
|
|
604
|
-
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
605
592
|
*/
|
|
606
593
|
/**
|
|
607
594
|
* Time Complexity: O(log n)
|
|
@@ -611,12 +598,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
611
598
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
612
599
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
613
600
|
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
|
|
614
|
-
* parameter of type `
|
|
601
|
+
* parameter of type `NODE` (the node type) and returns a value of any type.
|
|
615
602
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
616
603
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
617
604
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
618
605
|
* `lesserOrGreater` are
|
|
619
|
-
* @param {K |
|
|
606
|
+
* @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
620
607
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
621
608
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
622
609
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -725,8 +712,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
725
712
|
* 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).
|
|
726
713
|
*/
|
|
727
714
|
/**
|
|
728
|
-
* Time Complexity: O(n)
|
|
729
|
-
* Space Complexity: O(n)
|
|
715
|
+
* Time Complexity: O(n)
|
|
716
|
+
* Space Complexity: O(log n)
|
|
730
717
|
*/
|
|
731
718
|
/**
|
|
732
719
|
* Time Complexity: O(n)
|
|
@@ -783,6 +770,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
783
770
|
}
|
|
784
771
|
return balanced;
|
|
785
772
|
}
|
|
773
|
+
/**
|
|
774
|
+
* The function sets the root property of an object and updates the parent property of the new root.
|
|
775
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
|
|
776
|
+
* can either be an object of type `NODE` or it can be `undefined`.
|
|
777
|
+
*/
|
|
786
778
|
_setRoot(v) {
|
|
787
779
|
if (v) {
|
|
788
780
|
v.parent = undefined;
|