data-structure-typed 1.36.8 → 1.37.0
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/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 -95
- package/dist/data-structures/binary-tree/binary-tree.js +82 -183
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +6 -20
- package/dist/data-structures/binary-tree/bst.js +22 -122
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -67
- package/dist/data-structures/binary-tree/tree-multiset.js +10 -257
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +4 -3
- package/dist/data-structures/graph/abstract-graph.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 -95
- package/lib/data-structures/binary-tree/binary-tree.js +82 -183
- package/lib/data-structures/binary-tree/bst.d.ts +6 -20
- package/lib/data-structures/binary-tree/bst.js +22 -122
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +6 -67
- package/lib/data-structures/binary-tree/tree-multiset.js +10 -257
- package/lib/data-structures/graph/abstract-graph.js +4 -3
- 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 +9 -7
- package/src/data-structures/binary-tree/avl-tree.ts +6 -6
- package/src/data-structures/binary-tree/binary-tree.ts +85 -274
- package/src/data-structures/binary-tree/bst.ts +22 -106
- package/src/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +10 -249
- package/src/data-structures/graph/abstract-graph.ts +4 -3
- 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 +19 -17
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +72 -35
- package/test/unit/data-structures/binary-tree/overall.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +67 -37
- 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 +20 -3
- package/test/unit/data-structures/queue/queue.test.ts +42 -0
- package/test/unit/data-structures/trie/trie.test.ts +5 -5
- package/test/utils/big-o.ts +64 -57
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -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,43 +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
|
-
|
|
105
|
-
/**
|
|
106
|
-
* The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
|
|
107
|
-
* recursive or iterative traversal.
|
|
108
|
-
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
109
|
-
* binary tree.
|
|
110
|
-
* @returns The function `getSubTreeCount` returns an array `[number, number]`.
|
|
111
|
-
*/
|
|
112
|
-
getSubTreeCount(subTreeRoot: N | null | undefined): [number, number];
|
|
113
|
-
/**
|
|
114
|
-
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
115
|
-
* recursively or iteratively.
|
|
116
|
-
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
117
|
-
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree) or
|
|
118
|
-
* `null` if the subtree is empty.
|
|
119
|
-
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
120
|
-
*/
|
|
121
|
-
subTreeSumCount(subTreeRoot: N | BinaryTreeNodeKey | null): number;
|
|
122
|
-
/**
|
|
123
|
-
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
124
|
-
* the `count` property of each node.
|
|
125
|
-
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
126
|
-
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree), a
|
|
127
|
-
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
128
|
-
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
129
|
-
* in the subtree should be increased or decreased.
|
|
130
|
-
* @returns a boolean value.
|
|
131
|
-
*/
|
|
132
|
-
subTreeAddCount(subTreeRoot: N | BinaryTreeNodeKey | null, delta: number): boolean;
|
|
104
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
133
105
|
/**
|
|
134
106
|
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
135
107
|
* using a queue.
|
|
@@ -146,7 +118,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
146
118
|
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
147
119
|
* bfs traversal.
|
|
148
120
|
*/
|
|
149
|
-
|
|
121
|
+
bfsCount(): number[];
|
|
150
122
|
/**
|
|
151
123
|
* The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
|
|
152
124
|
* count property of each node at that level.
|
|
@@ -164,39 +136,6 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
164
136
|
* @returns The function `morrisCount` returns an array of numbers.
|
|
165
137
|
*/
|
|
166
138
|
morrisCount(pattern?: DFSOrderPattern): number[];
|
|
167
|
-
/**
|
|
168
|
-
* The function dfsCountIterative performs an iterative depth-first search and returns an array of node counts based on
|
|
169
|
-
* the specified traversal pattern.
|
|
170
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
|
|
171
|
-
* the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
|
|
172
|
-
* @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
|
|
173
|
-
* in the dfs traversal.
|
|
174
|
-
*/
|
|
175
|
-
dfsCountIterative(pattern?: DFSOrderPattern): number[];
|
|
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[];
|
|
184
|
-
/**
|
|
185
|
-
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
186
|
-
* value than a given node.
|
|
187
|
-
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
188
|
-
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
189
|
-
*/
|
|
190
|
-
lesserSumCount(beginNode: N | BinaryTreeNodeKey | null): number;
|
|
191
|
-
/**
|
|
192
|
-
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
193
|
-
* greater than a given ID by a specified delta value.
|
|
194
|
-
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
|
|
195
|
-
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
196
|
-
* of each node should be increased.
|
|
197
|
-
* @returns a boolean value.
|
|
198
|
-
*/
|
|
199
|
-
allGreaterNodesAddCount(node: N | BinaryTreeNodeKey | null, delta: number): boolean;
|
|
200
139
|
/**
|
|
201
140
|
* The clear() function clears the data and sets the count to 0.
|
|
202
141
|
*/
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CP, FamilyPosition, LoopType } from '../../types';
|
|
2
2
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
3
|
+
import { Queue } from '../queue';
|
|
3
4
|
export class TreeMultisetNode extends AVLTreeNode {
|
|
4
5
|
/**
|
|
5
6
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
@@ -47,12 +48,12 @@ export class TreeMultiset extends AVLTree {
|
|
|
47
48
|
}
|
|
48
49
|
/**
|
|
49
50
|
* The function swaps the location of two nodes in a tree data structure.
|
|
50
|
-
* @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.
|
|
51
52
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
52
53
|
* be swapped with.
|
|
53
54
|
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
54
55
|
*/
|
|
55
|
-
|
|
56
|
+
_swap(srcNode, destNode) {
|
|
56
57
|
const { key, val, count, height } = destNode;
|
|
57
58
|
const tempNode = this.createNode(key, val, count);
|
|
58
59
|
if (tempNode) {
|
|
@@ -257,15 +258,15 @@ export class TreeMultiset extends AVLTree {
|
|
|
257
258
|
}
|
|
258
259
|
}
|
|
259
260
|
/**
|
|
260
|
-
* The `
|
|
261
|
+
* The `delete` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
261
262
|
* node that needs to be balanced.
|
|
262
263
|
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
263
264
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
264
265
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
265
266
|
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
266
|
-
* @returns The function `
|
|
267
|
+
* @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
267
268
|
*/
|
|
268
|
-
|
|
269
|
+
delete(nodeOrKey, ignoreCount = false) {
|
|
269
270
|
const bstDeletedResult = [];
|
|
270
271
|
if (!this.root)
|
|
271
272
|
return bstDeletedResult;
|
|
@@ -299,7 +300,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
299
300
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
300
301
|
if (leftSubTreeRightMost) {
|
|
301
302
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
302
|
-
orgCurrent = this.
|
|
303
|
+
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
303
304
|
if (parentOfLeftSubTreeMax) {
|
|
304
305
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
305
306
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -321,110 +322,6 @@ export class TreeMultiset extends AVLTree {
|
|
|
321
322
|
}
|
|
322
323
|
return bstDeletedResult;
|
|
323
324
|
}
|
|
324
|
-
/**
|
|
325
|
-
* The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
|
|
326
|
-
* recursive or iterative traversal.
|
|
327
|
-
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
328
|
-
* binary tree.
|
|
329
|
-
* @returns The function `getSubTreeCount` returns an array `[number, number]`.
|
|
330
|
-
*/
|
|
331
|
-
getSubTreeCount(subTreeRoot) {
|
|
332
|
-
const res = [0, 0];
|
|
333
|
-
if (!subTreeRoot)
|
|
334
|
-
return res;
|
|
335
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
336
|
-
const _traverse = (cur) => {
|
|
337
|
-
res[0]++;
|
|
338
|
-
res[1] += cur.count;
|
|
339
|
-
cur.left && _traverse(cur.left);
|
|
340
|
-
cur.right && _traverse(cur.right);
|
|
341
|
-
};
|
|
342
|
-
_traverse(subTreeRoot);
|
|
343
|
-
return res;
|
|
344
|
-
}
|
|
345
|
-
else {
|
|
346
|
-
const stack = [subTreeRoot];
|
|
347
|
-
while (stack.length > 0) {
|
|
348
|
-
const cur = stack.pop();
|
|
349
|
-
res[0]++;
|
|
350
|
-
res[1] += cur.count;
|
|
351
|
-
cur.right && stack.push(cur.right);
|
|
352
|
-
cur.left && stack.push(cur.left);
|
|
353
|
-
}
|
|
354
|
-
return res;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
359
|
-
* recursively or iteratively.
|
|
360
|
-
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
361
|
-
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree) or
|
|
362
|
-
* `null` if the subtree is empty.
|
|
363
|
-
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
364
|
-
*/
|
|
365
|
-
subTreeSumCount(subTreeRoot) {
|
|
366
|
-
if (typeof subTreeRoot === 'number')
|
|
367
|
-
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
368
|
-
if (!subTreeRoot)
|
|
369
|
-
return 0;
|
|
370
|
-
let sum = 0;
|
|
371
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
372
|
-
const _traverse = (cur) => {
|
|
373
|
-
sum += cur.count;
|
|
374
|
-
cur.left && _traverse(cur.left);
|
|
375
|
-
cur.right && _traverse(cur.right);
|
|
376
|
-
};
|
|
377
|
-
_traverse(subTreeRoot);
|
|
378
|
-
}
|
|
379
|
-
else {
|
|
380
|
-
const stack = [subTreeRoot];
|
|
381
|
-
while (stack.length > 0) {
|
|
382
|
-
const cur = stack.pop();
|
|
383
|
-
sum += cur.count;
|
|
384
|
-
cur.right && stack.push(cur.right);
|
|
385
|
-
cur.left && stack.push(cur.left);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
return sum;
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
392
|
-
* the `count` property of each node.
|
|
393
|
-
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
394
|
-
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree), a
|
|
395
|
-
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
396
|
-
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
397
|
-
* in the subtree should be increased or decreased.
|
|
398
|
-
* @returns a boolean value.
|
|
399
|
-
*/
|
|
400
|
-
subTreeAddCount(subTreeRoot, delta) {
|
|
401
|
-
if (typeof subTreeRoot === 'number')
|
|
402
|
-
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
403
|
-
if (!subTreeRoot)
|
|
404
|
-
return false;
|
|
405
|
-
const _addByProperty = (cur) => {
|
|
406
|
-
cur.count += delta;
|
|
407
|
-
this._setCount(this.count + delta);
|
|
408
|
-
};
|
|
409
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
410
|
-
const _traverse = (cur) => {
|
|
411
|
-
_addByProperty(cur);
|
|
412
|
-
cur.left && _traverse(cur.left);
|
|
413
|
-
cur.right && _traverse(cur.right);
|
|
414
|
-
};
|
|
415
|
-
_traverse(subTreeRoot);
|
|
416
|
-
}
|
|
417
|
-
else {
|
|
418
|
-
const stack = [subTreeRoot];
|
|
419
|
-
while (stack.length > 0) {
|
|
420
|
-
const cur = stack.pop();
|
|
421
|
-
_addByProperty(cur);
|
|
422
|
-
cur.right && stack.push(cur.right);
|
|
423
|
-
cur.left && stack.push(cur.left);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
return true;
|
|
427
|
-
}
|
|
428
325
|
/**
|
|
429
326
|
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
430
327
|
* using a queue.
|
|
@@ -454,8 +351,8 @@ export class TreeMultiset extends AVLTree {
|
|
|
454
351
|
_traverse(this.root);
|
|
455
352
|
}
|
|
456
353
|
else {
|
|
457
|
-
const queue = [this.root];
|
|
458
|
-
while (queue.
|
|
354
|
+
const queue = new Queue([this.root]);
|
|
355
|
+
while (queue.size > 0) {
|
|
459
356
|
const cur = queue.shift();
|
|
460
357
|
if (cur) {
|
|
461
358
|
if (cur.count === nodeProperty) {
|
|
@@ -475,7 +372,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
475
372
|
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
476
373
|
* bfs traversal.
|
|
477
374
|
*/
|
|
478
|
-
|
|
375
|
+
bfsCount() {
|
|
479
376
|
const nodes = super.bfs('node');
|
|
480
377
|
return nodes.map(node => node.count);
|
|
481
378
|
}
|
|
@@ -502,150 +399,6 @@ export class TreeMultiset extends AVLTree {
|
|
|
502
399
|
const nodes = super.morris(pattern, 'node');
|
|
503
400
|
return nodes.map(node => node.count);
|
|
504
401
|
}
|
|
505
|
-
/**
|
|
506
|
-
* The function dfsCountIterative performs an iterative depth-first search and returns an array of node counts based on
|
|
507
|
-
* the specified traversal pattern.
|
|
508
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
|
|
509
|
-
* the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
|
|
510
|
-
* @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
|
|
511
|
-
* in the dfs traversal.
|
|
512
|
-
*/
|
|
513
|
-
dfsCountIterative(pattern = 'in') {
|
|
514
|
-
const nodes = super.dfsIterative(pattern, 'node');
|
|
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');
|
|
526
|
-
return nodes.map(node => node.count);
|
|
527
|
-
}
|
|
528
|
-
/**
|
|
529
|
-
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
530
|
-
* value than a given node.
|
|
531
|
-
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
532
|
-
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
533
|
-
*/
|
|
534
|
-
lesserSumCount(beginNode) {
|
|
535
|
-
if (typeof beginNode === 'number')
|
|
536
|
-
beginNode = this.get(beginNode, 'key');
|
|
537
|
-
if (!beginNode)
|
|
538
|
-
return 0;
|
|
539
|
-
if (!this.root)
|
|
540
|
-
return 0;
|
|
541
|
-
const key = beginNode.key;
|
|
542
|
-
let sum = 0;
|
|
543
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
544
|
-
const _traverse = (cur) => {
|
|
545
|
-
const compared = this._compare(cur.key, key);
|
|
546
|
-
if (compared === CP.eq) {
|
|
547
|
-
if (cur.right)
|
|
548
|
-
sum += this.subTreeSumCount(cur.right);
|
|
549
|
-
return;
|
|
550
|
-
}
|
|
551
|
-
else if (compared === CP.lt) {
|
|
552
|
-
if (cur.left)
|
|
553
|
-
sum += this.subTreeSumCount(cur.left);
|
|
554
|
-
sum += cur.count;
|
|
555
|
-
if (cur.right)
|
|
556
|
-
_traverse(cur.right);
|
|
557
|
-
else
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
|
-
else {
|
|
561
|
-
if (cur.left)
|
|
562
|
-
_traverse(cur.left);
|
|
563
|
-
else
|
|
564
|
-
return;
|
|
565
|
-
}
|
|
566
|
-
};
|
|
567
|
-
_traverse(this.root);
|
|
568
|
-
}
|
|
569
|
-
else {
|
|
570
|
-
const queue = [this.root];
|
|
571
|
-
while (queue.length > 0) {
|
|
572
|
-
const cur = queue.shift();
|
|
573
|
-
if (cur) {
|
|
574
|
-
const compared = this._compare(cur.key, key);
|
|
575
|
-
if (compared === CP.eq) {
|
|
576
|
-
if (cur.right)
|
|
577
|
-
sum += this.subTreeSumCount(cur.right);
|
|
578
|
-
return sum;
|
|
579
|
-
}
|
|
580
|
-
else if (compared === CP.lt) {
|
|
581
|
-
// todo maybe a bug
|
|
582
|
-
if (cur.left)
|
|
583
|
-
sum += this.subTreeSumCount(cur.left);
|
|
584
|
-
sum += cur.count;
|
|
585
|
-
if (cur.right)
|
|
586
|
-
queue.push(cur.right);
|
|
587
|
-
else
|
|
588
|
-
return sum;
|
|
589
|
-
}
|
|
590
|
-
else {
|
|
591
|
-
if (cur.left)
|
|
592
|
-
queue.push(cur.left);
|
|
593
|
-
else
|
|
594
|
-
return sum;
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
return sum;
|
|
600
|
-
}
|
|
601
|
-
/**
|
|
602
|
-
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
603
|
-
* greater than a given ID by a specified delta value.
|
|
604
|
-
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
|
|
605
|
-
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
606
|
-
* of each node should be increased.
|
|
607
|
-
* @returns a boolean value.
|
|
608
|
-
*/
|
|
609
|
-
allGreaterNodesAddCount(node, delta) {
|
|
610
|
-
if (typeof node === 'number')
|
|
611
|
-
node = this.get(node, 'key');
|
|
612
|
-
if (!node)
|
|
613
|
-
return false;
|
|
614
|
-
const key = node.key;
|
|
615
|
-
if (!this.root)
|
|
616
|
-
return false;
|
|
617
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
618
|
-
const _traverse = (cur) => {
|
|
619
|
-
const compared = this._compare(cur.key, key);
|
|
620
|
-
if (compared === CP.gt)
|
|
621
|
-
cur.count += delta;
|
|
622
|
-
if (!cur.left && !cur.right)
|
|
623
|
-
return;
|
|
624
|
-
if (cur.left && this._compare(cur.left.key, key) === CP.gt)
|
|
625
|
-
_traverse(cur.left);
|
|
626
|
-
if (cur.right && this._compare(cur.right.key, key) === CP.gt)
|
|
627
|
-
_traverse(cur.right);
|
|
628
|
-
};
|
|
629
|
-
_traverse(this.root);
|
|
630
|
-
return true;
|
|
631
|
-
}
|
|
632
|
-
else {
|
|
633
|
-
const queue = [this.root];
|
|
634
|
-
while (queue.length > 0) {
|
|
635
|
-
const cur = queue.shift();
|
|
636
|
-
if (cur) {
|
|
637
|
-
const compared = this._compare(cur.key, key);
|
|
638
|
-
if (compared === CP.gt)
|
|
639
|
-
cur.count += delta;
|
|
640
|
-
if (cur.left && this._compare(cur.left.key, key) === CP.gt)
|
|
641
|
-
queue.push(cur.left);
|
|
642
|
-
if (cur.right && this._compare(cur.right.key, key) === CP.gt)
|
|
643
|
-
queue.push(cur.right);
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
return true;
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
402
|
/**
|
|
650
403
|
* The clear() function clears the data and sets the count to 0.
|
|
651
404
|
*/
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { arrayRemove, uuidV4 } from '../../utils';
|
|
9
9
|
import { PriorityQueue } from '../priority-queue';
|
|
10
|
+
import { Queue } from '../queue';
|
|
10
11
|
export class AbstractVertex {
|
|
11
12
|
/**
|
|
12
13
|
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
@@ -265,11 +266,11 @@ export class AbstractGraph {
|
|
|
265
266
|
return null;
|
|
266
267
|
}
|
|
267
268
|
const visited = new Map();
|
|
268
|
-
const queue = [vertex1];
|
|
269
|
+
const queue = new Queue([vertex1]);
|
|
269
270
|
visited.set(vertex1, true);
|
|
270
271
|
let cost = 0;
|
|
271
|
-
while (queue.
|
|
272
|
-
for (let i = 0; i < queue.
|
|
272
|
+
while (queue.size > 0) {
|
|
273
|
+
for (let i = 0; i < queue.size; i++) {
|
|
273
274
|
const cur = queue.shift();
|
|
274
275
|
if (cur === vertex2) {
|
|
275
276
|
return cost;
|
|
@@ -48,7 +48,7 @@ export declare class HashMap<K, V> {
|
|
|
48
48
|
private resizeTable;
|
|
49
49
|
set(key: K, value: V): void;
|
|
50
50
|
get(key: K): V | undefined;
|
|
51
|
-
|
|
51
|
+
delete(key: K): void;
|
|
52
52
|
entries(): IterableIterator<[K, V]>;
|
|
53
53
|
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
54
54
|
clear(): void;
|
|
@@ -90,13 +90,13 @@ export declare class HashTable<K, V> {
|
|
|
90
90
|
*/
|
|
91
91
|
get(key: K): V | undefined;
|
|
92
92
|
/**
|
|
93
|
-
* The
|
|
93
|
+
* The delete function removes a key-value pair from a hash table.
|
|
94
94
|
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
95
95
|
* table.
|
|
96
|
-
* @returns Nothing is being returned. The `
|
|
96
|
+
* @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
|
|
97
97
|
* any value.
|
|
98
98
|
*/
|
|
99
|
-
|
|
99
|
+
delete(key: K): void;
|
|
100
100
|
/**
|
|
101
101
|
* The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
|
|
102
102
|
* capacity and rehashing all the existing key-value pairs into the new buckets.
|
|
@@ -178,13 +178,13 @@ export class HashTable {
|
|
|
178
178
|
return undefined; // Key not found
|
|
179
179
|
}
|
|
180
180
|
/**
|
|
181
|
-
* The
|
|
181
|
+
* The delete function removes a key-value pair from a hash table.
|
|
182
182
|
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
183
183
|
* table.
|
|
184
|
-
* @returns Nothing is being returned. The `
|
|
184
|
+
* @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
|
|
185
185
|
* any value.
|
|
186
186
|
*/
|
|
187
|
-
|
|
187
|
+
delete(key) {
|
|
188
188
|
const index = this._hash(key);
|
|
189
189
|
let currentNode = this._buckets[index];
|
|
190
190
|
let prevNode = null;
|
|
@@ -52,10 +52,10 @@ export declare class SkipList<K, V> {
|
|
|
52
52
|
*/
|
|
53
53
|
get(key: K): V | undefined;
|
|
54
54
|
/**
|
|
55
|
-
* The `
|
|
55
|
+
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
56
56
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
57
|
-
* @returns The `
|
|
57
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
|
|
58
58
|
* skip list, and `false` if the key was not found in the skip list.
|
|
59
59
|
*/
|
|
60
|
-
|
|
60
|
+
delete(key: K): boolean;
|
|
61
61
|
}
|
|
@@ -105,12 +105,12 @@ export class SkipList {
|
|
|
105
105
|
return undefined;
|
|
106
106
|
}
|
|
107
107
|
/**
|
|
108
|
-
* The `
|
|
108
|
+
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
109
109
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
110
|
-
* @returns The `
|
|
110
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
|
|
111
111
|
* skip list, and `false` if the key was not found in the skip list.
|
|
112
112
|
*/
|
|
113
|
-
|
|
113
|
+
delete(key) {
|
|
114
114
|
const update = new Array(this.maxLevel).fill(this.head);
|
|
115
115
|
let current = this.head;
|
|
116
116
|
for (let i = this.level - 1; i >= 0; i--) {
|
|
@@ -150,12 +150,12 @@ export declare class ArrayDeque<E> {
|
|
|
150
150
|
*/
|
|
151
151
|
insert(index: number, value: E): E[];
|
|
152
152
|
/**
|
|
153
|
-
* The
|
|
153
|
+
* The delete function removes an element from an array at a specified index.
|
|
154
154
|
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
155
155
|
* is a number that represents the index of the element to be removed.
|
|
156
156
|
* @returns The method is returning an array containing the removed element.
|
|
157
157
|
*/
|
|
158
|
-
|
|
158
|
+
delete(index: number): E[];
|
|
159
159
|
/**
|
|
160
160
|
* The function checks if an array called "_nodes" is empty.
|
|
161
161
|
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
@@ -251,12 +251,12 @@ export class ArrayDeque {
|
|
|
251
251
|
return this._nodes.splice(index, 0, value);
|
|
252
252
|
}
|
|
253
253
|
/**
|
|
254
|
-
* The
|
|
254
|
+
* The delete function removes an element from an array at a specified index.
|
|
255
255
|
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
256
256
|
* is a number that represents the index of the element to be removed.
|
|
257
257
|
* @returns The method is returning an array containing the removed element.
|
|
258
258
|
*/
|
|
259
|
-
|
|
259
|
+
delete(index) {
|
|
260
260
|
return this._nodes.splice(index, 1);
|
|
261
261
|
}
|
|
262
262
|
/**
|
|
@@ -90,7 +90,7 @@ export class Queue {
|
|
|
90
90
|
this.offset += 1;
|
|
91
91
|
if (this.offset * 2 < this.nodes.length)
|
|
92
92
|
return first;
|
|
93
|
-
// only
|
|
93
|
+
// only delete dequeued elements when reaching half size
|
|
94
94
|
// to decrease latency of shifting elements.
|
|
95
95
|
this.nodes = this.nodes.slice(this.offset);
|
|
96
96
|
this.offset = 0;
|
|
@@ -45,10 +45,10 @@ export declare class Trie {
|
|
|
45
45
|
private _caseProcess;
|
|
46
46
|
/**
|
|
47
47
|
* Remove a word from the Trie structure.
|
|
48
|
-
* @param{string} word - The word to
|
|
48
|
+
* @param{string} word - The word to delete.
|
|
49
49
|
* @returns {boolean} True if the word was successfully removed.
|
|
50
50
|
*/
|
|
51
|
-
|
|
51
|
+
delete(word: string): boolean;
|
|
52
52
|
getHeight(): number;
|
|
53
53
|
/**
|
|
54
54
|
* Check if a given input string has an absolute prefix in the Trie, meaning it's not a complete word.
|
|
@@ -96,10 +96,10 @@ export class Trie {
|
|
|
96
96
|
}
|
|
97
97
|
/**
|
|
98
98
|
* Remove a word from the Trie structure.
|
|
99
|
-
* @param{string} word - The word to
|
|
99
|
+
* @param{string} word - The word to delete.
|
|
100
100
|
* @returns {boolean} True if the word was successfully removed.
|
|
101
101
|
*/
|
|
102
|
-
|
|
102
|
+
delete(word) {
|
|
103
103
|
word = this._caseProcess(word);
|
|
104
104
|
let isDeleted = false;
|
|
105
105
|
const dfs = (cur, i) => {
|
|
@@ -3,5 +3,5 @@ import { BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../types';
|
|
|
3
3
|
export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
|
|
4
4
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
5
5
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
6
|
-
|
|
6
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
7
7
|
}
|