min-heap-typed 1.39.2 → 1.39.4
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/binary-tree/avl-tree.d.ts +11 -11
- package/dist/data-structures/binary-tree/avl-tree.js +8 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +50 -117
- package/dist/data-structures/binary-tree/binary-tree.js +43 -51
- package/dist/data-structures/binary-tree/bst.d.ts +23 -23
- package/dist/data-structures/binary-tree/bst.js +18 -18
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +15 -15
- package/dist/data-structures/binary-tree/tree-multiset.js +9 -9
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/helpers.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +16 -14
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +145 -84
- package/src/data-structures/binary-tree/bst.ts +37 -38
- package/src/data-structures/binary-tree/rb-tree.ts +7 -6
- package/src/data-structures/binary-tree/tree-multiset.ts +18 -17
- package/src/data-structures/graph/abstract-graph.ts +11 -10
- package/src/data-structures/graph/directed-graph.ts +2 -1
- package/src/data-structures/graph/undirected-graph.ts +5 -4
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +1 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +4 -5
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +4 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -19,7 +19,7 @@ const queue_1 = require("../queue");
|
|
|
19
19
|
class BinaryTreeNode {
|
|
20
20
|
/**
|
|
21
21
|
* Creates a new instance of BinaryTreeNode.
|
|
22
|
-
* @param {
|
|
22
|
+
* @param {BTNKey} key - The key associated with the node.
|
|
23
23
|
* @param {V} val - The value stored in the node.
|
|
24
24
|
*/
|
|
25
25
|
constructor(key, val) {
|
|
@@ -90,14 +90,6 @@ class BinaryTree {
|
|
|
90
90
|
this._iterationType = types_1.IterationType.ITERATIVE;
|
|
91
91
|
this._root = null;
|
|
92
92
|
this._size = 0;
|
|
93
|
-
/**
|
|
94
|
-
* Time complexity is O(n)
|
|
95
|
-
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
96
|
-
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
97
|
-
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
98
|
-
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
99
|
-
*/
|
|
100
|
-
this._defaultCallbackByKey = node => node.key;
|
|
101
93
|
if (options !== undefined) {
|
|
102
94
|
const { iterationType = types_1.IterationType.ITERATIVE } = options;
|
|
103
95
|
this._iterationType = iterationType;
|
|
@@ -130,7 +122,7 @@ class BinaryTree {
|
|
|
130
122
|
}
|
|
131
123
|
/**
|
|
132
124
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
133
|
-
* @param {
|
|
125
|
+
* @param {BTNKey} key - The key for the new node.
|
|
134
126
|
* @param {V} val - The value for the new node.
|
|
135
127
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
136
128
|
*/
|
|
@@ -153,7 +145,7 @@ class BinaryTree {
|
|
|
153
145
|
}
|
|
154
146
|
/**
|
|
155
147
|
* Add a node with the given key and value to the binary tree.
|
|
156
|
-
* @param {
|
|
148
|
+
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
157
149
|
* @param {V} val - The value for the new node (optional).
|
|
158
150
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
159
151
|
*/
|
|
@@ -192,7 +184,7 @@ class BinaryTree {
|
|
|
192
184
|
return;
|
|
193
185
|
}
|
|
194
186
|
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
195
|
-
const existNode = key !== undefined ? this.get(key,
|
|
187
|
+
const existNode = key !== undefined ? this.get(key, (node) => node.key) : undefined;
|
|
196
188
|
if (this.root) {
|
|
197
189
|
if (existNode) {
|
|
198
190
|
existNode.val = val;
|
|
@@ -217,7 +209,7 @@ class BinaryTree {
|
|
|
217
209
|
/**
|
|
218
210
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
219
211
|
* values, and adds them to the binary tree.
|
|
220
|
-
* @param {(
|
|
212
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of BTNKey or BinaryTreeNode
|
|
221
213
|
* objects, or null values.
|
|
222
214
|
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
223
215
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
@@ -239,8 +231,8 @@ class BinaryTree {
|
|
|
239
231
|
}
|
|
240
232
|
/**
|
|
241
233
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
242
|
-
* @param {(
|
|
243
|
-
* `
|
|
234
|
+
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
235
|
+
* `BTNKey` or `N` values.
|
|
244
236
|
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
245
237
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
246
238
|
* array. Each value in the `data` array will be assigned to the
|
|
@@ -253,18 +245,18 @@ class BinaryTree {
|
|
|
253
245
|
/**
|
|
254
246
|
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
255
247
|
* with the parent node that needs to be balanced.
|
|
256
|
-
* a key (`
|
|
248
|
+
* a key (`BTNKey`). If it is a key, the function will find the corresponding node in the
|
|
257
249
|
* binary tree.
|
|
258
250
|
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
259
251
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
260
|
-
* `
|
|
252
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
261
253
|
* searching for. It can be a specific key value or any other property of the node.
|
|
262
254
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
263
255
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
264
256
|
* included in the result. The `callback` parameter has a default value of
|
|
265
|
-
* `
|
|
257
|
+
* `((node: N) => node.key)`, which
|
|
266
258
|
*/
|
|
267
|
-
delete(identifier, callback =
|
|
259
|
+
delete(identifier, callback = ((node) => node.key)) {
|
|
268
260
|
const bstDeletedResult = [];
|
|
269
261
|
if (!this.root)
|
|
270
262
|
return bstDeletedResult;
|
|
@@ -312,10 +304,10 @@ class BinaryTree {
|
|
|
312
304
|
/**
|
|
313
305
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
314
306
|
* specified root node.
|
|
315
|
-
* @param {
|
|
307
|
+
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
316
308
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
317
|
-
* of the node (`
|
|
318
|
-
* @param {
|
|
309
|
+
* of the node (`BTNKey`), or `null`.
|
|
310
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
319
311
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
320
312
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
321
313
|
* node of the binary tree.
|
|
@@ -339,9 +331,9 @@ class BinaryTree {
|
|
|
339
331
|
/**
|
|
340
332
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
341
333
|
* iterative approach.
|
|
342
|
-
* @param {
|
|
334
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
343
335
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
344
|
-
* object (`N`), a key value of a node in the tree (`
|
|
336
|
+
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
345
337
|
* node is specified. If `
|
|
346
338
|
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
347
339
|
* height of the binary tree using a recursive approach or an iterative approach. It can have two
|
|
@@ -450,12 +442,12 @@ class BinaryTree {
|
|
|
450
442
|
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
451
443
|
* recursive or iterative traversal.
|
|
452
444
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
453
|
-
* `
|
|
445
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
454
446
|
* searching for. It can be a specific key value or any other property of the node.
|
|
455
447
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
456
448
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
457
449
|
* included in the result. The `callback` parameter has a default value of
|
|
458
|
-
* `
|
|
450
|
+
* `((node: N) => node.key)`, which
|
|
459
451
|
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
460
452
|
* first node that matches the identifier. If set to true, the function will return an array with
|
|
461
453
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
@@ -467,7 +459,7 @@ class BinaryTree {
|
|
|
467
459
|
* traverse the binary tree. It can have two possible values:
|
|
468
460
|
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
469
461
|
*/
|
|
470
|
-
getNodes(identifier, callback =
|
|
462
|
+
getNodes(identifier, callback = ((node) => node.key), onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
471
463
|
if (!beginRoot)
|
|
472
464
|
return [];
|
|
473
465
|
if (identifier instanceof BinaryTreeNode)
|
|
@@ -506,13 +498,13 @@ class BinaryTree {
|
|
|
506
498
|
}
|
|
507
499
|
/**
|
|
508
500
|
* The function checks if a binary tree has a node with a given property or key.
|
|
509
|
-
* @param {
|
|
510
|
-
* the node that you want to find in the binary tree. It can be either a `
|
|
501
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
502
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or a
|
|
511
503
|
* generic type `N`.
|
|
512
504
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
513
505
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
514
506
|
* whether the node matches the criteria or not. The default callback function
|
|
515
|
-
* `
|
|
507
|
+
* `((node: N) => node.key)` is used if no callback function is
|
|
516
508
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
517
509
|
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
518
510
|
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
@@ -521,7 +513,7 @@ class BinaryTree {
|
|
|
521
513
|
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
522
514
|
* @returns a boolean value.
|
|
523
515
|
*/
|
|
524
|
-
has(identifier, callback =
|
|
516
|
+
has(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
525
517
|
if (identifier instanceof BinaryTreeNode)
|
|
526
518
|
callback = (node => node);
|
|
527
519
|
// TODO may support finding node by value equal
|
|
@@ -529,20 +521,20 @@ class BinaryTree {
|
|
|
529
521
|
}
|
|
530
522
|
/**
|
|
531
523
|
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
532
|
-
* @param {
|
|
533
|
-
* the node that you want to find in the binary tree. It can be either a `
|
|
524
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
525
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
534
526
|
* type.
|
|
535
527
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
536
528
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
537
529
|
* whether the node matches the criteria or not. The default callback function
|
|
538
|
-
* (`
|
|
530
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
539
531
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
540
532
|
* the root node from which the search should begin.
|
|
541
533
|
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
542
534
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
543
535
|
* @returns either the found node (of type N) or null if no node is found.
|
|
544
536
|
*/
|
|
545
|
-
get(identifier, callback =
|
|
537
|
+
get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
546
538
|
var _a;
|
|
547
539
|
if (identifier instanceof BinaryTreeNode)
|
|
548
540
|
callback = (node => node);
|
|
@@ -574,9 +566,9 @@ class BinaryTree {
|
|
|
574
566
|
/**
|
|
575
567
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
576
568
|
* iterative traversal.
|
|
577
|
-
* @param {
|
|
569
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
578
570
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
579
|
-
* of a node (`
|
|
571
|
+
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
580
572
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
581
573
|
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
582
574
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
|
|
@@ -698,14 +690,14 @@ class BinaryTree {
|
|
|
698
690
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
699
691
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
700
692
|
* an array.
|
|
701
|
-
* @param {
|
|
693
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
702
694
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
703
695
|
* start from the root of the tree.
|
|
704
696
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
705
697
|
* performed on the binary tree. It can have two possible values:
|
|
706
|
-
* @returns The function `subTreeTraverse` returns an array of `ReturnType<
|
|
698
|
+
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
707
699
|
*/
|
|
708
|
-
subTreeTraverse(callback =
|
|
700
|
+
subTreeTraverse(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
709
701
|
if (typeof beginRoot === 'number')
|
|
710
702
|
beginRoot = this.get(beginRoot);
|
|
711
703
|
const ans = [];
|
|
@@ -735,7 +727,7 @@ class BinaryTree {
|
|
|
735
727
|
* function on each node according to a specified order pattern.
|
|
736
728
|
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
737
729
|
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
738
|
-
* is `
|
|
730
|
+
* is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
|
|
739
731
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
740
732
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
741
733
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
@@ -743,9 +735,9 @@ class BinaryTree {
|
|
|
743
735
|
* is `null`, an empty array will be returned.
|
|
744
736
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
745
737
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
746
|
-
* @returns The function `dfs` returns an array of `ReturnType<
|
|
738
|
+
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
747
739
|
*/
|
|
748
|
-
dfs(callback =
|
|
740
|
+
dfs(callback = ((node) => node.key), pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
|
|
749
741
|
if (!beginRoot)
|
|
750
742
|
return [];
|
|
751
743
|
const ans = [];
|
|
@@ -820,15 +812,15 @@ class BinaryTree {
|
|
|
820
812
|
* function on each node.
|
|
821
813
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
822
814
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
823
|
-
* `ReturnType<
|
|
815
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
|
|
824
816
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
825
817
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
826
818
|
* will not be performed and an empty array will be returned.
|
|
827
819
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
828
820
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
829
|
-
* @returns The function `bfs` returns an array of `ReturnType<
|
|
821
|
+
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
|
|
830
822
|
*/
|
|
831
|
-
bfs(callback =
|
|
823
|
+
bfs(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
832
824
|
if (!beginRoot)
|
|
833
825
|
return [];
|
|
834
826
|
const ans = [];
|
|
@@ -878,7 +870,7 @@ class BinaryTree {
|
|
|
878
870
|
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
879
871
|
* function `C` applied to the nodes at that level.
|
|
880
872
|
*/
|
|
881
|
-
listLevels(callback =
|
|
873
|
+
listLevels(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
882
874
|
if (!beginRoot)
|
|
883
875
|
return [];
|
|
884
876
|
const levelsNodes = [];
|
|
@@ -934,17 +926,17 @@ class BinaryTree {
|
|
|
934
926
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
935
927
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
936
928
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
937
|
-
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<
|
|
938
|
-
* default value for this parameter is `
|
|
929
|
+
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
|
|
930
|
+
* default value for this parameter is `((node: N) => node.key)`.
|
|
939
931
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
940
932
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
941
933
|
* following values:
|
|
942
934
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
943
935
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
944
936
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
945
|
-
* @returns The `morris` function returns an array of `ReturnType<
|
|
937
|
+
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
946
938
|
*/
|
|
947
|
-
morris(callback =
|
|
939
|
+
morris(callback = ((node) => node.key), pattern = 'in', beginRoot = this.root) {
|
|
948
940
|
if (beginRoot === null)
|
|
949
941
|
return [];
|
|
950
942
|
const ans = [];
|
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { BTNKey, BSTComparator, BSTNodeNested, BSTOptions, BTNCallback } from '../../types';
|
|
9
9
|
import { CP, 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> {
|
|
13
|
-
constructor(key:
|
|
13
|
+
constructor(key: BTNKey, val?: V);
|
|
14
14
|
}
|
|
15
15
|
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
16
16
|
/**
|
|
@@ -22,30 +22,30 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
22
22
|
constructor(options?: BSTOptions);
|
|
23
23
|
/**
|
|
24
24
|
* The function creates a new binary search tree node with the given key and value.
|
|
25
|
-
* @param {
|
|
25
|
+
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
26
26
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
27
27
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
|
|
28
28
|
* represents the value associated with the node in a binary search tree.
|
|
29
29
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
30
30
|
*/
|
|
31
|
-
createNode(key:
|
|
31
|
+
createNode(key: BTNKey, val?: V): N;
|
|
32
32
|
/**
|
|
33
33
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
34
34
|
* into the tree.
|
|
35
|
-
* @param {
|
|
36
|
-
* `
|
|
35
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
36
|
+
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
37
37
|
* @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
|
|
38
38
|
* binary search tree.
|
|
39
39
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
40
40
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
41
41
|
*/
|
|
42
|
-
add(keyOrNode:
|
|
42
|
+
add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined;
|
|
43
43
|
/**
|
|
44
44
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
45
45
|
* maintaining balance.
|
|
46
|
-
* @param {[
|
|
46
|
+
* @param {[BTNKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
47
47
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
48
|
-
* array of `
|
|
48
|
+
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
49
49
|
* `null
|
|
50
50
|
* @param {V[]} data - The values of tree nodes
|
|
51
51
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
@@ -53,13 +53,13 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
53
53
|
* It can have two possible values:
|
|
54
54
|
* @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
|
|
55
55
|
*/
|
|
56
|
-
addMany(keysOrNodes: (
|
|
56
|
+
addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: V[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
|
|
57
57
|
/**
|
|
58
58
|
* The function returns the first node in the binary tree that matches the given node property and
|
|
59
59
|
* callback.
|
|
60
60
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
61
61
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
62
|
-
* value (`
|
|
62
|
+
* value (`BTNKey`) or a custom callback function (`BTNCallback<N>`) that determines
|
|
63
63
|
* whether a node matches the desired property.
|
|
64
64
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
65
65
|
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
@@ -72,7 +72,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
72
72
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
73
73
|
* matching node is found.
|
|
74
74
|
*/
|
|
75
|
-
get<C extends
|
|
75
|
+
get<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
76
76
|
/**
|
|
77
77
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
78
78
|
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
@@ -88,16 +88,16 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
88
88
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
89
89
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
90
90
|
*/
|
|
91
|
-
lastKey(beginRoot?: N | null, iterationType?: IterationType):
|
|
91
|
+
lastKey(beginRoot?: N | null, iterationType?: IterationType): BTNKey;
|
|
92
92
|
/**
|
|
93
93
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
94
94
|
* using either recursive or iterative traversal.
|
|
95
95
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
96
|
-
* of the binary tree node that you want to search for. It can be either a `
|
|
96
|
+
* of the binary tree node that you want to search for. It can be either a `BTNKey` or a
|
|
97
97
|
* generic type `N`.
|
|
98
98
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
99
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
100
|
-
* included in the result. The default value for `callback` is `
|
|
100
|
+
* included in the result. The default value for `callback` is `((node: N) => node.key)`, which is
|
|
101
101
|
* a
|
|
102
102
|
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
103
103
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
@@ -110,7 +110,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
110
110
|
* traverse the binary tree. It can have one of the following values:
|
|
111
111
|
* @returns an array of nodes (N[]).
|
|
112
112
|
*/
|
|
113
|
-
getNodes<C extends
|
|
113
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
114
114
|
/**
|
|
115
115
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
116
116
|
* nodes that have a key value lesser or greater than a target key value.
|
|
@@ -120,15 +120,15 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
120
120
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
121
121
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
122
122
|
* of the following values:
|
|
123
|
-
* @param {
|
|
123
|
+
* @param {BTNKey | N | null} targetNode - The `targetNode` parameter in the
|
|
124
124
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
125
125
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
126
|
-
* (`
|
|
126
|
+
* (`BTNKey`), or `null` to
|
|
127
127
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
128
128
|
* done recursively or iteratively. It can have two possible values:
|
|
129
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<
|
|
129
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
130
130
|
*/
|
|
131
|
-
lesserOrGreaterTraverse<C extends
|
|
131
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | null, iterationType?: IterationType): ReturnType<C>[];
|
|
132
132
|
/**
|
|
133
133
|
* Balancing Adjustment:
|
|
134
134
|
* 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.
|
|
@@ -158,10 +158,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
158
158
|
/**
|
|
159
159
|
* The function compares two values using a comparator function and returns whether the first value
|
|
160
160
|
* is greater than, less than, or equal to the second value.
|
|
161
|
-
* @param {
|
|
162
|
-
* @param {
|
|
161
|
+
* @param {BTNKey} a - The parameter "a" is of type BTNKey.
|
|
162
|
+
* @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
|
|
163
163
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
164
164
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
165
165
|
*/
|
|
166
|
-
protected _compare(a:
|
|
166
|
+
protected _compare(a: BTNKey, b: BTNKey): CP;
|
|
167
167
|
}
|
|
@@ -29,7 +29,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* The function creates a new binary search tree node with the given key and value.
|
|
32
|
-
* @param {
|
|
32
|
+
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
33
33
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
34
34
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
|
|
35
35
|
* represents the value associated with the node in a binary search tree.
|
|
@@ -41,8 +41,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
41
41
|
/**
|
|
42
42
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
43
43
|
* into the tree.
|
|
44
|
-
* @param {
|
|
45
|
-
* `
|
|
44
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
45
|
+
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
46
46
|
* @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
|
|
47
47
|
* binary search tree.
|
|
48
48
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
@@ -126,9 +126,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
126
126
|
/**
|
|
127
127
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
128
128
|
* maintaining balance.
|
|
129
|
-
* @param {[
|
|
129
|
+
* @param {[BTNKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
130
130
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
131
|
-
* array of `
|
|
131
|
+
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
132
132
|
* `null
|
|
133
133
|
* @param {V[]} data - The values of tree nodes
|
|
134
134
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
@@ -210,7 +210,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
210
210
|
* callback.
|
|
211
211
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
212
212
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
213
|
-
* value (`
|
|
213
|
+
* value (`BTNKey`) or a custom callback function (`BTNCallback<N>`) that determines
|
|
214
214
|
* whether a node matches the desired property.
|
|
215
215
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
216
216
|
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
@@ -223,7 +223,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
223
223
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
224
224
|
* matching node is found.
|
|
225
225
|
*/
|
|
226
|
-
get(identifier, callback =
|
|
226
|
+
get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
227
227
|
var _a;
|
|
228
228
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
229
229
|
}
|
|
@@ -255,11 +255,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
255
255
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
256
256
|
* using either recursive or iterative traversal.
|
|
257
257
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
258
|
-
* of the binary tree node that you want to search for. It can be either a `
|
|
258
|
+
* of the binary tree node that you want to search for. It can be either a `BTNKey` or a
|
|
259
259
|
* generic type `N`.
|
|
260
260
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
261
261
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
262
|
-
* included in the result. The default value for `callback` is `
|
|
262
|
+
* included in the result. The default value for `callback` is `((node: N) => node.key)`, which is
|
|
263
263
|
* a
|
|
264
264
|
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
265
265
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
@@ -272,7 +272,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
272
272
|
* traverse the binary tree. It can have one of the following values:
|
|
273
273
|
* @returns an array of nodes (N[]).
|
|
274
274
|
*/
|
|
275
|
-
getNodes(identifier, callback =
|
|
275
|
+
getNodes(identifier, callback = ((node) => node.key), onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
276
276
|
if (!beginRoot)
|
|
277
277
|
return [];
|
|
278
278
|
const ans = [];
|
|
@@ -287,7 +287,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
287
287
|
if (!cur.left && !cur.right)
|
|
288
288
|
return;
|
|
289
289
|
// TODO potential bug
|
|
290
|
-
if (callback ===
|
|
290
|
+
if (callback === ((node) => node.key)) {
|
|
291
291
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
292
292
|
cur.left && _traverse(cur.left);
|
|
293
293
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
@@ -312,7 +312,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
312
312
|
return ans;
|
|
313
313
|
}
|
|
314
314
|
// TODO potential bug
|
|
315
|
-
if (callback ===
|
|
315
|
+
if (callback === ((node) => node.key)) {
|
|
316
316
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
317
317
|
cur.left && queue.push(cur.left);
|
|
318
318
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
@@ -337,15 +337,15 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
337
337
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
338
338
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
339
339
|
* of the following values:
|
|
340
|
-
* @param {
|
|
340
|
+
* @param {BTNKey | N | null} targetNode - The `targetNode` parameter in the
|
|
341
341
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
342
342
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
343
|
-
* (`
|
|
343
|
+
* (`BTNKey`), or `null` to
|
|
344
344
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
345
345
|
* done recursively or iteratively. It can have two possible values:
|
|
346
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<
|
|
346
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
347
347
|
*/
|
|
348
|
-
lesserOrGreaterTraverse(callback =
|
|
348
|
+
lesserOrGreaterTraverse(callback = ((node) => node.key), lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
349
349
|
if (typeof targetNode === 'number')
|
|
350
350
|
targetNode = this.get(targetNode);
|
|
351
351
|
const ans = [];
|
|
@@ -494,8 +494,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
494
494
|
/**
|
|
495
495
|
* The function compares two values using a comparator function and returns whether the first value
|
|
496
496
|
* is greater than, less than, or equal to the second value.
|
|
497
|
-
* @param {
|
|
498
|
-
* @param {
|
|
497
|
+
* @param {BTNKey} a - The parameter "a" is of type BTNKey.
|
|
498
|
+
* @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
|
|
499
499
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
500
500
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
501
501
|
*/
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BTNKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
|
|
2
2
|
import { IBinaryTree } from '../../interfaces';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
4
|
export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
5
|
-
constructor(key:
|
|
5
|
+
constructor(key: BTNKey, val?: V);
|
|
6
6
|
private _color;
|
|
7
7
|
get color(): RBColor;
|
|
8
8
|
set color(value: RBColor);
|
|
9
9
|
}
|
|
10
10
|
export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
11
11
|
constructor(options?: RBTreeOptions);
|
|
12
|
-
createNode(key:
|
|
12
|
+
createNode(key: BTNKey, val?: V): N;
|
|
13
13
|
}
|