data-structure-typed 1.37.2 → 1.37.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 +3 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +44 -38
- package/dist/data-structures/binary-tree/avl-tree.js +46 -40
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +305 -192
- package/dist/data-structures/binary-tree/binary-tree.js +304 -201
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +111 -64
- package/dist/data-structures/binary-tree/bst.js +132 -85
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +49 -41
- package/dist/data-structures/binary-tree/tree-multiset.js +49 -41
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/types/data-structures/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree.js +6 -6
- package/dist/types/data-structures/binary-tree.js.map +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +44 -38
- package/lib/data-structures/binary-tree/avl-tree.js +46 -40
- package/lib/data-structures/binary-tree/binary-tree.d.ts +305 -192
- package/lib/data-structures/binary-tree/binary-tree.js +305 -202
- package/lib/data-structures/binary-tree/bst.d.ts +111 -64
- package/lib/data-structures/binary-tree/bst.js +133 -86
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +49 -41
- package/lib/data-structures/binary-tree/tree-multiset.js +50 -42
- package/lib/types/data-structures/binary-tree.d.ts +2 -2
- package/lib/types/data-structures/binary-tree.js +5 -5
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +46 -40
- package/src/data-structures/binary-tree/binary-tree.ts +328 -207
- package/src/data-structures/binary-tree/bst.ts +135 -88
- package/src/data-structures/binary-tree/tree-multiset.ts +50 -42
- package/src/types/data-structures/binary-tree.ts +2 -2
- package/test/config.ts +1 -0
- package/test/integration/avl-tree.test.ts +7 -8
- package/test/integration/bst.test.ts +17 -16
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -1
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/utils/big-o.ts +2 -1
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -13,7 +13,7 @@ import type {
|
|
|
13
13
|
MapCallback,
|
|
14
14
|
MapCallbackReturn
|
|
15
15
|
} from '../../types';
|
|
16
|
-
import {CP,
|
|
16
|
+
import {CP, IterationType} from '../../types';
|
|
17
17
|
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
18
18
|
import {IBinaryTree} from '../../interfaces';
|
|
19
19
|
import {Queue} from '../queue';
|
|
@@ -25,9 +25,12 @@ export class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBinaryTree<N> {
|
|
28
|
+
|
|
28
29
|
/**
|
|
29
|
-
* The constructor function initializes a binary search tree object with an optional comparator
|
|
30
|
-
*
|
|
30
|
+
* The constructor function initializes a binary search tree object with an optional comparator
|
|
31
|
+
* function.
|
|
32
|
+
* @param {BSTOptions} [options] - An optional object that contains configuration options for the
|
|
33
|
+
* binary search tree.
|
|
31
34
|
*/
|
|
32
35
|
constructor(options?: BSTOptions) {
|
|
33
36
|
super(options);
|
|
@@ -41,10 +44,10 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
41
44
|
|
|
42
45
|
/**
|
|
43
46
|
* The function creates a new binary search tree node with the given key and value.
|
|
44
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
45
|
-
*
|
|
46
|
-
* @param [val] - The `val`
|
|
47
|
-
*
|
|
47
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
|
|
48
|
+
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
49
|
+
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
|
|
50
|
+
* represents the value associated with the node in a binary search tree.
|
|
48
51
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
49
52
|
*/
|
|
50
53
|
override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
@@ -52,13 +55,14 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
/**
|
|
55
|
-
* The `add` function
|
|
56
|
-
*
|
|
57
|
-
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
58
|
-
* (which
|
|
59
|
-
* @param [val] - The `val` parameter is
|
|
60
|
-
*
|
|
61
|
-
* @returns
|
|
58
|
+
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
59
|
+
* into the tree.
|
|
60
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
61
|
+
* `BinaryTreeNodeKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
62
|
+
* @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
|
|
63
|
+
* binary search tree.
|
|
64
|
+
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
65
|
+
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
62
66
|
*/
|
|
63
67
|
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
|
64
68
|
// TODO support node as a parameter
|
|
@@ -127,20 +131,20 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
127
131
|
}
|
|
128
132
|
|
|
129
133
|
/**
|
|
130
|
-
* The `addMany` function
|
|
131
|
-
*
|
|
132
|
-
* @param {[BinaryTreeNodeKey | N
|
|
133
|
-
*
|
|
134
|
-
*
|
|
134
|
+
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
135
|
+
* maintaining balance.
|
|
136
|
+
* @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function
|
|
137
|
+
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
138
|
+
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
139
|
+
* `null
|
|
135
140
|
* @param {N['val'][]} data - The values of tree nodes
|
|
136
141
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
137
|
-
* @
|
|
142
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
143
|
+
* It can have two possible values:
|
|
144
|
+
* @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
|
|
138
145
|
*/
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
data?: N['val'][],
|
|
142
|
-
isBalanceAdd = true
|
|
143
|
-
): (N | null | undefined)[] {
|
|
146
|
+
|
|
147
|
+
override addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd = true, iterationType = this.iterationType): (N | null | undefined)[] {
|
|
144
148
|
// TODO this addMany function is inefficient, it should be optimized
|
|
145
149
|
function hasNoNull(arr: (BinaryTreeNodeKey | null)[] | (N | null)[]): arr is BinaryTreeNodeKey[] | N[] {
|
|
146
150
|
return arr.indexOf(null) === -1;
|
|
@@ -199,7 +203,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
199
203
|
}
|
|
200
204
|
}
|
|
201
205
|
};
|
|
202
|
-
if (
|
|
206
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
203
207
|
recursive(sortedKeysOrNodes, sortedData);
|
|
204
208
|
} else {
|
|
205
209
|
iterative();
|
|
@@ -209,52 +213,80 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
209
213
|
}
|
|
210
214
|
|
|
211
215
|
/**
|
|
212
|
-
* The function returns the first node in
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
216
|
+
* The function returns the first node in the binary tree that matches the given node property and
|
|
217
|
+
* callback.
|
|
218
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the
|
|
219
|
+
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
220
|
+
* value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
|
|
221
|
+
* whether a node matches the desired property.
|
|
222
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
223
|
+
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
224
|
+
* whether the node matches the property or not. If no callback function is provided, the default
|
|
225
|
+
* callback function `_defaultCallbackByKey` is used
|
|
226
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
227
|
+
* the root node from which the search should begin.
|
|
228
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
229
|
+
* be performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
230
|
+
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
231
|
+
* matching node is found.
|
|
218
232
|
*/
|
|
219
|
-
override get(nodeProperty: BinaryTreeNodeKey | N, callback: MapCallback<N> = this._defaultCallbackByKey): N | null {
|
|
220
|
-
return this.getNodes(nodeProperty, callback, true)[0] ?? null;
|
|
233
|
+
override get(nodeProperty: BinaryTreeNodeKey | N, callback: MapCallback<N> = this._defaultCallbackByKey,beginRoot = this.root, iterationType = this.iterationType): N | null {
|
|
234
|
+
return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
221
235
|
}
|
|
222
236
|
|
|
223
237
|
/**
|
|
224
|
-
* The function returns the key of the rightmost node if the comparison
|
|
225
|
-
* leftmost node if the comparison is greater than, and the key of the
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
238
|
+
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
239
|
+
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
240
|
+
* rightmost node otherwise.
|
|
241
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting point for finding the last
|
|
242
|
+
* key in a binary tree. It represents the root node of the subtree from which the search for the
|
|
243
|
+
* last key should begin. If no specific `beginRoot` is provided, the search will start from the root
|
|
244
|
+
* of the entire binary
|
|
245
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
246
|
+
* be performed when finding the last key. It determines whether the iteration should be performed in
|
|
247
|
+
* pre-order, in-order, or post-order.
|
|
248
|
+
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
249
|
+
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
250
|
+
* rightmost node otherwise. If no node is found, it returns 0.
|
|
229
251
|
*/
|
|
230
|
-
lastKey(): BinaryTreeNodeKey {
|
|
231
|
-
if (this._compare(0, 1) === CP.lt) return this.getRightMost()?.key ?? 0;
|
|
232
|
-
else if (this._compare(0, 1) === CP.gt) return this.getLeftMost()?.key ?? 0;
|
|
233
|
-
else return this.getRightMost()?.key ?? 0;
|
|
252
|
+
lastKey(beginRoot: N | null = this.root, iterationType = this.iterationType): BinaryTreeNodeKey {
|
|
253
|
+
if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
254
|
+
else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
|
|
255
|
+
else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
234
256
|
}
|
|
235
257
|
|
|
236
258
|
/**
|
|
237
|
-
* The function `getNodes`
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
* @param
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
* @
|
|
259
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
260
|
+
* using either recursive or iterative traversal.
|
|
261
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property
|
|
262
|
+
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
|
|
263
|
+
* generic type `N`.
|
|
264
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
265
|
+
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
266
|
+
* included in the result. The default value for `callback` is `this._defaultCallbackByKey`, which is
|
|
267
|
+
* a
|
|
268
|
+
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
269
|
+
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
270
|
+
* containing only that node. If set to false (default), the function will continue the traversal and
|
|
271
|
+
* return an array containing all nodes that match the node
|
|
272
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
|
|
273
|
+
* specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
|
|
274
|
+
* is `null`, an empty array will be returned.
|
|
275
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
276
|
+
* traverse the binary tree. It can have one of the following values:
|
|
277
|
+
* @returns an array of nodes (N[]).
|
|
247
278
|
*/
|
|
248
279
|
override getNodes(
|
|
249
280
|
nodeProperty: BinaryTreeNodeKey | N,
|
|
250
281
|
callback: MapCallback<N> = this._defaultCallbackByKey,
|
|
251
282
|
onlyOne = false,
|
|
252
|
-
beginRoot: N | null = this.root
|
|
283
|
+
beginRoot: N | null = this.root,
|
|
284
|
+
iterationType = this.iterationType
|
|
253
285
|
): N[] {
|
|
254
286
|
if (!beginRoot) return [];
|
|
255
287
|
const ans: N[] = [];
|
|
256
288
|
|
|
257
|
-
if (
|
|
289
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
258
290
|
const _traverse = (cur: N) => {
|
|
259
291
|
const callbackResult = callback(cur);
|
|
260
292
|
if (callbackResult === nodeProperty) {
|
|
@@ -302,46 +334,56 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
302
334
|
// --- start additional functions
|
|
303
335
|
|
|
304
336
|
/**
|
|
305
|
-
* The `lesserOrGreaterTraverse` function
|
|
306
|
-
* have a
|
|
307
|
-
* @param callback - The `callback` parameter is a function that
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
* @param lesserOrGreater - The `lesserOrGreater` parameter is
|
|
337
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
338
|
+
* nodes that have a key value lesser or greater than a target key value.
|
|
339
|
+
* @param callback - The `callback` parameter is a function that will be called for each node that
|
|
340
|
+
* meets the condition specified by the `lesserOrGreater` parameter. It takes a node as an argument
|
|
341
|
+
* and returns a value.
|
|
342
|
+
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
343
|
+
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
344
|
+
* of the following values:
|
|
345
|
+
* @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the
|
|
346
|
+
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
347
|
+
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
348
|
+
* (`BinaryTreeNodeKey`), or `null` to
|
|
349
|
+
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
350
|
+
* done recursively or iteratively. It can have two possible values:
|
|
351
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
311
352
|
*/
|
|
312
353
|
lesserOrGreaterTraverse(
|
|
313
354
|
callback: MapCallback<N> = this._defaultCallbackByKey,
|
|
314
355
|
lesserOrGreater: CP = CP.lt,
|
|
315
|
-
|
|
356
|
+
targetNode: N | BinaryTreeNodeKey | null = this.root,
|
|
357
|
+
iterationType = this.iterationType
|
|
316
358
|
): MapCallbackReturn<N> {
|
|
317
|
-
if (typeof
|
|
359
|
+
if (typeof targetNode === 'number') targetNode = this.get(targetNode);
|
|
318
360
|
const ans: MapCallbackReturn<N>[] = [];
|
|
319
|
-
if (!
|
|
320
|
-
const
|
|
321
|
-
if (!this.root) return
|
|
361
|
+
if (!targetNode) return ans;
|
|
362
|
+
const targetKey = targetNode.key;
|
|
363
|
+
if (!this.root) return ans;
|
|
322
364
|
|
|
323
|
-
if (
|
|
365
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
324
366
|
const _traverse = (cur: N) => {
|
|
325
|
-
const compared = this._compare(cur.key,
|
|
367
|
+
const compared = this._compare(cur.key, targetKey);
|
|
326
368
|
if (compared === lesserOrGreater) ans.push(callback(cur));
|
|
327
369
|
|
|
328
370
|
if (!cur.left && !cur.right) return;
|
|
329
|
-
if (cur.left && this._compare(cur.left.key,
|
|
330
|
-
if (cur.right && this._compare(cur.right.key,
|
|
371
|
+
if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater) _traverse(cur.left);
|
|
372
|
+
if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater) _traverse(cur.right);
|
|
331
373
|
};
|
|
332
374
|
|
|
333
375
|
_traverse(this.root);
|
|
334
|
-
return
|
|
376
|
+
return ans;
|
|
335
377
|
} else {
|
|
336
378
|
const queue = new Queue<N>([this.root]);
|
|
337
379
|
while (queue.size > 0) {
|
|
338
380
|
const cur = queue.shift();
|
|
339
381
|
if (cur) {
|
|
340
|
-
const compared = this._compare(cur.key,
|
|
382
|
+
const compared = this._compare(cur.key, targetKey);
|
|
341
383
|
if (compared === lesserOrGreater) ans.push(callback(cur));
|
|
342
384
|
|
|
343
|
-
if (cur.left && this._compare(cur.left.key,
|
|
344
|
-
if (cur.right && this._compare(cur.right.key,
|
|
385
|
+
if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater) queue.push(cur.left);
|
|
386
|
+
if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater) queue.push(cur.right);
|
|
345
387
|
}
|
|
346
388
|
}
|
|
347
389
|
return ans;
|
|
@@ -359,17 +401,20 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
359
401
|
*/
|
|
360
402
|
|
|
361
403
|
/**
|
|
362
|
-
* The `perfectlyBalance` function
|
|
363
|
-
*
|
|
364
|
-
* @
|
|
404
|
+
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
405
|
+
* ensures the tree is perfectly balanced.
|
|
406
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
407
|
+
* type of iteration to use when building a balanced binary search tree. It can have two possible
|
|
408
|
+
* values:
|
|
409
|
+
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
365
410
|
*/
|
|
366
|
-
perfectlyBalance(): boolean {
|
|
411
|
+
perfectlyBalance(iterationType = this.iterationType): boolean {
|
|
367
412
|
const sorted = this.dfs(node => node, 'in'),
|
|
368
413
|
n = sorted.length;
|
|
369
414
|
this.clear();
|
|
370
415
|
|
|
371
416
|
if (sorted.length < 1) return false;
|
|
372
|
-
if (
|
|
417
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
373
418
|
const buildBalanceBST = (l: number, r: number) => {
|
|
374
419
|
if (l > r) return;
|
|
375
420
|
const m = l + Math.floor((r - l) / 2);
|
|
@@ -401,15 +446,17 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
401
446
|
}
|
|
402
447
|
|
|
403
448
|
/**
|
|
404
|
-
* The function
|
|
449
|
+
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
450
|
+
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
451
|
+
* to check if the AVL tree is balanced. It can have two possible values:
|
|
405
452
|
* @returns a boolean value.
|
|
406
453
|
*/
|
|
407
|
-
isAVLBalanced(): boolean {
|
|
454
|
+
isAVLBalanced(iterationType = this.iterationType): boolean {
|
|
408
455
|
if (!this.root) return true;
|
|
409
456
|
|
|
410
457
|
let balanced = true;
|
|
411
458
|
|
|
412
|
-
if (
|
|
459
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
413
460
|
const _height = (cur: N | null | undefined): number => {
|
|
414
461
|
if (!cur) return 0;
|
|
415
462
|
const leftHeight = _height(cur.left),
|
|
@@ -451,12 +498,12 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
451
498
|
protected _comparator: BSTComparator = (a, b) => a - b;
|
|
452
499
|
|
|
453
500
|
/**
|
|
454
|
-
* The function compares two
|
|
455
|
-
* greater than, less than, or equal to the second
|
|
456
|
-
* @param {BinaryTreeNodeKey} a - "a" is
|
|
457
|
-
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code
|
|
458
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
459
|
-
* than), or CP.eq (equal).
|
|
501
|
+
* The function compares two values using a comparator function and returns whether the first value
|
|
502
|
+
* is greater than, less than, or equal to the second value.
|
|
503
|
+
* @param {BinaryTreeNodeKey} a - The parameter "a" is of type BinaryTreeNodeKey.
|
|
504
|
+
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code represents a BinaryTreeNodeKey.
|
|
505
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
506
|
+
* than), CP.lt (less than), or CP.eq (equal).
|
|
460
507
|
*/
|
|
461
508
|
protected _compare(a: BinaryTreeNodeKey, b: BinaryTreeNodeKey): CP {
|
|
462
509
|
const compared = this._comparator(a, b);
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
|
|
9
|
-
import {BinaryTreeDeletedResult, CP, FamilyPosition,
|
|
9
|
+
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType} from '../../types';
|
|
10
10
|
import {IBinaryTree} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
12
|
|
|
@@ -69,11 +69,11 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
|
-
* The function swaps the
|
|
73
|
-
* @param {N} srcNode - The source node that
|
|
74
|
-
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
75
|
-
* be swapped
|
|
76
|
-
* @returns the `destNode` after swapping its
|
|
72
|
+
* The function swaps the values of two nodes in a binary tree.
|
|
73
|
+
* @param {N} srcNode - The source node that needs to be swapped with the destination node.
|
|
74
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
75
|
+
* from `srcNode` will be swapped into.
|
|
76
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
77
77
|
*/
|
|
78
78
|
protected override _swap(srcNode: N, destNode: N): N {
|
|
79
79
|
const {key, val, count, height} = destNode;
|
|
@@ -96,14 +96,17 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
/**
|
|
99
|
-
* The `add` function adds a new node to a binary search tree,
|
|
100
|
-
* necessary.
|
|
101
|
-
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a
|
|
102
|
-
* represents a `
|
|
103
|
-
*
|
|
104
|
-
* @param
|
|
105
|
-
*
|
|
106
|
-
* @
|
|
99
|
+
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
100
|
+
* exists, and balancing the tree if necessary.
|
|
101
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
102
|
+
* `BinaryTreeNodeKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
103
|
+
* node to be added), or `null` (which represents a null node).
|
|
104
|
+
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
105
|
+
* added to the binary tree.
|
|
106
|
+
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
107
|
+
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
108
|
+
* count is specified, the default count will be 1.
|
|
109
|
+
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
107
110
|
*/
|
|
108
111
|
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count = 1): N | null | undefined {
|
|
109
112
|
let inserted: N | null | undefined = undefined,
|
|
@@ -174,13 +177,12 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
174
177
|
}
|
|
175
178
|
|
|
176
179
|
/**
|
|
177
|
-
* The function adds a new node to a binary tree if there is an available slot
|
|
178
|
-
* node
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
* @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
180
|
+
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
181
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
182
|
+
* the tree. It can be either a node object (`N`) or `null`.
|
|
183
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
184
|
+
* be added as a child.
|
|
185
|
+
* @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
184
186
|
*/
|
|
185
187
|
override _addTo(newNode: N | null, parent: N): N | null | undefined {
|
|
186
188
|
if (parent) {
|
|
@@ -208,13 +210,13 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
208
210
|
}
|
|
209
211
|
|
|
210
212
|
/**
|
|
211
|
-
* The `addMany` function
|
|
212
|
-
*
|
|
213
|
-
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of
|
|
214
|
-
*
|
|
215
|
-
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values
|
|
216
|
-
* the nodes being added. It is used
|
|
217
|
-
*
|
|
213
|
+
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
214
|
+
* inserted nodes.
|
|
215
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
216
|
+
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
|
|
217
|
+
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
|
|
218
|
+
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
219
|
+
* each key or node.
|
|
218
220
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
219
221
|
*/
|
|
220
222
|
override addMany(
|
|
@@ -242,18 +244,21 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
242
244
|
}
|
|
243
245
|
|
|
244
246
|
/**
|
|
245
|
-
* The `perfectlyBalance` function
|
|
246
|
-
*
|
|
247
|
-
* @
|
|
247
|
+
* The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
|
|
248
|
+
* binary search tree using either a recursive or iterative approach.
|
|
249
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
250
|
+
* type of iteration to use when building a balanced binary search tree. It can have two possible
|
|
251
|
+
* values:
|
|
252
|
+
* @returns a boolean value.
|
|
248
253
|
*/
|
|
249
|
-
override perfectlyBalance(): boolean {
|
|
254
|
+
override perfectlyBalance(iterationType = this.iterationType): boolean {
|
|
250
255
|
const sorted = this.dfs(node => node, 'in'),
|
|
251
256
|
n = sorted.length;
|
|
252
257
|
if (sorted.length < 1) return false;
|
|
253
258
|
|
|
254
259
|
this.clear();
|
|
255
260
|
|
|
256
|
-
if (
|
|
261
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
257
262
|
const buildBalanceBST = (l: number, r: number) => {
|
|
258
263
|
if (l > r) return;
|
|
259
264
|
const m = l + Math.floor((r - l) / 2);
|
|
@@ -285,13 +290,16 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
285
290
|
}
|
|
286
291
|
|
|
287
292
|
/**
|
|
288
|
-
* The `delete` function
|
|
289
|
-
* node that needs to be balanced.
|
|
290
|
-
* @param {N | BinaryTreeNodeKey
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
293
|
+
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
294
|
+
* node along with the parent node that needs to be balanced.
|
|
295
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
|
|
296
|
+
* (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted
|
|
297
|
+
* from the binary tree.
|
|
298
|
+
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
299
|
+
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
300
|
+
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
301
|
+
* decremented by 1 and
|
|
302
|
+
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
295
303
|
*/
|
|
296
304
|
override delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount = false): BinaryTreeDeletedResult<N>[] {
|
|
297
305
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
@@ -350,7 +358,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
350
358
|
}
|
|
351
359
|
|
|
352
360
|
/**
|
|
353
|
-
* The clear() function clears the data and sets the count to
|
|
361
|
+
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
354
362
|
*/
|
|
355
363
|
clear() {
|
|
356
364
|
super.clear();
|
|
@@ -358,7 +366,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
358
366
|
}
|
|
359
367
|
|
|
360
368
|
/**
|
|
361
|
-
* The function
|
|
369
|
+
* The function sets the value of the "_count" property.
|
|
362
370
|
* @param {number} v - number
|
|
363
371
|
*/
|
|
364
372
|
protected _setCount(v: number) {
|
|
@@ -7,7 +7,7 @@ import {BinaryTreeNode} from '../../data-structures/binary-tree';
|
|
|
7
7
|
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
export enum
|
|
10
|
+
export enum IterationType {
|
|
11
11
|
ITERATIVE = 'ITERATIVE',
|
|
12
12
|
RECURSIVE = 'RECURSIVE'
|
|
13
13
|
}
|
|
@@ -44,4 +44,4 @@ export type BinaryTreeNodeProperties<N extends BinaryTreeNode<N['val'], N>> =
|
|
|
44
44
|
|
|
45
45
|
export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
46
46
|
|
|
47
|
-
export type BinaryTreeOptions = {
|
|
47
|
+
export type BinaryTreeOptions = { iterationType?: IterationType }
|
package/test/config.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const isDebugTest = true;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {AVLTree} from 'avl-tree-typed';
|
|
2
|
-
import {CP} from "data-structure-typed";
|
|
1
|
+
import {AVLTree, CP} from 'avl-tree-typed';
|
|
3
2
|
|
|
4
3
|
describe('AVL Tree Test', () => {
|
|
5
4
|
it('should perform various operations on a AVL Tree', () => {
|
|
@@ -13,7 +12,7 @@ describe('AVL Tree Test', () => {
|
|
|
13
12
|
expect(node6 && tree.getHeight(node6)).toBe(3);
|
|
14
13
|
expect(node6 && tree.getDepth(node6)).toBe(1);
|
|
15
14
|
|
|
16
|
-
const getNodeById = tree.get(10
|
|
15
|
+
const getNodeById = tree.get(10);
|
|
17
16
|
expect(getNodeById?.key).toBe(10);
|
|
18
17
|
|
|
19
18
|
const getMinNodeByRoot = tree.getLeftMost();
|
|
@@ -24,22 +23,22 @@ describe('AVL Tree Test', () => {
|
|
|
24
23
|
expect(getMinNodeBySpecificNode?.key).toBe(12);
|
|
25
24
|
|
|
26
25
|
let subTreeSum = 0;
|
|
27
|
-
node15 && tree.
|
|
26
|
+
node15 && tree.subTreeTraverse(node => (subTreeSum += node.key), 15);
|
|
28
27
|
expect(subTreeSum).toBe(70);
|
|
29
28
|
|
|
30
29
|
let lesserSum = 0;
|
|
31
|
-
tree.
|
|
30
|
+
tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), CP.lt, 10);
|
|
32
31
|
expect(lesserSum).toBe(45);
|
|
33
32
|
|
|
34
33
|
// node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
|
|
35
34
|
expect(node15?.val).toBe(15);
|
|
36
35
|
|
|
37
|
-
const dfs = tree.dfs(
|
|
36
|
+
const dfs = tree.dfs(node => node, 'in');
|
|
38
37
|
expect(dfs[0].key).toBe(1);
|
|
39
38
|
expect(dfs[dfs.length - 1].key).toBe(16);
|
|
40
39
|
|
|
41
40
|
tree.perfectlyBalance();
|
|
42
|
-
const bfs = tree.bfs(
|
|
41
|
+
const bfs = tree.bfs(node => node);
|
|
43
42
|
expect(tree.isPerfectlyBalanced()).toBe(true);
|
|
44
43
|
expect(bfs[0].key).toBe(8);
|
|
45
44
|
expect(bfs[bfs.length - 1].key).toBe(16);
|
|
@@ -103,7 +102,7 @@ describe('AVL Tree Test', () => {
|
|
|
103
102
|
expect(lastBFSIds[1]).toBe(2);
|
|
104
103
|
expect(lastBFSIds[2]).toBe(16);
|
|
105
104
|
|
|
106
|
-
const lastBFSNodes = tree.bfs(
|
|
105
|
+
const lastBFSNodes = tree.bfs(node => node);
|
|
107
106
|
expect(lastBFSNodes[0].key).toBe(12);
|
|
108
107
|
expect(lastBFSNodes[1].key).toBe(2);
|
|
109
108
|
expect(lastBFSNodes[2].key).toBe(16);
|