data-structure-typed 1.36.9 → 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.
Files changed (33) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -11
  3. package/dist/data-structures/binary-tree/binary-tree.js +8 -57
  4. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  5. package/dist/data-structures/binary-tree/bst.d.ts +6 -20
  6. package/dist/data-structures/binary-tree/bst.js +22 -122
  7. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  8. package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -55
  9. package/dist/data-structures/binary-tree/tree-multiset.js +3 -240
  10. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  11. package/dist/data-structures/graph/abstract-graph.js +4 -3
  12. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  13. package/lib/data-structures/binary-tree/binary-tree.d.ts +1 -11
  14. package/lib/data-structures/binary-tree/binary-tree.js +8 -57
  15. package/lib/data-structures/binary-tree/bst.d.ts +6 -20
  16. package/lib/data-structures/binary-tree/bst.js +22 -122
  17. package/lib/data-structures/binary-tree/tree-multiset.d.ts +1 -55
  18. package/lib/data-structures/binary-tree/tree-multiset.js +3 -240
  19. package/lib/data-structures/graph/abstract-graph.js +4 -3
  20. package/package.json +9 -7
  21. package/src/data-structures/binary-tree/binary-tree.ts +8 -62
  22. package/src/data-structures/binary-tree/bst.ts +22 -106
  23. package/src/data-structures/binary-tree/tree-multiset.ts +3 -231
  24. package/src/data-structures/graph/abstract-graph.ts +4 -3
  25. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +5 -3
  26. package/test/unit/data-structures/binary-tree/bst.test.ts +44 -7
  27. package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
  28. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +39 -9
  29. package/test/unit/data-structures/queue/deque.test.ts +17 -0
  30. package/test/unit/data-structures/queue/queue.test.ts +42 -0
  31. package/test/utils/big-o.ts +5 -4
  32. package/umd/bundle.min.js +1 -1
  33. package/umd/bundle.min.js.map +1 -1
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import { FamilyPosition, LoopType } from '../../types';
9
9
  import { trampoline } from '../../utils';
10
+ import { Queue } from '../queue';
10
11
  export class BinaryTreeNode {
11
12
  /**
12
13
  * The constructor function initializes a BinaryTreeNode object with a key and an optional value.
@@ -165,8 +166,8 @@ export class BinaryTree {
165
166
  */
166
167
  add(keyOrNode, val) {
167
168
  const _bfs = (root, newNode) => {
168
- const queue = [root];
169
- while (queue.length > 0) {
169
+ const queue = new Queue([root]);
170
+ while (queue.size > 0) {
170
171
  const cur = queue.shift();
171
172
  if (cur) {
172
173
  if (newNode && cur.key === newNode.key)
@@ -461,8 +462,8 @@ export class BinaryTree {
461
462
  _traverse(this.root);
462
463
  }
463
464
  else {
464
- const queue = [this.root];
465
- while (queue.length > 0) {
465
+ const queue = new Queue([this.root]);
466
+ while (queue.size > 0) {
466
467
  const cur = queue.shift();
467
468
  if (cur) {
468
469
  if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
@@ -664,60 +665,11 @@ export class BinaryTree {
664
665
  return size;
665
666
  }
666
667
  }
667
- /**
668
- * The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
669
- * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
670
- * tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
671
- * @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
672
- * property of the binary tree node to use for calculating the sum. It can be either 'key' or 'val'. If propertyName is
673
- * not provided, it defaults to 'key'.
674
- * @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
675
- */
676
- subTreeSum(subTreeRoot, propertyName = 'key') {
677
- if (typeof subTreeRoot === 'number')
678
- subTreeRoot = this.get(subTreeRoot, 'key');
679
- if (!subTreeRoot)
680
- return 0;
681
- let sum = 0;
682
- const _sumByProperty = (cur) => {
683
- let needSum;
684
- switch (propertyName) {
685
- case 'key':
686
- needSum = cur.key;
687
- break;
688
- case 'val':
689
- needSum = typeof cur.val === 'number' ? cur.val : 0;
690
- break;
691
- default:
692
- needSum = cur.key;
693
- break;
694
- }
695
- return needSum;
696
- };
697
- if (this._loopType === LoopType.RECURSIVE) {
698
- const _traverse = (cur) => {
699
- sum += _sumByProperty(cur);
700
- cur.left && _traverse(cur.left);
701
- cur.right && _traverse(cur.right);
702
- };
703
- _traverse(subTreeRoot);
704
- }
705
- else {
706
- const stack = [subTreeRoot];
707
- while (stack.length > 0) {
708
- const cur = stack.pop();
709
- sum += _sumByProperty(cur);
710
- cur.right && stack.push(cur.right);
711
- cur.left && stack.push(cur.left);
712
- }
713
- }
714
- return sum;
715
- }
716
668
  /**
717
669
  * The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
718
670
  * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
719
671
  * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
720
- * @param callBack - The `callBack` parameter is a function that takes a node as a parameter and returns a number.
672
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
721
673
  * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
722
674
  * @returns a boolean value.
723
675
  */
@@ -754,9 +706,8 @@ export class BinaryTree {
754
706
  */
755
707
  bfs(nodeOrPropertyName = 'key') {
756
708
  this._clearResults();
757
- const queue = [this.root];
758
- while (queue.length !== 0) {
759
- // TODO Array.shift is not efficient, consider using Deque
709
+ const queue = new Queue([this.root]);
710
+ while (queue.size !== 0) {
760
711
  const cur = queue.shift();
761
712
  if (cur) {
762
713
  this._accumulatedByPropertyName(cur, nodeOrPropertyName);
@@ -78,28 +78,14 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
78
78
  */
79
79
  getNodes(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[];
80
80
  /**
81
- * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
82
- * less than a given node.
83
- * @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
84
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
85
- * specifies the property name to use for calculating the sum. If not provided, it defaults to `'key'`.
86
- * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
87
- * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
88
- */
89
- lesserSum(beginNode: N | BinaryTreeNodeKey | null, propertyName?: BinaryTreeNodePropertyName): number;
90
- /**
91
- * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
81
+ * The `lesserOrGreaterForeach` function adds a delta value to the specified property of all nodes in a binary tree that
92
82
  * have a greater value than a given node.
93
- * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type),
94
- * `BinaryTreeNodeKey`, or `null`. It represents the node in the binary tree to which the delta value will be added.
95
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
96
- * each greater node should be increased.
97
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
98
- * specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
99
- * 'key'.
100
- * @returns a boolean value.
83
+ * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It
84
+ * represents the node in the binary tree to which the delta value will be added.
85
+ * @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta
86
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a boolean
101
87
  */
102
- allGreaterNodesAdd(node: N | BinaryTreeNodeKey | null, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
88
+ lesserOrGreaterForeach(node: N | BinaryTreeNodeKey | null, lesserOrGreater: CP | undefined, callback: (node: N) => void): boolean;
103
89
  /**
104
90
  * Balancing Adjustment:
105
91
  * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
@@ -1,5 +1,6 @@
1
1
  import { CP, LoopType } from '../../types';
2
2
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
3
+ import { Queue } from '../queue';
3
4
  export class BSTNode extends BinaryTreeNode {
4
5
  constructor(key, val) {
5
6
  super(key, val);
@@ -41,7 +42,7 @@ export class BST extends BinaryTree {
41
42
  * @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
42
43
  */
43
44
  add(keyOrNode, val) {
44
- // TODO support node as a param
45
+ // TODO support node as a parameter
45
46
  let inserted = null;
46
47
  let newNode = null;
47
48
  if (keyOrNode instanceof BSTNode) {
@@ -125,7 +126,8 @@ export class BST extends BinaryTree {
125
126
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
126
127
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
127
128
  */
128
- addMany(keysOrNodes, data, isBalanceAdd = false) {
129
+ addMany(keysOrNodes, data, isBalanceAdd = true) {
130
+ // TODO this addMany function is inefficient, it should be optimized
129
131
  function hasNoNull(arr) {
130
132
  return arr.indexOf(null) === -1;
131
133
  }
@@ -256,8 +258,8 @@ export class BST extends BinaryTree {
256
258
  _traverse(this.root);
257
259
  }
258
260
  else {
259
- const queue = [this.root];
260
- while (queue.length > 0) {
261
+ const queue = new Queue([this.root]);
262
+ while (queue.size > 0) {
261
263
  const cur = queue.shift();
262
264
  if (cur) {
263
265
  if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
@@ -279,106 +281,14 @@ export class BST extends BinaryTree {
279
281
  }
280
282
  // --- start additional functions
281
283
  /**
282
- * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
283
- * less than a given node.
284
- * @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
285
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
286
- * specifies the property name to use for calculating the sum. If not provided, it defaults to `'key'`.
287
- * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
288
- * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
289
- */
290
- lesserSum(beginNode, propertyName = 'key') {
291
- if (typeof beginNode === 'number')
292
- beginNode = this.get(beginNode, 'key');
293
- if (!beginNode)
294
- return 0;
295
- if (!this.root)
296
- return 0;
297
- const key = beginNode.key;
298
- const getSumByPropertyName = (cur) => {
299
- let needSum;
300
- switch (propertyName) {
301
- case 'key':
302
- needSum = cur.key;
303
- break;
304
- default:
305
- needSum = cur.key;
306
- break;
307
- }
308
- return needSum;
309
- };
310
- let sum = 0;
311
- if (this.loopType === LoopType.RECURSIVE) {
312
- const _traverse = (cur) => {
313
- const compared = this._compare(cur.key, key);
314
- if (compared === CP.eq) {
315
- if (cur.right)
316
- sum += this.subTreeSum(cur.right, propertyName);
317
- return;
318
- }
319
- else if (compared === CP.lt) {
320
- if (cur.left)
321
- sum += this.subTreeSum(cur.left, propertyName);
322
- sum += getSumByPropertyName(cur);
323
- if (cur.right)
324
- _traverse(cur.right);
325
- else
326
- return;
327
- }
328
- else {
329
- if (cur.left)
330
- _traverse(cur.left);
331
- else
332
- return;
333
- }
334
- };
335
- _traverse(this.root);
336
- }
337
- else {
338
- const queue = [this.root];
339
- while (queue.length > 0) {
340
- const cur = queue.shift();
341
- if (cur) {
342
- const compared = this._compare(cur.key, key);
343
- if (compared === CP.eq) {
344
- if (cur.right)
345
- sum += this.subTreeSum(cur.right, propertyName);
346
- return sum;
347
- }
348
- else if (compared === CP.lt) {
349
- // todo maybe a bug
350
- if (cur.left)
351
- sum += this.subTreeSum(cur.left, propertyName);
352
- sum += getSumByPropertyName(cur);
353
- if (cur.right)
354
- queue.push(cur.right);
355
- else
356
- return sum;
357
- }
358
- else {
359
- if (cur.left)
360
- queue.push(cur.left);
361
- else
362
- return sum;
363
- }
364
- }
365
- }
366
- }
367
- return sum;
368
- }
369
- /**
370
- * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
284
+ * The `lesserOrGreaterForeach` function adds a delta value to the specified property of all nodes in a binary tree that
371
285
  * have a greater value than a given node.
372
- * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type),
373
- * `BinaryTreeNodeKey`, or `null`. It represents the node in the binary tree to which the delta value will be added.
374
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
375
- * each greater node should be increased.
376
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
377
- * specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
378
- * 'key'.
379
- * @returns a boolean value.
286
+ * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It
287
+ * represents the node in the binary tree to which the delta value will be added.
288
+ * @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta
289
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a boolean
380
290
  */
381
- allGreaterNodesAdd(node, delta, propertyName = 'key') {
291
+ lesserOrGreaterForeach(node, lesserOrGreater = CP.lt, callback) {
382
292
  if (typeof node === 'number')
383
293
  node = this.get(node, 'key');
384
294
  if (!node)
@@ -386,42 +296,32 @@ export class BST extends BinaryTree {
386
296
  const key = node.key;
387
297
  if (!this.root)
388
298
  return false;
389
- const _sumByPropertyName = (cur) => {
390
- switch (propertyName) {
391
- case 'key':
392
- cur.key += delta;
393
- break;
394
- default:
395
- cur.key += delta;
396
- break;
397
- }
398
- };
399
299
  if (this.loopType === LoopType.RECURSIVE) {
400
300
  const _traverse = (cur) => {
401
301
  const compared = this._compare(cur.key, key);
402
- if (compared === CP.gt)
403
- _sumByPropertyName(cur);
302
+ if (compared === lesserOrGreater)
303
+ callback(cur);
404
304
  if (!cur.left && !cur.right)
405
305
  return;
406
- if (cur.left && this._compare(cur.left.key, key) === CP.gt)
306
+ if (cur.left && this._compare(cur.left.key, key) === lesserOrGreater)
407
307
  _traverse(cur.left);
408
- if (cur.right && this._compare(cur.right.key, key) === CP.gt)
308
+ if (cur.right && this._compare(cur.right.key, key) === lesserOrGreater)
409
309
  _traverse(cur.right);
410
310
  };
411
311
  _traverse(this.root);
412
312
  return true;
413
313
  }
414
314
  else {
415
- const queue = [this.root];
416
- while (queue.length > 0) {
315
+ const queue = new Queue([this.root]);
316
+ while (queue.size > 0) {
417
317
  const cur = queue.shift();
418
318
  if (cur) {
419
319
  const compared = this._compare(cur.key, key);
420
- if (compared === CP.gt)
421
- _sumByPropertyName(cur);
422
- if (cur.left && this._compare(cur.left.key, key) === CP.gt)
320
+ if (compared === lesserOrGreater)
321
+ callback(cur);
322
+ if (cur.left && this._compare(cur.left.key, key) === lesserOrGreater)
423
323
  queue.push(cur.left);
424
- if (cur.right && this._compare(cur.right.key, key) === CP.gt)
324
+ if (cur.right && this._compare(cur.right.key, key) === lesserOrGreater)
425
325
  queue.push(cur.right);
426
326
  }
427
327
  }
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
9
- import { BinaryTreeDeletedResult, DFSOrderPattern, LoopType } from '../../types';
9
+ import { BinaryTreeDeletedResult, DFSOrderPattern } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
12
  export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> {
@@ -102,34 +102,6 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
102
102
  * @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
103
103
  */
104
104
  delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
105
- /**
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;
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.
@@ -164,32 +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
- * @param loopType - The loopType parameter is a string that specifies the type of loop to use when traversing the
173
- * @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
174
- * in the dfs traversal.
175
- */
176
- dfsCount(pattern?: DFSOrderPattern, loopType?: LoopType): number[];
177
- /**
178
- * The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
179
- * value than a given node.
180
- * @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
181
- * @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
182
- */
183
- lesserSumCount(beginNode: N | BinaryTreeNodeKey | null): number;
184
- /**
185
- * The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
186
- * greater than a given ID by a specified delta value.
187
- * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
188
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
189
- * of each node should be increased.
190
- * @returns a boolean value.
191
- */
192
- allGreaterNodesAddCount(node: N | BinaryTreeNodeKey | null, delta: number): boolean;
193
139
  /**
194
140
  * The clear() function clears the data and sets the count to 0.
195
141
  */