data-structure-typed 1.50.6 → 1.50.7

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -18
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +155 -155
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js +13 -9
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/bst.js +17 -17
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -8
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +7 -16
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/types/common.d.ts +6 -0
  17. package/dist/cjs/types/common.js +8 -1
  18. package/dist/cjs/types/common.js.map +1 -1
  19. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
  20. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
  21. package/dist/mjs/data-structures/binary-tree/binary-tree.js +13 -9
  22. package/dist/mjs/data-structures/binary-tree/bst.js +17 -17
  23. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -8
  24. package/dist/mjs/data-structures/binary-tree/rb-tree.js +8 -17
  25. package/dist/mjs/types/common.d.ts +6 -0
  26. package/dist/mjs/types/common.js +7 -0
  27. package/dist/umd/data-structure-typed.js +48 -42
  28. package/dist/umd/data-structure-typed.min.js +2 -2
  29. package/dist/umd/data-structure-typed.min.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -1
  32. package/src/data-structures/binary-tree/avl-tree.ts +1 -1
  33. package/src/data-structures/binary-tree/binary-tree.ts +12 -10
  34. package/src/data-structures/binary-tree/bst.ts +18 -18
  35. package/src/data-structures/binary-tree/rb-tree.ts +11 -22
  36. package/src/types/common.ts +7 -0
  37. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +4 -1
  38. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +266 -266
  39. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +247 -235
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.50.6",
3
+ "version": "1.50.7",
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",
@@ -83,6 +83,10 @@ export class AVLTreeMultiMap<
83
83
  * @returns the sum of the count property of all nodes in the tree.
84
84
  */
85
85
  get count(): number {
86
+ return this._count;
87
+ }
88
+
89
+ getMutableCount(): number {
86
90
  let sum = 0;
87
91
  this.dfs(node => (sum += node.count));
88
92
  return sum;
@@ -414,7 +418,7 @@ export class AVLTreeMultiMap<
414
418
  * @returns The method is returning the result of calling the `_replaceNode` method from the
415
419
  * superclass, after updating the `count` property of the `newNode` object.
416
420
  */
417
- protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
421
+ protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
418
422
  newNode.count = oldNode.count + newNode.count;
419
423
  return super._replaceNode(oldNode, newNode);
420
424
  }
@@ -522,7 +522,7 @@ export class AVLTree<
522
522
  * @returns the result of calling the `_replaceNode` method on the superclass, passing in the
523
523
  * `oldNode` and `newNode` as arguments.
524
524
  */
525
- protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
525
+ protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
526
526
  newNode.height = oldNode.height;
527
527
 
528
528
  return super._replaceNode(oldNode, newNode);
@@ -1021,7 +1021,7 @@ export class BinaryTree<
1021
1021
  */
1022
1022
  getHeight(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, iterationType = this.iterationType): number {
1023
1023
  beginRoot = this.ensureNode(beginRoot);
1024
- if (!beginRoot) return -1;
1024
+ if (!this.isRealNode(beginRoot)) return -1;
1025
1025
 
1026
1026
  if (iterationType === IterationType.RECURSIVE) {
1027
1027
  const _getMaxHeight = (cur: NODE | null | undefined): number => {
@@ -1073,8 +1073,8 @@ export class BinaryTree<
1073
1073
 
1074
1074
  if (iterationType === IterationType.RECURSIVE) {
1075
1075
  const _getMinHeight = (cur: NODE | null | undefined): number => {
1076
- if (!cur) return 0;
1077
- if (!cur.left && !cur.right) return 0;
1076
+ if (!this.isRealNode(cur)) return 0;
1077
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return 0;
1078
1078
  const leftMinHeight = _getMinHeight(cur.left);
1079
1079
  const rightMinHeight = _getMinHeight(cur.right);
1080
1080
  return Math.min(leftMinHeight, rightMinHeight) + 1;
@@ -1088,16 +1088,16 @@ export class BinaryTree<
1088
1088
  const depths: Map<NODE, number> = new Map();
1089
1089
 
1090
1090
  while (stack.length > 0 || node) {
1091
- if (node) {
1091
+ if (this.isRealNode(node)) {
1092
1092
  stack.push(node);
1093
1093
  node = node.left;
1094
1094
  } else {
1095
1095
  node = stack[stack.length - 1];
1096
- if (!node.right || last === node.right) {
1096
+ if (!this.isRealNode(node.right) || last === node.right) {
1097
1097
  node = stack.pop();
1098
- if (node) {
1099
- const leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
1100
- const rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
1098
+ if (this.isRealNode(node)) {
1099
+ const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) ?? -1 : -1;
1100
+ const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) ?? -1 : -1;
1101
1101
  depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
1102
1102
  last = node;
1103
1103
  node = null;
@@ -1169,9 +1169,10 @@ export class BinaryTree<
1169
1169
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1170
1170
  iterationType = this.iterationType
1171
1171
  ): NODE | null | undefined {
1172
+ if (this.isNIL(beginRoot)) return beginRoot as NODE;
1172
1173
  beginRoot = this.ensureNode(beginRoot);
1173
1174
 
1174
- if (!beginRoot) return beginRoot;
1175
+ if (!this.isRealNode(beginRoot)) return beginRoot;
1175
1176
 
1176
1177
  if (iterationType === IterationType.RECURSIVE) {
1177
1178
  const _traverse = (cur: NODE): NODE => {
@@ -1215,6 +1216,7 @@ export class BinaryTree<
1215
1216
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1216
1217
  iterationType = this.iterationType
1217
1218
  ): NODE | null | undefined {
1219
+ if (this.isNIL(beginRoot)) return beginRoot as NODE;
1218
1220
  // TODO support get right most by passing key in
1219
1221
  beginRoot = this.ensureNode(beginRoot);
1220
1222
  if (!beginRoot) return beginRoot;
@@ -1837,7 +1839,7 @@ export class BinaryTree<
1837
1839
  * following types:
1838
1840
  * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
1839
1841
  */
1840
- print(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
1842
+ override print(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
1841
1843
  const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
1842
1844
  beginRoot = this.ensureNode(beginRoot);
1843
1845
  if (!beginRoot) return;
@@ -426,14 +426,14 @@ export class BST<
426
426
  * found in the binary tree. If no node is found, it returns `undefined`.
427
427
  */
428
428
  override getNodeByKey(key: K, iterationType = IterationType.ITERATIVE): NODE | undefined {
429
- if (!this.root) return undefined;
429
+ if (!this.isRealNode(this.root)) return undefined;
430
430
  if (iterationType === IterationType.RECURSIVE) {
431
431
  const _dfs = (cur: NODE): NODE | undefined => {
432
432
  if (cur.key === key) return cur;
433
- if (!cur.left && !cur.right) return;
433
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
434
434
 
435
- if (this._compare(cur.key, key) === CP.gt && cur.left) return _dfs(cur.left);
436
- if (this._compare(cur.key, key) === CP.lt && cur.right) return _dfs(cur.right);
435
+ if (this._compare(cur.key, key) === CP.gt && this.isRealNode(cur.left)) return _dfs(cur.left);
436
+ if (this._compare(cur.key, key) === CP.lt && this.isRealNode(cur.right)) return _dfs(cur.right);
437
437
  };
438
438
 
439
439
  return _dfs(this.root);
@@ -441,10 +441,10 @@ export class BST<
441
441
  const queue = new Queue<NODE>([this.root]);
442
442
  while (queue.size > 0) {
443
443
  const cur = queue.shift();
444
- if (cur) {
444
+ if (this.isRealNode(cur)) {
445
445
  if (this._compare(cur.key, key) === CP.eq) return cur;
446
- if (this._compare(cur.key, key) === CP.gt) cur.left && queue.push(cur.left);
447
- if (this._compare(cur.key, key) === CP.lt) cur.right && queue.push(cur.right);
446
+ if (this._compare(cur.key, key) === CP.gt) this.isRealNode(cur.left) && queue.push(cur.left);
447
+ if (this._compare(cur.key, key) === CP.lt) this.isRealNode(cur.right) && queue.push(cur.right);
448
448
  }
449
449
  }
450
450
  }
@@ -497,14 +497,14 @@ export class BST<
497
497
  if (onlyOne) return;
498
498
  }
499
499
 
500
- if (!cur.left && !cur.right) return;
500
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
501
501
  // TODO potential bug
502
502
  if (callback === this._defaultOneParamCallback) {
503
- if (this._compare(cur.key, identifier as K) === CP.gt) cur.left && _traverse(cur.left);
504
- if (this._compare(cur.key, identifier as K) === CP.lt) cur.right && _traverse(cur.right);
503
+ if (this._compare(cur.key, identifier as K) === CP.gt) this.isRealNode(cur.left) && _traverse(cur.left);
504
+ if (this._compare(cur.key, identifier as K) === CP.lt) this.isRealNode(cur.right) && _traverse(cur.right);
505
505
  } else {
506
- cur.left && _traverse(cur.left);
507
- cur.right && _traverse(cur.right);
506
+ this.isRealNode(cur.left) && _traverse(cur.left);
507
+ this.isRealNode(cur.right) && _traverse(cur.right);
508
508
  }
509
509
  };
510
510
 
@@ -513,7 +513,7 @@ export class BST<
513
513
  const queue = new Queue<NODE>([beginRoot]);
514
514
  while (queue.size > 0) {
515
515
  const cur = queue.shift();
516
- if (cur) {
516
+ if (this.isRealNode(cur)) {
517
517
  const callbackResult = callback(cur);
518
518
  if (callbackResult === identifier) {
519
519
  ans.push(cur);
@@ -521,11 +521,11 @@ export class BST<
521
521
  }
522
522
  // TODO potential bug
523
523
  if (callback === this._defaultOneParamCallback) {
524
- if (this._compare(cur.key, identifier as K) === CP.gt) cur.left && queue.push(cur.left);
525
- if (this._compare(cur.key, identifier as K) === CP.lt) cur.right && queue.push(cur.right);
524
+ if (this._compare(cur.key, identifier as K) === CP.gt) this.isRealNode(cur.left) && queue.push(cur.left);
525
+ if (this._compare(cur.key, identifier as K) === CP.lt) this.isRealNode(cur.right) && queue.push(cur.right);
526
526
  } else {
527
- cur.left && queue.push(cur.left);
528
- cur.right && queue.push(cur.right);
527
+ this.isRealNode(cur.left) && queue.push(cur.left);
528
+ this.isRealNode(cur.right) && queue.push(cur.right);
529
529
  }
530
530
  }
531
531
  }
@@ -856,7 +856,7 @@ export class BST<
856
856
  * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
857
857
  * can either be an object of type `NODE` or it can be `undefined`.
858
858
  */
859
- protected _setRoot(v: NODE | undefined) {
859
+ protected override _setRoot(v: NODE | undefined) {
860
860
  if (v) {
861
861
  v.parent = undefined;
862
862
  }
@@ -7,7 +7,7 @@ import type {
7
7
  RedBlackTreeNested,
8
8
  RedBlackTreeNodeNested
9
9
  } from '../../types';
10
- import { RBTNColor } from '../../types';
10
+ import { CRUD, RBTNColor } from '../../types';
11
11
  import { BST, BSTNode } from './bst';
12
12
  import { IBinaryTree } from '../../interfaces';
13
13
 
@@ -89,26 +89,16 @@ export class RedBlackTree<
89
89
  return this._SENTINEL;
90
90
  }
91
91
 
92
- protected _root: NODE | undefined;
92
+ protected override _root: NODE | undefined;
93
93
 
94
94
  /**
95
95
  * The function returns the root node of a tree or undefined if there is no root.
96
96
  * @returns The root node of the tree structure, or undefined if there is no root node.
97
97
  */
98
- get root(): NODE | undefined {
98
+ override get root(): NODE | undefined {
99
99
  return this._root;
100
100
  }
101
101
 
102
- protected _size: number = 0;
103
-
104
- /**
105
- * The function returns the size of an object.
106
- * @returns The size of the object, which is a number.
107
- */
108
- get size(): number {
109
- return this._size;
110
- }
111
-
112
102
  /**
113
103
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
114
104
  * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
@@ -208,7 +198,7 @@ export class RedBlackTree<
208
198
  * @returns a boolean value.
209
199
  */
210
200
  override isRealNode(node: NODE | undefined): node is NODE {
211
- if (node === this._SENTINEL || node === undefined) return false;
201
+ if (node === this.SENTINEL || node === undefined) return false;
212
202
  return node instanceof RedBlackTreeNode;
213
203
  }
214
204
 
@@ -245,8 +235,7 @@ export class RedBlackTree<
245
235
  iterationType = this.iterationType
246
236
  ): NODE | null | undefined {
247
237
  if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
248
- beginRoot = this.ensureNode(beginRoot);
249
- return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
238
+ return super.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
250
239
  }
251
240
 
252
241
  /**
@@ -262,8 +251,8 @@ export class RedBlackTree<
262
251
  * size counter to zero.
263
252
  */
264
253
  override clear() {
254
+ super.clear();
265
255
  this._root = this.SENTINEL;
266
- this._size = 0;
267
256
  }
268
257
 
269
258
  /**
@@ -290,7 +279,7 @@ export class RedBlackTree<
290
279
 
291
280
  const insertStatus = this._insert(newNode);
292
281
 
293
- if (insertStatus === 'inserted') {
282
+ if (insertStatus === CRUD.CREATED) {
294
283
  // Ensure the root is black
295
284
  if (this.isRealNode(this._root)) {
296
285
  this._root.color = RBTNColor.BLACK;
@@ -299,7 +288,7 @@ export class RedBlackTree<
299
288
  }
300
289
  this._size++;
301
290
  return true;
302
- } else return insertStatus === 'updated';
291
+ } else return insertStatus === CRUD.UPDATED;
303
292
  }
304
293
 
305
294
  /**
@@ -434,7 +423,7 @@ export class RedBlackTree<
434
423
  * node in the tree.
435
424
  * @returns {'inserted' | 'updated'} - The result of the insertion.
436
425
  */
437
- protected _insert(node: NODE): 'inserted' | 'updated' {
426
+ protected _insert(node: NODE): CRUD {
438
427
  let current = this.root;
439
428
  let parent: NODE | undefined = undefined;
440
429
 
@@ -446,7 +435,7 @@ export class RedBlackTree<
446
435
  current = current.right ?? this.SENTINEL;
447
436
  } else {
448
437
  this._replaceNode(current, node);
449
- return 'updated';
438
+ return CRUD.UPDATED;
450
439
  }
451
440
  }
452
441
 
@@ -465,7 +454,7 @@ export class RedBlackTree<
465
454
  node.color = RBTNColor.RED;
466
455
 
467
456
  this._insertFixup(node);
468
- return 'inserted';
457
+ return CRUD.CREATED;
469
458
  }
470
459
 
471
460
  /**
@@ -63,3 +63,10 @@ export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNo
63
63
  export type BSTNKeyOrNode<K, N> = K | undefined | N;
64
64
 
65
65
  export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
66
+
67
+ export enum CRUD {
68
+ CREATED = 'CREATED',
69
+ READ = 'READ',
70
+ UPDATED = 'UPDATED',
71
+ DELETED = 'DELETED'
72
+ }
@@ -35,8 +35,11 @@ describe('AVLTreeMultiMap count', () => {
35
35
  [2, 2],
36
36
  [3, 3]
37
37
  ]);
38
+ tm.add([2, 2], undefined, 10);
38
39
  tm.lesserOrGreaterTraverse(node => (node.count += 2), CP.gt, 1);
39
- expect(tm.count).toBe(7);
40
+ tm.delete(2);
41
+ expect(tm.count).toBe(12);
42
+ expect(tm.getMutableCount()).toBe(16);
40
43
  });
41
44
  });
42
45