data-structure-typed 1.36.8 → 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/CHANGELOG.md +2 -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.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/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 +6 -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 +5 -2
- 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/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 +15 -12
- 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 +61 -55
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -145,6 +145,10 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
145
145
|
return this._loopType;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
+
set loopType(v: LoopType) {
|
|
149
|
+
this._loopType = v;
|
|
150
|
+
}
|
|
151
|
+
|
|
148
152
|
visitedKey: BinaryTreeNodeKey[] = [];
|
|
149
153
|
|
|
150
154
|
visitedVal: N['val'][] = [];
|
|
@@ -152,13 +156,13 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
152
156
|
visitedNode: N[] = [];
|
|
153
157
|
|
|
154
158
|
/**
|
|
155
|
-
* The `
|
|
156
|
-
* @param {N} srcNode - The source node that you want to
|
|
159
|
+
* The `_swap` function swaps the location of two nodes in a binary tree.
|
|
160
|
+
* @param {N} srcNode - The source node that you want to _swap with the destination node.
|
|
157
161
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
158
162
|
* be swapped to.
|
|
159
163
|
* @returns The `destNode` is being returned.
|
|
160
164
|
*/
|
|
161
|
-
|
|
165
|
+
protected _swap(srcNode: N, destNode: N): N {
|
|
162
166
|
const {key, val} = destNode;
|
|
163
167
|
const tempNode = this.createNode(key, val);
|
|
164
168
|
|
|
@@ -300,13 +304,13 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
300
304
|
}
|
|
301
305
|
|
|
302
306
|
/**
|
|
303
|
-
* The `
|
|
307
|
+
* The `delete` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
|
|
304
308
|
* containing the deleted node and the node that needs to be balanced.
|
|
305
309
|
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
|
|
306
310
|
* node ID (`BinaryTreeNodeKey`).
|
|
307
|
-
* @returns The function `
|
|
311
|
+
* @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
308
312
|
*/
|
|
309
|
-
|
|
313
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
|
|
310
314
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
311
315
|
if (!this.root) return bstDeletedResult;
|
|
312
316
|
|
|
@@ -333,7 +337,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
333
337
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
334
338
|
if (leftSubTreeRightMost) {
|
|
335
339
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
336
|
-
orgCurrent = this.
|
|
340
|
+
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
337
341
|
if (parentOfLeftSubTreeMax) {
|
|
338
342
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
339
343
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -683,12 +687,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
683
687
|
|
|
684
688
|
/**
|
|
685
689
|
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
686
|
-
* @param {N | null}
|
|
690
|
+
* @param {N | null} subTreeRoot - The `node` parameter represents the root node of a binary search tree (BST).
|
|
687
691
|
* @returns a boolean value.
|
|
688
692
|
*/
|
|
689
|
-
isSubtreeBST(
|
|
693
|
+
isSubtreeBST(subTreeRoot: N | null): boolean {
|
|
690
694
|
// TODO there is a bug
|
|
691
|
-
if (!
|
|
695
|
+
if (!subTreeRoot) return true;
|
|
692
696
|
|
|
693
697
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
694
698
|
const dfs = (cur: N | null | undefined, min: BinaryTreeNodeKey, max: BinaryTreeNodeKey): boolean => {
|
|
@@ -697,11 +701,11 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
697
701
|
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
698
702
|
};
|
|
699
703
|
|
|
700
|
-
return dfs(
|
|
704
|
+
return dfs(subTreeRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
701
705
|
} else {
|
|
702
706
|
const stack = [];
|
|
703
707
|
let prev = Number.MIN_SAFE_INTEGER,
|
|
704
|
-
curr: N | null | undefined =
|
|
708
|
+
curr: N | null | undefined = subTreeRoot;
|
|
705
709
|
while (curr || stack.length > 0) {
|
|
706
710
|
while (curr) {
|
|
707
711
|
stack.push(curr);
|
|
@@ -813,38 +817,21 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
813
817
|
}
|
|
814
818
|
|
|
815
819
|
/**
|
|
816
|
-
* The function `
|
|
820
|
+
* The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
|
|
817
821
|
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
818
822
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
819
|
-
* @param
|
|
820
|
-
* each node in the subtree should be incremented.
|
|
821
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
823
|
+
* @param callBack - The `callBack` parameter is a function that takes a node as a parameter and returns a number.
|
|
822
824
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
823
825
|
* @returns a boolean value.
|
|
824
826
|
*/
|
|
825
|
-
|
|
826
|
-
subTreeRoot: N | BinaryTreeNodeKey | null,
|
|
827
|
-
delta: number,
|
|
828
|
-
propertyName: BinaryTreeNodePropertyName = 'key'
|
|
829
|
-
): boolean {
|
|
827
|
+
subTreeForeach(subTreeRoot: N | BinaryTreeNodeKey | null, callback: (node: N) => any): boolean {
|
|
830
828
|
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
|
|
831
829
|
|
|
832
830
|
if (!subTreeRoot) return false;
|
|
833
831
|
|
|
834
|
-
const _addByProperty = (cur: N) => {
|
|
835
|
-
switch (propertyName) {
|
|
836
|
-
case 'key':
|
|
837
|
-
cur.key += delta;
|
|
838
|
-
break;
|
|
839
|
-
default:
|
|
840
|
-
cur.key += delta;
|
|
841
|
-
break;
|
|
842
|
-
}
|
|
843
|
-
};
|
|
844
|
-
|
|
845
832
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
846
833
|
const _traverse = (cur: N) => {
|
|
847
|
-
|
|
834
|
+
callback(cur);
|
|
848
835
|
cur.left && _traverse(cur.left);
|
|
849
836
|
cur.right && _traverse(cur.right);
|
|
850
837
|
};
|
|
@@ -856,7 +843,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
856
843
|
while (stack.length > 0) {
|
|
857
844
|
const cur = stack.pop()!;
|
|
858
845
|
|
|
859
|
-
|
|
846
|
+
callback(cur);
|
|
860
847
|
cur.right && stack.push(cur.right);
|
|
861
848
|
cur.left && stack.push(cur.left);
|
|
862
849
|
}
|
|
@@ -903,6 +890,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
903
890
|
const queue: Array<N | null | undefined> = [this.root];
|
|
904
891
|
|
|
905
892
|
while (queue.length !== 0) {
|
|
893
|
+
// TODO Array.shift is not efficient, consider using Deque
|
|
906
894
|
const cur = queue.shift();
|
|
907
895
|
if (cur) {
|
|
908
896
|
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
@@ -931,215 +919,98 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
931
919
|
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
932
920
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
933
921
|
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
922
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
934
923
|
* @returns An array of values corresponding to the specified property.
|
|
935
924
|
*/
|
|
936
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
925
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key', loopType?: LoopType): BinaryTreeNodeKey[];
|
|
937
926
|
|
|
938
927
|
/**
|
|
939
928
|
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
940
929
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
941
930
|
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
931
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
942
932
|
* @returns An array of 'val' properties from each node.
|
|
943
933
|
*/
|
|
944
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[];
|
|
934
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val', loopType?: LoopType): N[];
|
|
945
935
|
|
|
946
936
|
/**
|
|
947
937
|
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
|
|
948
938
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
949
939
|
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
940
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
950
941
|
* @returns An array of binary tree nodes.
|
|
951
942
|
*/
|
|
952
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
943
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node', loopType?: LoopType): N[];
|
|
953
944
|
|
|
954
945
|
/**
|
|
955
946
|
* The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
956
947
|
* each node based on the specified pattern and property name.
|
|
957
948
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
958
949
|
* @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'`.
|
|
950
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
959
951
|
* @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.
|
|
960
952
|
*/
|
|
961
|
-
dfs(
|
|
962
|
-
this._clearResults();
|
|
963
|
-
const _traverse = (node: N) => {
|
|
964
|
-
switch (pattern) {
|
|
965
|
-
case 'in':
|
|
966
|
-
if (node.left) _traverse(node.left);
|
|
967
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
968
|
-
if (node.right) _traverse(node.right);
|
|
969
|
-
break;
|
|
970
|
-
case 'pre':
|
|
971
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
972
|
-
if (node.left) _traverse(node.left);
|
|
973
|
-
if (node.right) _traverse(node.right);
|
|
974
|
-
break;
|
|
975
|
-
case 'post':
|
|
976
|
-
if (node.left) _traverse(node.left);
|
|
977
|
-
if (node.right) _traverse(node.right);
|
|
978
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
979
|
-
break;
|
|
980
|
-
}
|
|
981
|
-
};
|
|
982
|
-
|
|
983
|
-
this.root && _traverse(this.root);
|
|
984
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
// --- start additional methods ---
|
|
988
|
-
|
|
989
|
-
/**
|
|
990
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
991
|
-
* @returns An array of binary tree node IDs.
|
|
992
|
-
*/
|
|
993
|
-
dfsIterative(): BinaryTreeNodeKey[];
|
|
994
|
-
|
|
995
|
-
/**
|
|
996
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
997
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
998
|
-
* @returns An array of values corresponding to the specified property.
|
|
999
|
-
*/
|
|
1000
|
-
dfsIterative(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
|
|
1001
|
-
|
|
1002
|
-
/**
|
|
1003
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
1004
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1005
|
-
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
1006
|
-
* @returns An array of values corresponding to the specified property.
|
|
1007
|
-
*/
|
|
1008
|
-
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
1009
|
-
|
|
1010
|
-
/**
|
|
1011
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
1012
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1013
|
-
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1014
|
-
* @returns An array of 'val' properties from each node.
|
|
1015
|
-
*/
|
|
1016
|
-
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N['val'][];
|
|
1017
|
-
|
|
1018
|
-
/**
|
|
1019
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
|
|
1020
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1021
|
-
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1022
|
-
* @returns An array of binary tree nodes.
|
|
1023
|
-
*/
|
|
1024
|
-
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
1025
|
-
|
|
1026
|
-
/**
|
|
1027
|
-
* The dfsIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
|
|
1028
|
-
* specify the traversal pattern and the property name to accumulate results by.
|
|
1029
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1030
|
-
* @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'`.
|
|
1031
|
-
* @returns An object of type BinaryTreeNodeProperties<N>.
|
|
1032
|
-
*/
|
|
1033
|
-
dfsIterative(
|
|
953
|
+
dfs(
|
|
1034
954
|
pattern: DFSOrderPattern = 'in',
|
|
1035
|
-
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
|
955
|
+
nodeOrPropertyName: NodeOrPropertyName = 'key',
|
|
956
|
+
loopType: LoopType = LoopType.ITERATIVE
|
|
1036
957
|
): BinaryTreeNodeProperties<N> {
|
|
1037
958
|
this._clearResults();
|
|
1038
|
-
if (
|
|
1039
|
-
|
|
1040
|
-
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: this.root}];
|
|
1041
|
-
|
|
1042
|
-
while (stack.length > 0) {
|
|
1043
|
-
const cur = stack.pop();
|
|
1044
|
-
if (!cur || !cur.node) continue;
|
|
1045
|
-
if (cur.opt === 1) {
|
|
1046
|
-
this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
|
|
1047
|
-
} else {
|
|
959
|
+
if (loopType === LoopType.RECURSIVE) {
|
|
960
|
+
const _traverse = (node: N) => {
|
|
1048
961
|
switch (pattern) {
|
|
1049
962
|
case 'in':
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
963
|
+
if (node.left) _traverse(node.left);
|
|
964
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
965
|
+
if (node.right) _traverse(node.right);
|
|
1053
966
|
break;
|
|
1054
967
|
case 'pre':
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
968
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
969
|
+
if (node.left) _traverse(node.left);
|
|
970
|
+
if (node.right) _traverse(node.right);
|
|
1058
971
|
break;
|
|
1059
972
|
case 'post':
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
break;
|
|
1064
|
-
default:
|
|
1065
|
-
stack.push({opt: 0, node: cur.node.right});
|
|
1066
|
-
stack.push({opt: 1, node: cur.node});
|
|
1067
|
-
stack.push({opt: 0, node: cur.node.left});
|
|
973
|
+
if (node.left) _traverse(node.left);
|
|
974
|
+
if (node.right) _traverse(node.right);
|
|
975
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
1068
976
|
break;
|
|
1069
977
|
}
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
|
|
1073
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1074
|
-
}
|
|
1075
|
-
|
|
1076
|
-
/**
|
|
1077
|
-
* 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.
|
|
1078
|
-
* @returns An array of binary tree node IDs.
|
|
1079
|
-
*/
|
|
1080
|
-
levelIterative(): BinaryTreeNodeKey[];
|
|
1081
|
-
|
|
1082
|
-
/**
|
|
1083
|
-
* 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.
|
|
1084
|
-
* @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.
|
|
1085
|
-
* @returns An array of binary tree node IDs.
|
|
1086
|
-
*/
|
|
1087
|
-
levelIterative(node: N | null): BinaryTreeNodeKey[];
|
|
1088
|
-
|
|
1089
|
-
/**
|
|
1090
|
-
* 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.
|
|
1091
|
-
* @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.
|
|
1092
|
-
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
1093
|
-
* @returns An array of values corresponding to the specified property.
|
|
1094
|
-
*/
|
|
1095
|
-
levelIterative(node: N | null, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
1096
|
-
|
|
1097
|
-
/**
|
|
1098
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates the 'val' property of each node.
|
|
1099
|
-
* @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.
|
|
1100
|
-
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1101
|
-
* @returns An array of 'val' properties from each node.
|
|
1102
|
-
*/
|
|
1103
|
-
levelIterative(node: N | null, nodeOrPropertyName: 'val'): N['val'][];
|
|
1104
|
-
|
|
1105
|
-
/**
|
|
1106
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates nodes themselves.
|
|
1107
|
-
* @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.
|
|
1108
|
-
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1109
|
-
* @returns An array of binary tree nodes.
|
|
1110
|
-
*/
|
|
1111
|
-
levelIterative(node: N | null, nodeOrPropertyName: 'node'): N[];
|
|
1112
|
-
|
|
1113
|
-
/**
|
|
1114
|
-
* The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
|
|
1115
|
-
* in an array, based on a specified property name.
|
|
1116
|
-
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
|
|
1117
|
-
* node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
|
|
1118
|
-
* the tree is used as the starting node.
|
|
1119
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1120
|
-
* can be either a `BinaryTreeNode` property name or the string `'key'`. If a property name is provided, the function
|
|
1121
|
-
* will accumulate results based on that property. If no property name is provided, the function will default to
|
|
1122
|
-
* accumulating results based on the 'key' property.
|
|
1123
|
-
* @returns An object of type `BinaryTreeNodeProperties<N>`.
|
|
1124
|
-
*/
|
|
1125
|
-
levelIterative(
|
|
1126
|
-
node: N | null = this.root,
|
|
1127
|
-
nodeOrPropertyName: NodeOrPropertyName = 'key'
|
|
1128
|
-
): BinaryTreeNodeProperties<N> {
|
|
1129
|
-
if (!node) return [];
|
|
978
|
+
};
|
|
1130
979
|
|
|
1131
|
-
|
|
1132
|
-
|
|
980
|
+
this.root && _traverse(this.root);
|
|
981
|
+
} else {
|
|
982
|
+
if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName);
|
|
983
|
+
// 0: visit, 1: print
|
|
984
|
+
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: this.root}];
|
|
1133
985
|
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
986
|
+
while (stack.length > 0) {
|
|
987
|
+
const cur = stack.pop();
|
|
988
|
+
if (!cur || !cur.node) continue;
|
|
989
|
+
if (cur.opt === 1) {
|
|
990
|
+
this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
|
|
991
|
+
} else {
|
|
992
|
+
switch (pattern) {
|
|
993
|
+
case 'in':
|
|
994
|
+
stack.push({opt: 0, node: cur.node.right});
|
|
995
|
+
stack.push({opt: 1, node: cur.node});
|
|
996
|
+
stack.push({opt: 0, node: cur.node.left});
|
|
997
|
+
break;
|
|
998
|
+
case 'pre':
|
|
999
|
+
stack.push({opt: 0, node: cur.node.right});
|
|
1000
|
+
stack.push({opt: 0, node: cur.node.left});
|
|
1001
|
+
stack.push({opt: 1, node: cur.node});
|
|
1002
|
+
break;
|
|
1003
|
+
case 'post':
|
|
1004
|
+
stack.push({opt: 1, node: cur.node});
|
|
1005
|
+
stack.push({opt: 0, node: cur.node.right});
|
|
1006
|
+
stack.push({opt: 0, node: cur.node.left});
|
|
1007
|
+
break;
|
|
1008
|
+
default:
|
|
1009
|
+
stack.push({opt: 0, node: cur.node.right});
|
|
1010
|
+
stack.push({opt: 1, node: cur.node});
|
|
1011
|
+
stack.push({opt: 0, node: cur.node.left});
|
|
1012
|
+
break;
|
|
1013
|
+
}
|
|
1143
1014
|
}
|
|
1144
1015
|
}
|
|
1145
1016
|
}
|
|
@@ -1147,6 +1018,8 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1147
1018
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1148
1019
|
}
|
|
1149
1020
|
|
|
1021
|
+
// --- start additional methods ---
|
|
1022
|
+
|
|
1150
1023
|
/**
|
|
1151
1024
|
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1152
1025
|
* @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
|
|
@@ -1425,14 +1298,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1425
1298
|
}
|
|
1426
1299
|
}
|
|
1427
1300
|
|
|
1428
|
-
/**
|
|
1429
|
-
* The function sets the loop type for a protected variable.
|
|
1430
|
-
* @param {LoopType} value - The value parameter is of type LoopType.
|
|
1431
|
-
*/
|
|
1432
|
-
protected _setLoopType(value: LoopType) {
|
|
1433
|
-
this._loopType = value;
|
|
1434
|
-
}
|
|
1435
|
-
|
|
1436
1301
|
/**
|
|
1437
1302
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
1438
1303
|
* parent property of the value to undefined.
|
|
@@ -164,10 +164,10 @@ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<
|
|
|
164
164
|
// // Remove a node
|
|
165
165
|
// private _removeNode(node: N, replacement: N | null | undefined): void {
|
|
166
166
|
// if (node === this.root && !replacement) {
|
|
167
|
-
// // If there's only the root node and no replacement, simply
|
|
167
|
+
// // If there's only the root node and no replacement, simply delete the root node
|
|
168
168
|
// this._setRoot(null);
|
|
169
169
|
// } else if (node === this.root || this._isNodeRed(node)) {
|
|
170
|
-
// // If the node is the root or a red node,
|
|
170
|
+
// // If the node is the root or a red node, delete it directly
|
|
171
171
|
// if (node.parent!.left === node) {
|
|
172
172
|
// node.parent!.left = replacement;
|
|
173
173
|
// } else {
|
|
@@ -205,7 +205,7 @@ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<
|
|
|
205
205
|
// node.right = null;
|
|
206
206
|
// }
|
|
207
207
|
//
|
|
208
|
-
// override
|
|
208
|
+
// override delete(nodeOrKey: BinaryTreeNodeKey | N): BinaryTreeDeletedResult<N>[] {
|
|
209
209
|
// const node = this.get(nodeOrKey);
|
|
210
210
|
// const result: BinaryTreeDeletedResult<N>[] = [{deleted: undefined, needBalanced: null}];
|
|
211
211
|
// if (!node) return result; // Node does not exist
|
|
@@ -70,12 +70,12 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
72
|
* The function swaps the location of two nodes in a tree data structure.
|
|
73
|
-
* @param {N} srcNode - The source node that we want to
|
|
73
|
+
* @param {N} srcNode - The source node that we want to _swap with the destination node.
|
|
74
74
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
75
75
|
* be swapped with.
|
|
76
76
|
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
77
77
|
*/
|
|
78
|
-
override
|
|
78
|
+
protected override _swap(srcNode: N, destNode: N): N {
|
|
79
79
|
const {key, val, count, height} = destNode;
|
|
80
80
|
const tempNode = this.createNode(key, val, count);
|
|
81
81
|
if (tempNode) {
|
|
@@ -285,15 +285,15 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
285
285
|
}
|
|
286
286
|
|
|
287
287
|
/**
|
|
288
|
-
* The `
|
|
288
|
+
* The `delete` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
289
289
|
* node that needs to be balanced.
|
|
290
290
|
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
291
291
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
292
292
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
293
293
|
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
294
|
-
* @returns The function `
|
|
294
|
+
* @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
295
295
|
*/
|
|
296
|
-
override
|
|
296
|
+
override delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount = false): BinaryTreeDeletedResult<N>[] {
|
|
297
297
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
298
298
|
if (!this.root) return bstDeletedResult;
|
|
299
299
|
|
|
@@ -324,7 +324,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
324
324
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
325
325
|
if (leftSubTreeRightMost) {
|
|
326
326
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
327
|
-
orgCurrent = this.
|
|
327
|
+
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
328
328
|
if (parentOfLeftSubTreeMax) {
|
|
329
329
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
330
330
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -515,7 +515,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
515
515
|
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
516
516
|
* bfs traversal.
|
|
517
517
|
*/
|
|
518
|
-
|
|
518
|
+
bfsCount(): number[] {
|
|
519
519
|
const nodes = super.bfs('node');
|
|
520
520
|
return nodes.map(node => node.count);
|
|
521
521
|
}
|
|
@@ -550,23 +550,12 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
550
550
|
* the specified traversal pattern.
|
|
551
551
|
* @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
|
|
552
552
|
* the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
|
|
553
|
+
* @param loopType - The loopType parameter is a string that specifies the type of loop to use when traversing the
|
|
553
554
|
* @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
|
|
554
555
|
* in the dfs traversal.
|
|
555
556
|
*/
|
|
556
|
-
|
|
557
|
-
const nodes = super.
|
|
558
|
-
return nodes.map(node => node.count);
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
/**
|
|
562
|
-
* The dfsCount function returns an array of counts for each node in a depth-first search traversal.
|
|
563
|
-
* @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
|
|
564
|
-
* the Depth-First Search (dfs) algorithm should traverse the nodes. It can have one of the following values:
|
|
565
|
-
* @returns The dfsCount function returns an array of numbers, specifically the count property of each node in the dfs
|
|
566
|
-
* traversal.
|
|
567
|
-
*/
|
|
568
|
-
dfsCount(pattern: DFSOrderPattern = 'in'): number[] {
|
|
569
|
-
const nodes = super.dfs(pattern, 'node');
|
|
557
|
+
dfsCount(pattern: DFSOrderPattern = 'in', loopType: LoopType = LoopType.ITERATIVE): number[] {
|
|
558
|
+
const nodes = super.dfs(pattern, 'node', loopType);
|
|
570
559
|
return nodes.map(node => node.count);
|
|
571
560
|
}
|
|
572
561
|
|
|
@@ -213,13 +213,13 @@ export class HashTable<K, V> {
|
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
/**
|
|
216
|
-
* The
|
|
216
|
+
* The delete function removes a key-value pair from a hash table.
|
|
217
217
|
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
218
218
|
* table.
|
|
219
|
-
* @returns Nothing is being returned. The `
|
|
219
|
+
* @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
|
|
220
220
|
* any value.
|
|
221
221
|
*/
|
|
222
|
-
|
|
222
|
+
delete(key: K): void {
|
|
223
223
|
const index = this._hash(key);
|
|
224
224
|
let currentNode = this._buckets[index];
|
|
225
225
|
let prevNode: HashTableNode<K, V> | null = null;
|
|
@@ -375,7 +375,7 @@ export class FibonacciHeap<E> {
|
|
|
375
375
|
* @protected
|
|
376
376
|
* @returns FibonacciHeapNode<E>[] - An array containing the nodes of the linked list.
|
|
377
377
|
*/
|
|
378
|
-
|
|
378
|
+
consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[] {
|
|
379
379
|
const nodes: FibonacciHeapNode<E>[] = [];
|
|
380
380
|
if (!head) return nodes;
|
|
381
381
|
|
|
@@ -448,7 +448,10 @@ export class FibonacciHeap<E> {
|
|
|
448
448
|
protected consolidate(): void {
|
|
449
449
|
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.size);
|
|
450
450
|
const nodes = this.consumeLinkedList(this.root);
|
|
451
|
-
let x: FibonacciHeapNode<E> | undefined,
|
|
451
|
+
let x: FibonacciHeapNode<E> | undefined,
|
|
452
|
+
y: FibonacciHeapNode<E> | undefined,
|
|
453
|
+
d: number,
|
|
454
|
+
t: FibonacciHeapNode<E> | undefined;
|
|
452
455
|
|
|
453
456
|
for (const node of nodes) {
|
|
454
457
|
x = node;
|
|
@@ -130,12 +130,12 @@ export class SkipList<K, V> {
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
/**
|
|
133
|
-
* The `
|
|
133
|
+
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
134
134
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
135
|
-
* @returns The `
|
|
135
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
|
|
136
136
|
* skip list, and `false` if the key was not found in the skip list.
|
|
137
137
|
*/
|
|
138
|
-
|
|
138
|
+
delete(key: K): boolean {
|
|
139
139
|
const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
|
|
140
140
|
let current = this.head;
|
|
141
141
|
|
|
@@ -277,12 +277,12 @@ export class ArrayDeque<E> {
|
|
|
277
277
|
}
|
|
278
278
|
|
|
279
279
|
/**
|
|
280
|
-
* The
|
|
280
|
+
* The delete function removes an element from an array at a specified index.
|
|
281
281
|
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
282
282
|
* is a number that represents the index of the element to be removed.
|
|
283
283
|
* @returns The method is returning an array containing the removed element.
|
|
284
284
|
*/
|
|
285
|
-
|
|
285
|
+
delete(index: number) {
|
|
286
286
|
return this._nodes.splice(index, 1);
|
|
287
287
|
}
|
|
288
288
|
|
|
@@ -106,7 +106,7 @@ export class Queue<E = any> {
|
|
|
106
106
|
|
|
107
107
|
if (this.offset * 2 < this.nodes.length) return first;
|
|
108
108
|
|
|
109
|
-
// only
|
|
109
|
+
// only delete dequeued elements when reaching half size
|
|
110
110
|
// to decrease latency of shifting elements.
|
|
111
111
|
this.nodes = this.nodes.slice(this.offset);
|
|
112
112
|
this.offset = 0;
|
|
@@ -119,10 +119,10 @@ export class Trie {
|
|
|
119
119
|
|
|
120
120
|
/**
|
|
121
121
|
* Remove a word from the Trie structure.
|
|
122
|
-
* @param{string} word - The word to
|
|
122
|
+
* @param{string} word - The word to delete.
|
|
123
123
|
* @returns {boolean} True if the word was successfully removed.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
delete(word: string) {
|
|
126
126
|
word = this._caseProcess(word);
|
|
127
127
|
let isDeleted = false;
|
|
128
128
|
const dfs = (cur: TrieNode, i: number): boolean => {
|
|
@@ -6,5 +6,5 @@ export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
|
|
|
6
6
|
|
|
7
7
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
10
10
|
}
|