min-heap-typed 1.39.1 → 1.39.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/binary-tree/avl-tree.d.ts +10 -10
- package/dist/data-structures/binary-tree/avl-tree.js +4 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -107
- package/dist/data-structures/binary-tree/binary-tree.js +65 -27
- package/dist/data-structures/binary-tree/bst.d.ts +22 -22
- package/dist/data-structures/binary-tree/bst.js +12 -12
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +14 -14
- package/dist/data-structures/binary-tree/tree-multiset.js +7 -7
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/data-structures/queue/deque.d.ts +20 -20
- package/dist/data-structures/queue/deque.js +22 -22
- package/dist/data-structures/queue/queue.d.ts +3 -3
- package/dist/data-structures/queue/queue.js +3 -3
- 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 +10 -10
- package/src/data-structures/binary-tree/binary-tree.ts +165 -53
- package/src/data-structures/binary-tree/bst.ts +29 -31
- package/src/data-structures/binary-tree/rb-tree.ts +5 -5
- package/src/data-structures/binary-tree/tree-multiset.ts +14 -14
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
- package/src/data-structures/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- 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/helpers.ts +1 -1
|
@@ -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) {
|
|
@@ -130,7 +130,7 @@ class BinaryTree {
|
|
|
130
130
|
}
|
|
131
131
|
/**
|
|
132
132
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
133
|
-
* @param {
|
|
133
|
+
* @param {BTNKey} key - The key for the new node.
|
|
134
134
|
* @param {V} val - The value for the new node.
|
|
135
135
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
136
136
|
*/
|
|
@@ -153,7 +153,7 @@ class BinaryTree {
|
|
|
153
153
|
}
|
|
154
154
|
/**
|
|
155
155
|
* Add a node with the given key and value to the binary tree.
|
|
156
|
-
* @param {
|
|
156
|
+
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
157
157
|
* @param {V} val - The value for the new node (optional).
|
|
158
158
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
159
159
|
*/
|
|
@@ -217,7 +217,7 @@ class BinaryTree {
|
|
|
217
217
|
/**
|
|
218
218
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
219
219
|
* values, and adds them to the binary tree.
|
|
220
|
-
* @param {(
|
|
220
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of BTNKey or BinaryTreeNode
|
|
221
221
|
* objects, or null values.
|
|
222
222
|
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
223
223
|
* 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 +239,8 @@ class BinaryTree {
|
|
|
239
239
|
}
|
|
240
240
|
/**
|
|
241
241
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
242
|
-
* @param {(
|
|
243
|
-
* `
|
|
242
|
+
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
243
|
+
* `BTNKey` or `N` values.
|
|
244
244
|
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
245
245
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
246
246
|
* array. Each value in the `data` array will be assigned to the
|
|
@@ -253,11 +253,11 @@ class BinaryTree {
|
|
|
253
253
|
/**
|
|
254
254
|
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
255
255
|
* with the parent node that needs to be balanced.
|
|
256
|
-
* a key (`
|
|
256
|
+
* a key (`BTNKey`). If it is a key, the function will find the corresponding node in the
|
|
257
257
|
* binary tree.
|
|
258
258
|
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
259
259
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
260
|
-
* `
|
|
260
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
261
261
|
* searching for. It can be a specific key value or any other property of the node.
|
|
262
262
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
263
263
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
@@ -312,10 +312,10 @@ class BinaryTree {
|
|
|
312
312
|
/**
|
|
313
313
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
314
314
|
* specified root node.
|
|
315
|
-
* @param {
|
|
315
|
+
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
316
316
|
* 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 {
|
|
317
|
+
* of the node (`BTNKey`), or `null`.
|
|
318
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
319
319
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
320
320
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
321
321
|
* node of the binary tree.
|
|
@@ -339,9 +339,9 @@ class BinaryTree {
|
|
|
339
339
|
/**
|
|
340
340
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
341
341
|
* iterative approach.
|
|
342
|
-
* @param {
|
|
342
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
343
343
|
* 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 (`
|
|
344
|
+
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
345
345
|
* node is specified. If `
|
|
346
346
|
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
347
347
|
* height of the binary tree using a recursive approach or an iterative approach. It can have two
|
|
@@ -450,7 +450,7 @@ class BinaryTree {
|
|
|
450
450
|
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
451
451
|
* recursive or iterative traversal.
|
|
452
452
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
453
|
-
* `
|
|
453
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
454
454
|
* searching for. It can be a specific key value or any other property of the node.
|
|
455
455
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
456
456
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
@@ -506,8 +506,8 @@ class BinaryTree {
|
|
|
506
506
|
}
|
|
507
507
|
/**
|
|
508
508
|
* 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 `
|
|
509
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
510
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or a
|
|
511
511
|
* generic type `N`.
|
|
512
512
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
513
513
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
@@ -529,8 +529,8 @@ class BinaryTree {
|
|
|
529
529
|
}
|
|
530
530
|
/**
|
|
531
531
|
* 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 `
|
|
532
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
533
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
534
534
|
* type.
|
|
535
535
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
536
536
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
@@ -574,9 +574,9 @@ class BinaryTree {
|
|
|
574
574
|
/**
|
|
575
575
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
576
576
|
* iterative traversal.
|
|
577
|
-
* @param {
|
|
577
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
578
578
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
579
|
-
* of a node (`
|
|
579
|
+
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
580
580
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
581
581
|
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
582
582
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
|
|
@@ -698,12 +698,12 @@ class BinaryTree {
|
|
|
698
698
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
699
699
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
700
700
|
* an array.
|
|
701
|
-
* @param {
|
|
701
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
702
702
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
703
703
|
* start from the root of the tree.
|
|
704
704
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
705
705
|
* performed on the binary tree. It can have two possible values:
|
|
706
|
-
* @returns The function `subTreeTraverse` returns an array of `ReturnType<
|
|
706
|
+
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
707
707
|
*/
|
|
708
708
|
subTreeTraverse(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
709
709
|
if (typeof beginRoot === 'number')
|
|
@@ -743,7 +743,7 @@ class BinaryTree {
|
|
|
743
743
|
* is `null`, an empty array will be returned.
|
|
744
744
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
745
745
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
746
|
-
* @returns The function `dfs` returns an array of `ReturnType<
|
|
746
|
+
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
747
747
|
*/
|
|
748
748
|
dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
|
|
749
749
|
if (!beginRoot)
|
|
@@ -820,13 +820,13 @@ class BinaryTree {
|
|
|
820
820
|
* function on each node.
|
|
821
821
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
822
822
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
823
|
-
* `ReturnType<
|
|
823
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
824
824
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
825
825
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
826
826
|
* will not be performed and an empty array will be returned.
|
|
827
827
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
828
828
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
829
|
-
* @returns The function `bfs` returns an array of `ReturnType<
|
|
829
|
+
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
|
|
830
830
|
*/
|
|
831
831
|
bfs(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
832
832
|
if (!beginRoot)
|
|
@@ -934,7 +934,7 @@ class BinaryTree {
|
|
|
934
934
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
935
935
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
936
936
|
* @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<
|
|
937
|
+
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
|
|
938
938
|
* default value for this parameter is `this._defaultCallbackByKey`.
|
|
939
939
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
940
940
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
@@ -942,7 +942,7 @@ class BinaryTree {
|
|
|
942
942
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
943
943
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
944
944
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
945
|
-
* @returns The `morris` function returns an array of `ReturnType<
|
|
945
|
+
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
946
946
|
*/
|
|
947
947
|
morris(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root) {
|
|
948
948
|
if (beginRoot === null)
|
|
@@ -1028,6 +1028,44 @@ class BinaryTree {
|
|
|
1028
1028
|
}
|
|
1029
1029
|
return ans;
|
|
1030
1030
|
}
|
|
1031
|
+
/**
|
|
1032
|
+
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1033
|
+
* either an iterative or recursive manner.
|
|
1034
|
+
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
1035
|
+
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
1036
|
+
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
1037
|
+
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1038
|
+
* binary tree nodes in a specific order.
|
|
1039
|
+
*/
|
|
1040
|
+
*[Symbol.iterator](node = this.root) {
|
|
1041
|
+
if (!node) {
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
if (this.iterationType === types_1.IterationType.ITERATIVE) {
|
|
1045
|
+
const stack = [];
|
|
1046
|
+
let current = node;
|
|
1047
|
+
while (current || stack.length > 0) {
|
|
1048
|
+
while (current) {
|
|
1049
|
+
stack.push(current);
|
|
1050
|
+
current = current.left;
|
|
1051
|
+
}
|
|
1052
|
+
current = stack.pop();
|
|
1053
|
+
if (current)
|
|
1054
|
+
yield current.key;
|
|
1055
|
+
if (current)
|
|
1056
|
+
current = current.right;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
else {
|
|
1060
|
+
if (node.left) {
|
|
1061
|
+
yield* this[Symbol.iterator](node.left);
|
|
1062
|
+
}
|
|
1063
|
+
yield node.key;
|
|
1064
|
+
if (node.right) {
|
|
1065
|
+
yield* this[Symbol.iterator](node.right);
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1031
1069
|
/**
|
|
1032
1070
|
* Swap the data of two nodes in the binary tree.
|
|
1033
1071
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -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,12 +88,12 @@ 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
|
|
@@ -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
|
|
@@ -255,7 +255,7 @@ 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
|
|
@@ -337,13 +337,13 @@ 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
348
|
lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
349
349
|
if (typeof targetNode === 'number')
|
|
@@ -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
|
}
|
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
import { BinaryTreeDeletedResult, IterationType,
|
|
8
|
+
import type { BTNKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, IterationType, BTNCallback } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
13
13
|
count: number;
|
|
14
14
|
/**
|
|
15
15
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
16
|
-
* @param {
|
|
16
|
+
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
17
17
|
* of the binary tree node.
|
|
18
18
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
19
19
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -21,7 +21,7 @@ export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N>
|
|
|
21
21
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
22
22
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
23
23
|
*/
|
|
24
|
-
constructor(key:
|
|
24
|
+
constructor(key: BTNKey, val?: V, count?: number);
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
@@ -38,19 +38,19 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
38
38
|
get count(): number;
|
|
39
39
|
/**
|
|
40
40
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
41
|
-
* @param {
|
|
41
|
+
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
42
42
|
* distinguish one node from another in the tree.
|
|
43
43
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
44
44
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
45
45
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
46
46
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
47
47
|
*/
|
|
48
|
-
createNode(key:
|
|
48
|
+
createNode(key: BTNKey, val?: V, count?: number): N;
|
|
49
49
|
/**
|
|
50
50
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
51
51
|
* exists, and balancing the tree if necessary.
|
|
52
|
-
* @param {
|
|
53
|
-
* `
|
|
52
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
53
|
+
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
54
54
|
* node to be added), or `null` (which represents a null node).
|
|
55
55
|
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
56
56
|
* added to the binary tree.
|
|
@@ -59,7 +59,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
59
59
|
* count is specified, the default count will be 1.
|
|
60
60
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
61
61
|
*/
|
|
62
|
-
add(keyOrNode:
|
|
62
|
+
add(keyOrNode: BTNKey | N | null, val?: V, count?: number): N | null | undefined;
|
|
63
63
|
/**
|
|
64
64
|
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
65
65
|
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
@@ -72,14 +72,14 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
72
72
|
/**
|
|
73
73
|
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
74
74
|
* inserted nodes.
|
|
75
|
-
* @param {(
|
|
76
|
-
* added to the multiset. Each element can be either a
|
|
75
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
76
|
+
* added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
|
|
77
77
|
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
78
78
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
79
79
|
* each key or node.
|
|
80
80
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
81
81
|
*/
|
|
82
|
-
addMany(keysOrNodes: (
|
|
82
|
+
addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[];
|
|
83
83
|
/**
|
|
84
84
|
* The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
|
|
85
85
|
* binary search tree using either a recursive or iterative approach.
|
|
@@ -93,7 +93,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
93
93
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
94
94
|
* node along with the parent node that needs to be balanced.
|
|
95
95
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
96
|
-
* `
|
|
96
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
97
97
|
* searching for. It can be a specific key value or any other property of the node.
|
|
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 `identifier` parameter to determine if the node should be
|
|
@@ -105,7 +105,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
105
105
|
* decremented by 1 and
|
|
106
106
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
107
107
|
*/
|
|
108
|
-
delete<C extends
|
|
108
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
109
109
|
/**
|
|
110
110
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
111
111
|
*/
|
|
@@ -6,7 +6,7 @@ const avl_tree_1 = require("./avl-tree");
|
|
|
6
6
|
class TreeMultisetNode extends avl_tree_1.AVLTreeNode {
|
|
7
7
|
/**
|
|
8
8
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
9
|
-
* @param {
|
|
9
|
+
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
10
10
|
* of the binary tree node.
|
|
11
11
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
12
12
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -39,7 +39,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
42
|
-
* @param {
|
|
42
|
+
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
43
43
|
* distinguish one node from another in the tree.
|
|
44
44
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
45
45
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
@@ -52,8 +52,8 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
52
52
|
/**
|
|
53
53
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
54
54
|
* exists, and balancing the tree if necessary.
|
|
55
|
-
* @param {
|
|
56
|
-
* `
|
|
55
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
56
|
+
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
57
57
|
* node to be added), or `null` (which represents a null node).
|
|
58
58
|
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
59
59
|
* added to the binary tree.
|
|
@@ -175,8 +175,8 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
175
175
|
/**
|
|
176
176
|
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
177
177
|
* inserted nodes.
|
|
178
|
-
* @param {(
|
|
179
|
-
* added to the multiset. Each element can be either a
|
|
178
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
179
|
+
* added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
|
|
180
180
|
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
181
181
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
182
182
|
* each key or node.
|
|
@@ -246,7 +246,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
246
246
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
247
247
|
* node along with the parent node that needs to be balanced.
|
|
248
248
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
249
|
-
* `
|
|
249
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
250
250
|
* searching for. It can be a specific key value or any other property of the node.
|
|
251
251
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
252
252
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|