data-structure-typed 1.36.7 → 1.36.9
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/.eslintrc.js +1 -1
- package/CHANGELOG.md +3 -1
- package/README.md +8 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -5
- package/dist/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -85
- package/dist/data-structures/binary-tree/binary-tree.js +76 -128
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -16
- package/dist/data-structures/binary-tree/tree-multiset.js +10 -20
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +3 -3
- package/dist/data-structures/hash/hash-table.js +3 -3
- package/dist/data-structures/heap/heap.d.ts +136 -11
- package/dist/data-structures/heap/heap.js +293 -13
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -3
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -3
- package/dist/data-structures/queue/deque.d.ts +2 -2
- package/dist/data-structures/queue/deque.js +2 -2
- package/dist/data-structures/queue/queue.js +1 -1
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +5 -5
- package/lib/data-structures/binary-tree/avl-tree.js +6 -6
- package/lib/data-structures/binary-tree/binary-tree.d.ts +18 -85
- package/lib/data-structures/binary-tree/binary-tree.js +76 -128
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +9 -16
- package/lib/data-structures/binary-tree/tree-multiset.js +10 -20
- package/lib/data-structures/hash/hash-map.d.ts +1 -1
- package/lib/data-structures/hash/hash-map.js +1 -1
- package/lib/data-structures/hash/hash-table.d.ts +3 -3
- package/lib/data-structures/hash/hash-table.js +3 -3
- package/lib/data-structures/heap/heap.d.ts +136 -11
- package/lib/data-structures/heap/heap.js +290 -12
- package/lib/data-structures/linked-list/skip-linked-list.d.ts +3 -3
- package/lib/data-structures/linked-list/skip-linked-list.js +3 -3
- package/lib/data-structures/queue/deque.d.ts +2 -2
- package/lib/data-structures/queue/deque.js +2 -2
- package/lib/data-structures/queue/queue.js +1 -1
- package/lib/data-structures/trie/trie.d.ts +2 -2
- package/lib/data-structures/trie/trie.js +2 -2
- package/lib/interfaces/binary-tree.d.ts +1 -1
- package/package.json +7 -6
- package/src/data-structures/binary-tree/avl-tree.ts +6 -6
- package/src/data-structures/binary-tree/binary-tree.ts +79 -214
- package/src/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +10 -21
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +340 -16
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -3
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/test/types/index.ts +1 -0
- package/test/types/utils/big-o.ts +1 -0
- package/test/types/utils/index.ts +1 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -14
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +28 -28
- package/test/unit/data-structures/binary-tree/overall.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +28 -28
- package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-table.test.ts +5 -5
- package/test/unit/data-structures/heap/heap.test.ts +192 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +7 -7
- package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +3 -3
- package/test/unit/data-structures/trie/trie.test.ts +5 -5
- package/test/utils/big-o.ts +199 -0
- package/test/utils/index.ts +1 -1
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
- package/test/utils/magnitude.ts +0 -21
|
@@ -55,17 +55,18 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
55
55
|
get size(): number;
|
|
56
56
|
private _loopType;
|
|
57
57
|
get loopType(): LoopType;
|
|
58
|
+
set loopType(v: LoopType);
|
|
58
59
|
visitedKey: BinaryTreeNodeKey[];
|
|
59
60
|
visitedVal: N['val'][];
|
|
60
61
|
visitedNode: N[];
|
|
61
62
|
/**
|
|
62
|
-
* The `
|
|
63
|
-
* @param {N} srcNode - The source node that you want to
|
|
63
|
+
* The `_swap` function swaps the location of two nodes in a binary tree.
|
|
64
|
+
* @param {N} srcNode - The source node that you want to _swap with the destination node.
|
|
64
65
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
65
66
|
* be swapped to.
|
|
66
67
|
* @returns The `destNode` is being returned.
|
|
67
68
|
*/
|
|
68
|
-
|
|
69
|
+
protected _swap(srcNode: N, destNode: N): N;
|
|
69
70
|
/**
|
|
70
71
|
* The clear() function resets the root, size, and maxKey properties to their initial values.
|
|
71
72
|
*/
|
|
@@ -111,13 +112,13 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
111
112
|
*/
|
|
112
113
|
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean;
|
|
113
114
|
/**
|
|
114
|
-
* The `
|
|
115
|
+
* The `delete` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
|
|
115
116
|
* containing the deleted node and the node that needs to be balanced.
|
|
116
117
|
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
|
|
117
118
|
* node ID (`BinaryTreeNodeKey`).
|
|
118
|
-
* @returns The function `
|
|
119
|
+
* @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
119
120
|
*/
|
|
120
|
-
|
|
121
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
121
122
|
/**
|
|
122
123
|
* The function calculates the depth of a node in a binary tree.
|
|
123
124
|
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter can be any node of the tree
|
|
@@ -236,10 +237,10 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
236
237
|
getRightMost(beginRoot: N): N;
|
|
237
238
|
/**
|
|
238
239
|
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
239
|
-
* @param {N | null}
|
|
240
|
+
* @param {N | null} subTreeRoot - The `node` parameter represents the root node of a binary search tree (BST).
|
|
240
241
|
* @returns a boolean value.
|
|
241
242
|
*/
|
|
242
|
-
isSubtreeBST(
|
|
243
|
+
isSubtreeBST(subTreeRoot: N | null): boolean;
|
|
243
244
|
/**
|
|
244
245
|
* The function isBST checks if the binary tree is valid binary search tree.
|
|
245
246
|
* @returns The `isBST()` function is returning a boolean value.
|
|
@@ -263,16 +264,14 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
263
264
|
*/
|
|
264
265
|
subTreeSum(subTreeRoot: N | BinaryTreeNodeKey | null, propertyName?: BinaryTreeNodePropertyName): number;
|
|
265
266
|
/**
|
|
266
|
-
* The function `
|
|
267
|
+
* The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
|
|
267
268
|
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
268
269
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
269
|
-
* @param
|
|
270
|
-
* each node in the subtree should be incremented.
|
|
271
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
270
|
+
* @param callBack - The `callBack` parameter is a function that takes a node as a parameter and returns a number.
|
|
272
271
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
273
272
|
* @returns a boolean value.
|
|
274
273
|
*/
|
|
275
|
-
|
|
274
|
+
subTreeForeach(subTreeRoot: N | BinaryTreeNodeKey | null, callback: (node: N) => any): boolean;
|
|
276
275
|
/**
|
|
277
276
|
* Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on their 'key' property.
|
|
278
277
|
* @returns An array of binary tree node IDs.
|
|
@@ -311,87 +310,26 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
311
310
|
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
312
311
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
313
312
|
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
313
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
314
314
|
* @returns An array of values corresponding to the specified property.
|
|
315
315
|
*/
|
|
316
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
316
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key', loopType?: LoopType): BinaryTreeNodeKey[];
|
|
317
317
|
/**
|
|
318
318
|
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
319
319
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
320
320
|
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
321
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
321
322
|
* @returns An array of 'val' properties from each node.
|
|
322
323
|
*/
|
|
323
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[];
|
|
324
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val', loopType?: LoopType): N[];
|
|
324
325
|
/**
|
|
325
326
|
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
|
|
326
327
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
327
328
|
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
329
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
328
330
|
* @returns An array of binary tree nodes.
|
|
329
331
|
*/
|
|
330
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
331
|
-
/**
|
|
332
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
333
|
-
* @returns An array of binary tree node IDs.
|
|
334
|
-
*/
|
|
335
|
-
dfsIterative(): BinaryTreeNodeKey[];
|
|
336
|
-
/**
|
|
337
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
338
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
339
|
-
* @returns An array of values corresponding to the specified property.
|
|
340
|
-
*/
|
|
341
|
-
dfsIterative(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
|
|
342
|
-
/**
|
|
343
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
344
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
345
|
-
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
346
|
-
* @returns An array of values corresponding to the specified property.
|
|
347
|
-
*/
|
|
348
|
-
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
349
|
-
/**
|
|
350
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
351
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
352
|
-
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
353
|
-
* @returns An array of 'val' properties from each node.
|
|
354
|
-
*/
|
|
355
|
-
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N['val'][];
|
|
356
|
-
/**
|
|
357
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
|
|
358
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
359
|
-
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
360
|
-
* @returns An array of binary tree nodes.
|
|
361
|
-
*/
|
|
362
|
-
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
363
|
-
/**
|
|
364
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'key' property.
|
|
365
|
-
* @returns An array of binary tree node IDs.
|
|
366
|
-
*/
|
|
367
|
-
levelIterative(): BinaryTreeNodeKey[];
|
|
368
|
-
/**
|
|
369
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'key' property.
|
|
370
|
-
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
|
|
371
|
-
* @returns An array of binary tree node IDs.
|
|
372
|
-
*/
|
|
373
|
-
levelIterative(node: N | null): BinaryTreeNodeKey[];
|
|
374
|
-
/**
|
|
375
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on the specified property name.
|
|
376
|
-
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
|
|
377
|
-
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
378
|
-
* @returns An array of values corresponding to the specified property.
|
|
379
|
-
*/
|
|
380
|
-
levelIterative(node: N | null, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
381
|
-
/**
|
|
382
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates the 'val' property of each node.
|
|
383
|
-
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
|
|
384
|
-
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
385
|
-
* @returns An array of 'val' properties from each node.
|
|
386
|
-
*/
|
|
387
|
-
levelIterative(node: N | null, nodeOrPropertyName: 'val'): N['val'][];
|
|
388
|
-
/**
|
|
389
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates nodes themselves.
|
|
390
|
-
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
|
|
391
|
-
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
392
|
-
* @returns An array of binary tree nodes.
|
|
393
|
-
*/
|
|
394
|
-
levelIterative(node: N | null, nodeOrPropertyName: 'node'): N[];
|
|
332
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node', loopType?: LoopType): N[];
|
|
395
333
|
/**
|
|
396
334
|
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
397
335
|
* @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
|
|
@@ -477,11 +415,6 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
477
415
|
* undefined.
|
|
478
416
|
*/
|
|
479
417
|
protected _addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
480
|
-
/**
|
|
481
|
-
* The function sets the loop type for a protected variable.
|
|
482
|
-
* @param {LoopType} value - The value parameter is of type LoopType.
|
|
483
|
-
*/
|
|
484
|
-
protected _setLoopType(value: LoopType): void;
|
|
485
418
|
/**
|
|
486
419
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
487
420
|
* parent property of the value to undefined.
|
|
@@ -114,14 +114,17 @@ export class BinaryTree {
|
|
|
114
114
|
get loopType() {
|
|
115
115
|
return this._loopType;
|
|
116
116
|
}
|
|
117
|
+
set loopType(v) {
|
|
118
|
+
this._loopType = v;
|
|
119
|
+
}
|
|
117
120
|
/**
|
|
118
|
-
* The `
|
|
119
|
-
* @param {N} srcNode - The source node that you want to
|
|
121
|
+
* The `_swap` function swaps the location of two nodes in a binary tree.
|
|
122
|
+
* @param {N} srcNode - The source node that you want to _swap with the destination node.
|
|
120
123
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
121
124
|
* be swapped to.
|
|
122
125
|
* @returns The `destNode` is being returned.
|
|
123
126
|
*/
|
|
124
|
-
|
|
127
|
+
_swap(srcNode, destNode) {
|
|
125
128
|
const { key, val } = destNode;
|
|
126
129
|
const tempNode = this.createNode(key, val);
|
|
127
130
|
if (tempNode) {
|
|
@@ -258,13 +261,13 @@ export class BinaryTree {
|
|
|
258
261
|
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
259
262
|
}
|
|
260
263
|
/**
|
|
261
|
-
* The `
|
|
264
|
+
* The `delete` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
|
|
262
265
|
* containing the deleted node and the node that needs to be balanced.
|
|
263
266
|
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
|
|
264
267
|
* node ID (`BinaryTreeNodeKey`).
|
|
265
|
-
* @returns The function `
|
|
268
|
+
* @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
266
269
|
*/
|
|
267
|
-
|
|
270
|
+
delete(nodeOrKey) {
|
|
268
271
|
const bstDeletedResult = [];
|
|
269
272
|
if (!this.root)
|
|
270
273
|
return bstDeletedResult;
|
|
@@ -293,7 +296,7 @@ export class BinaryTree {
|
|
|
293
296
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
294
297
|
if (leftSubTreeRightMost) {
|
|
295
298
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
296
|
-
orgCurrent = this.
|
|
299
|
+
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
297
300
|
if (parentOfLeftSubTreeMax) {
|
|
298
301
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
299
302
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -589,12 +592,12 @@ export class BinaryTree {
|
|
|
589
592
|
}
|
|
590
593
|
/**
|
|
591
594
|
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
592
|
-
* @param {N | null}
|
|
595
|
+
* @param {N | null} subTreeRoot - The `node` parameter represents the root node of a binary search tree (BST).
|
|
593
596
|
* @returns a boolean value.
|
|
594
597
|
*/
|
|
595
|
-
isSubtreeBST(
|
|
598
|
+
isSubtreeBST(subTreeRoot) {
|
|
596
599
|
// TODO there is a bug
|
|
597
|
-
if (!
|
|
600
|
+
if (!subTreeRoot)
|
|
598
601
|
return true;
|
|
599
602
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
600
603
|
const dfs = (cur, min, max) => {
|
|
@@ -604,11 +607,11 @@ export class BinaryTree {
|
|
|
604
607
|
return false;
|
|
605
608
|
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
606
609
|
};
|
|
607
|
-
return dfs(
|
|
610
|
+
return dfs(subTreeRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
608
611
|
}
|
|
609
612
|
else {
|
|
610
613
|
const stack = [];
|
|
611
|
-
let prev = Number.MIN_SAFE_INTEGER, curr =
|
|
614
|
+
let prev = Number.MIN_SAFE_INTEGER, curr = subTreeRoot;
|
|
612
615
|
while (curr || stack.length > 0) {
|
|
613
616
|
while (curr) {
|
|
614
617
|
stack.push(curr);
|
|
@@ -711,33 +714,21 @@ export class BinaryTree {
|
|
|
711
714
|
return sum;
|
|
712
715
|
}
|
|
713
716
|
/**
|
|
714
|
-
* The function `
|
|
717
|
+
* The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
|
|
715
718
|
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
716
719
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
717
|
-
* @param
|
|
718
|
-
* each node in the subtree should be incremented.
|
|
719
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
720
|
+
* @param callBack - The `callBack` parameter is a function that takes a node as a parameter and returns a number.
|
|
720
721
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
721
722
|
* @returns a boolean value.
|
|
722
723
|
*/
|
|
723
|
-
|
|
724
|
+
subTreeForeach(subTreeRoot, callback) {
|
|
724
725
|
if (typeof subTreeRoot === 'number')
|
|
725
726
|
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
726
727
|
if (!subTreeRoot)
|
|
727
728
|
return false;
|
|
728
|
-
const _addByProperty = (cur) => {
|
|
729
|
-
switch (propertyName) {
|
|
730
|
-
case 'key':
|
|
731
|
-
cur.key += delta;
|
|
732
|
-
break;
|
|
733
|
-
default:
|
|
734
|
-
cur.key += delta;
|
|
735
|
-
break;
|
|
736
|
-
}
|
|
737
|
-
};
|
|
738
729
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
739
730
|
const _traverse = (cur) => {
|
|
740
|
-
|
|
731
|
+
callback(cur);
|
|
741
732
|
cur.left && _traverse(cur.left);
|
|
742
733
|
cur.right && _traverse(cur.right);
|
|
743
734
|
};
|
|
@@ -747,7 +738,7 @@ export class BinaryTree {
|
|
|
747
738
|
const stack = [subTreeRoot];
|
|
748
739
|
while (stack.length > 0) {
|
|
749
740
|
const cur = stack.pop();
|
|
750
|
-
|
|
741
|
+
callback(cur);
|
|
751
742
|
cur.right && stack.push(cur.right);
|
|
752
743
|
cur.left && stack.push(cur.left);
|
|
753
744
|
}
|
|
@@ -765,6 +756,7 @@ export class BinaryTree {
|
|
|
765
756
|
this._clearResults();
|
|
766
757
|
const queue = [this.root];
|
|
767
758
|
while (queue.length !== 0) {
|
|
759
|
+
// TODO Array.shift is not efficient, consider using Deque
|
|
768
760
|
const cur = queue.shift();
|
|
769
761
|
if (cur) {
|
|
770
762
|
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
@@ -781,111 +773,74 @@ export class BinaryTree {
|
|
|
781
773
|
* each node based on the specified pattern and property name.
|
|
782
774
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
783
775
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'key'`.
|
|
776
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
784
777
|
* @returns an instance of the BinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
|
|
785
778
|
*/
|
|
786
|
-
dfs(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
779
|
+
dfs(pattern = 'in', nodeOrPropertyName = 'key', loopType = LoopType.ITERATIVE) {
|
|
787
780
|
this._clearResults();
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
case 'in':
|
|
791
|
-
if (node.left)
|
|
792
|
-
_traverse(node.left);
|
|
793
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
794
|
-
if (node.right)
|
|
795
|
-
_traverse(node.right);
|
|
796
|
-
break;
|
|
797
|
-
case 'pre':
|
|
798
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
799
|
-
if (node.left)
|
|
800
|
-
_traverse(node.left);
|
|
801
|
-
if (node.right)
|
|
802
|
-
_traverse(node.right);
|
|
803
|
-
break;
|
|
804
|
-
case 'post':
|
|
805
|
-
if (node.left)
|
|
806
|
-
_traverse(node.left);
|
|
807
|
-
if (node.right)
|
|
808
|
-
_traverse(node.right);
|
|
809
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
810
|
-
break;
|
|
811
|
-
}
|
|
812
|
-
};
|
|
813
|
-
this.root && _traverse(this.root);
|
|
814
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
815
|
-
}
|
|
816
|
-
/**
|
|
817
|
-
* The dfsIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
|
|
818
|
-
* specify the traversal pattern and the property name to accumulate results by.
|
|
819
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
820
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'key'`.
|
|
821
|
-
* @returns An object of type BinaryTreeNodeProperties<N>.
|
|
822
|
-
*/
|
|
823
|
-
dfsIterative(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
824
|
-
this._clearResults();
|
|
825
|
-
if (!this.root)
|
|
826
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
827
|
-
// 0: visit, 1: print
|
|
828
|
-
const stack = [{ opt: 0, node: this.root }];
|
|
829
|
-
while (stack.length > 0) {
|
|
830
|
-
const cur = stack.pop();
|
|
831
|
-
if (!cur || !cur.node)
|
|
832
|
-
continue;
|
|
833
|
-
if (cur.opt === 1) {
|
|
834
|
-
this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
|
|
835
|
-
}
|
|
836
|
-
else {
|
|
781
|
+
if (loopType === LoopType.RECURSIVE) {
|
|
782
|
+
const _traverse = (node) => {
|
|
837
783
|
switch (pattern) {
|
|
838
784
|
case 'in':
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
785
|
+
if (node.left)
|
|
786
|
+
_traverse(node.left);
|
|
787
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
788
|
+
if (node.right)
|
|
789
|
+
_traverse(node.right);
|
|
842
790
|
break;
|
|
843
791
|
case 'pre':
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
792
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
793
|
+
if (node.left)
|
|
794
|
+
_traverse(node.left);
|
|
795
|
+
if (node.right)
|
|
796
|
+
_traverse(node.right);
|
|
847
797
|
break;
|
|
848
798
|
case 'post':
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
stack.push({ opt: 0, node: cur.node.right });
|
|
855
|
-
stack.push({ opt: 1, node: cur.node });
|
|
856
|
-
stack.push({ opt: 0, node: cur.node.left });
|
|
799
|
+
if (node.left)
|
|
800
|
+
_traverse(node.left);
|
|
801
|
+
if (node.right)
|
|
802
|
+
_traverse(node.right);
|
|
803
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
857
804
|
break;
|
|
858
805
|
}
|
|
859
|
-
}
|
|
806
|
+
};
|
|
807
|
+
this.root && _traverse(this.root);
|
|
860
808
|
}
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
* accumulating results based on the 'key' property.
|
|
873
|
-
* @returns An object of type `BinaryTreeNodeProperties<N>`.
|
|
874
|
-
*/
|
|
875
|
-
levelIterative(node = this.root, nodeOrPropertyName = 'key') {
|
|
876
|
-
if (!node)
|
|
877
|
-
return [];
|
|
878
|
-
this._clearResults();
|
|
879
|
-
const queue = [node];
|
|
880
|
-
while (queue.length > 0) {
|
|
881
|
-
const cur = queue.shift();
|
|
882
|
-
if (cur) {
|
|
883
|
-
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
884
|
-
if (cur.left) {
|
|
885
|
-
queue.push(cur.left);
|
|
809
|
+
else {
|
|
810
|
+
if (!this.root)
|
|
811
|
+
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
812
|
+
// 0: visit, 1: print
|
|
813
|
+
const stack = [{ opt: 0, node: this.root }];
|
|
814
|
+
while (stack.length > 0) {
|
|
815
|
+
const cur = stack.pop();
|
|
816
|
+
if (!cur || !cur.node)
|
|
817
|
+
continue;
|
|
818
|
+
if (cur.opt === 1) {
|
|
819
|
+
this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
|
|
886
820
|
}
|
|
887
|
-
|
|
888
|
-
|
|
821
|
+
else {
|
|
822
|
+
switch (pattern) {
|
|
823
|
+
case 'in':
|
|
824
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
825
|
+
stack.push({ opt: 1, node: cur.node });
|
|
826
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
827
|
+
break;
|
|
828
|
+
case 'pre':
|
|
829
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
830
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
831
|
+
stack.push({ opt: 1, node: cur.node });
|
|
832
|
+
break;
|
|
833
|
+
case 'post':
|
|
834
|
+
stack.push({ opt: 1, node: cur.node });
|
|
835
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
836
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
837
|
+
break;
|
|
838
|
+
default:
|
|
839
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
840
|
+
stack.push({ opt: 1, node: cur.node });
|
|
841
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
842
|
+
break;
|
|
843
|
+
}
|
|
889
844
|
}
|
|
890
845
|
}
|
|
891
846
|
}
|
|
@@ -1090,13 +1045,6 @@ export class BinaryTree {
|
|
|
1090
1045
|
return;
|
|
1091
1046
|
}
|
|
1092
1047
|
}
|
|
1093
|
-
/**
|
|
1094
|
-
* The function sets the loop type for a protected variable.
|
|
1095
|
-
* @param {LoopType} value - The value parameter is of type LoopType.
|
|
1096
|
-
*/
|
|
1097
|
-
_setLoopType(value) {
|
|
1098
|
-
this._loopType = value;
|
|
1099
|
-
}
|
|
1100
1048
|
/**
|
|
1101
1049
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
1102
1050
|
* parent property of the value to undefined.
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
-
import { BinaryTreeDeletedResult, DFSOrderPattern } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, DFSOrderPattern, LoopType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> {
|
|
@@ -48,12 +48,12 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
48
48
|
createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
|
|
49
49
|
/**
|
|
50
50
|
* The function swaps the location of two nodes in a tree data structure.
|
|
51
|
-
* @param {N} srcNode - The source node that we want to
|
|
51
|
+
* @param {N} srcNode - The source node that we want to _swap with the destination node.
|
|
52
52
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
53
53
|
* be swapped with.
|
|
54
54
|
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
55
55
|
*/
|
|
56
|
-
|
|
56
|
+
protected _swap(srcNode: N, destNode: N): N;
|
|
57
57
|
/**
|
|
58
58
|
* The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
|
|
59
59
|
* necessary.
|
|
@@ -93,15 +93,15 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
93
93
|
*/
|
|
94
94
|
perfectlyBalance(): boolean;
|
|
95
95
|
/**
|
|
96
|
-
* The `
|
|
96
|
+
* The `delete` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
97
97
|
* node that needs to be balanced.
|
|
98
98
|
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
99
99
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
100
100
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
101
101
|
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
102
|
-
* @returns The function `
|
|
102
|
+
* @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
103
103
|
*/
|
|
104
|
-
|
|
104
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
105
105
|
/**
|
|
106
106
|
* The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
|
|
107
107
|
* recursive or iterative traversal.
|
|
@@ -146,7 +146,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
146
146
|
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
147
147
|
* bfs traversal.
|
|
148
148
|
*/
|
|
149
|
-
|
|
149
|
+
bfsCount(): number[];
|
|
150
150
|
/**
|
|
151
151
|
* The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
|
|
152
152
|
* count property of each node at that level.
|
|
@@ -169,18 +169,11 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
169
169
|
* the specified traversal pattern.
|
|
170
170
|
* @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
|
|
171
171
|
* the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
|
|
172
|
+
* @param loopType - The loopType parameter is a string that specifies the type of loop to use when traversing the
|
|
172
173
|
* @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
|
|
173
174
|
* in the dfs traversal.
|
|
174
175
|
*/
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* The dfsCount function returns an array of counts for each node in a depth-first search traversal.
|
|
178
|
-
* @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
|
|
179
|
-
* the Depth-First Search (dfs) algorithm should traverse the nodes. It can have one of the following values:
|
|
180
|
-
* @returns The dfsCount function returns an array of numbers, specifically the count property of each node in the dfs
|
|
181
|
-
* traversal.
|
|
182
|
-
*/
|
|
183
|
-
dfsCount(pattern?: DFSOrderPattern): number[];
|
|
176
|
+
dfsCount(pattern?: DFSOrderPattern, loopType?: LoopType): number[];
|
|
184
177
|
/**
|
|
185
178
|
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
186
179
|
* value than a given node.
|
|
@@ -47,12 +47,12 @@ export class TreeMultiset extends AVLTree {
|
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
49
|
* The function swaps the location of two nodes in a tree data structure.
|
|
50
|
-
* @param {N} srcNode - The source node that we want to
|
|
50
|
+
* @param {N} srcNode - The source node that we want to _swap with the destination node.
|
|
51
51
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
52
52
|
* be swapped with.
|
|
53
53
|
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
54
54
|
*/
|
|
55
|
-
|
|
55
|
+
_swap(srcNode, destNode) {
|
|
56
56
|
const { key, val, count, height } = destNode;
|
|
57
57
|
const tempNode = this.createNode(key, val, count);
|
|
58
58
|
if (tempNode) {
|
|
@@ -257,15 +257,15 @@ export class TreeMultiset extends AVLTree {
|
|
|
257
257
|
}
|
|
258
258
|
}
|
|
259
259
|
/**
|
|
260
|
-
* The `
|
|
260
|
+
* The `delete` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
261
261
|
* node that needs to be balanced.
|
|
262
262
|
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
263
263
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
264
264
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
265
265
|
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
266
|
-
* @returns The function `
|
|
266
|
+
* @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
267
267
|
*/
|
|
268
|
-
|
|
268
|
+
delete(nodeOrKey, ignoreCount = false) {
|
|
269
269
|
const bstDeletedResult = [];
|
|
270
270
|
if (!this.root)
|
|
271
271
|
return bstDeletedResult;
|
|
@@ -299,7 +299,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
299
299
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
300
300
|
if (leftSubTreeRightMost) {
|
|
301
301
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
302
|
-
orgCurrent = this.
|
|
302
|
+
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
303
303
|
if (parentOfLeftSubTreeMax) {
|
|
304
304
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
305
305
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -475,7 +475,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
475
475
|
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
476
476
|
* bfs traversal.
|
|
477
477
|
*/
|
|
478
|
-
|
|
478
|
+
bfsCount() {
|
|
479
479
|
const nodes = super.bfs('node');
|
|
480
480
|
return nodes.map(node => node.count);
|
|
481
481
|
}
|
|
@@ -507,22 +507,12 @@ export class TreeMultiset extends AVLTree {
|
|
|
507
507
|
* the specified traversal pattern.
|
|
508
508
|
* @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
|
|
509
509
|
* the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
|
|
510
|
+
* @param loopType - The loopType parameter is a string that specifies the type of loop to use when traversing the
|
|
510
511
|
* @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
|
|
511
512
|
* in the dfs traversal.
|
|
512
513
|
*/
|
|
513
|
-
|
|
514
|
-
const nodes = super.
|
|
515
|
-
return nodes.map(node => node.count);
|
|
516
|
-
}
|
|
517
|
-
/**
|
|
518
|
-
* The dfsCount function returns an array of counts for each node in a depth-first search traversal.
|
|
519
|
-
* @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
|
|
520
|
-
* the Depth-First Search (dfs) algorithm should traverse the nodes. It can have one of the following values:
|
|
521
|
-
* @returns The dfsCount function returns an array of numbers, specifically the count property of each node in the dfs
|
|
522
|
-
* traversal.
|
|
523
|
-
*/
|
|
524
|
-
dfsCount(pattern = 'in') {
|
|
525
|
-
const nodes = super.dfs(pattern, 'node');
|
|
514
|
+
dfsCount(pattern = 'in', loopType = LoopType.ITERATIVE) {
|
|
515
|
+
const nodes = super.dfs(pattern, 'node', loopType);
|
|
526
516
|
return nodes.map(node => node.count);
|
|
527
517
|
}
|
|
528
518
|
/**
|