data-structure-typed 1.49.5 → 1.49.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +14 -23
- package/benchmark/report.html +14 -23
- package/benchmark/report.json +163 -256
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/cjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +3 -3
- package/dist/cjs/types/common.js +2 -2
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/mjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +3 -3
- package/dist/mjs/types/common.js +2 -2
- package/dist/umd/data-structure-typed.js +497 -419
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +253 -205
- package/src/data-structures/binary-tree/bst.ts +125 -104
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -15
- package/test/performance/data-structures/comparison/comparison.test.ts +13 -36
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
- package/test/performance/data-structures/hash/hash-map.test.ts +11 -34
- package/test/performance/data-structures/heap/heap.test.ts +5 -18
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
- package/test/performance/data-structures/queue/queue.test.ts +8 -25
- package/test/performance/data-structures/stack/stack.test.ts +6 -18
- package/test/performance/data-structures/trie/trie.test.ts +2 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -5
- package/test/unit/data-structures/binary-tree/bst.test.ts +17 -1
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BSTNested,
|
|
8
|
+
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
9
|
import { BSTVariant, CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -45,13 +45,13 @@ export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTN
|
|
|
45
45
|
export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>> extends BinaryTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
46
46
|
/**
|
|
47
47
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
48
|
-
* the tree with optional
|
|
49
|
-
* @param [
|
|
48
|
+
* the tree with optional nodes and options.
|
|
49
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
50
50
|
* binary search tree.
|
|
51
51
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
52
52
|
* configuration options for the binary search tree. It can have the following properties:
|
|
53
53
|
*/
|
|
54
|
-
constructor(
|
|
54
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BSTOptions<K>>);
|
|
55
55
|
protected _root?: N;
|
|
56
56
|
get root(): N | undefined;
|
|
57
57
|
protected _variant: BSTVariant;
|
|
@@ -74,27 +74,53 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
74
74
|
*/
|
|
75
75
|
createTree(options?: Partial<BSTOptions<K>>): TREE;
|
|
76
76
|
/**
|
|
77
|
-
* The function
|
|
78
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
79
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
80
|
-
*/
|
|
81
|
-
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
|
|
82
|
-
/**
|
|
83
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
77
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
84
78
|
* otherwise it returns undefined.
|
|
85
|
-
* @param
|
|
79
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
86
80
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
87
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
81
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
88
82
|
* @returns a node of type N or undefined.
|
|
89
83
|
*/
|
|
90
|
-
exemplarToNode(
|
|
84
|
+
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined;
|
|
85
|
+
/**
|
|
86
|
+
* Time Complexity: O(log n)
|
|
87
|
+
* Space Complexity: O(log n)
|
|
88
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
89
|
+
*/
|
|
90
|
+
/**
|
|
91
|
+
* Time Complexity: O(log n)
|
|
92
|
+
* Space Complexity: O(log n)
|
|
93
|
+
*
|
|
94
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
95
|
+
* otherwise it returns the key itself.
|
|
96
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
97
|
+
* `undefined`.
|
|
98
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
99
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
100
|
+
* @returns either a node object (N) or undefined.
|
|
101
|
+
*/
|
|
102
|
+
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
105
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
106
|
+
* data type.
|
|
107
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
108
|
+
*/
|
|
109
|
+
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
110
|
+
/**
|
|
111
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
112
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
113
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
114
|
+
*/
|
|
115
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
|
|
91
116
|
/**
|
|
92
|
-
* Time Complexity: O(log n)
|
|
93
|
-
* Space Complexity: O(1)
|
|
117
|
+
* Time Complexity: O(log n)
|
|
118
|
+
* Space Complexity: O(1)
|
|
119
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
94
120
|
*/
|
|
95
121
|
/**
|
|
96
|
-
* Time Complexity: O(log n)
|
|
97
|
-
* Space Complexity: O(1)
|
|
122
|
+
* Time Complexity: O(log n)
|
|
123
|
+
* Space Complexity: O(1)
|
|
98
124
|
*
|
|
99
125
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
100
126
|
* or inserting a new node if the key is unique.
|
|
@@ -104,14 +130,15 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
104
130
|
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
105
131
|
* node was not added.
|
|
106
132
|
*/
|
|
107
|
-
add(keyOrNodeOrEntry:
|
|
133
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean;
|
|
108
134
|
/**
|
|
109
|
-
* Time Complexity: O(k log n)
|
|
110
|
-
* Space Complexity: O(k)
|
|
135
|
+
* Time Complexity: O(k log n)
|
|
136
|
+
* Space Complexity: O(k)
|
|
137
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
111
138
|
*/
|
|
112
139
|
/**
|
|
113
|
-
* Time Complexity: O(k log n)
|
|
114
|
-
* Space Complexity: O(k)
|
|
140
|
+
* Time Complexity: O(k log n)
|
|
141
|
+
* Space Complexity: O(k)
|
|
115
142
|
*
|
|
116
143
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
117
144
|
* balancing the tree after each addition.
|
|
@@ -122,41 +149,40 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
122
149
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
123
150
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
124
151
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
125
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
152
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
126
153
|
* in the order they appear in the input.
|
|
127
154
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
128
155
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
129
156
|
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
130
157
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
131
158
|
*/
|
|
132
|
-
addMany(keysOrNodesOrEntries: Iterable<
|
|
159
|
+
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
133
160
|
/**
|
|
134
|
-
* Time Complexity: O(n log n)
|
|
135
|
-
* Space Complexity: O(n)
|
|
161
|
+
* Time Complexity: O(n log n)
|
|
162
|
+
* Space Complexity: O(n)
|
|
163
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
136
164
|
*/
|
|
137
165
|
/**
|
|
138
|
-
* Time Complexity: O(log n)
|
|
139
|
-
* Space Complexity: O(
|
|
166
|
+
* Time Complexity: O(n log n)
|
|
167
|
+
* Space Complexity: O(n)
|
|
140
168
|
*
|
|
141
169
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
142
170
|
* leftmost node if the comparison result is greater than.
|
|
143
171
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
144
172
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
145
173
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
146
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
147
|
-
* be performed. It can have one of the following values:
|
|
148
174
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
149
175
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
150
176
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
151
177
|
*/
|
|
152
|
-
lastKey(beginRoot?:
|
|
178
|
+
lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, N>): K | undefined;
|
|
153
179
|
/**
|
|
154
|
-
* Time Complexity: O(log n)
|
|
155
|
-
* Space Complexity: O(1)
|
|
180
|
+
* Time Complexity: O(log n)
|
|
181
|
+
* Space Complexity: O(1)
|
|
156
182
|
*/
|
|
157
183
|
/**
|
|
158
|
-
* Time Complexity: O(log n)
|
|
159
|
-
* Space Complexity: O(
|
|
184
|
+
* Time Complexity: O(log n)
|
|
185
|
+
* Space Complexity: O(1)
|
|
160
186
|
*
|
|
161
187
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
162
188
|
* either recursive or iterative methods.
|
|
@@ -170,29 +196,14 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
170
196
|
*/
|
|
171
197
|
getNodeByKey(key: K, iterationType?: IterationType): N | undefined;
|
|
172
198
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
*
|
|
181
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
182
|
-
*/
|
|
183
|
-
/**
|
|
184
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
185
|
-
* otherwise it returns the key itself.
|
|
186
|
-
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
187
|
-
* `undefined`.
|
|
188
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
189
|
-
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
190
|
-
* @returns either a node object (N) or undefined.
|
|
191
|
-
*/
|
|
192
|
-
ensureNode(key: BSTNKeyOrNode<K, N>, iterationType?: IterationType): N | undefined;
|
|
193
|
-
/**
|
|
194
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
195
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
199
|
+
* Time Complexity: O(log n)
|
|
200
|
+
* Space Complexity: O(log n)
|
|
201
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
202
|
+
* /
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Time Complexity: O(log n)
|
|
206
|
+
* Space Complexity: O(log n)
|
|
196
207
|
*
|
|
197
208
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
198
209
|
* recursive or iterative approach.
|
|
@@ -213,14 +224,15 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
213
224
|
* performed on the binary tree. It can have two possible values:
|
|
214
225
|
* @returns The method returns an array of nodes (`N[]`).
|
|
215
226
|
*/
|
|
216
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?:
|
|
227
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N[];
|
|
217
228
|
/**
|
|
218
|
-
* Time Complexity: O(log n)
|
|
219
|
-
* Space Complexity: O(log n)
|
|
229
|
+
* Time Complexity: O(log n)
|
|
230
|
+
* Space Complexity: O(log n)
|
|
231
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
220
232
|
*/
|
|
221
233
|
/**
|
|
222
|
-
* Time Complexity: O(log n)
|
|
223
|
-
* Space Complexity: O(log n)
|
|
234
|
+
* Time Complexity: O(log n)
|
|
235
|
+
* Space Complexity: O(log n)
|
|
224
236
|
*
|
|
225
237
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
226
238
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -239,14 +251,14 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
239
251
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
240
252
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
241
253
|
*/
|
|
242
|
-
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?:
|
|
254
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[];
|
|
243
255
|
/**
|
|
244
|
-
* Time Complexity: O(log n)
|
|
245
|
-
* Space Complexity: O(log n)
|
|
256
|
+
* Time Complexity: O(log n)
|
|
257
|
+
* Space Complexity: O(log n)
|
|
246
258
|
*/
|
|
247
259
|
/**
|
|
248
|
-
* Time Complexity: O(n)
|
|
249
|
-
* Space Complexity: O(n)
|
|
260
|
+
* Time Complexity: O(log n)
|
|
261
|
+
* Space Complexity: O(log n)
|
|
250
262
|
*
|
|
251
263
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
252
264
|
* ensures the tree is perfectly balanced.
|
|
@@ -270,8 +282,8 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
270
282
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
271
283
|
*/
|
|
272
284
|
/**
|
|
273
|
-
* Time Complexity: O(n)
|
|
274
|
-
* Space Complexity: O(log n)
|
|
285
|
+
* Time Complexity: O(n)
|
|
286
|
+
* Space Complexity: O(log n)
|
|
275
287
|
*
|
|
276
288
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
277
289
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -56,13 +56,13 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
56
56
|
export class BST extends BinaryTree {
|
|
57
57
|
/**
|
|
58
58
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
59
|
-
* the tree with optional
|
|
60
|
-
* @param [
|
|
59
|
+
* the tree with optional nodes and options.
|
|
60
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
61
61
|
* binary search tree.
|
|
62
62
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
63
63
|
* configuration options for the binary search tree. It can have the following properties:
|
|
64
64
|
*/
|
|
65
|
-
constructor(
|
|
65
|
+
constructor(nodes, options) {
|
|
66
66
|
super([], options);
|
|
67
67
|
if (options) {
|
|
68
68
|
const { variant } = options;
|
|
@@ -71,14 +71,14 @@ export class BST extends BinaryTree {
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
this._root = undefined;
|
|
74
|
-
if (
|
|
75
|
-
this.addMany(
|
|
74
|
+
if (nodes)
|
|
75
|
+
this.addMany(nodes);
|
|
76
76
|
}
|
|
77
77
|
_root;
|
|
78
78
|
get root() {
|
|
79
79
|
return this._root;
|
|
80
80
|
}
|
|
81
|
-
_variant = BSTVariant.
|
|
81
|
+
_variant = BSTVariant.STANDARD;
|
|
82
82
|
get variant() {
|
|
83
83
|
return this._variant;
|
|
84
84
|
}
|
|
@@ -108,31 +108,23 @@ export class BST extends BinaryTree {
|
|
|
108
108
|
});
|
|
109
109
|
}
|
|
110
110
|
/**
|
|
111
|
-
* The function
|
|
112
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
113
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
114
|
-
*/
|
|
115
|
-
isNode(exemplar) {
|
|
116
|
-
return exemplar instanceof BSTNode;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
111
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
120
112
|
* otherwise it returns undefined.
|
|
121
|
-
* @param
|
|
113
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
122
114
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
123
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
115
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
124
116
|
* @returns a node of type N or undefined.
|
|
125
117
|
*/
|
|
126
|
-
exemplarToNode(
|
|
118
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
127
119
|
let node;
|
|
128
|
-
if (
|
|
120
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
129
121
|
return;
|
|
130
122
|
}
|
|
131
|
-
else if (this.isNode(
|
|
132
|
-
node =
|
|
123
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
124
|
+
node = keyOrNodeOrEntry;
|
|
133
125
|
}
|
|
134
|
-
else if (this.isEntry(
|
|
135
|
-
const [key, value] =
|
|
126
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
127
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
136
128
|
if (key === undefined || key === null) {
|
|
137
129
|
return;
|
|
138
130
|
}
|
|
@@ -140,8 +132,8 @@ export class BST extends BinaryTree {
|
|
|
140
132
|
node = this.createNode(key, value);
|
|
141
133
|
}
|
|
142
134
|
}
|
|
143
|
-
else if (this.isNotNodeInstance(
|
|
144
|
-
node = this.createNode(
|
|
135
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
136
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
145
137
|
}
|
|
146
138
|
else {
|
|
147
139
|
return;
|
|
@@ -149,12 +141,62 @@ export class BST extends BinaryTree {
|
|
|
149
141
|
return node;
|
|
150
142
|
}
|
|
151
143
|
/**
|
|
152
|
-
* Time Complexity: O(log n)
|
|
153
|
-
* Space Complexity: O(
|
|
144
|
+
* Time Complexity: O(log n)
|
|
145
|
+
* Space Complexity: O(log n)
|
|
146
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
154
147
|
*/
|
|
155
148
|
/**
|
|
156
|
-
* Time Complexity: O(log n)
|
|
157
|
-
* Space Complexity: O(
|
|
149
|
+
* Time Complexity: O(log n)
|
|
150
|
+
* Space Complexity: O(log n)
|
|
151
|
+
*
|
|
152
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
153
|
+
* otherwise it returns the key itself.
|
|
154
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
155
|
+
* `undefined`.
|
|
156
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
157
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
158
|
+
* @returns either a node object (N) or undefined.
|
|
159
|
+
*/
|
|
160
|
+
ensureNode(keyOrNodeOrEntry, iterationType = IterationType.ITERATIVE) {
|
|
161
|
+
let res;
|
|
162
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
163
|
+
res = keyOrNodeOrEntry;
|
|
164
|
+
}
|
|
165
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
166
|
+
if (keyOrNodeOrEntry[0])
|
|
167
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
if (keyOrNodeOrEntry)
|
|
171
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
172
|
+
}
|
|
173
|
+
return res;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
177
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
178
|
+
* data type.
|
|
179
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
180
|
+
*/
|
|
181
|
+
isNotNodeInstance(potentialKey) {
|
|
182
|
+
return !(potentialKey instanceof BSTNode);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
186
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
187
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
188
|
+
*/
|
|
189
|
+
isNode(keyOrNodeOrEntry) {
|
|
190
|
+
return keyOrNodeOrEntry instanceof BSTNode;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Time Complexity: O(log n)
|
|
194
|
+
* Space Complexity: O(1)
|
|
195
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
196
|
+
*/
|
|
197
|
+
/**
|
|
198
|
+
* Time Complexity: O(log n)
|
|
199
|
+
* Space Complexity: O(1)
|
|
158
200
|
*
|
|
159
201
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
160
202
|
* or inserting a new node if the key is unique.
|
|
@@ -167,11 +209,11 @@ export class BST extends BinaryTree {
|
|
|
167
209
|
add(keyOrNodeOrEntry, value) {
|
|
168
210
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
169
211
|
if (newNode === undefined)
|
|
170
|
-
return;
|
|
212
|
+
return false;
|
|
171
213
|
if (this.root === undefined) {
|
|
172
214
|
this._setRoot(newNode);
|
|
173
215
|
this._size++;
|
|
174
|
-
return
|
|
216
|
+
return true;
|
|
175
217
|
}
|
|
176
218
|
let current = this.root;
|
|
177
219
|
while (current !== undefined) {
|
|
@@ -179,7 +221,7 @@ export class BST extends BinaryTree {
|
|
|
179
221
|
// if (current !== newNode) {
|
|
180
222
|
// The key value is the same but the reference is different, update the value of the existing node
|
|
181
223
|
this._replaceNode(current, newNode);
|
|
182
|
-
return
|
|
224
|
+
return true;
|
|
183
225
|
// } else {
|
|
184
226
|
// The key value is the same and the reference is the same, replace the entire node
|
|
185
227
|
// this._replaceNode(current, newNode);
|
|
@@ -191,7 +233,7 @@ export class BST extends BinaryTree {
|
|
|
191
233
|
current.left = newNode;
|
|
192
234
|
newNode.parent = current;
|
|
193
235
|
this._size++;
|
|
194
|
-
return
|
|
236
|
+
return true;
|
|
195
237
|
}
|
|
196
238
|
current = current.left;
|
|
197
239
|
}
|
|
@@ -200,20 +242,21 @@ export class BST extends BinaryTree {
|
|
|
200
242
|
current.right = newNode;
|
|
201
243
|
newNode.parent = current;
|
|
202
244
|
this._size++;
|
|
203
|
-
return
|
|
245
|
+
return true;
|
|
204
246
|
}
|
|
205
247
|
current = current.right;
|
|
206
248
|
}
|
|
207
249
|
}
|
|
208
|
-
return
|
|
250
|
+
return false;
|
|
209
251
|
}
|
|
210
252
|
/**
|
|
211
|
-
* Time Complexity: O(k log n)
|
|
212
|
-
* Space Complexity: O(k)
|
|
253
|
+
* Time Complexity: O(k log n)
|
|
254
|
+
* Space Complexity: O(k)
|
|
255
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
213
256
|
*/
|
|
214
257
|
/**
|
|
215
|
-
* Time Complexity: O(k log n)
|
|
216
|
-
* Space Complexity: O(k)
|
|
258
|
+
* Time Complexity: O(k log n)
|
|
259
|
+
* Space Complexity: O(k)
|
|
217
260
|
*
|
|
218
261
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
219
262
|
* balancing the tree after each addition.
|
|
@@ -224,7 +267,7 @@ export class BST extends BinaryTree {
|
|
|
224
267
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
225
268
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
226
269
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
227
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
270
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
228
271
|
* in the order they appear in the input.
|
|
229
272
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
230
273
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
@@ -306,20 +349,19 @@ export class BST extends BinaryTree {
|
|
|
306
349
|
return inserted;
|
|
307
350
|
}
|
|
308
351
|
/**
|
|
309
|
-
* Time Complexity: O(n log n)
|
|
310
|
-
* Space Complexity: O(n)
|
|
352
|
+
* Time Complexity: O(n log n)
|
|
353
|
+
* Space Complexity: O(n)
|
|
354
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
311
355
|
*/
|
|
312
356
|
/**
|
|
313
|
-
* Time Complexity: O(log n)
|
|
314
|
-
* Space Complexity: O(
|
|
357
|
+
* Time Complexity: O(n log n)
|
|
358
|
+
* Space Complexity: O(n)
|
|
315
359
|
*
|
|
316
360
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
317
361
|
* leftmost node if the comparison result is greater than.
|
|
318
362
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
319
363
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
320
364
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
321
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
322
|
-
* be performed. It can have one of the following values:
|
|
323
365
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
324
366
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
325
367
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
@@ -328,7 +370,7 @@ export class BST extends BinaryTree {
|
|
|
328
370
|
let current = this.ensureNode(beginRoot);
|
|
329
371
|
if (!current)
|
|
330
372
|
return undefined;
|
|
331
|
-
if (this._variant === BSTVariant.
|
|
373
|
+
if (this._variant === BSTVariant.STANDARD) {
|
|
332
374
|
// For BSTVariant.MIN, find the rightmost node
|
|
333
375
|
while (current.right !== undefined) {
|
|
334
376
|
current = current.right;
|
|
@@ -343,12 +385,12 @@ export class BST extends BinaryTree {
|
|
|
343
385
|
return current.key;
|
|
344
386
|
}
|
|
345
387
|
/**
|
|
346
|
-
* Time Complexity: O(log n)
|
|
347
|
-
* Space Complexity: O(1)
|
|
388
|
+
* Time Complexity: O(log n)
|
|
389
|
+
* Space Complexity: O(1)
|
|
348
390
|
*/
|
|
349
391
|
/**
|
|
350
|
-
* Time Complexity: O(log n)
|
|
351
|
-
* Space Complexity: O(
|
|
392
|
+
* Time Complexity: O(log n)
|
|
393
|
+
* Space Complexity: O(1)
|
|
352
394
|
*
|
|
353
395
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
354
396
|
* either recursive or iterative methods.
|
|
@@ -392,33 +434,14 @@ export class BST extends BinaryTree {
|
|
|
392
434
|
}
|
|
393
435
|
}
|
|
394
436
|
/**
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
405
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
406
|
-
*/
|
|
407
|
-
/**
|
|
408
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
409
|
-
* otherwise it returns the key itself.
|
|
410
|
-
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
411
|
-
* `undefined`.
|
|
412
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
413
|
-
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
414
|
-
* @returns either a node object (N) or undefined.
|
|
415
|
-
*/
|
|
416
|
-
ensureNode(key, iterationType = IterationType.ITERATIVE) {
|
|
417
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
418
|
-
}
|
|
419
|
-
/**
|
|
420
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
421
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
437
|
+
* Time Complexity: O(log n)
|
|
438
|
+
* Space Complexity: O(log n)
|
|
439
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
440
|
+
* /
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Time Complexity: O(log n)
|
|
444
|
+
* Space Complexity: O(log n)
|
|
422
445
|
*
|
|
423
446
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
424
447
|
* recursive or iterative approach.
|
|
@@ -496,12 +519,13 @@ export class BST extends BinaryTree {
|
|
|
496
519
|
return ans;
|
|
497
520
|
}
|
|
498
521
|
/**
|
|
499
|
-
* Time Complexity: O(log n)
|
|
500
|
-
* Space Complexity: O(log n)
|
|
522
|
+
* Time Complexity: O(log n)
|
|
523
|
+
* Space Complexity: O(log n)
|
|
524
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
501
525
|
*/
|
|
502
526
|
/**
|
|
503
|
-
* Time Complexity: O(log n)
|
|
504
|
-
* Space Complexity: O(log n)
|
|
527
|
+
* Time Complexity: O(log n)
|
|
528
|
+
* Space Complexity: O(log n)
|
|
505
529
|
*
|
|
506
530
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
507
531
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -561,12 +585,12 @@ export class BST extends BinaryTree {
|
|
|
561
585
|
}
|
|
562
586
|
}
|
|
563
587
|
/**
|
|
564
|
-
* Time Complexity: O(log n)
|
|
565
|
-
* Space Complexity: O(log n)
|
|
588
|
+
* Time Complexity: O(log n)
|
|
589
|
+
* Space Complexity: O(log n)
|
|
566
590
|
*/
|
|
567
591
|
/**
|
|
568
|
-
* Time Complexity: O(n)
|
|
569
|
-
* Space Complexity: O(n)
|
|
592
|
+
* Time Complexity: O(log n)
|
|
593
|
+
* Space Complexity: O(log n)
|
|
570
594
|
*
|
|
571
595
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
572
596
|
* ensures the tree is perfectly balanced.
|
|
@@ -625,8 +649,8 @@ export class BST extends BinaryTree {
|
|
|
625
649
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
626
650
|
*/
|
|
627
651
|
/**
|
|
628
|
-
* Time Complexity: O(n)
|
|
629
|
-
* Space Complexity: O(log n)
|
|
652
|
+
* Time Complexity: O(n)
|
|
653
|
+
* Space Complexity: O(log n)
|
|
630
654
|
*
|
|
631
655
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
632
656
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -695,7 +719,7 @@ export class BST extends BinaryTree {
|
|
|
695
719
|
_compare(a, b) {
|
|
696
720
|
const extractedA = this.extractor(a);
|
|
697
721
|
const extractedB = this.extractor(b);
|
|
698
|
-
const compared = this.variant === BSTVariant.
|
|
722
|
+
const compared = this.variant === BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA;
|
|
699
723
|
return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
|
|
700
724
|
}
|
|
701
725
|
}
|