data-structure-typed 1.34.7 → 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 (167) 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 +314 -457
  8. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  9. package/dist/data-structures/binary-tree/avl-tree.js +64 -90
  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 +12 -31
  16. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  17. package/dist/data-structures/binary-tree/bst.js +157 -244
  18. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  19. package/dist/data-structures/binary-tree/rb-tree.js +20 -44
  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 +168 -239
  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 +351 -585
  30. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  31. package/dist/data-structures/graph/directed-graph.js +152 -282
  32. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  33. package/dist/data-structures/graph/map-graph.js +47 -88
  34. package/dist/data-structures/graph/map-graph.js.map +1 -1
  35. package/dist/data-structures/graph/undirected-graph.js +91 -180
  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 +33 -46
  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 +124 -123
  91. package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
  92. package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
  93. package/lib/data-structures/binary-tree/avl-tree.js +35 -13
  94. package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
  95. package/lib/data-structures/binary-tree/binary-tree.js +7 -7
  96. package/lib/data-structures/binary-tree/bst.d.ts +34 -34
  97. package/lib/data-structures/binary-tree/bst.js +86 -89
  98. package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
  99. package/lib/data-structures/binary-tree/rb-tree.js +4 -4
  100. package/lib/data-structures/binary-tree/tree-multiset.d.ts +40 -42
  101. package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
  102. package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
  103. package/lib/data-structures/graph/abstract-graph.js +86 -86
  104. package/lib/data-structures/graph/directed-graph.d.ts +51 -51
  105. package/lib/data-structures/graph/directed-graph.js +63 -63
  106. package/lib/data-structures/graph/map-graph.d.ts +13 -13
  107. package/lib/data-structures/graph/map-graph.js +12 -12
  108. package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
  109. package/lib/data-structures/graph/undirected-graph.js +32 -32
  110. package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
  111. package/lib/data-structures/priority-queue/priority-queue.js +3 -3
  112. package/lib/data-structures/tree/tree.d.ts +4 -4
  113. package/lib/data-structures/tree/tree.js +6 -6
  114. package/lib/interfaces/abstract-binary-tree.d.ts +37 -42
  115. package/lib/interfaces/abstract-graph.d.ts +13 -13
  116. package/lib/interfaces/avl-tree.d.ts +6 -4
  117. package/lib/interfaces/bst.d.ts +10 -9
  118. package/lib/interfaces/directed-graph.d.ts +5 -5
  119. package/lib/interfaces/rb-tree.d.ts +2 -2
  120. package/lib/interfaces/undirected-graph.d.ts +2 -2
  121. package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
  122. package/lib/types/data-structures/abstract-binary-tree.js +0 -1
  123. package/lib/types/data-structures/abstract-graph.d.ts +2 -2
  124. package/lib/types/data-structures/bst.d.ts +2 -2
  125. package/lib/types/data-structures/tree-multiset.d.ts +1 -1
  126. package/lib/types/utils/validate-type.d.ts +8 -8
  127. package/package.json +6 -6
  128. package/scripts/rename_clear_files.sh +29 -0
  129. package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
  130. package/src/data-structures/binary-tree/avl-tree.ts +43 -14
  131. package/src/data-structures/binary-tree/binary-tree.ts +8 -8
  132. package/src/data-structures/binary-tree/bst.ts +101 -96
  133. package/src/data-structures/binary-tree/rb-tree.ts +9 -9
  134. package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
  135. package/src/data-structures/graph/abstract-graph.ts +114 -109
  136. package/src/data-structures/graph/directed-graph.ts +77 -77
  137. package/src/data-structures/graph/map-graph.ts +20 -15
  138. package/src/data-structures/graph/undirected-graph.ts +39 -39
  139. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  140. package/src/data-structures/tree/tree.ts +7 -7
  141. package/src/interfaces/abstract-binary-tree.ts +37 -50
  142. package/src/interfaces/abstract-graph.ts +13 -13
  143. package/src/interfaces/avl-tree.ts +6 -4
  144. package/src/interfaces/bst.ts +9 -9
  145. package/src/interfaces/directed-graph.ts +5 -5
  146. package/src/interfaces/rb-tree.ts +2 -2
  147. package/src/interfaces/undirected-graph.ts +2 -2
  148. package/src/types/data-structures/abstract-binary-tree.ts +3 -4
  149. package/src/types/data-structures/abstract-graph.ts +2 -2
  150. package/src/types/data-structures/bst.ts +2 -2
  151. package/src/types/data-structures/tree-multiset.ts +1 -1
  152. package/src/types/utils/validate-type.ts +10 -10
  153. package/test/integration/avl-tree.test.ts +24 -24
  154. package/test/integration/bst.test.ts +71 -71
  155. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +28 -28
  156. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
  157. package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
  158. package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
  159. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
  160. package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
  161. package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
  162. package/test/unit/data-structures/graph/overall.test.ts +2 -2
  163. package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
  164. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -5
  165. package/test/unit/data-structures/tree/tree.test.ts +2 -2
  166. package/umd/bundle.min.js +1 -1
  167. package/umd/bundle.min.js.map +1 -1
@@ -1,8 +1,8 @@
1
1
  import { CP, LoopType } from '../../types';
2
2
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
3
3
  export class BSTNode extends BinaryTreeNode {
4
- constructor(id, val) {
5
- super(id, val);
4
+ constructor(key, val) {
5
+ super(key, val);
6
6
  }
7
7
  }
8
8
  export class BST extends BinaryTree {
@@ -21,36 +21,36 @@ export class BST extends BinaryTree {
21
21
  }
22
22
  }
23
23
  /**
24
- * The function creates a new binary search tree node with the given id and value.
25
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
24
+ * The function creates a new binary search tree node with the given key and value.
25
+ * @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
26
26
  * identify each node in the binary tree.
27
27
  * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
28
28
  * that will be stored in the node.
29
- * @returns a new instance of the BSTNode class with the specified id and value.
29
+ * @returns a new instance of the BSTNode class with the specified key and value.
30
30
  */
31
- createNode(id, val) {
32
- return new BSTNode(id, val);
31
+ createNode(key, val) {
32
+ return new BSTNode(key, val);
33
33
  }
34
34
  /**
35
35
  * The `add` function adds a new node to a binary search tree, either by creating a new node or by updating an existing
36
36
  * node with the same ID.
37
- * @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId` or a `N`
37
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N`
38
38
  * (which represents a binary tree node) or `null`.
39
39
  * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
40
40
  * being added to the binary search tree.
41
41
  * @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
42
42
  */
43
- add(idOrNode, val) {
43
+ add(keyOrNode, val) {
44
44
  // TODO support node as a param
45
45
  let inserted = null;
46
46
  let newNode = null;
47
- if (idOrNode instanceof BSTNode) {
48
- newNode = idOrNode;
47
+ if (keyOrNode instanceof BSTNode) {
48
+ newNode = keyOrNode;
49
49
  }
50
- else if (typeof idOrNode === 'number') {
51
- newNode = this.createNode(idOrNode, val);
50
+ else if (typeof keyOrNode === 'number') {
51
+ newNode = this.createNode(keyOrNode, val);
52
52
  }
53
- else if (idOrNode === null) {
53
+ else if (keyOrNode === null) {
54
54
  newNode = null;
55
55
  }
56
56
  if (this.root === null) {
@@ -63,7 +63,7 @@ export class BST extends BinaryTree {
63
63
  let traversing = true;
64
64
  while (traversing) {
65
65
  if (cur !== null && newNode !== null) {
66
- if (this._compare(cur.id, newNode.id) === CP.eq) {
66
+ if (this._compare(cur.key, newNode.key) === CP.eq) {
67
67
  if (newNode) {
68
68
  cur.val = newNode.val;
69
69
  }
@@ -71,7 +71,7 @@ export class BST extends BinaryTree {
71
71
  traversing = false;
72
72
  inserted = cur;
73
73
  }
74
- else if (this._compare(cur.id, newNode.id) === CP.gt) {
74
+ else if (this._compare(cur.key, newNode.key) === CP.gt) {
75
75
  // Traverse left of the node
76
76
  if (cur.left === undefined) {
77
77
  if (newNode) {
@@ -89,7 +89,7 @@ export class BST extends BinaryTree {
89
89
  cur = cur.left;
90
90
  }
91
91
  }
92
- else if (this._compare(cur.id, newNode.id) === CP.lt) {
92
+ else if (this._compare(cur.key, newNode.key) === CP.lt) {
93
93
  // Traverse right of the node
94
94
  if (cur.right === undefined) {
95
95
  if (newNode) {
@@ -118,46 +118,46 @@ export class BST extends BinaryTree {
118
118
  /**
119
119
  * The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
120
120
  * manner.
121
- * @param {[BinaryTreeNodeId | N , N['val']][]} idsOrNodes - The `idsOrNodes` parameter in the `addMany` function is an array of
122
- * `BinaryTreeNodeId` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
121
+ * @param {[BinaryTreeNodeKey | N , N['val']][]} keysOrNodes - The `keysOrNodes` parameter in the `addMany` function is an array of
122
+ * `BinaryTreeNodeKey` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
123
123
  * to the binary search tree.
124
124
  * @param {N['val'][]} data - The values of tree nodes
125
125
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
126
126
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
127
127
  */
128
- addMany(idsOrNodes, data, isBalanceAdd = false) {
128
+ addMany(keysOrNodes, data, isBalanceAdd = false) {
129
129
  function hasNoNull(arr) {
130
130
  return arr.indexOf(null) === -1;
131
131
  }
132
- if (!isBalanceAdd || !hasNoNull(idsOrNodes)) {
133
- return super.addMany(idsOrNodes, data);
132
+ if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
133
+ return super.addMany(keysOrNodes, data);
134
134
  }
135
135
  const inserted = [];
136
- const combinedArr = idsOrNodes.map((value, index) => [value, data === null || data === void 0 ? void 0 : data[index]]);
136
+ const combinedArr = keysOrNodes.map((value, index) => [value, data === null || data === void 0 ? void 0 : data[index]]);
137
137
  let sorted = [];
138
138
  function isNodeOrNullTuple(arr) {
139
- for (const [idOrNode] of arr)
140
- if (idOrNode instanceof BSTNode)
139
+ for (const [keyOrNode] of arr)
140
+ if (keyOrNode instanceof BSTNode)
141
141
  return true;
142
142
  return false;
143
143
  }
144
- function isBinaryTreeIdOrNullTuple(arr) {
145
- for (const [idOrNode] of arr)
146
- if (typeof idOrNode === 'number')
144
+ function isBinaryTreeKeyOrNullTuple(arr) {
145
+ for (const [keyOrNode] of arr)
146
+ if (typeof keyOrNode === 'number')
147
147
  return true;
148
148
  return false;
149
149
  }
150
- let sortedIdsOrNodes = [], sortedData = [];
150
+ let sortedKeysOrNodes = [], sortedData = [];
151
151
  if (isNodeOrNullTuple(combinedArr)) {
152
- sorted = combinedArr.sort((a, b) => a[0].id - b[0].id);
152
+ sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
153
153
  }
154
- else if (isBinaryTreeIdOrNullTuple(combinedArr)) {
154
+ else if (isBinaryTreeKeyOrNullTuple(combinedArr)) {
155
155
  sorted = combinedArr.sort((a, b) => a[0] - b[0]);
156
156
  }
157
157
  else {
158
- throw new Error('Invalid input idsOrNodes');
158
+ throw new Error('Invalid input keysOrNodes');
159
159
  }
160
- sortedIdsOrNodes = sorted.map(([idOrNode]) => idOrNode);
160
+ sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
161
161
  sortedData = sorted.map(([, val]) => val);
162
162
  const recursive = (arr, data) => {
163
163
  if (arr.length === 0)
@@ -177,7 +177,7 @@ export class BST extends BinaryTree {
177
177
  const [l, r] = popped;
178
178
  if (l <= r) {
179
179
  const m = l + Math.floor((r - l) / 2);
180
- const newNode = this.add(sortedIdsOrNodes[m], sortedData === null || sortedData === void 0 ? void 0 : sortedData[m]);
180
+ const newNode = this.add(sortedKeysOrNodes[m], sortedData === null || sortedData === void 0 ? void 0 : sortedData[m]);
181
181
  inserted.push(newNode);
182
182
  stack.push([m + 1, r]);
183
183
  stack.push([l, m - 1]);
@@ -186,7 +186,7 @@ export class BST extends BinaryTree {
186
186
  }
187
187
  };
188
188
  if (this.loopType === LoopType.RECURSIVE) {
189
- recursive(sortedIdsOrNodes, sortedData);
189
+ recursive(sortedKeysOrNodes, sortedData);
190
190
  }
191
191
  else {
192
192
  iterative();
@@ -195,45 +195,44 @@ export class BST extends BinaryTree {
195
195
  }
196
196
  /**
197
197
  * The function returns the first node in a binary tree that matches the given property name and value.
198
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
198
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
199
199
  * generic type `N`. It represents the property of the binary tree node that you want to search for.
200
200
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
201
- * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
202
- * @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
201
+ * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
202
+ * @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
203
203
  */
204
- get(nodeProperty, propertyName) {
204
+ get(nodeProperty, propertyName = 'key') {
205
205
  var _a;
206
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
207
206
  return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
208
207
  }
209
208
  /**
210
- * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
211
- * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
212
- * @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
213
- * the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
214
- * equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
209
+ * The function returns the key of the rightmost node if the comparison between two values is less than, the key of the
210
+ * leftmost node if the comparison is greater than, and the key of the rightmost node otherwise.
211
+ * @returns The method `lastKey()` returns the key of the rightmost node in the binary tree if the comparison between
212
+ * the values at index 0 and 1 is less than, otherwise it returns the key of the leftmost node. If the comparison is
213
+ * equal, it returns the key of the rightmost node. If there are no nodes in the tree, it returns 0.
215
214
  */
216
215
  lastKey() {
217
216
  var _a, _b, _c, _d, _e, _f;
218
217
  if (this._compare(0, 1) === CP.lt)
219
- return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
218
+ return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
220
219
  else if (this._compare(0, 1) === CP.gt)
221
- return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
220
+ return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : 0;
222
221
  else
223
- return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
222
+ return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
224
223
  }
225
224
  /**
226
225
  * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
227
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
226
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
228
227
  * `N` type. It represents the property of the binary tree node that you want to compare with.
229
228
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
230
- * specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
229
+ * specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
231
230
  * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
232
231
  * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
233
232
  * is set to `true`, the function will return an array with only one node (if
234
233
  * @returns an array of nodes (type N).
235
234
  */
236
- getNodes(nodeProperty, propertyName = 'id', onlyOne) {
235
+ getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
237
236
  if (!this.root)
238
237
  return [];
239
238
  const result = [];
@@ -243,10 +242,10 @@ export class BST extends BinaryTree {
243
242
  return;
244
243
  if (!cur.left && !cur.right)
245
244
  return;
246
- if (propertyName === 'id') {
247
- if (this._compare(cur.id, nodeProperty) === CP.gt)
245
+ if (propertyName === 'key') {
246
+ if (this._compare(cur.key, nodeProperty) === CP.gt)
248
247
  cur.left && _traverse(cur.left);
249
- if (this._compare(cur.id, nodeProperty) === CP.lt)
248
+ if (this._compare(cur.key, nodeProperty) === CP.lt)
250
249
  cur.right && _traverse(cur.right);
251
250
  }
252
251
  else {
@@ -263,10 +262,10 @@ export class BST extends BinaryTree {
263
262
  if (cur) {
264
263
  if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
265
264
  return result;
266
- if (propertyName === 'id') {
267
- if (this._compare(cur.id, nodeProperty) === CP.gt)
265
+ if (propertyName === 'key') {
266
+ if (this._compare(cur.key, nodeProperty) === CP.gt)
268
267
  cur.left && queue.push(cur.left);
269
- if (this._compare(cur.id, nodeProperty) === CP.lt)
268
+ if (this._compare(cur.key, nodeProperty) === CP.lt)
270
269
  cur.right && queue.push(cur.right);
271
270
  }
272
271
  else {
@@ -282,29 +281,28 @@ export class BST extends BinaryTree {
282
281
  /**
283
282
  * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
284
283
  * less than a given node.
285
- * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
284
+ * @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
286
285
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
287
- * specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
286
+ * specifies the property name to use for calculating the sum. If not provided, it defaults to `'key'`.
288
287
  * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
289
288
  * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
290
289
  */
291
- lesserSum(beginNode, propertyName) {
292
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
290
+ lesserSum(beginNode, propertyName = 'key') {
293
291
  if (typeof beginNode === 'number')
294
- beginNode = this.get(beginNode, 'id');
292
+ beginNode = this.get(beginNode, 'key');
295
293
  if (!beginNode)
296
294
  return 0;
297
295
  if (!this.root)
298
296
  return 0;
299
- const id = beginNode.id;
297
+ const key = beginNode.key;
300
298
  const getSumByPropertyName = (cur) => {
301
299
  let needSum;
302
300
  switch (propertyName) {
303
- case 'id':
304
- needSum = cur.id;
301
+ case 'key':
302
+ needSum = cur.key;
305
303
  break;
306
304
  default:
307
- needSum = cur.id;
305
+ needSum = cur.key;
308
306
  break;
309
307
  }
310
308
  return needSum;
@@ -312,7 +310,7 @@ export class BST extends BinaryTree {
312
310
  let sum = 0;
313
311
  if (this.loopType === LoopType.RECURSIVE) {
314
312
  const _traverse = (cur) => {
315
- const compared = this._compare(cur.id, id);
313
+ const compared = this._compare(cur.key, key);
316
314
  if (compared === CP.eq) {
317
315
  if (cur.right)
318
316
  sum += this.subTreeSum(cur.right, propertyName);
@@ -341,7 +339,7 @@ export class BST extends BinaryTree {
341
339
  while (queue.length > 0) {
342
340
  const cur = queue.shift();
343
341
  if (cur) {
344
- const compared = this._compare(cur.id, id);
342
+ const compared = this._compare(cur.key, key);
345
343
  if (compared === CP.eq) {
346
344
  if (cur.right)
347
345
  sum += this.subTreeSum(cur.right, propertyName);
@@ -371,44 +369,43 @@ export class BST extends BinaryTree {
371
369
  /**
372
370
  * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
373
371
  * have a greater value than a given node.
374
- * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
375
- * `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
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.
376
374
  * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
377
375
  * each greater node should be increased.
378
376
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
379
377
  * specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
380
- * 'id'.
378
+ * 'key'.
381
379
  * @returns a boolean value.
382
380
  */
383
- allGreaterNodesAdd(node, delta, propertyName) {
384
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
381
+ allGreaterNodesAdd(node, delta, propertyName = 'key') {
385
382
  if (typeof node === 'number')
386
- node = this.get(node, 'id');
383
+ node = this.get(node, 'key');
387
384
  if (!node)
388
385
  return false;
389
- const id = node.id;
386
+ const key = node.key;
390
387
  if (!this.root)
391
388
  return false;
392
389
  const _sumByPropertyName = (cur) => {
393
390
  switch (propertyName) {
394
- case 'id':
395
- cur.id += delta;
391
+ case 'key':
392
+ cur.key += delta;
396
393
  break;
397
394
  default:
398
- cur.id += delta;
395
+ cur.key += delta;
399
396
  break;
400
397
  }
401
398
  };
402
399
  if (this.loopType === LoopType.RECURSIVE) {
403
400
  const _traverse = (cur) => {
404
- const compared = this._compare(cur.id, id);
401
+ const compared = this._compare(cur.key, key);
405
402
  if (compared === CP.gt)
406
403
  _sumByPropertyName(cur);
407
404
  if (!cur.left && !cur.right)
408
405
  return;
409
- if (cur.left && this._compare(cur.left.id, id) === CP.gt)
406
+ if (cur.left && this._compare(cur.left.key, key) === CP.gt)
410
407
  _traverse(cur.left);
411
- if (cur.right && this._compare(cur.right.id, id) === CP.gt)
408
+ if (cur.right && this._compare(cur.right.key, key) === CP.gt)
412
409
  _traverse(cur.right);
413
410
  };
414
411
  _traverse(this.root);
@@ -419,12 +416,12 @@ export class BST extends BinaryTree {
419
416
  while (queue.length > 0) {
420
417
  const cur = queue.shift();
421
418
  if (cur) {
422
- const compared = this._compare(cur.id, id);
419
+ const compared = this._compare(cur.key, key);
423
420
  if (compared === CP.gt)
424
421
  _sumByPropertyName(cur);
425
- if (cur.left && this._compare(cur.left.id, id) === CP.gt)
422
+ if (cur.left && this._compare(cur.left.key, key) === CP.gt)
426
423
  queue.push(cur.left);
427
- if (cur.right && this._compare(cur.right.id, id) === CP.gt)
424
+ if (cur.right && this._compare(cur.right.key, key) === CP.gt)
428
425
  queue.push(cur.right);
429
426
  }
430
427
  }
@@ -446,7 +443,7 @@ export class BST extends BinaryTree {
446
443
  * @returns The function `perfectlyBalance()` returns a boolean value.
447
444
  */
448
445
  perfectlyBalance() {
449
- const sorted = this.DFS('in', 'node'), n = sorted.length;
446
+ const sorted = this.dfs('in', 'node'), n = sorted.length;
450
447
  this.clear();
451
448
  if (sorted.length < 1)
452
449
  return false;
@@ -456,7 +453,7 @@ export class BST extends BinaryTree {
456
453
  return;
457
454
  const m = l + Math.floor((r - l) / 2);
458
455
  const midNode = sorted[m];
459
- this.add(midNode.id, midNode.val);
456
+ this.add(midNode.key, midNode.val);
460
457
  buildBalanceBST(l, m - 1);
461
458
  buildBalanceBST(m + 1, r);
462
459
  };
@@ -472,7 +469,7 @@ export class BST extends BinaryTree {
472
469
  if (l <= r) {
473
470
  const m = l + Math.floor((r - l) / 2);
474
471
  const midNode = sorted[m];
475
- this.add(midNode.id, midNode.val);
472
+ this.add(midNode.key, midNode.val);
476
473
  stack.push([m + 1, r]);
477
474
  stack.push([l, m - 1]);
478
475
  }
@@ -534,8 +531,8 @@ export class BST extends BinaryTree {
534
531
  /**
535
532
  * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
536
533
  * greater than, less than, or equal to the second ID.
537
- * @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
538
- * @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
534
+ * @param {BinaryTreeNodeKey} a - "a" is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
535
+ * @param {BinaryTreeNodeKey} b - The parameter "b" in the above code refers to a BinaryTreeNodeKey.
539
536
  * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
540
537
  * than), or CP.eq (equal).
541
538
  */
@@ -1,13 +1,13 @@
1
- import { BinaryTreeNodeId, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
1
+ import { BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
2
2
  import { IRBTree, IRBTreeNode } from '../../interfaces';
3
3
  import { BST, BSTNode } from './bst';
4
4
  export declare class RBTreeNode<V = any, NEIGHBOR extends RBTreeNode<V, NEIGHBOR> = RBTreeNodeNested<V>> extends BSTNode<V, NEIGHBOR> implements IRBTreeNode<V, NEIGHBOR> {
5
5
  private _color;
6
- constructor(id: BinaryTreeNodeId, val?: V);
6
+ constructor(key: BinaryTreeNodeKey, val?: V);
7
7
  get color(): RBColor;
8
8
  set color(value: RBColor);
9
9
  }
10
10
  export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
11
11
  constructor(options?: RBTreeOptions);
12
- createNode(id: BinaryTreeNodeId, val?: N['val']): N;
12
+ createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
13
13
  }
@@ -1,8 +1,8 @@
1
1
  import { RBColor } from '../../types';
2
2
  import { BST, BSTNode } from './bst';
3
3
  export class RBTreeNode extends BSTNode {
4
- constructor(id, val) {
5
- super(id, val);
4
+ constructor(key, val) {
5
+ super(key, val);
6
6
  this._color = RBColor.RED;
7
7
  }
8
8
  get color() {
@@ -16,7 +16,7 @@ export class RBTree extends BST {
16
16
  constructor(options) {
17
17
  super(options);
18
18
  }
19
- createNode(id, val) {
20
- return new RBTreeNode(id, val);
19
+ createNode(key, val) {
20
+ return new RBTreeNode(key, val);
21
21
  }
22
22
  }