data-structure-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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +11 -11
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +8 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +50 -117
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +43 -51
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +23 -23
- package/dist/cjs/data-structures/binary-tree/bst.js +18 -18
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +15 -15
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +9 -9
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/helpers.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +11 -11
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +8 -6
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +50 -117
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +43 -51
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +23 -23
- package/dist/mjs/data-structures/binary-tree/bst.js +18 -18
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +15 -15
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +9 -9
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/helpers.d.ts +1 -1
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- 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
- package/test/integration/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +14 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -2
- package/test/unit/data-structures/hash/coordinate-map.test.ts +2 -2
- package/test/unit/data-structures/hash/coordinate-set.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -6
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +4 -7
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +2 -2
- package/test/utils/big-o.ts +1 -1
|
@@ -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
|
}
|
|
@@ -28,7 +28,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
30
|
* The function creates a new binary search tree node with the given key and value.
|
|
31
|
-
* @param {
|
|
31
|
+
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
32
32
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
33
33
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
|
|
34
34
|
* represents the value associated with the node in a binary search tree.
|
|
@@ -40,8 +40,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
40
40
|
/**
|
|
41
41
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
42
42
|
* into the tree.
|
|
43
|
-
* @param {
|
|
44
|
-
* `
|
|
43
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
44
|
+
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
45
45
|
* @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
|
|
46
46
|
* binary search tree.
|
|
47
47
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
@@ -125,9 +125,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
125
125
|
/**
|
|
126
126
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
127
127
|
* maintaining balance.
|
|
128
|
-
* @param {[
|
|
128
|
+
* @param {[BTNKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
129
129
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
130
|
-
* array of `
|
|
130
|
+
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
131
131
|
* `null
|
|
132
132
|
* @param {V[]} data - The values of tree nodes
|
|
133
133
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
@@ -209,7 +209,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
209
209
|
* callback.
|
|
210
210
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
211
211
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
212
|
-
* value (`
|
|
212
|
+
* value (`BTNKey`) or a custom callback function (`BTNCallback<N>`) that determines
|
|
213
213
|
* whether a node matches the desired property.
|
|
214
214
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
215
215
|
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
@@ -222,7 +222,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
222
222
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
223
223
|
* matching node is found.
|
|
224
224
|
*/
|
|
225
|
-
get(identifier, callback =
|
|
225
|
+
get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
226
226
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
227
227
|
}
|
|
228
228
|
/**
|
|
@@ -252,11 +252,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
252
252
|
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
253
253
|
* using either recursive or iterative traversal.
|
|
254
254
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
|
|
255
|
-
* of the binary tree node that you want to search for. It can be either a `
|
|
255
|
+
* of the binary tree node that you want to search for. It can be either a `BTNKey` or a
|
|
256
256
|
* generic type `N`.
|
|
257
257
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
258
258
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
259
|
-
* included in the result. The default value for `callback` is `
|
|
259
|
+
* included in the result. The default value for `callback` is `((node: N) => node.key)`, which is
|
|
260
260
|
* a
|
|
261
261
|
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
262
262
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
@@ -269,7 +269,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
269
269
|
* traverse the binary tree. It can have one of the following values:
|
|
270
270
|
* @returns an array of nodes (N[]).
|
|
271
271
|
*/
|
|
272
|
-
getNodes(identifier, callback =
|
|
272
|
+
getNodes(identifier, callback = ((node) => node.key), onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
273
273
|
if (!beginRoot)
|
|
274
274
|
return [];
|
|
275
275
|
const ans = [];
|
|
@@ -284,7 +284,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
284
284
|
if (!cur.left && !cur.right)
|
|
285
285
|
return;
|
|
286
286
|
// TODO potential bug
|
|
287
|
-
if (callback ===
|
|
287
|
+
if (callback === ((node) => node.key)) {
|
|
288
288
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
289
289
|
cur.left && _traverse(cur.left);
|
|
290
290
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
@@ -309,7 +309,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
309
309
|
return ans;
|
|
310
310
|
}
|
|
311
311
|
// TODO potential bug
|
|
312
|
-
if (callback ===
|
|
312
|
+
if (callback === ((node) => node.key)) {
|
|
313
313
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
314
314
|
cur.left && queue.push(cur.left);
|
|
315
315
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
@@ -334,15 +334,15 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
334
334
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
335
335
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
336
336
|
* of the following values:
|
|
337
|
-
* @param {
|
|
337
|
+
* @param {BTNKey | N | null} targetNode - The `targetNode` parameter in the
|
|
338
338
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
339
339
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
340
|
-
* (`
|
|
340
|
+
* (`BTNKey`), or `null` to
|
|
341
341
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
342
342
|
* done recursively or iteratively. It can have two possible values:
|
|
343
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<
|
|
343
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
344
344
|
*/
|
|
345
|
-
lesserOrGreaterTraverse(callback =
|
|
345
|
+
lesserOrGreaterTraverse(callback = ((node) => node.key), lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
346
346
|
if (typeof targetNode === 'number')
|
|
347
347
|
targetNode = this.get(targetNode);
|
|
348
348
|
const ans = [];
|
|
@@ -491,8 +491,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
491
491
|
/**
|
|
492
492
|
* The function compares two values using a comparator function and returns whether the first value
|
|
493
493
|
* is greater than, less than, or equal to the second value.
|
|
494
|
-
* @param {
|
|
495
|
-
* @param {
|
|
494
|
+
* @param {BTNKey} a - The parameter "a" is of type BTNKey.
|
|
495
|
+
* @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
|
|
496
496
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
497
497
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
498
498
|
*/
|
|
@@ -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,19 +93,19 @@ 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
|
|
100
100
|
* included in the result. The `callback` parameter has a default value of
|
|
101
|
-
* `
|
|
101
|
+
* `((node: N) => node.key)`
|
|
102
102
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
103
103
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
104
104
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
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
|
*/
|
|
@@ -7,7 +7,7 @@ class TreeMultisetNode extends avl_tree_1.AVLTreeNode {
|
|
|
7
7
|
count;
|
|
8
8
|
/**
|
|
9
9
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
10
|
-
* @param {
|
|
10
|
+
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
11
11
|
* of the binary tree node.
|
|
12
12
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
13
13
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -40,7 +40,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
43
|
-
* @param {
|
|
43
|
+
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
44
44
|
* distinguish one node from another in the tree.
|
|
45
45
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
46
46
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
@@ -53,8 +53,8 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
53
53
|
/**
|
|
54
54
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
55
55
|
* exists, and balancing the tree if necessary.
|
|
56
|
-
* @param {
|
|
57
|
-
* `
|
|
56
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
57
|
+
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
58
58
|
* node to be added), or `null` (which represents a null node).
|
|
59
59
|
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
60
60
|
* added to the binary tree.
|
|
@@ -176,8 +176,8 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
176
176
|
/**
|
|
177
177
|
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
178
178
|
* inserted nodes.
|
|
179
|
-
* @param {(
|
|
180
|
-
* added to the multiset. Each element can be either a
|
|
179
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
180
|
+
* added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
|
|
181
181
|
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
182
182
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
183
183
|
* each key or node.
|
|
@@ -247,19 +247,19 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
247
247
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
248
248
|
* node along with the parent node that needs to be balanced.
|
|
249
249
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
250
|
-
* `
|
|
250
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
251
251
|
* searching for. It can be a specific key value or any other property of the node.
|
|
252
252
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
253
253
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
254
254
|
* included in the result. The `callback` parameter has a default value of
|
|
255
|
-
* `
|
|
255
|
+
* `((node: N) => node.key)`
|
|
256
256
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
257
257
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
258
258
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
259
259
|
* decremented by 1 and
|
|
260
260
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
261
261
|
*/
|
|
262
|
-
delete(identifier, callback =
|
|
262
|
+
delete(identifier, callback = ((node) => node.key), ignoreCount = false) {
|
|
263
263
|
const bstDeletedResult = [];
|
|
264
264
|
if (!this.root)
|
|
265
265
|
return bstDeletedResult;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeletedResult,
|
|
2
|
+
import { BinaryTreeDeletedResult, BTNKey, BinaryTreeNodeNested, BTNCallback } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
4
|
-
createNode(key:
|
|
5
|
-
add(keyOrNode:
|
|
6
|
-
delete<C extends
|
|
4
|
+
createNode(key: BTNKey, val?: N['val']): N;
|
|
5
|
+
add(keyOrNode: BTNKey | N | null, val?: N['val']): N | null | undefined;
|
|
6
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
7
7
|
}
|
|
@@ -18,7 +18,7 @@ export declare enum FamilyPosition {
|
|
|
18
18
|
ISOLATED = "ISOLATED",
|
|
19
19
|
MAL_NODE = "MAL_NODE"
|
|
20
20
|
}
|
|
21
|
-
export type
|
|
21
|
+
export type BTNKey = number;
|
|
22
22
|
export type BinaryTreeDeletedResult<N> = {
|
|
23
23
|
deleted: N | null | undefined;
|
|
24
24
|
needBalanced: N | null;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BSTNode } from '../../../data-structures';
|
|
2
|
-
import type {
|
|
3
|
-
export type BSTComparator = (a:
|
|
2
|
+
import type { BTNKey, BinaryTreeOptions } from './binary-tree';
|
|
3
|
+
export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
|
|
4
4
|
export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
5
|
export type BSTOptions = BinaryTreeOptions & {
|
|
6
6
|
comparator?: BSTComparator;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
2
2
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
3
|
-
export type
|
|
3
|
+
export type BTNCallback<N, D = any> = (node: N) => D;
|
|
4
4
|
export declare enum CP {
|
|
5
5
|
lt = "lt",
|
|
6
6
|
eq = "eq",
|