data-structure-typed 1.34.8 → 1.34.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.
Files changed (126) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +1 -1
  3. package/CONTRIBUTING.md +0 -0
  4. package/README.md +8 -8
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +280 -423
  8. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  9. package/dist/data-structures/binary-tree/avl-tree.js +63 -89
  10. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  12. package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
  13. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
  14. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  15. package/dist/data-structures/binary-tree/binary-tree.js +11 -30
  16. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  17. package/dist/data-structures/binary-tree/bst.js +127 -214
  18. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  19. package/dist/data-structures/binary-tree/rb-tree.js +19 -43
  20. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
  21. package/dist/data-structures/binary-tree/segment-tree.js +80 -122
  22. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
  25. package/dist/data-structures/binary-tree/tree-multiset.js +147 -218
  26. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  27. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  28. package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
  29. package/dist/data-structures/graph/abstract-graph.js +338 -572
  30. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  31. package/dist/data-structures/graph/directed-graph.js +145 -275
  32. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  33. package/dist/data-structures/graph/map-graph.js +46 -87
  34. package/dist/data-structures/graph/map-graph.js.map +1 -1
  35. package/dist/data-structures/graph/undirected-graph.js +90 -179
  36. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  37. package/dist/data-structures/hash/coordinate-map.js +23 -45
  38. package/dist/data-structures/hash/coordinate-map.js.map +1 -1
  39. package/dist/data-structures/hash/coordinate-set.js +20 -42
  40. package/dist/data-structures/hash/coordinate-set.js.map +1 -1
  41. package/dist/data-structures/hash/hash-map.js +85 -247
  42. package/dist/data-structures/hash/hash-map.js.map +1 -1
  43. package/dist/data-structures/hash/hash-table.js +87 -128
  44. package/dist/data-structures/hash/hash-table.js.map +1 -1
  45. package/dist/data-structures/hash/pair.js +2 -5
  46. package/dist/data-structures/hash/pair.js.map +1 -1
  47. package/dist/data-structures/hash/tree-map.js +2 -5
  48. package/dist/data-structures/hash/tree-map.js.map +1 -1
  49. package/dist/data-structures/hash/tree-set.js +2 -5
  50. package/dist/data-structures/hash/tree-set.js.map +1 -1
  51. package/dist/data-structures/heap/heap.js +56 -80
  52. package/dist/data-structures/heap/heap.js.map +1 -1
  53. package/dist/data-structures/heap/max-heap.js +8 -26
  54. package/dist/data-structures/heap/max-heap.js.map +1 -1
  55. package/dist/data-structures/heap/min-heap.js +8 -26
  56. package/dist/data-structures/heap/min-heap.js.map +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
  58. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  59. package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
  60. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
  61. package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
  62. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
  63. package/dist/data-structures/matrix/matrix.js +7 -8
  64. package/dist/data-structures/matrix/matrix.js.map +1 -1
  65. package/dist/data-structures/matrix/matrix2d.js +57 -70
  66. package/dist/data-structures/matrix/matrix2d.js.map +1 -1
  67. package/dist/data-structures/matrix/navigator.js +18 -37
  68. package/dist/data-structures/matrix/navigator.js.map +1 -1
  69. package/dist/data-structures/matrix/vector2d.js +63 -84
  70. package/dist/data-structures/matrix/vector2d.js.map +1 -1
  71. package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
  72. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  73. package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
  74. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  75. package/dist/data-structures/priority-queue/priority-queue.js +93 -139
  76. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  77. package/dist/data-structures/queue/deque.js +82 -128
  78. package/dist/data-structures/queue/deque.js.map +1 -1
  79. package/dist/data-structures/queue/queue.js +57 -157
  80. package/dist/data-structures/queue/queue.js.map +1 -1
  81. package/dist/data-structures/stack/stack.js +21 -22
  82. package/dist/data-structures/stack/stack.js.map +1 -1
  83. package/dist/data-structures/tree/tree.js +32 -45
  84. package/dist/data-structures/tree/tree.js.map +1 -1
  85. package/dist/data-structures/trie/trie.js +93 -200
  86. package/dist/data-structures/trie/trie.js.map +1 -1
  87. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
  88. package/dist/utils/utils.js +22 -107
  89. package/dist/utils/utils.js.map +1 -1
  90. package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +79 -78
  91. package/lib/data-structures/binary-tree/abstract-binary-tree.js +61 -141
  92. package/lib/data-structures/binary-tree/avl-tree.d.ts +9 -0
  93. package/lib/data-structures/binary-tree/avl-tree.js +22 -0
  94. package/lib/data-structures/binary-tree/bst.d.ts +3 -3
  95. package/lib/data-structures/binary-tree/bst.js +12 -15
  96. package/lib/data-structures/binary-tree/tree-multiset.d.ts +16 -18
  97. package/lib/data-structures/binary-tree/tree-multiset.js +26 -36
  98. package/lib/data-structures/graph/abstract-graph.d.ts +2 -2
  99. package/lib/data-structures/graph/abstract-graph.js +5 -5
  100. package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
  101. package/lib/data-structures/priority-queue/priority-queue.js +3 -3
  102. package/lib/interfaces/abstract-binary-tree.d.ts +23 -28
  103. package/lib/interfaces/avl-tree.d.ts +3 -1
  104. package/lib/interfaces/bst.d.ts +2 -1
  105. package/lib/types/data-structures/abstract-binary-tree.js +0 -1
  106. package/package.json +6 -6
  107. package/src/data-structures/binary-tree/abstract-binary-tree.ts +151 -203
  108. package/src/data-structures/binary-tree/avl-tree.ts +29 -0
  109. package/src/data-structures/binary-tree/bst.ts +12 -15
  110. package/src/data-structures/binary-tree/tree-multiset.ts +27 -39
  111. package/src/data-structures/graph/abstract-graph.ts +5 -5
  112. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  113. package/src/interfaces/abstract-binary-tree.ts +23 -36
  114. package/src/interfaces/avl-tree.ts +3 -1
  115. package/src/interfaces/bst.ts +1 -1
  116. package/src/types/data-structures/abstract-binary-tree.ts +0 -1
  117. package/test/integration/avl-tree.test.ts +24 -24
  118. package/test/integration/bst.test.ts +71 -71
  119. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
  120. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -9
  121. package/test/unit/data-structures/binary-tree/bst.test.ts +8 -8
  122. package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
  123. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +9 -9
  124. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
  125. package/umd/bundle.min.js +1 -1
  126. package/umd/bundle.min.js.map +1 -1
@@ -13,8 +13,11 @@ export class AVLTreeNode<V = any, NEIGHBOR extends AVLTreeNode<V, NEIGHBOR> = AV
13
13
  extends BSTNode<V, NEIGHBOR>
14
14
  implements IAVLTreeNode<V, NEIGHBOR>
15
15
  {
16
+ height: number;
17
+
16
18
  constructor(key: BinaryTreeNodeKey, val?: V) {
17
19
  super(key, val);
20
+ this.height = 0;
18
21
  }
19
22
  }
20
23
 
@@ -29,6 +32,32 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
29
32
  super(options);
30
33
  }
31
34
 
35
+ /**
36
+ * The `swapLocation` function swaps the location of two nodes in a binary tree.
37
+ * @param {N} srcNode - The source node that you want to swap with the destination node.
38
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
39
+ * be swapped to.
40
+ * @returns The `destNode` is being returned.
41
+ */
42
+ override swapLocation(srcNode: N, destNode: N): N {
43
+ const {key, val, height} = destNode;
44
+ const tempNode = this.createNode(key, val);
45
+
46
+ if (tempNode) {
47
+ tempNode.height = height;
48
+
49
+ destNode.key = srcNode.key;
50
+ destNode.val = srcNode.val;
51
+ destNode.height = srcNode.height;
52
+
53
+ srcNode.key = tempNode.key;
54
+ srcNode.val = tempNode.val;
55
+ srcNode.height = tempNode.height;
56
+ }
57
+
58
+ return destNode;
59
+ }
60
+
32
61
  /**
33
62
  * The function creates a new AVL tree node with the given key and value.
34
63
  * @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
@@ -130,7 +130,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
130
130
  /**
131
131
  * The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
132
132
  * manner.
133
- * @param {[BinaryTreeNodeKey | N , N['val']][]} idsOrNodes - The `idsOrNodes` parameter in the `addMany` function is an array of
133
+ * @param {[BinaryTreeNodeKey | N , N['val']][]} keysOrNodes - The `keysOrNodes` parameter in the `addMany` function is an array of
134
134
  * `BinaryTreeNodeKey` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
135
135
  * to the binary search tree.
136
136
  * @param {N['val'][]} data - The values of tree nodes
@@ -138,18 +138,18 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
138
138
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
139
139
  */
140
140
  override addMany(
141
- idsOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
141
+ keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
142
142
  data?: N['val'][],
143
143
  isBalanceAdd = false
144
144
  ): (N | null | undefined)[] {
145
145
  function hasNoNull(arr: (BinaryTreeNodeKey | null)[] | (N | null)[]): arr is BinaryTreeNodeKey[] | N[] {
146
146
  return arr.indexOf(null) === -1;
147
147
  }
148
- if (!isBalanceAdd || !hasNoNull(idsOrNodes)) {
149
- return super.addMany(idsOrNodes, data);
148
+ if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
149
+ return super.addMany(keysOrNodes, data);
150
150
  }
151
151
  const inserted: (N | null | undefined)[] = [];
152
- const combinedArr: [BinaryTreeNodeKey | N, N['val']][] = idsOrNodes.map((value, index) => [value, data?.[index]]);
152
+ const combinedArr: [BinaryTreeNodeKey | N, N['val']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
153
153
  let sorted = [];
154
154
  function isNodeOrNullTuple(arr: [BinaryTreeNodeKey | N, N['val']][]): arr is [N, N['val']][] {
155
155
  for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
@@ -169,7 +169,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
169
169
  } else if (isBinaryTreeKeyOrNullTuple(combinedArr)) {
170
170
  sorted = combinedArr.sort((a, b) => a[0] - b[0]);
171
171
  } else {
172
- throw new Error('Invalid input idsOrNodes');
172
+ throw new Error('Invalid input keysOrNodes');
173
173
  }
174
174
  sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
175
175
  sortedData = sorted.map(([, val]) => val);
@@ -216,8 +216,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
216
216
  * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
217
217
  * @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
218
218
  */
219
- override get(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName): N | null {
220
- propertyName = propertyName ?? 'key';
219
+ override get(nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key'): N | null {
221
220
  return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
222
221
  }
223
222
 
@@ -248,7 +247,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
248
247
  override getNodes(
249
248
  nodeProperty: BinaryTreeNodeKey | N,
250
249
  propertyName: BinaryTreeNodePropertyName = 'key',
251
- onlyOne?: boolean
250
+ onlyOne = false
252
251
  ): N[] {
253
252
  if (!this.root) return [];
254
253
  const result: N[] = [];
@@ -298,8 +297,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
298
297
  * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
299
298
  * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
300
299
  */
301
- lesserSum(beginNode: N | BinaryTreeNodeKey | null, propertyName?: BinaryTreeNodePropertyName): number {
302
- propertyName = propertyName ?? 'key';
300
+ lesserSum(beginNode: N | BinaryTreeNodeKey | null, propertyName: BinaryTreeNodePropertyName = 'key'): number {
303
301
  if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'key');
304
302
  if (!beginNode) return 0;
305
303
  if (!this.root) return 0;
@@ -378,9 +376,8 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
378
376
  allGreaterNodesAdd(
379
377
  node: N | BinaryTreeNodeKey | null,
380
378
  delta: number,
381
- propertyName?: BinaryTreeNodePropertyName
379
+ propertyName: BinaryTreeNodePropertyName = 'key'
382
380
  ): boolean {
383
- propertyName = propertyName ?? 'key';
384
381
  if (typeof node === 'number') node = this.get(node, 'key');
385
382
  if (!node) return false;
386
383
  const key = node.key;
@@ -440,7 +437,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
440
437
  * @returns The function `perfectlyBalance()` returns a boolean value.
441
438
  */
442
439
  perfectlyBalance(): boolean {
443
- const sorted = this.DFS('in', 'node'),
440
+ const sorted = this.dfs('in', 'node'),
444
441
  n = sorted.length;
445
442
  this.clear();
446
443
 
@@ -529,7 +526,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
529
526
  /**
530
527
  * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
531
528
  * greater than, less than, or equal to the second ID.
532
- * @param {BinaryTreeNodeKey} a - a is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
529
+ * @param {BinaryTreeNodeKey} a - "a" is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
533
530
  * @param {BinaryTreeNodeKey} b - The parameter "b" in the above code refers to a BinaryTreeNodeKey.
534
531
  * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
535
532
  * than), or CP.eq (equal).
@@ -15,29 +15,21 @@ export class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIG
15
15
  implements ITreeMultisetNode<V, NEIGHBOR>
16
16
  {
17
17
  /**
18
- * The constructor function initializes a BinaryTreeNode object with an key, value, and count.
18
+ * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
19
19
  * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
20
20
  * of the binary tree node.
21
21
  * @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
22
22
  * tree node. If no value is provided, it will be `undefined`.
23
23
  * @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
24
24
  * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
25
- * parameter when creating a new instance of the `BinaryTreeNode` class,
25
+ * parameter when creating a new instance of the `BinaryTreeNode` class.
26
26
  */
27
27
  constructor(key: BinaryTreeNodeKey, val?: V, count = 1) {
28
28
  super(key, val);
29
- this._count = count;
29
+ this.count = count;
30
30
  }
31
31
 
32
- private _count: number;
33
-
34
- get count(): number {
35
- return this._count;
36
- }
37
-
38
- set count(v: number) {
39
- this._count = v;
40
- }
32
+ count: number;
41
33
  }
42
34
 
43
35
  /**
@@ -54,7 +46,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
54
46
  * TreeMultiset.
55
47
  */
56
48
  constructor(options?: TreeMultisetOptions) {
57
- super({...options});
49
+ super(options);
58
50
  }
59
51
 
60
52
  private _count = 0;
@@ -113,8 +105,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
113
105
  * value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
114
106
  * @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
115
107
  */
116
- override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined {
117
- count = count ?? 1;
108
+ override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count = 1): N | null | undefined {
118
109
  let inserted: N | null | undefined = undefined,
119
110
  newNode: N | null;
120
111
  if (keyOrNode instanceof TreeMultisetNode) {
@@ -219,7 +210,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
219
210
  /**
220
211
  * The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
221
212
  * the inserted nodes.
222
- * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
213
+ * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
223
214
  * objects, or null values.
224
215
  * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
225
216
  * the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
@@ -227,13 +218,13 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
227
218
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
228
219
  */
229
220
  override addMany(
230
- idsOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
221
+ keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
231
222
  data?: N['val'][]
232
223
  ): (N | null | undefined)[] {
233
224
  const inserted: (N | null | undefined)[] = [];
234
225
 
235
- for (let i = 0; i < idsOrNodes.length; i++) {
236
- const keyOrNode = idsOrNodes[i];
226
+ for (let i = 0; i < keysOrNodes.length; i++) {
227
+ const keyOrNode = keysOrNodes[i];
237
228
 
238
229
  if (keyOrNode instanceof TreeMultisetNode) {
239
230
  inserted.push(this.add(keyOrNode.key, keyOrNode.val, keyOrNode.count));
@@ -256,7 +247,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
256
247
  * @returns The function `perfectlyBalance()` returns a boolean value.
257
248
  */
258
249
  override perfectlyBalance(): boolean {
259
- const sorted = this.DFS('in', 'node'),
250
+ const sorted = this.dfs('in', 'node'),
260
251
  n = sorted.length;
261
252
  if (sorted.length < 1) return false;
262
253
 
@@ -302,7 +293,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
302
293
  * not be taken into account when removing it. If `ignoreCount` is set to `false
303
294
  * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
304
295
  */
305
- override remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[] {
296
+ override remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount = false): BinaryTreeDeletedResult<N>[] {
306
297
  const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
307
298
  if (!this.root) return bstDeletedResult;
308
299
 
@@ -483,7 +474,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
483
474
  * to `true`, the function will return only one node. If `onlyOne`
484
475
  * @returns an array of nodes that match the given nodeProperty.
485
476
  */
486
- getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne?: boolean): N[] {
477
+ getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne = false): N[] {
487
478
  if (!this.root) return [];
488
479
  const result: N[] = [];
489
480
 
@@ -522,10 +513,10 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
522
513
  /**
523
514
  * The BFSCount function returns an array of counts from a breadth-first search of nodes.
524
515
  * @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
525
- * BFS traversal.
516
+ * bfs traversal.
526
517
  */
527
518
  BFSCount(): number[] {
528
- const nodes = super.BFS('node');
519
+ const nodes = super.bfs('node');
529
520
  return nodes.map(node => node.count);
530
521
  }
531
522
 
@@ -549,36 +540,33 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
549
540
  * traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
550
541
  * @returns The function `morrisCount` returns an array of numbers.
551
542
  */
552
- morrisCount(pattern?: 'in' | 'pre' | 'post'): number[] {
553
- pattern = pattern || 'in';
543
+ morrisCount(pattern: DFSOrderPattern = 'in'): number[] {
554
544
  const nodes = super.morris(pattern, 'node');
555
545
  return nodes.map(node => node.count);
556
546
  }
557
547
 
558
548
  /**
559
- * The function DFSIterativeCount performs an iterative depth-first search and returns an array of node counts based on
549
+ * The function dfsCountIterative performs an iterative depth-first search and returns an array of node counts based on
560
550
  * the specified traversal pattern.
561
551
  * @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
562
- * the Depth-First Search (DFS) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
563
- * @returns The DFSIterativeCount function returns an array of numbers, which represents the count property of each node
564
- * in the DFS traversal.
552
+ * the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
553
+ * @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
554
+ * in the dfs traversal.
565
555
  */
566
- DFSIterativeCount(pattern?: 'in' | 'pre' | 'post'): number[] {
567
- pattern = pattern ?? 'in';
568
- const nodes = super.DFSIterative(pattern, 'node');
556
+ dfsCountIterative(pattern: DFSOrderPattern = 'in'): number[] {
557
+ const nodes = super.dfsIterative(pattern, 'node');
569
558
  return nodes.map(node => node.count);
570
559
  }
571
560
 
572
561
  /**
573
- * The DFSCount function returns an array of counts for each node in a depth-first search traversal.
562
+ * The dfsCount function returns an array of counts for each node in a depth-first search traversal.
574
563
  * @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
575
- * the Depth-First Search (DFS) algorithm should traverse the nodes. It can have one of the following values:
576
- * @returns The DFSCount function returns an array of numbers, specifically the count property of each node in the DFS
564
+ * the Depth-First Search (dfs) algorithm should traverse the nodes. It can have one of the following values:
565
+ * @returns The dfsCount function returns an array of numbers, specifically the count property of each node in the dfs
577
566
  * traversal.
578
567
  */
579
- DFSCount(pattern?: DFSOrderPattern): number[] {
580
- pattern = pattern ?? 'in';
581
- const nodes = super.DFS(pattern, 'node');
568
+ dfsCount(pattern: DFSOrderPattern = 'in'): number[] {
569
+ const nodes = super.dfs(pattern, 'node');
582
570
  return nodes.map(node => node.count);
583
571
  }
584
572
 
@@ -889,7 +889,7 @@ export abstract class AbstractGraph<
889
889
  }
890
890
 
891
891
  /**
892
- * Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
892
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
893
893
  * Tarjan can find cycles in directed or undirected graph
894
894
  * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
895
895
  * Tarjan solve the bi-connected components of undirected graphs;
@@ -897,7 +897,7 @@ export abstract class AbstractGraph<
897
897
  * /
898
898
 
899
899
  /**
900
- * Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
900
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
901
901
  * Tarjan can find cycles in directed or undirected graph
902
902
  * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
903
903
  * Tarjan solve the bi-connected components of undirected graphs;
@@ -918,8 +918,8 @@ export abstract class AbstractGraph<
918
918
  * @returns The function `tarjan` returns an object with the following properties:
919
919
  */
920
920
  tarjan(needArticulationPoints?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean) {
921
- // !! in undirected graph we will not let child visit parent when DFS
922
- // !! articulation point(in DFS search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
921
+ // !! in undirected graph we will not let child visit parent when dfs
922
+ // !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
923
923
  // !! bridge: low(child) > dfn(cur)
924
924
 
925
925
  const defaultConfig = false;
@@ -947,7 +947,7 @@ export abstract class AbstractGraph<
947
947
  lowMap.set(cur, dfn);
948
948
 
949
949
  const neighbors = this.getNeighbors(cur);
950
- let childCount = 0; // child in DFS tree not child in graph
950
+ let childCount = 0; // child in dfs tree not child in graph
951
951
  for (const neighbor of neighbors) {
952
952
  if (neighbor !== parent) {
953
953
  if (dfnMap.get(neighbor) === -1) {
@@ -195,13 +195,13 @@ export class PriorityQueue<E = any> {
195
195
  }
196
196
 
197
197
  /**
198
- * The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
198
+ * The dfs function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
199
199
  * based on the specified traversal order.
200
200
  * @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which
201
- * the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
201
+ * the nodes should be visited during the Depth-First Search (dfs) traversal. It can have one of the following values:
202
202
  * @returns an array of type `(E | null)[]`.
203
203
  */
204
- DFS(dfsMode: PriorityQueueDFSOrderPattern): (E | null)[] {
204
+ dfs(dfsMode: PriorityQueueDFSOrderPattern): (E | null)[] {
205
205
  const visitedNode: (E | null)[] = [];
206
206
 
207
207
  const traverse = (cur: number) => {
@@ -12,13 +12,9 @@ import {
12
12
  import {AbstractBinaryTreeNode} from '../data-structures';
13
13
 
14
14
  export interface IAbstractBinaryTreeNode<T, NEIGHBOR extends IAbstractBinaryTreeNode<T, NEIGHBOR>> {
15
- get key(): BinaryTreeNodeKey;
15
+ key: BinaryTreeNodeKey;
16
16
 
17
- set key(v: BinaryTreeNodeKey);
18
-
19
- get val(): T | undefined;
20
-
21
- set val(v: T | undefined);
17
+ val: T | undefined;
22
18
 
23
19
  get left(): NEIGHBOR | null | undefined;
24
20
 
@@ -28,15 +24,9 @@ export interface IAbstractBinaryTreeNode<T, NEIGHBOR extends IAbstractBinaryTree
28
24
 
29
25
  set right(v: NEIGHBOR | null | undefined);
30
26
 
31
- get parent(): NEIGHBOR | null | undefined;
32
-
33
- set parent(v: NEIGHBOR | null | undefined);
27
+ parent: NEIGHBOR | null | undefined;
34
28
 
35
29
  get familyPosition(): FamilyPosition;
36
-
37
- get height(): number;
38
-
39
- set height(v: number);
40
30
  }
41
31
 
42
32
  export interface IAbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'], N>> {
@@ -62,9 +52,9 @@ export interface IAbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'],
62
52
 
63
53
  add(key: BinaryTreeNodeKey | N, val?: N['val']): N | null | undefined;
64
54
 
65
- addMany(idsOrNodes: (BinaryTreeNodeKey | N | null)[], data?: N['val'][]): (N | null | undefined)[];
55
+ addMany(keysOrNodes: (BinaryTreeNodeKey | N | null)[], data?: N['val'][]): (N | null | undefined)[];
66
56
 
67
- fill(idsOrNodes: (BinaryTreeNodeKey | N | null)[], data?: N[] | Array<N['val']>): boolean;
57
+ refill(keysOrNodes: (BinaryTreeNodeKey | N | null)[], data?: N[] | Array<N['val']>): boolean;
68
58
 
69
59
  remove(key: BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
70
60
 
@@ -108,44 +98,41 @@ export interface IAbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'],
108
98
 
109
99
  subTreeAdd(subTreeRoot: N, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
110
100
 
111
- BFS(): BinaryTreeNodeKey[];
101
+ bfs(): BinaryTreeNodeKey[];
112
102
 
113
- BFS(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
103
+ bfs(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
114
104
 
115
- BFS(nodeOrPropertyName: 'val'): N['val'][];
105
+ bfs(nodeOrPropertyName: 'val'): N['val'][];
116
106
 
117
- BFS(nodeOrPropertyName: 'node'): N[];
107
+ bfs(nodeOrPropertyName: 'node'): N[];
118
108
 
119
- BFS(nodeOrPropertyName: 'count'): number[];
109
+ bfs(nodeOrPropertyName: 'count'): number[];
120
110
 
121
- BFS(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N>;
111
+ bfs(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N>;
122
112
 
123
- DFS(): BinaryTreeNodeKey[];
113
+ dfs(): BinaryTreeNodeKey[];
124
114
 
125
- DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[];
115
+ dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[];
126
116
 
127
- DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
117
+ dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
128
118
 
129
- DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
119
+ dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
130
120
 
131
- DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
121
+ dfs(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
132
122
 
133
- DFS(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N>;
123
+ dfs(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N>;
134
124
 
135
- DFSIterative(): BinaryTreeNodeKey[];
125
+ dfsIterative(): BinaryTreeNodeKey[];
136
126
 
137
- DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'key'): BinaryTreeNodeKey[];
127
+ dfsIterative(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
138
128
 
139
- DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
129
+ dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
140
130
 
141
- DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
131
+ dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N['val'][];
142
132
 
143
- DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
133
+ dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
144
134
 
145
- DFSIterative(
146
- pattern?: 'in' | 'pre' | 'post',
147
- nodeOrPropertyName?: NodeOrPropertyName
148
- ): AbstractBinaryTreeNodeProperties<N>;
135
+ dfsIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N>;
149
136
 
150
137
  levelIterative(node: N | null): BinaryTreeNodeKey[];
151
138
 
@@ -2,7 +2,9 @@ import {AVLTreeNode} from '../data-structures';
2
2
  import {IBST, IBSTNode} from './bst';
3
3
  import {BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../types';
4
4
 
5
- export type IAVLTreeNode<T, NEIGHBOR extends IAVLTreeNode<T, NEIGHBOR>> = IBSTNode<T, NEIGHBOR>;
5
+ export interface IAVLTreeNode<T, NEIGHBOR extends IAVLTreeNode<T, NEIGHBOR>> extends IBSTNode<T, NEIGHBOR> {
6
+ height: number;
7
+ }
6
8
 
7
9
  export interface IAVLTree<N extends AVLTreeNode<N['val'], N>> extends IBST<N> {
8
10
  add(key: BinaryTreeNodeKey, val?: N['val'] | null): N | null | undefined;
@@ -2,7 +2,7 @@ import {BSTNode} from '../data-structures';
2
2
  import {IBinaryTree, IBinaryTreeNode} from './binary-tree';
3
3
  import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodePropertyName} from '../types';
4
4
 
5
- export type IBSTNode<T, NEIGHBOR extends IBSTNode<T, NEIGHBOR>> = IBinaryTreeNode<T, NEIGHBOR>;
5
+ export interface IBSTNode<T, NEIGHBOR extends IBSTNode<T, NEIGHBOR>> extends IBinaryTreeNode<T, NEIGHBOR> {}
6
6
 
7
7
  export interface IBST<N extends BSTNode<N['val'], N>> extends IBinaryTree<N> {
8
8
  createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
@@ -12,7 +12,6 @@ export enum LoopType {
12
12
  RECURSIVE = 'RECURSIVE'
13
13
  }
14
14
 
15
- /* This enumeration defines the position of a node within a family tree composed of three associated nodes, where 'root' represents the root node of the family tree, 'left' represents the left child node, and 'right' represents the right child node. */
16
15
  export enum FamilyPosition {
17
16
  ROOT = 'ROOT',
18
17
  LEFT = 'LEFT',
@@ -12,15 +12,15 @@ describe('AVL Tree Test', () => {
12
12
  expect(node6 && tree.getHeight(node6)).toBe(3);
13
13
  expect(node6 && tree.getDepth(node6)).toBe(1);
14
14
 
15
- const getNodeById = tree.get(10, 'id');
16
- expect(getNodeById?.id).toBe(10);
15
+ const getNodeById = tree.get(10, 'key');
16
+ expect(getNodeById?.key).toBe(10);
17
17
 
18
18
  const getMinNodeByRoot = tree.getLeftMost();
19
- expect(getMinNodeByRoot?.id).toBe(1);
19
+ expect(getMinNodeByRoot?.key).toBe(1);
20
20
 
21
21
  const node15 = tree.get(15);
22
22
  const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15);
23
- expect(getMinNodeBySpecificNode?.id).toBe(12);
23
+ expect(getMinNodeBySpecificNode?.key).toBe(12);
24
24
 
25
25
  const subTreeSum = node15 && tree.subTreeSum(node15);
26
26
  expect(subTreeSum).toBe(70);
@@ -32,65 +32,65 @@ describe('AVL Tree Test', () => {
32
32
  expect(node15?.val).toBe(15);
33
33
 
34
34
  const dfs = tree.DFS('in', 'node');
35
- expect(dfs[0].id).toBe(1);
36
- expect(dfs[dfs.length - 1].id).toBe(16);
35
+ expect(dfs[0].key).toBe(1);
36
+ expect(dfs[dfs.length - 1].key).toBe(16);
37
37
 
38
38
  tree.perfectlyBalance();
39
39
  const bfs = tree.BFS('node');
40
40
  expect(tree.isPerfectlyBalanced()).toBe(true);
41
- expect(bfs[0].id).toBe(8);
42
- expect(bfs[bfs.length - 1].id).toBe(16);
41
+ expect(bfs[0].key).toBe(8);
42
+ expect(bfs[bfs.length - 1].key).toBe(16);
43
43
 
44
- expect(tree.remove(11)[0].deleted?.id).toBe(11);
44
+ expect(tree.remove(11)[0].deleted?.key).toBe(11);
45
45
  expect(tree.isAVLBalanced()).toBe(true);
46
46
  expect(node15 && tree.getHeight(node15)).toBe(2);
47
47
 
48
- expect(tree.remove(1)[0].deleted?.id).toBe(1);
48
+ expect(tree.remove(1)[0].deleted?.key).toBe(1);
49
49
  expect(tree.isAVLBalanced()).toBe(true);
50
50
  expect(tree.getHeight()).toBe(4);
51
51
 
52
- expect(tree.remove(4)[0].deleted?.id).toBe(4);
52
+ expect(tree.remove(4)[0].deleted?.key).toBe(4);
53
53
  expect(tree.isAVLBalanced()).toBe(true);
54
54
  expect(tree.getHeight()).toBe(4);
55
55
 
56
- expect(tree.remove(10)[0].deleted?.id).toBe(10);
56
+ expect(tree.remove(10)[0].deleted?.key).toBe(10);
57
57
  expect(tree.isAVLBalanced()).toBe(true);
58
58
  expect(tree.getHeight()).toBe(3);
59
59
 
60
- expect(tree.remove(15)[0].deleted?.id).toBe(15);
60
+ expect(tree.remove(15)[0].deleted?.key).toBe(15);
61
61
  expect(tree.isAVLBalanced()).toBe(true);
62
62
 
63
63
  expect(tree.getHeight()).toBe(3);
64
64
 
65
- expect(tree.remove(5)[0].deleted?.id).toBe(5);
65
+ expect(tree.remove(5)[0].deleted?.key).toBe(5);
66
66
  expect(tree.isAVLBalanced()).toBe(true);
67
67
  expect(tree.getHeight()).toBe(3);
68
68
 
69
- expect(tree.remove(13)[0].deleted?.id).toBe(13);
69
+ expect(tree.remove(13)[0].deleted?.key).toBe(13);
70
70
  expect(tree.isAVLBalanced()).toBe(true);
71
71
  expect(tree.getHeight()).toBe(3);
72
72
 
73
- expect(tree.remove(3)[0].deleted?.id).toBe(3);
73
+ expect(tree.remove(3)[0].deleted?.key).toBe(3);
74
74
  expect(tree.isAVLBalanced()).toBe(true);
75
75
  expect(tree.getHeight()).toBe(3);
76
76
 
77
- expect(tree.remove(8)[0].deleted?.id).toBe(8);
77
+ expect(tree.remove(8)[0].deleted?.key).toBe(8);
78
78
  expect(tree.isAVLBalanced()).toBe(true);
79
79
  expect(tree.getHeight()).toBe(3);
80
80
 
81
- expect(tree.remove(6)[0].deleted?.id).toBe(6);
81
+ expect(tree.remove(6)[0].deleted?.key).toBe(6);
82
82
  expect(tree.remove(6).length).toBe(0);
83
83
  expect(tree.isAVLBalanced()).toBe(true);
84
84
  expect(tree.getHeight()).toBe(2);
85
85
 
86
- expect(tree.remove(7)[0].deleted?.id).toBe(7);
86
+ expect(tree.remove(7)[0].deleted?.key).toBe(7);
87
87
  expect(tree.isAVLBalanced()).toBe(true);
88
88
  expect(tree.getHeight()).toBe(2);
89
89
 
90
- expect(tree.remove(9)[0].deleted?.id).toBe(9);
90
+ expect(tree.remove(9)[0].deleted?.key).toBe(9);
91
91
  expect(tree.isAVLBalanced()).toBe(true);
92
92
  expect(tree.getHeight()).toBe(2);
93
- expect(tree.remove(14)[0].deleted?.id).toBe(14);
93
+ expect(tree.remove(14)[0].deleted?.key).toBe(14);
94
94
  expect(tree.isAVLBalanced()).toBe(true);
95
95
  expect(tree.getHeight()).toBe(1);
96
96
 
@@ -101,8 +101,8 @@ describe('AVL Tree Test', () => {
101
101
  expect(lastBFSIds[2]).toBe(16);
102
102
 
103
103
  const lastBFSNodes = tree.BFS('node');
104
- expect(lastBFSNodes[0].id).toBe(12);
105
- expect(lastBFSNodes[1].id).toBe(2);
106
- expect(lastBFSNodes[2].id).toBe(16);
104
+ expect(lastBFSNodes[0].key).toBe(12);
105
+ expect(lastBFSNodes[1].key).toBe(2);
106
+ expect(lastBFSNodes[2].key).toBe(16);
107
107
  });
108
108
  });