data-structure-typed 1.52.7 → 1.52.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 +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +146 -146
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -4
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +4 -4
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +3 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +62 -5
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +26 -11
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +9 -14
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +8 -6
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +4 -5
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +9 -8
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -4
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +4 -4
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +3 -3
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +62 -5
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +26 -11
- package/dist/mjs/data-structures/binary-tree/bst.js +9 -14
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +8 -6
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +4 -5
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +9 -8
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +58 -45
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/data-structures/binary-tree/binary-tree.ts +26 -30
- package/src/data-structures/binary-tree/bst.ts +16 -19
- package/src/data-structures/binary-tree/rb-tree.ts +8 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +9 -8
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +37 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +11 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.52.
|
|
3
|
+
"version": "1.52.9",
|
|
4
4
|
"description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -68,11 +68,11 @@
|
|
|
68
68
|
"@typescript-eslint/eslint-plugin": "^8.12.1",
|
|
69
69
|
"@typescript-eslint/parser": "^8.12.1",
|
|
70
70
|
"auto-changelog": "^2.5.0",
|
|
71
|
-
"avl-tree-typed": "^1.52.
|
|
71
|
+
"avl-tree-typed": "^1.52.8",
|
|
72
72
|
"benchmark": "^2.1.4",
|
|
73
|
-
"binary-tree-typed": "^1.52.
|
|
74
|
-
"bst-typed": "^1.52.
|
|
75
|
-
"data-structure-typed": "^1.52.
|
|
73
|
+
"binary-tree-typed": "^1.52.8",
|
|
74
|
+
"bst-typed": "^1.52.8",
|
|
75
|
+
"data-structure-typed": "^1.52.8",
|
|
76
76
|
"dependency-cruiser": "^16.5.0",
|
|
77
77
|
"doctoc": "^2.2.1",
|
|
78
78
|
"eslint": "^9.13.0",
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"eslint-import-resolver-typescript": "^3.6.3",
|
|
82
82
|
"eslint-plugin-import": "^2.31.0",
|
|
83
83
|
"fast-glob": "^3.3.2",
|
|
84
|
-
"heap-typed": "^1.52.
|
|
84
|
+
"heap-typed": "^1.52.8",
|
|
85
85
|
"istanbul-badges-readme": "^1.9.0",
|
|
86
86
|
"jest": "^29.7.0",
|
|
87
87
|
"js-sdsl": "^4.4.2",
|
|
@@ -12,7 +12,6 @@ import type {
|
|
|
12
12
|
BinaryTreeDeleteResult,
|
|
13
13
|
BSTNKeyOrNode,
|
|
14
14
|
BTNKeyOrNodeOrEntry,
|
|
15
|
-
BTNPredicate,
|
|
16
15
|
IterationType,
|
|
17
16
|
BTNEntry
|
|
18
17
|
} from '../../types';
|
|
@@ -232,9 +231,9 @@ export class AVLTreeMultiMap<
|
|
|
232
231
|
*
|
|
233
232
|
* The function overrides the delete method in a binary tree data structure, handling deletion of
|
|
234
233
|
* nodes and maintaining balance in the tree.
|
|
235
|
-
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R
|
|
234
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `predicate`
|
|
236
235
|
* parameter in the `delete` method is used to specify the condition for deleting a node from the
|
|
237
|
-
* binary tree. It can be a key, node,
|
|
236
|
+
* binary tree. It can be a key, node, or entry that determines which
|
|
238
237
|
* node(s) should be deleted.
|
|
239
238
|
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
240
239
|
* boolean flag that determines whether to ignore the count of the node being deleted. If
|
|
@@ -246,13 +245,13 @@ export class AVLTreeMultiMap<
|
|
|
246
245
|
* deleted node and whether balancing is needed in the tree.
|
|
247
246
|
*/
|
|
248
247
|
override delete(
|
|
249
|
-
|
|
248
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
250
249
|
ignoreCount = false
|
|
251
250
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
252
251
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
253
252
|
if (!this.root) return deletedResult;
|
|
254
253
|
|
|
255
|
-
const curr: NODE | undefined = this.getNode(
|
|
254
|
+
const curr: NODE | undefined = this.getNode(keyOrNodeOrEntryOrRaw) ?? undefined;
|
|
256
255
|
if (!curr) return deletedResult;
|
|
257
256
|
|
|
258
257
|
const parent: NODE | undefined = curr?.parent ? curr.parent : undefined;
|
|
@@ -13,7 +13,6 @@ import type {
|
|
|
13
13
|
BinaryTreeDeleteResult,
|
|
14
14
|
BSTNKeyOrNode,
|
|
15
15
|
BTNKeyOrNodeOrEntry,
|
|
16
|
-
BTNPredicate,
|
|
17
16
|
BTNEntry
|
|
18
17
|
} from '../../types';
|
|
19
18
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -160,15 +159,15 @@ export class AVLTree<
|
|
|
160
159
|
*
|
|
161
160
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
162
161
|
* balances the tree if necessary.
|
|
163
|
-
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R
|
|
162
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
164
163
|
* parameter in the `override delete` method can be one of the following types:
|
|
165
164
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
166
165
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
167
166
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
168
167
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
169
168
|
*/
|
|
170
|
-
override delete(
|
|
171
|
-
const deletedResults = super.delete(
|
|
169
|
+
override delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
|
|
170
|
+
const deletedResults = super.delete(keyOrNodeOrEntryOrRaw);
|
|
172
171
|
for (const { needBalanced } of deletedResults) {
|
|
173
172
|
if (needBalanced) {
|
|
174
173
|
this._balancePath(needBalanced);
|
|
@@ -515,22 +515,20 @@ export class BinaryTree<
|
|
|
515
515
|
*
|
|
516
516
|
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
517
517
|
* the deleted node along with information for tree balancing.
|
|
518
|
-
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R
|
|
518
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw
|
|
519
519
|
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
520
|
-
* node, entry
|
|
520
|
+
* node, entry or raw data. The method returns an array of
|
|
521
521
|
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
522
522
|
* balancing is needed.
|
|
523
523
|
* @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
|
|
524
524
|
* the array contains information about the node that was deleted (`deleted`) and the node that may
|
|
525
525
|
* need to be balanced (`needBalanced`).
|
|
526
526
|
*/
|
|
527
|
-
delete(
|
|
528
|
-
keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>
|
|
529
|
-
): BinaryTreeDeleteResult<NODE>[] {
|
|
527
|
+
delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
|
|
530
528
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
531
529
|
if (!this._root) return deletedResult;
|
|
532
530
|
|
|
533
|
-
const curr = this.getNode(
|
|
531
|
+
const curr = this.getNode(keyOrNodeOrEntryOrRaw);
|
|
534
532
|
if (!curr) return deletedResult;
|
|
535
533
|
|
|
536
534
|
const parent: NODE | undefined = curr?.parent;
|
|
@@ -1683,20 +1681,14 @@ export class BinaryTree<
|
|
|
1683
1681
|
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1684
1682
|
options?: BinaryTreePrintOptions
|
|
1685
1683
|
): string {
|
|
1686
|
-
const opts = { isShowUndefined: false, isShowNull:
|
|
1684
|
+
const opts = { isShowUndefined: false, isShowNull: true, isShowRedBlackNIL: false, ...options };
|
|
1687
1685
|
beginRoot = this.ensureNode(beginRoot);
|
|
1688
1686
|
let output = '';
|
|
1689
1687
|
if (!beginRoot) return output;
|
|
1690
1688
|
|
|
1691
|
-
if (opts.isShowUndefined)
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
if (opts.isShowNull)
|
|
1695
|
-
output += `N for null
|
|
1696
|
-
`;
|
|
1697
|
-
if (opts.isShowRedBlackNIL)
|
|
1698
|
-
output += `S for Sentinel Node(NIL)
|
|
1699
|
-
`;
|
|
1689
|
+
if (opts.isShowUndefined) output += `U for undefined\n`;
|
|
1690
|
+
if (opts.isShowNull) output += `N for null\n`;
|
|
1691
|
+
if (opts.isShowRedBlackNIL) output += `S for Sentinel Node(NIL)\n`;
|
|
1700
1692
|
|
|
1701
1693
|
const display = (root: OptBTNOrNull<NODE>): void => {
|
|
1702
1694
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
@@ -1711,20 +1703,24 @@ export class BinaryTree<
|
|
|
1711
1703
|
return output;
|
|
1712
1704
|
}
|
|
1713
1705
|
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1706
|
+
/**
|
|
1707
|
+
* Time Complexity: O(n)
|
|
1708
|
+
* Space Complexity: O(n)
|
|
1709
|
+
*
|
|
1710
|
+
* The function `print` in TypeScript overrides the default print behavior to log a visual
|
|
1711
|
+
* representation of the binary tree to the console.
|
|
1712
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is used to specify the
|
|
1713
|
+
* printing options for the binary tree. It is an optional parameter that allows you to customize how
|
|
1714
|
+
* the binary tree is printed, such as choosing between different traversal orders or formatting
|
|
1715
|
+
* options.
|
|
1716
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1717
|
+
* `override print` method is used to specify the starting point for printing the binary tree. It can
|
|
1718
|
+
* be either a key, a node, an entry, or the root of the tree. If no specific starting point is
|
|
1719
|
+
* provided, the default value is set to
|
|
1720
|
+
*/
|
|
1721
|
+
override print(options?: BinaryTreePrintOptions, beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root) {
|
|
1722
|
+
console.log(this.toVisual(beginRoot, options));
|
|
1723
|
+
}
|
|
1728
1724
|
|
|
1729
1725
|
/**
|
|
1730
1726
|
* Time complexity: O(n)
|
|
@@ -14,7 +14,6 @@ import type {
|
|
|
14
14
|
BTNEntry,
|
|
15
15
|
BTNKeyOrNodeOrEntry,
|
|
16
16
|
BTNPredicate,
|
|
17
|
-
BTNPureKeyOrNodeOrEntry,
|
|
18
17
|
Comparator,
|
|
19
18
|
CP,
|
|
20
19
|
DFSOrderPattern,
|
|
@@ -310,28 +309,26 @@ export class BST<
|
|
|
310
309
|
if (!isBalanceAdd) {
|
|
311
310
|
for (const kve of keysOrNodesOrEntriesOrRaws) {
|
|
312
311
|
const value = valuesIterator?.next().value;
|
|
313
|
-
|
|
314
|
-
inserted.push(nn);
|
|
312
|
+
inserted.push(this.add(kve, value));
|
|
315
313
|
}
|
|
316
314
|
return inserted;
|
|
317
315
|
}
|
|
318
316
|
|
|
319
|
-
const realBTNExemplars:
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
if (kve === undefined || kve === null) return false;
|
|
325
|
-
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
326
|
-
};
|
|
317
|
+
const realBTNExemplars: {
|
|
318
|
+
key: R | BTNKeyOrNodeOrEntry<K, V, NODE>;
|
|
319
|
+
value: V | undefined;
|
|
320
|
+
orgIndex: number;
|
|
321
|
+
}[] = [];
|
|
327
322
|
|
|
323
|
+
let i = 0;
|
|
328
324
|
for (const kve of keysOrNodesOrEntriesOrRaws) {
|
|
329
|
-
|
|
325
|
+
realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i });
|
|
326
|
+
i++;
|
|
330
327
|
}
|
|
331
328
|
|
|
332
|
-
let sorted:
|
|
329
|
+
let sorted: { key: R | BTNKeyOrNodeOrEntry<K, V, NODE>; value: V | undefined; orgIndex: number }[] = [];
|
|
333
330
|
|
|
334
|
-
sorted = realBTNExemplars.sort((a, b) => {
|
|
331
|
+
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
335
332
|
let keyA: K | undefined | null, keyB: K | undefined | null;
|
|
336
333
|
if (this.isEntry(a)) keyA = a[0];
|
|
337
334
|
else if (this.isRealNode(a)) keyA = a.key;
|
|
@@ -355,12 +352,12 @@ export class BST<
|
|
|
355
352
|
return 0;
|
|
356
353
|
});
|
|
357
354
|
|
|
358
|
-
const _dfs = (arr:
|
|
355
|
+
const _dfs = (arr: { key: R | BTNKeyOrNodeOrEntry<K, V, NODE>; value: V | undefined; orgIndex: number }[]) => {
|
|
359
356
|
if (arr.length === 0) return;
|
|
360
357
|
|
|
361
358
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
362
|
-
const
|
|
363
|
-
inserted.
|
|
359
|
+
const { key, value, orgIndex } = arr[mid];
|
|
360
|
+
inserted[orgIndex] = this.add(key, value);
|
|
364
361
|
_dfs(arr.slice(0, mid));
|
|
365
362
|
_dfs(arr.slice(mid + 1));
|
|
366
363
|
};
|
|
@@ -374,8 +371,8 @@ export class BST<
|
|
|
374
371
|
const [l, r] = popped;
|
|
375
372
|
if (l <= r) {
|
|
376
373
|
const m = l + Math.floor((r - l) / 2);
|
|
377
|
-
const
|
|
378
|
-
inserted.
|
|
374
|
+
const { key, value, orgIndex } = sorted[m];
|
|
375
|
+
inserted[orgIndex] = this.add(key, value);
|
|
379
376
|
stack.push([m + 1, r]);
|
|
380
377
|
stack.push([l, m - 1]);
|
|
381
378
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
3
|
BTNKeyOrNodeOrEntry,
|
|
4
|
-
BTNPredicate,
|
|
5
4
|
CRUD,
|
|
6
5
|
OptBSTN,
|
|
7
6
|
RBTNColor,
|
|
@@ -230,7 +229,7 @@ export class RedBlackTree<
|
|
|
230
229
|
*
|
|
231
230
|
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
232
231
|
* a given predicate and maintain the binary search tree properties.
|
|
233
|
-
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R
|
|
232
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
234
233
|
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
235
234
|
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
236
235
|
* function that determines which node(s) should be deleted.
|
|
@@ -238,13 +237,16 @@ export class RedBlackTree<
|
|
|
238
237
|
* objects. Each object in the array contains information about the deleted node and whether
|
|
239
238
|
* balancing is needed.
|
|
240
239
|
*/
|
|
241
|
-
override delete(
|
|
242
|
-
if (
|
|
240
|
+
override delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
|
|
241
|
+
if (keyOrNodeOrEntryOrRaw === null) return [];
|
|
243
242
|
|
|
244
243
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
245
244
|
let nodeToDelete: OptBSTN<NODE>;
|
|
246
|
-
if (this._isPredicated(
|
|
247
|
-
else
|
|
245
|
+
if (this._isPredicated(keyOrNodeOrEntryOrRaw)) nodeToDelete = this.getNode(keyOrNodeOrEntryOrRaw);
|
|
246
|
+
else
|
|
247
|
+
nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw)
|
|
248
|
+
? keyOrNodeOrEntryOrRaw
|
|
249
|
+
: this.getNode(keyOrNodeOrEntryOrRaw);
|
|
248
250
|
|
|
249
251
|
if (!nodeToDelete) {
|
|
250
252
|
return results;
|
|
@@ -9,7 +9,6 @@ import type {
|
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNKeyOrNode,
|
|
11
11
|
BTNKeyOrNodeOrEntry,
|
|
12
|
-
BTNPredicate,
|
|
13
12
|
IterationType,
|
|
14
13
|
OptBSTN,
|
|
15
14
|
RBTNColor,
|
|
@@ -232,10 +231,9 @@ export class TreeMultiMap<
|
|
|
232
231
|
*
|
|
233
232
|
* The function `delete` in TypeScript overrides the deletion operation in a binary tree data
|
|
234
233
|
* structure, handling cases where nodes have children and maintaining balance in the tree.
|
|
235
|
-
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R
|
|
234
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `predicate`
|
|
236
235
|
* parameter in the `delete` method is used to specify the condition or key based on which a node
|
|
237
|
-
* should be deleted from the binary tree. It can be a key, a node, an entry
|
|
238
|
-
* function.
|
|
236
|
+
* should be deleted from the binary tree. It can be a key, a node, or an entry.
|
|
239
237
|
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
240
238
|
* boolean flag that determines whether to ignore the count of nodes when performing deletion. If
|
|
241
239
|
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
|
|
@@ -243,16 +241,19 @@ export class TreeMultiMap<
|
|
|
243
241
|
* @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
|
|
244
242
|
*/
|
|
245
243
|
override delete(
|
|
246
|
-
|
|
244
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
247
245
|
ignoreCount = false
|
|
248
246
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
249
|
-
if (
|
|
247
|
+
if (keyOrNodeOrEntryOrRaw === null) return [];
|
|
250
248
|
|
|
251
249
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
252
250
|
|
|
253
251
|
let nodeToDelete: OptBSTN<NODE>;
|
|
254
|
-
if (this._isPredicated(
|
|
255
|
-
else
|
|
252
|
+
if (this._isPredicated(keyOrNodeOrEntryOrRaw)) nodeToDelete = this.getNode(keyOrNodeOrEntryOrRaw);
|
|
253
|
+
else
|
|
254
|
+
nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw)
|
|
255
|
+
? keyOrNodeOrEntryOrRaw
|
|
256
|
+
: this.getNode(keyOrNodeOrEntryOrRaw);
|
|
256
257
|
|
|
257
258
|
if (!nodeToDelete) {
|
|
258
259
|
return results;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
|
2
2
|
import { IterationType, OptValue } from '../../common';
|
|
3
|
-
import { DFSOperation } from
|
|
3
|
+
import { DFSOperation } from '../../../constants';
|
|
4
4
|
|
|
5
5
|
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
6
6
|
|
|
@@ -23,10 +23,10 @@ describe('Individual package BST operations test', () => {
|
|
|
23
23
|
const nodeId10 = bst.getNode(10);
|
|
24
24
|
expect(nodeId10?.key).toBe(10);
|
|
25
25
|
|
|
26
|
-
const nodeVal9 = bst.getNode(
|
|
26
|
+
const nodeVal9 = bst.getNode(node => node.value === 9);
|
|
27
27
|
expect(nodeVal9?.key).toBe(undefined);
|
|
28
28
|
|
|
29
|
-
const nodeVal11 = bst.getNode(
|
|
29
|
+
const nodeVal11 = bst.getNode(node => node.value === 11);
|
|
30
30
|
expect(nodeVal11?.key).toBe(11);
|
|
31
31
|
|
|
32
32
|
const leftMost = bst.getLeftMost(node => node);
|
|
@@ -292,6 +292,26 @@ describe('AVLTree', () => {
|
|
|
292
292
|
// You can add more specific assertions to check the tree's balance and structure.
|
|
293
293
|
});
|
|
294
294
|
|
|
295
|
+
it('should addMany undefined and null', () => {
|
|
296
|
+
const avl = new AVLTree<number, string>();
|
|
297
|
+
const addManyWithUndefined = avl.addMany([1, undefined, 3]);
|
|
298
|
+
expect(addManyWithUndefined).toEqual([true, false, true]);
|
|
299
|
+
expect(avl.get(undefined)).toBe(undefined);
|
|
300
|
+
const addManyWithNull = avl.addMany([1, null, 3, 4]);
|
|
301
|
+
expect(addManyWithNull).toEqual([true, false, true, true]);
|
|
302
|
+
const addManyEntriesWithNull = avl.addMany([
|
|
303
|
+
[1, '1'],
|
|
304
|
+
[null, 'null'],
|
|
305
|
+
[3, '3'],
|
|
306
|
+
[4, '4']
|
|
307
|
+
]);
|
|
308
|
+
expect(addManyEntriesWithNull).toEqual([true, false, true, true]);
|
|
309
|
+
expect(avl.get(null)).toBe(undefined);
|
|
310
|
+
const node0 = avl.add(0, '0');
|
|
311
|
+
expect(node0).toBe(true);
|
|
312
|
+
expect(avl.get(0)).toBe('0');
|
|
313
|
+
});
|
|
314
|
+
|
|
295
315
|
it('should balance the tree using _balanceLR when nodes are deleted', () => {
|
|
296
316
|
const avlTree = new AVLTree();
|
|
297
317
|
avlTree.add([10, 'A']);
|
|
@@ -105,6 +105,27 @@ describe('BinaryTree addMany', () => {
|
|
|
105
105
|
expect(tree.getNodeByKey(4)?.value).toBe(44);
|
|
106
106
|
expect(tree.getNodeByKey(1)?.value).toBe(1);
|
|
107
107
|
});
|
|
108
|
+
|
|
109
|
+
it('should addMany undefined and null', () => {
|
|
110
|
+
const binaryTree = new BinaryTree<number, string>();
|
|
111
|
+
const addManyWithUndefined = binaryTree.addMany([1, undefined, 3]);
|
|
112
|
+
expect(addManyWithUndefined).toEqual([true, false, true]);
|
|
113
|
+
expect(binaryTree.get(undefined)).toBe(undefined);
|
|
114
|
+
const addManyWithNull = binaryTree.addMany([1, null, 3, 4]);
|
|
115
|
+
expect(addManyWithNull).toEqual([true, true, true, true]);
|
|
116
|
+
const addManyEntriesWithNull = binaryTree.addMany([
|
|
117
|
+
[1, '1'],
|
|
118
|
+
[null, 'null'],
|
|
119
|
+
[3, '3'],
|
|
120
|
+
[4, '4']
|
|
121
|
+
]);
|
|
122
|
+
expect(addManyEntriesWithNull).toEqual([true, true, true, true]);
|
|
123
|
+
expect(binaryTree.get(null)).toBe(undefined);
|
|
124
|
+
expect(binaryTree.getNode(null)).toBe(null);
|
|
125
|
+
const node0 = binaryTree.add(0, '0');
|
|
126
|
+
expect(node0).toBe(true);
|
|
127
|
+
expect(binaryTree.get(0)).toBe('0');
|
|
128
|
+
});
|
|
108
129
|
});
|
|
109
130
|
|
|
110
131
|
describe('BinaryTree', () => {
|
|
@@ -298,27 +319,26 @@ describe('BinaryTree', () => {
|
|
|
298
319
|
it('should isSubtreeBST', () => {
|
|
299
320
|
expect(tree.toVisual()).toBe('');
|
|
300
321
|
tree.addMany([4, 2, 6, 1, 3, 5, 7, 4]);
|
|
301
|
-
expect(tree.toVisual()).toBe(
|
|
322
|
+
expect(tree.toVisual()).toBe('N for null\n' +
|
|
302
323
|
' ___4___ \n' +
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
);
|
|
324
|
+
' / \\ \n' +
|
|
325
|
+
' _2_ _6_ \n' +
|
|
326
|
+
' / \\ / \\ \n' +
|
|
327
|
+
' 1 3 5 7 \n' +
|
|
328
|
+
' \n');
|
|
309
329
|
const visualized = tree.toVisual(undefined, { isShowUndefined: true, isShowNull: true, isShowRedBlackNIL: true });
|
|
310
330
|
expect(visualized).toBe(
|
|
311
331
|
'U for undefined\n' +
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
332
|
+
'N for null\n' +
|
|
333
|
+
'S for Sentinel Node(NIL)\n' +
|
|
334
|
+
' _______4_______ \n' +
|
|
335
|
+
' / \\ \n' +
|
|
336
|
+
' ___2___ ___6___ \n' +
|
|
337
|
+
' / \\ / \\ \n' +
|
|
338
|
+
' _1_ _3_ _5_ _7_ \n' +
|
|
339
|
+
' / \\ / \\ / \\ / \\ \n' +
|
|
340
|
+
' U U U U U U U U \n' +
|
|
341
|
+
' \n'
|
|
322
342
|
);
|
|
323
343
|
|
|
324
344
|
expect(tree.isBST(tree.getNode(4), 'RECURSIVE')).toBe(true);
|
|
@@ -16,22 +16,27 @@ describe('BST operations test', () => {
|
|
|
16
16
|
expect(isAdd0).toBe(true);
|
|
17
17
|
expect(bst.get(0)).toBe('0');
|
|
18
18
|
});
|
|
19
|
+
|
|
19
20
|
it('should addMany undefined and null', () => {
|
|
20
21
|
const bst = new BST<number, string>();
|
|
21
22
|
const addManyWithUndefined = bst.addMany([1, undefined, 3]);
|
|
22
|
-
|
|
23
|
-
// expect(addManyWithUndefined).toEqual([true, false, true]);
|
|
24
|
-
expect(addManyWithUndefined).toEqual([true, true]);
|
|
23
|
+
expect(addManyWithUndefined).toEqual([true, false, true]);
|
|
25
24
|
expect(bst.get(undefined)).toBe(undefined);
|
|
26
25
|
const addManyWithNull = bst.addMany([1, null, 3, 4]);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
expect(addManyWithNull).toEqual([true, false, true, true]);
|
|
27
|
+
const addManyEntriesWithNull = bst.addMany([
|
|
28
|
+
[1, '1'],
|
|
29
|
+
[null, 'null'],
|
|
30
|
+
[3, '3'],
|
|
31
|
+
[4, '4']
|
|
32
|
+
]);
|
|
33
|
+
expect(addManyEntriesWithNull).toEqual([true, false, true, true]);
|
|
30
34
|
expect(bst.get(null)).toBe(undefined);
|
|
31
35
|
const node0 = bst.add(0, '0');
|
|
32
36
|
expect(node0).toBe(true);
|
|
33
37
|
expect(bst.get(0)).toBe('0');
|
|
34
38
|
});
|
|
39
|
+
|
|
35
40
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
36
41
|
const bst = new BST<number, number>();
|
|
37
42
|
expect(bst).toBeInstanceOf(BST);
|