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
@@ -9,28 +9,15 @@ import { trampoline } from '../../utils';
9
9
  import { FamilyPosition, LoopType } from '../../types';
10
10
  export class AbstractBinaryTreeNode {
11
11
  /**
12
- * The constructor function initializes a BinaryTreeNode object with an key and an optional value.
12
+ * The constructor function initializes a BinaryTreeNode object with a key and an optional value.
13
13
  * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
14
14
  * of the binary tree node. It is used to distinguish one node from another in the binary tree.
15
15
  * @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
16
16
  * stored in the binary tree node. If no value is provided, it will be set to undefined.
17
17
  */
18
18
  constructor(key, val) {
19
- this._height = 0;
20
- this._key = key;
21
- this._val = val;
22
- }
23
- get key() {
24
- return this._key;
25
- }
26
- set key(v) {
27
- this._key = v;
28
- }
29
- get val() {
30
- return this._val;
31
- }
32
- set val(value) {
33
- this._val = value;
19
+ this.key = key;
20
+ this.val = val;
34
21
  }
35
22
  get left() {
36
23
  return this._left;
@@ -50,18 +37,6 @@ export class AbstractBinaryTreeNode {
50
37
  }
51
38
  this._right = v;
52
39
  }
53
- get parent() {
54
- return this._parent;
55
- }
56
- set parent(v) {
57
- this._parent = v;
58
- }
59
- get height() {
60
- return this._height;
61
- }
62
- set height(v) {
63
- this._height = v;
64
- }
65
40
  /**
66
41
  * The function determines the position of a node in a family tree structure.
67
42
  * @returns a value of type `FamilyPosition`.
@@ -110,9 +85,9 @@ export class AbstractBinaryTree {
110
85
  this._root = null;
111
86
  this._size = 0;
112
87
  this._loopType = LoopType.ITERATIVE;
113
- this._visitedKey = [];
114
- this._visitedVal = [];
115
- this._visitedNode = [];
88
+ this.visitedKey = [];
89
+ this.visitedVal = [];
90
+ this.visitedNode = [];
116
91
  if (options !== undefined) {
117
92
  const { loopType = LoopType.ITERATIVE } = options;
118
93
  this._loopType = loopType;
@@ -128,15 +103,6 @@ export class AbstractBinaryTree {
128
103
  get loopType() {
129
104
  return this._loopType;
130
105
  }
131
- get visitedKey() {
132
- return this._visitedKey;
133
- }
134
- get visitedVal() {
135
- return this._visitedVal;
136
- }
137
- get visitedNode() {
138
- return this._visitedNode;
139
- }
140
106
  /**
141
107
  * The `swapLocation` function swaps the location of two nodes in a binary tree.
142
108
  * @param {N} srcNode - The source node that you want to swap with the destination node.
@@ -145,16 +111,13 @@ export class AbstractBinaryTree {
145
111
  * @returns The `destNode` is being returned.
146
112
  */
147
113
  swapLocation(srcNode, destNode) {
148
- const { key, val, height } = destNode;
114
+ const { key, val } = destNode;
149
115
  const tempNode = this.createNode(key, val);
150
116
  if (tempNode) {
151
- tempNode.height = height;
152
117
  destNode.key = srcNode.key;
153
118
  destNode.val = srcNode.val;
154
- destNode.height = srcNode.height;
155
119
  srcNode.key = tempNode.key;
156
120
  srcNode.val = tempNode.val;
157
- srcNode.height = tempNode.height;
158
121
  }
159
122
  return destNode;
160
123
  }
@@ -245,18 +208,18 @@ export class AbstractBinaryTree {
245
208
  /**
246
209
  * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
247
210
  * values, and adds them to the binary tree.
248
- * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
211
+ * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
249
212
  * objects, or null values.
250
213
  * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
251
214
  * the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
252
215
  * the value of the nodes will be `undefined`.
253
216
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
254
217
  */
255
- addMany(idsOrNodes, data) {
218
+ addMany(keysOrNodes, data) {
256
219
  // TODO not sure addMany not be run multi times
257
220
  const inserted = [];
258
- for (let i = 0; i < idsOrNodes.length; i++) {
259
- const keyOrNode = idsOrNodes[i];
221
+ for (let i = 0; i < keysOrNodes.length; i++) {
222
+ const keyOrNode = keysOrNodes[i];
260
223
  if (keyOrNode instanceof AbstractBinaryTreeNode) {
261
224
  inserted.push(this.add(keyOrNode.key, keyOrNode.val));
262
225
  continue;
@@ -271,17 +234,17 @@ export class AbstractBinaryTree {
271
234
  return inserted;
272
235
  }
273
236
  /**
274
- * The `fill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
275
- * @param {(BinaryTreeNodeKey | N)[]} idsOrNodes - The `idsOrNodes` parameter is an array that can contain either
237
+ * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
238
+ * @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
276
239
  * `BinaryTreeNodeKey` or `N` values.
277
240
  * @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
278
- * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `idsOrNodes`
241
+ * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
279
242
  * array. Each value in the `data` array will be assigned to the
280
243
  * @returns The method is returning a boolean value.
281
244
  */
282
- fill(idsOrNodes, data) {
245
+ refill(keysOrNodes, data) {
283
246
  this.clear();
284
- return idsOrNodes.length === this.addMany(idsOrNodes, data).length;
247
+ return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
285
248
  }
286
249
  /**
287
250
  * The `remove` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
@@ -338,7 +301,7 @@ export class AbstractBinaryTree {
338
301
  * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be one of the following:
339
302
  * @returns the depth of the given node or binary tree.
340
303
  */
341
- getDepth(beginRoot) {
304
+ getDepth(beginRoot = this.root) {
342
305
  if (typeof beginRoot === 'number')
343
306
  beginRoot = this.get(beginRoot, 'key');
344
307
  let depth = 0;
@@ -355,8 +318,7 @@ export class AbstractBinaryTree {
355
318
  * node), or `null`.
356
319
  * @returns the height of the binary tree.
357
320
  */
358
- getHeight(beginRoot) {
359
- beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
321
+ getHeight(beginRoot = this.root) {
360
322
  if (typeof beginRoot === 'number')
361
323
  beginRoot = this.get(beginRoot, 'key');
362
324
  if (!beginRoot)
@@ -398,9 +360,8 @@ export class AbstractBinaryTree {
398
360
  * for `beginRoot`, the `this.root` property is used as the default value.
399
361
  * @returns The function `getMinHeight` returns the minimum height of the binary tree.
400
362
  */
401
- getMinHeight(beginRoot) {
363
+ getMinHeight(beginRoot = this.root) {
402
364
  var _a, _b, _c;
403
- beginRoot = beginRoot || this.root;
404
365
  if (!beginRoot)
405
366
  return -1;
406
367
  if (this._loopType === LoopType.RECURSIVE) {
@@ -450,7 +411,7 @@ export class AbstractBinaryTree {
450
411
  * tree or null if the tree is empty.
451
412
  * @returns The method is returning a boolean value.
452
413
  */
453
- isPerfectlyBalanced(beginRoot) {
414
+ isPerfectlyBalanced(beginRoot = this.root) {
454
415
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
455
416
  }
456
417
  /**
@@ -464,10 +425,9 @@ export class AbstractBinaryTree {
464
425
  * function will stop traversing the tree and return the first matching node. If `only
465
426
  * @returns an array of nodes (type N).
466
427
  */
467
- getNodes(nodeProperty, propertyName, onlyOne) {
428
+ getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
468
429
  if (!this.root)
469
430
  return [];
470
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
471
431
  const result = [];
472
432
  if (this.loopType === LoopType.RECURSIVE) {
473
433
  const _traverse = (cur) => {
@@ -502,8 +462,7 @@ export class AbstractBinaryTree {
502
462
  * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
503
463
  * @returns a boolean value.
504
464
  */
505
- has(nodeProperty, propertyName) {
506
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
465
+ has(nodeProperty, propertyName = 'key') {
507
466
  // TODO may support finding node by value equal
508
467
  return this.getNodes(nodeProperty, propertyName).length > 0;
509
468
  }
@@ -518,9 +477,8 @@ export class AbstractBinaryTree {
518
477
  * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
519
478
  * If no matching node is found, it returns null.
520
479
  */
521
- get(nodeProperty, propertyName) {
480
+ get(nodeProperty, propertyName = 'key') {
522
481
  var _a;
523
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
524
482
  // TODO may support finding node by value equal
525
483
  return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
526
484
  }
@@ -557,10 +515,9 @@ export class AbstractBinaryTree {
557
515
  * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
558
516
  * node is found (
559
517
  */
560
- getLeftMost(beginRoot) {
518
+ getLeftMost(beginRoot = this.root) {
561
519
  if (typeof beginRoot === 'number')
562
520
  beginRoot = this.get(beginRoot, 'key');
563
- beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
564
521
  if (!beginRoot)
565
522
  return beginRoot;
566
523
  if (this._loopType === LoopType.RECURSIVE) {
@@ -584,25 +541,24 @@ export class AbstractBinaryTree {
584
541
  /**
585
542
  * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
586
543
  * recursion optimization.
587
- * @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
544
+ * @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
588
545
  * starting node from which we want to find the rightmost node. If no node is provided, the function will default to
589
546
  * using the root node of the data structure.
590
547
  * @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
591
548
  * it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
592
549
  * rightmost node starting from the root of the binary tree.
593
550
  */
594
- getRightMost(node) {
551
+ getRightMost(beginRoot = this.root) {
595
552
  // TODO support get right most by passing key in
596
- node = node !== null && node !== void 0 ? node : this.root;
597
- if (!node)
598
- return node;
553
+ if (!beginRoot)
554
+ return beginRoot;
599
555
  if (this._loopType === LoopType.RECURSIVE) {
600
556
  const _traverse = (cur) => {
601
557
  if (!cur.right)
602
558
  return cur;
603
559
  return _traverse(cur.right);
604
560
  };
605
- return _traverse(node);
561
+ return _traverse(beginRoot);
606
562
  }
607
563
  else {
608
564
  // Indirect implementation of iteration using tail recursion optimization
@@ -611,7 +567,7 @@ export class AbstractBinaryTree {
611
567
  return cur;
612
568
  return _traverse.cont(cur.right);
613
569
  });
614
- return _traverse(node);
570
+ return _traverse(beginRoot);
615
571
  }
616
572
  }
617
573
  /**
@@ -697,8 +653,7 @@ export class AbstractBinaryTree {
697
653
  * not provided, it defaults to 'key'.
698
654
  * @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
699
655
  */
700
- subTreeSum(subTreeRoot, propertyName) {
701
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
656
+ subTreeSum(subTreeRoot, propertyName = 'key') {
702
657
  if (typeof subTreeRoot === 'number')
703
658
  subTreeRoot = this.get(subTreeRoot, 'key');
704
659
  if (!subTreeRoot)
@@ -748,8 +703,7 @@ export class AbstractBinaryTree {
748
703
  * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
749
704
  * @returns a boolean value.
750
705
  */
751
- subTreeAdd(subTreeRoot, delta, propertyName) {
752
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'key';
706
+ subTreeAdd(subTreeRoot, delta, propertyName = 'key') {
753
707
  if (typeof subTreeRoot === 'number')
754
708
  subTreeRoot = this.get(subTreeRoot, 'key');
755
709
  if (!subTreeRoot)
@@ -784,14 +738,13 @@ export class AbstractBinaryTree {
784
738
  return true;
785
739
  }
786
740
  /**
787
- * The BFS function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
741
+ * The bfs function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
788
742
  * @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
789
- * If a node is provided, the BFS algorithm will be performed starting from that node.
790
- * If a property name is provided, the BFS algorithm will be performed starting from the root node, accumulating the specified property.
743
+ * If a node is provided, the bfs algorithm will be performed starting from that node.
744
+ * If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property.
791
745
  * @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
792
746
  */
793
- BFS(nodeOrPropertyName) {
794
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'key';
747
+ bfs(nodeOrPropertyName = 'key') {
795
748
  this._clearResults();
796
749
  const queue = [this.root];
797
750
  while (queue.length !== 0) {
@@ -807,15 +760,13 @@ export class AbstractBinaryTree {
807
760
  return this._getResultByPropertyName(nodeOrPropertyName);
808
761
  }
809
762
  /**
810
- * The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
763
+ * The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
811
764
  * each node based on the specified pattern and property name.
812
765
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
813
766
  * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'key'`.
814
767
  * @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
815
768
  */
816
- DFS(pattern, nodeOrPropertyName) {
817
- pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
818
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'key';
769
+ dfs(pattern = 'in', nodeOrPropertyName = 'key') {
819
770
  this._clearResults();
820
771
  const _traverse = (node) => {
821
772
  switch (pattern) {
@@ -846,15 +797,13 @@ export class AbstractBinaryTree {
846
797
  return this._getResultByPropertyName(nodeOrPropertyName);
847
798
  }
848
799
  /**
849
- * The DFSIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
800
+ * The dfsIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
850
801
  * specify the traversal pattern and the property name to accumulate results by.
851
802
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
852
803
  * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'key'`.
853
804
  * @returns An object of type AbstractBinaryTreeNodeProperties<N>.
854
805
  */
855
- DFSIterative(pattern, nodeOrPropertyName) {
856
- pattern = pattern || 'in';
857
- nodeOrPropertyName = nodeOrPropertyName || 'key';
806
+ dfsIterative(pattern = 'in', nodeOrPropertyName = 'key') {
858
807
  this._clearResults();
859
808
  if (!this.root)
860
809
  return this._getResultByPropertyName(nodeOrPropertyName);
@@ -906,9 +855,7 @@ export class AbstractBinaryTree {
906
855
  * accumulating results based on the 'key' property.
907
856
  * @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
908
857
  */
909
- levelIterative(node, nodeOrPropertyName) {
910
- nodeOrPropertyName = nodeOrPropertyName || 'key';
911
- node = node || this.root;
858
+ levelIterative(node = this.root, nodeOrPropertyName = 'key') {
912
859
  if (!node)
913
860
  return [];
914
861
  this._clearResults();
@@ -933,9 +880,7 @@ export class AbstractBinaryTree {
933
880
  * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: 'key', 'val', or 'node'. If not provided, it defaults to 'key'.
934
881
  * @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
935
882
  */
936
- listLevels(node, nodeOrPropertyName) {
937
- nodeOrPropertyName = nodeOrPropertyName || 'key';
938
- node = node || this.root;
883
+ listLevels(node = this.root, nodeOrPropertyName = 'key') {
939
884
  if (!node)
940
885
  return [];
941
886
  const levelsNodes = [];
@@ -1008,11 +953,9 @@ export class AbstractBinaryTree {
1008
953
  * @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'key'.
1009
954
  * @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
1010
955
  */
1011
- morris(pattern, nodeOrPropertyName) {
956
+ morris(pattern = 'in', nodeOrPropertyName = 'key') {
1012
957
  if (this.root === null)
1013
958
  return [];
1014
- pattern = pattern || 'in';
1015
- nodeOrPropertyName = nodeOrPropertyName || 'key';
1016
959
  this._clearResults();
1017
960
  let cur = this.root;
1018
961
  const _reverseEdge = (node) => {
@@ -1137,27 +1080,6 @@ export class AbstractBinaryTree {
1137
1080
  _setLoopType(value) {
1138
1081
  this._loopType = value;
1139
1082
  }
1140
- /**
1141
- * The function sets the value of the `_visitedKey` property in a protected manner.
1142
- * @param {BinaryTreeNodeKey[]} value - value is an array of BinaryTreeNodeKey values.
1143
- */
1144
- _setVisitedKey(value) {
1145
- this._visitedKey = value;
1146
- }
1147
- /**
1148
- * The function sets the value of the "_visitedVal" property to the given array.
1149
- * @param value - An array of type N.
1150
- */
1151
- _setVisitedVal(value) {
1152
- this._visitedVal = value;
1153
- }
1154
- /**
1155
- * The function sets the value of the _visitedNode property.
1156
- * @param {N[]} value - N[] is an array of elements of type N.
1157
- */
1158
- _setVisitedNode(value) {
1159
- this._visitedNode = value;
1160
- }
1161
1083
  /**
1162
1084
  * The function sets the root property of an object to a given value, and if the value is not null, it also sets the
1163
1085
  * parent property of the value to undefined.
@@ -1181,9 +1103,9 @@ export class AbstractBinaryTree {
1181
1103
  * properties.
1182
1104
  */
1183
1105
  _clearResults() {
1184
- this._visitedKey = [];
1185
- this._visitedVal = [];
1186
- this._visitedNode = [];
1106
+ this.visitedKey = [];
1107
+ this.visitedVal = [];
1108
+ this.visitedNode = [];
1187
1109
  }
1188
1110
  /**
1189
1111
  * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
@@ -1200,24 +1122,24 @@ export class AbstractBinaryTree {
1200
1122
  * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
1201
1123
  * @returns a boolean value indicating whether only one matching node should be pushed into the result array.
1202
1124
  */
1203
- _pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne) {
1125
+ _pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName = 'key', onlyOne = false) {
1204
1126
  switch (propertyName) {
1205
1127
  case 'key':
1206
1128
  if (cur.key === nodeProperty) {
1207
1129
  result.push(cur);
1208
- return !!onlyOne;
1130
+ return onlyOne;
1209
1131
  }
1210
1132
  break;
1211
1133
  case 'val':
1212
1134
  if (cur.val === nodeProperty) {
1213
1135
  result.push(cur);
1214
- return !!onlyOne;
1136
+ return onlyOne;
1215
1137
  }
1216
1138
  break;
1217
1139
  default:
1218
1140
  if (cur.key === nodeProperty) {
1219
1141
  result.push(cur);
1220
- return !!onlyOne;
1142
+ return onlyOne;
1221
1143
  }
1222
1144
  break;
1223
1145
  }
@@ -1229,25 +1151,24 @@ export class AbstractBinaryTree {
1229
1151
  * can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
1230
1152
  * specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
1231
1153
  */
1232
- _accumulatedByPropertyName(node, nodeOrPropertyName) {
1233
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'key';
1154
+ _accumulatedByPropertyName(node, nodeOrPropertyName = 'key') {
1234
1155
  switch (nodeOrPropertyName) {
1235
1156
  case 'key':
1236
- this._visitedKey.push(node.key);
1157
+ this.visitedKey.push(node.key);
1237
1158
  break;
1238
1159
  case 'val':
1239
- this._visitedVal.push(node.val);
1160
+ this.visitedVal.push(node.val);
1240
1161
  break;
1241
1162
  case 'node':
1242
- this._visitedNode.push(node);
1163
+ this.visitedNode.push(node);
1243
1164
  break;
1244
1165
  default:
1245
- this._visitedKey.push(node.key);
1166
+ this.visitedKey.push(node.key);
1246
1167
  break;
1247
1168
  }
1248
1169
  }
1249
1170
  /**
1250
- * The time complexity of Morris traversal is O(n), it's may slower than others
1171
+ * The time complexity of Morris traversal is O(n), it may slower than others
1251
1172
  * The space complexity Morris traversal is O(1) because no using stack
1252
1173
  */
1253
1174
  /**
@@ -1257,17 +1178,16 @@ export class AbstractBinaryTree {
1257
1178
  * can accept either a `NodeOrPropertyName` type or be undefined.
1258
1179
  * @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
1259
1180
  */
1260
- _getResultByPropertyName(nodeOrPropertyName) {
1261
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'key';
1181
+ _getResultByPropertyName(nodeOrPropertyName = 'key') {
1262
1182
  switch (nodeOrPropertyName) {
1263
1183
  case 'key':
1264
- return this._visitedKey;
1184
+ return this.visitedKey;
1265
1185
  case 'val':
1266
- return this._visitedVal;
1186
+ return this.visitedVal;
1267
1187
  case 'node':
1268
- return this._visitedNode;
1188
+ return this.visitedNode;
1269
1189
  default:
1270
- return this._visitedKey;
1190
+ return this.visitedKey;
1271
1191
  }
1272
1192
  }
1273
1193
  }
@@ -9,6 +9,7 @@ import { BST, BSTNode } from './bst';
9
9
  import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../../types';
10
10
  import { IAVLTree, IAVLTreeNode } from '../../interfaces';
11
11
  export declare class AVLTreeNode<V = any, NEIGHBOR extends AVLTreeNode<V, NEIGHBOR> = AVLTreeNodeNested<V>> extends BSTNode<V, NEIGHBOR> implements IAVLTreeNode<V, NEIGHBOR> {
12
+ height: number;
12
13
  constructor(key: BinaryTreeNodeKey, val?: V);
13
14
  }
14
15
  export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
@@ -19,6 +20,14 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
19
20
  * options.
20
21
  */
21
22
  constructor(options?: AVLTreeOptions);
23
+ /**
24
+ * The `swapLocation` function swaps the location of two nodes in a binary tree.
25
+ * @param {N} srcNode - The source node that you want to swap with the destination node.
26
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
27
+ * be swapped to.
28
+ * @returns The `destNode` is being returned.
29
+ */
30
+ swapLocation(srcNode: N, destNode: N): N;
22
31
  /**
23
32
  * The function creates a new AVL tree node with the given key and value.
24
33
  * @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
@@ -9,6 +9,7 @@ import { BST, BSTNode } from './bst';
9
9
  export class AVLTreeNode extends BSTNode {
10
10
  constructor(key, val) {
11
11
  super(key, val);
12
+ this.height = 0;
12
13
  }
13
14
  }
14
15
  export class AVLTree extends BST {
@@ -21,6 +22,27 @@ export class AVLTree extends BST {
21
22
  constructor(options) {
22
23
  super(options);
23
24
  }
25
+ /**
26
+ * The `swapLocation` function swaps the location of two nodes in a binary tree.
27
+ * @param {N} srcNode - The source node that you want to swap with the destination node.
28
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
29
+ * be swapped to.
30
+ * @returns The `destNode` is being returned.
31
+ */
32
+ swapLocation(srcNode, destNode) {
33
+ const { key, val, height } = destNode;
34
+ const tempNode = this.createNode(key, val);
35
+ if (tempNode) {
36
+ tempNode.height = height;
37
+ destNode.key = srcNode.key;
38
+ destNode.val = srcNode.val;
39
+ destNode.height = srcNode.height;
40
+ srcNode.key = tempNode.key;
41
+ srcNode.val = tempNode.val;
42
+ srcNode.height = tempNode.height;
43
+ }
44
+ return destNode;
45
+ }
24
46
  /**
25
47
  * The function creates a new AVL tree node with the given key and value.
26
48
  * @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
@@ -40,14 +40,14 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
40
40
  /**
41
41
  * The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
42
42
  * manner.
43
- * @param {[BinaryTreeNodeKey | N , N['val']][]} idsOrNodes - The `idsOrNodes` parameter in the `addMany` function is an array of
43
+ * @param {[BinaryTreeNodeKey | N , N['val']][]} keysOrNodes - The `keysOrNodes` parameter in the `addMany` function is an array of
44
44
  * `BinaryTreeNodeKey` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
45
45
  * to the binary search tree.
46
46
  * @param {N['val'][]} data - The values of tree nodes
47
47
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
48
48
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
49
49
  */
50
- addMany(idsOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean): (N | null | undefined)[];
50
+ addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean): (N | null | undefined)[];
51
51
  /**
52
52
  * The function returns the first node in a binary tree that matches the given property name and value.
53
53
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
@@ -124,7 +124,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
124
124
  /**
125
125
  * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
126
126
  * greater than, less than, or equal to the second ID.
127
- * @param {BinaryTreeNodeKey} a - a is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
127
+ * @param {BinaryTreeNodeKey} a - "a" is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
128
128
  * @param {BinaryTreeNodeKey} b - The parameter "b" in the above code refers to a BinaryTreeNodeKey.
129
129
  * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
130
130
  * than), or CP.eq (equal).