data-structure-typed 1.35.0 → 1.40.0-rc

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 (173) hide show
  1. package/.github/workflows/ci.yml +3 -0
  2. package/CHANGELOG.md +3 -1
  3. package/CONTRIBUTING.md +18 -0
  4. package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
  5. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +527 -0
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.js +323 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
  9. package/dist/data-structures/binary-tree/avl-tree.js +90 -3
  10. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
  13. package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
  14. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  15. package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -0
  16. package/dist/data-structures/binary-tree/binary-tree.js +21 -0
  17. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  18. package/dist/data-structures/binary-tree/bst.d.ts +133 -0
  19. package/dist/data-structures/binary-tree/bst.js +114 -0
  20. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  21. package/dist/data-structures/binary-tree/index.d.ts +12 -0
  22. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
  23. package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
  24. package/dist/data-structures/binary-tree/segment-tree.js +45 -0
  25. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
  26. package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
  27. package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
  28. package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
  29. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  30. package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
  31. package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
  32. package/dist/data-structures/graph/abstract-graph.js +269 -4
  33. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  34. package/dist/data-structures/graph/directed-graph.d.ts +200 -0
  35. package/dist/data-structures/graph/directed-graph.js +167 -0
  36. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  37. package/dist/data-structures/graph/index.d.ts +4 -0
  38. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  39. package/dist/data-structures/graph/map-graph.js +54 -0
  40. package/dist/data-structures/graph/map-graph.js.map +1 -1
  41. package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
  42. package/dist/data-structures/graph/undirected-graph.js +105 -0
  43. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  44. package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
  45. package/dist/data-structures/hash/coordinate-map.js +35 -0
  46. package/dist/data-structures/hash/coordinate-map.js.map +1 -1
  47. package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
  48. package/dist/data-structures/hash/coordinate-set.js +28 -0
  49. package/dist/data-structures/hash/coordinate-set.js.map +1 -1
  50. package/dist/data-structures/hash/hash-map.d.ts +56 -0
  51. package/dist/data-structures/hash/hash-map.js +29 -1
  52. package/dist/data-structures/hash/hash-map.js.map +1 -1
  53. package/dist/data-structures/hash/hash-table.d.ts +106 -0
  54. package/dist/data-structures/hash/hash-table.js +88 -6
  55. package/dist/data-structures/hash/hash-table.js.map +1 -1
  56. package/dist/data-structures/hash/index.d.ts +7 -0
  57. package/dist/data-structures/hash/pair.d.ts +2 -0
  58. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  59. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  60. package/dist/data-structures/heap/heap.d.ts +83 -0
  61. package/dist/data-structures/heap/heap.js +62 -0
  62. package/dist/data-structures/heap/heap.js.map +1 -1
  63. package/dist/data-structures/heap/index.d.ts +3 -0
  64. package/dist/data-structures/heap/max-heap.d.ts +23 -0
  65. package/dist/data-structures/heap/max-heap.js +16 -0
  66. package/dist/data-structures/heap/max-heap.js.map +1 -1
  67. package/dist/data-structures/heap/min-heap.d.ts +24 -0
  68. package/dist/data-structures/heap/min-heap.js +17 -0
  69. package/dist/data-structures/heap/min-heap.js.map +1 -1
  70. package/dist/data-structures/index.d.ts +11 -0
  71. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
  72. package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
  73. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  74. package/dist/data-structures/linked-list/index.d.ts +3 -0
  75. package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
  76. package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
  77. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
  78. package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
  79. package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
  80. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
  81. package/dist/data-structures/matrix/index.d.ts +4 -0
  82. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  83. package/dist/data-structures/matrix/matrix.js +15 -0
  84. package/dist/data-structures/matrix/matrix.js.map +1 -1
  85. package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
  86. package/dist/data-structures/matrix/matrix2d.js +91 -2
  87. package/dist/data-structures/matrix/matrix2d.js.map +1 -1
  88. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  89. package/dist/data-structures/matrix/navigator.js +28 -0
  90. package/dist/data-structures/matrix/navigator.js.map +1 -1
  91. package/dist/data-structures/matrix/vector2d.d.ts +201 -0
  92. package/dist/data-structures/matrix/vector2d.js +188 -1
  93. package/dist/data-structures/matrix/vector2d.js.map +1 -1
  94. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  95. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
  96. package/dist/data-structures/priority-queue/max-priority-queue.js +18 -0
  97. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  98. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
  99. package/dist/data-structures/priority-queue/min-priority-queue.js +19 -0
  100. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  101. package/dist/data-structures/priority-queue/priority-queue.d.ts +180 -0
  102. package/dist/data-structures/priority-queue/priority-queue.js +141 -0
  103. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  104. package/dist/data-structures/queue/deque.d.ts +165 -0
  105. package/dist/data-structures/queue/deque.js +124 -0
  106. package/dist/data-structures/queue/deque.js.map +1 -1
  107. package/dist/data-structures/queue/index.d.ts +2 -0
  108. package/dist/data-structures/queue/queue.d.ts +107 -0
  109. package/dist/data-structures/queue/queue.js +80 -0
  110. package/dist/data-structures/queue/queue.js.map +1 -1
  111. package/dist/data-structures/stack/index.d.ts +1 -0
  112. package/dist/data-structures/stack/stack.d.ts +63 -0
  113. package/dist/data-structures/stack/stack.js +50 -0
  114. package/dist/data-structures/stack/stack.js.map +1 -1
  115. package/dist/data-structures/tree/index.d.ts +1 -0
  116. package/dist/data-structures/tree/tree.d.ts +14 -0
  117. package/dist/data-structures/tree/tree.js +1 -0
  118. package/dist/data-structures/tree/tree.js.map +1 -1
  119. package/dist/data-structures/trie/index.d.ts +1 -0
  120. package/dist/data-structures/trie/trie.d.ts +61 -0
  121. package/dist/data-structures/trie/trie.js +36 -0
  122. package/dist/data-structures/trie/trie.js.map +1 -1
  123. package/dist/index.d.ts +4 -0
  124. package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
  125. package/dist/interfaces/abstract-graph.d.ts +5 -0
  126. package/dist/interfaces/avl-tree.d.ts +7 -0
  127. package/dist/interfaces/binary-tree.d.ts +6 -0
  128. package/dist/interfaces/bst.d.ts +6 -0
  129. package/dist/interfaces/directed-graph.d.ts +3 -0
  130. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  131. package/dist/interfaces/heap.d.ts +1 -0
  132. package/dist/interfaces/index.d.ts +15 -0
  133. package/dist/interfaces/navigator.d.ts +1 -0
  134. package/dist/interfaces/priority-queue.d.ts +1 -0
  135. package/dist/interfaces/rb-tree.d.ts +6 -0
  136. package/dist/interfaces/segment-tree.d.ts +1 -0
  137. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  138. package/dist/interfaces/tree-multiset.d.ts +6 -0
  139. package/dist/interfaces/undirected-graph.d.ts +3 -0
  140. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  141. package/dist/types/data-structures/abstract-binary-tree.js +6 -0
  142. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
  143. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  144. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  145. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  146. package/dist/types/data-structures/bst.d.ts +13 -0
  147. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  148. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  149. package/dist/types/data-structures/hash.d.ts +1 -0
  150. package/dist/types/data-structures/heap.d.ts +3 -0
  151. package/dist/types/data-structures/index.d.ts +16 -0
  152. package/dist/types/data-structures/map-graph.d.ts +1 -0
  153. package/dist/types/data-structures/navigator.d.ts +14 -0
  154. package/dist/types/data-structures/priority-queue.d.ts +7 -0
  155. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  156. package/dist/types/data-structures/segment-tree.d.ts +1 -0
  157. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  158. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  159. package/dist/types/helpers.d.ts +1 -0
  160. package/dist/types/index.d.ts +3 -0
  161. package/dist/types/utils/index.d.ts +2 -0
  162. package/dist/types/utils/utils.d.ts +7 -0
  163. package/dist/types/utils/validate-type.d.ts +19 -0
  164. package/dist/utils/index.d.ts +1 -0
  165. package/dist/utils/utils.d.ts +19 -0
  166. package/package.json +11 -7
  167. package/test/integration/avl-tree.test.ts +4 -4
  168. package/test/integration/bst.test.ts +8 -8
  169. package/test/unit/data-structures/graph/directed-graph.test.ts +5 -5
  170. package/test/unit/data-structures/graph/overall.test.ts +2 -2
  171. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +1 -1
  172. package/test/unit/data-structures/graph/index.ts +0 -2
  173. package/test/unit/data-structures/linked-list/index.ts +0 -4
@@ -1,9 +1,23 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Tyler Zeng
6
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.AbstractBinaryTree = exports.AbstractBinaryTreeNode = void 0;
4
11
  const utils_1 = require("../../utils");
5
12
  const types_1 = require("../../types");
6
13
  class AbstractBinaryTreeNode {
14
+ /**
15
+ * The constructor function initializes a BinaryTreeNode object with a key and an optional value.
16
+ * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
17
+ * of the binary tree node. It is used to distinguish one node from another in the binary tree.
18
+ * @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
19
+ * stored in the binary tree node. If no value is provided, it will be set to undefined.
20
+ */
7
21
  constructor(key, val) {
8
22
  this.key = key;
9
23
  this.val = val;
@@ -26,6 +40,10 @@ class AbstractBinaryTreeNode {
26
40
  }
27
41
  this._right = v;
28
42
  }
43
+ /**
44
+ * The function determines the position of a node in a family tree structure.
45
+ * @returns a value of type `FamilyPosition`.
46
+ */
29
47
  get familyPosition() {
30
48
  const that = this;
31
49
  if (that.parent) {
@@ -61,7 +79,13 @@ class AbstractBinaryTreeNode {
61
79
  }
62
80
  exports.AbstractBinaryTreeNode = AbstractBinaryTreeNode;
63
81
  class AbstractBinaryTree {
82
+ /**
83
+ * The protected constructor initializes the options for an abstract binary tree.
84
+ * @param {AbstractBinaryTreeOptions} [options] - An optional object that contains configuration options for the binary
85
+ * tree.
86
+ */
64
87
  constructor(options) {
88
+ // TODO placeholder node may need redesigned
65
89
  this._root = null;
66
90
  this._size = 0;
67
91
  this._loopType = types_1.LoopType.ITERATIVE;
@@ -83,6 +107,13 @@ class AbstractBinaryTree {
83
107
  get loopType() {
84
108
  return this._loopType;
85
109
  }
110
+ /**
111
+ * The `swapLocation` function swaps the location of two nodes in a binary tree.
112
+ * @param {N} srcNode - The source node that you want to swap with the destination node.
113
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
114
+ * be swapped to.
115
+ * @returns The `destNode` is being returned.
116
+ */
86
117
  swapLocation(srcNode, destNode) {
87
118
  const { key, val } = destNode;
88
119
  const tempNode = this.createNode(key, val);
@@ -94,14 +125,34 @@ class AbstractBinaryTree {
94
125
  }
95
126
  return destNode;
96
127
  }
128
+ /**
129
+ * The clear() function resets the root, size, and maxKey properties to their initial values.
130
+ */
97
131
  clear() {
98
132
  this._root = null;
99
133
  this._size = 0;
100
134
  this._clearResults();
101
135
  }
136
+ /**
137
+ * The function checks if the size of an object is equal to zero and returns a boolean value.
138
+ * @returns A boolean value indicating whether the size of the object is 0 or not.
139
+ */
102
140
  isEmpty() {
103
141
  return this.size === 0;
104
142
  }
143
+ /**
144
+ * When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
145
+ * In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
146
+ */
147
+ /**
148
+ * The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
149
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey`, which
150
+ * is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
151
+ * node itself. It can also be `null` if no node is specified.
152
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
153
+ * being added to the binary tree.
154
+ * @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
155
+ */
105
156
  add(keyOrNode, val) {
106
157
  const _bfs = (root, newNode) => {
107
158
  const queue = [root];
@@ -158,7 +209,18 @@ class AbstractBinaryTree {
158
209
  }
159
210
  return inserted;
160
211
  }
212
+ /**
213
+ * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
214
+ * values, and adds them to the binary tree.
215
+ * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
216
+ * objects, or null values.
217
+ * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
218
+ * the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
219
+ * the value of the nodes will be `undefined`.
220
+ * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
221
+ */
161
222
  addMany(keysOrNodes, data) {
223
+ // TODO not sure addMany not be run multi times
162
224
  const inserted = [];
163
225
  for (let i = 0; i < keysOrNodes.length; i++) {
164
226
  const keyOrNode = keysOrNodes[i];
@@ -175,10 +237,26 @@ class AbstractBinaryTree {
175
237
  }
176
238
  return inserted;
177
239
  }
240
+ /**
241
+ * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
242
+ * @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
243
+ * `BinaryTreeNodeKey` or `N` values.
244
+ * @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
245
+ * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
246
+ * array. Each value in the `data` array will be assigned to the
247
+ * @returns The method is returning a boolean value.
248
+ */
178
249
  refill(keysOrNodes, data) {
179
250
  this.clear();
180
251
  return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
181
252
  }
253
+ /**
254
+ * The `remove` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
255
+ * containing the deleted node and the node that needs to be balanced.
256
+ * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
257
+ * node ID (`BinaryTreeNodeKey`).
258
+ * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
259
+ */
182
260
  remove(nodeOrKey) {
183
261
  const bstDeletedResult = [];
184
262
  if (!this.root)
@@ -222,6 +300,11 @@ class AbstractBinaryTree {
222
300
  bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
223
301
  return bstDeletedResult;
224
302
  }
303
+ /**
304
+ * The function calculates the depth of a node in a binary tree.
305
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be one of the following:
306
+ * @returns the depth of the given node or binary tree.
307
+ */
225
308
  getDepth(beginRoot = this.root) {
226
309
  if (typeof beginRoot === 'number')
227
310
  beginRoot = this.get(beginRoot, 'key');
@@ -232,6 +315,13 @@ class AbstractBinaryTree {
232
315
  }
233
316
  return depth;
234
317
  }
318
+ /**
319
+ * The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
320
+ * @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
321
+ * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
322
+ * node), or `null`.
323
+ * @returns the height of the binary tree.
324
+ */
235
325
  getHeight(beginRoot = this.root) {
236
326
  if (typeof beginRoot === 'number')
237
327
  beginRoot = this.get(beginRoot, 'key');
@@ -266,6 +356,14 @@ class AbstractBinaryTree {
266
356
  return maxHeight;
267
357
  }
268
358
  }
359
+ /**
360
+ * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
361
+ * approach.
362
+ * @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
363
+ * represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
364
+ * for `beginRoot`, the `this.root` property is used as the default value.
365
+ * @returns The function `getMinHeight` returns the minimum height of the binary tree.
366
+ */
269
367
  getMinHeight(beginRoot = this.root) {
270
368
  var _a, _b, _c;
271
369
  if (!beginRoot)
@@ -310,9 +408,27 @@ class AbstractBinaryTree {
310
408
  return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
311
409
  }
312
410
  }
411
+ /**
412
+ * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
413
+ * tree.
414
+ * @param {N | null} [beginRoot] - The parameter `beginRoot` is of type `N` or `null`. It represents the root node of a
415
+ * tree or null if the tree is empty.
416
+ * @returns The method is returning a boolean value.
417
+ */
313
418
  isPerfectlyBalanced(beginRoot = this.root) {
314
419
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
315
420
  }
421
+ /**
422
+ * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
423
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
424
+ * generic type `N`. It represents the property of the binary tree node that you want to search for.
425
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
426
+ * specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
427
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
428
+ * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
429
+ * function will stop traversing the tree and return the first matching node. If `only
430
+ * @returns an array of nodes (type N).
431
+ */
316
432
  getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
317
433
  if (!this.root)
318
434
  return [];
@@ -342,22 +458,67 @@ class AbstractBinaryTree {
342
458
  }
343
459
  return result;
344
460
  }
461
+ /**
462
+ * The function checks if a binary tree node has a specific property.
463
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
464
+ * It represents the property of the binary tree node that you want to check.
465
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
466
+ * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
467
+ * @returns a boolean value.
468
+ */
345
469
  has(nodeProperty, propertyName = 'key') {
470
+ // TODO may support finding node by value equal
346
471
  return this.getNodes(nodeProperty, propertyName).length > 0;
347
472
  }
473
+ /**
474
+ * The function returns the first node that matches the given property name and value, or null if no matching node is
475
+ * found.
476
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
477
+ * It represents the property of the binary tree node that you want to search for.
478
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
479
+ * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
480
+ * default value is set to `'key'`.
481
+ * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
482
+ * If no matching node is found, it returns null.
483
+ */
348
484
  get(nodeProperty, propertyName = 'key') {
349
485
  var _a;
486
+ // TODO may support finding node by value equal
350
487
  return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
351
488
  }
489
+ /**
490
+ * The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
491
+ * an option to reverse the order of the nodes.
492
+ * @param {N} node - The `node` parameter represents a node in a tree structure. It is of type `N`, which could be any
493
+ * type that represents a node in your specific implementation.
494
+ * @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
495
+ * path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
496
+ * `isReverse` is set to `false` or not provided, the path will
497
+ * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
498
+ */
352
499
  getPathToRoot(node, isReverse = true) {
500
+ // TODO to support get path through passing key
353
501
  const result = [];
354
502
  while (node.parent) {
503
+ // Array.push + Array.reverse is more efficient than Array.unshift
504
+ // TODO may consider using Deque, so far this is not the performance bottleneck
355
505
  result.push(node);
356
506
  node = node.parent;
357
507
  }
358
508
  result.push(node);
359
509
  return isReverse ? result.reverse() : result;
360
510
  }
511
+ /**
512
+ * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
513
+ * no node is specified.
514
+ * @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
515
+ * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
516
+ * node), or `null`.
517
+ * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
518
+ * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
519
+ * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
520
+ * node is found (
521
+ */
361
522
  getLeftMost(beginRoot = this.root) {
362
523
  if (typeof beginRoot === 'number')
363
524
  beginRoot = this.get(beginRoot, 'key');
@@ -372,6 +533,7 @@ class AbstractBinaryTree {
372
533
  return _traverse(beginRoot);
373
534
  }
374
535
  else {
536
+ // Indirect implementation of iteration using tail recursion optimization
375
537
  const _traverse = (0, utils_1.trampoline)((cur) => {
376
538
  if (!cur.left)
377
539
  return cur;
@@ -380,7 +542,18 @@ class AbstractBinaryTree {
380
542
  return _traverse(beginRoot);
381
543
  }
382
544
  }
545
+ /**
546
+ * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
547
+ * recursion optimization.
548
+ * @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
549
+ * starting node from which we want to find the rightmost node. If no node is provided, the function will default to
550
+ * using the root node of the data structure.
551
+ * @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
552
+ * it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
553
+ * rightmost node starting from the root of the binary tree.
554
+ */
383
555
  getRightMost(beginRoot = this.root) {
556
+ // TODO support get right most by passing key in
384
557
  if (!beginRoot)
385
558
  return beginRoot;
386
559
  if (this._loopType === types_1.LoopType.RECURSIVE) {
@@ -392,6 +565,7 @@ class AbstractBinaryTree {
392
565
  return _traverse(beginRoot);
393
566
  }
394
567
  else {
568
+ // Indirect implementation of iteration using tail recursion optimization
395
569
  const _traverse = (0, utils_1.trampoline)((cur) => {
396
570
  if (!cur.right)
397
571
  return cur;
@@ -400,7 +574,13 @@ class AbstractBinaryTree {
400
574
  return _traverse(beginRoot);
401
575
  }
402
576
  }
577
+ /**
578
+ * The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
579
+ * @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
580
+ * @returns a boolean value.
581
+ */
403
582
  isSubtreeBST(node) {
583
+ // TODO there is a bug
404
584
  if (!node)
405
585
  return true;
406
586
  if (this._loopType === types_1.LoopType.RECURSIVE) {
@@ -430,10 +610,21 @@ class AbstractBinaryTree {
430
610
  return true;
431
611
  }
432
612
  }
613
+ /**
614
+ * The function isBST checks if the binary tree is valid binary search tree.
615
+ * @returns The `isBST()` function is returning a boolean value.
616
+ */
433
617
  isBST() {
434
618
  return this.isSubtreeBST(this.root);
435
619
  }
620
+ /**
621
+ * The function calculates the size of a subtree by traversing it either recursively or iteratively.
622
+ * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
623
+ * binary tree.
624
+ * @returns the size of the subtree rooted at `subTreeRoot`.
625
+ */
436
626
  getSubTreeSize(subTreeRoot) {
627
+ // TODO support key passed in
437
628
  let size = 0;
438
629
  if (!subTreeRoot)
439
630
  return size;
@@ -457,6 +648,15 @@ class AbstractBinaryTree {
457
648
  return size;
458
649
  }
459
650
  }
651
+ /**
652
+ * The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
653
+ * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
654
+ * tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
655
+ * @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
656
+ * property of the binary tree node to use for calculating the sum. It can be either 'key' or 'val'. If propertyName is
657
+ * not provided, it defaults to 'key'.
658
+ * @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
659
+ */
460
660
  subTreeSum(subTreeRoot, propertyName = 'key') {
461
661
  if (typeof subTreeRoot === 'number')
462
662
  subTreeRoot = this.get(subTreeRoot, 'key');
@@ -497,6 +697,16 @@ class AbstractBinaryTree {
497
697
  }
498
698
  return sum;
499
699
  }
700
+ /**
701
+ * The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
702
+ * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
703
+ * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
704
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
705
+ * each node in the subtree should be incremented.
706
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
707
+ * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
708
+ * @returns a boolean value.
709
+ */
500
710
  subTreeAdd(subTreeRoot, delta, propertyName = 'key') {
501
711
  if (typeof subTreeRoot === 'number')
502
712
  subTreeRoot = this.get(subTreeRoot, 'key');
@@ -531,6 +741,13 @@ class AbstractBinaryTree {
531
741
  }
532
742
  return true;
533
743
  }
744
+ /**
745
+ * The bfs function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
746
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
747
+ * If a node is provided, the bfs algorithm will be performed starting from that node.
748
+ * If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property.
749
+ * @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
750
+ */
534
751
  bfs(nodeOrPropertyName = 'key') {
535
752
  this._clearResults();
536
753
  const queue = [this.root];
@@ -546,6 +763,13 @@ class AbstractBinaryTree {
546
763
  }
547
764
  return this._getResultByPropertyName(nodeOrPropertyName);
548
765
  }
766
+ /**
767
+ * The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
768
+ * each node based on the specified pattern and property name.
769
+ * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
770
+ * @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'`.
771
+ * @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.
772
+ */
549
773
  dfs(pattern = 'in', nodeOrPropertyName = 'key') {
550
774
  this._clearResults();
551
775
  const _traverse = (node) => {
@@ -576,10 +800,18 @@ class AbstractBinaryTree {
576
800
  this.root && _traverse(this.root);
577
801
  return this._getResultByPropertyName(nodeOrPropertyName);
578
802
  }
803
+ /**
804
+ * The dfsIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
805
+ * specify the traversal pattern and the property name to accumulate results by.
806
+ * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
807
+ * @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'`.
808
+ * @returns An object of type AbstractBinaryTreeNodeProperties<N>.
809
+ */
579
810
  dfsIterative(pattern = 'in', nodeOrPropertyName = 'key') {
580
811
  this._clearResults();
581
812
  if (!this.root)
582
813
  return this._getResultByPropertyName(nodeOrPropertyName);
814
+ // 0: visit, 1: print
583
815
  const stack = [{ opt: 0, node: this.root }];
584
816
  while (stack.length > 0) {
585
817
  const cur = stack.pop();
@@ -615,6 +847,18 @@ class AbstractBinaryTree {
615
847
  }
616
848
  return this._getResultByPropertyName(nodeOrPropertyName);
617
849
  }
850
+ /**
851
+ * The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
852
+ * in an array, based on a specified property name.
853
+ * @param {N | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
854
+ * node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
855
+ * the tree is used as the starting node.
856
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
857
+ * can be either a `BinaryTreeNode` property name or the string `'key'`. If a property name is provided, the function
858
+ * will accumulate results based on that property. If no property name is provided, the function will default to
859
+ * accumulating results based on the 'key' property.
860
+ * @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
861
+ */
618
862
  levelIterative(node = this.root, nodeOrPropertyName = 'key') {
619
863
  if (!node)
620
864
  return [];
@@ -634,6 +878,12 @@ class AbstractBinaryTree {
634
878
  }
635
879
  return this._getResultByPropertyName(nodeOrPropertyName);
636
880
  }
881
+ /**
882
+ * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
883
+ * @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
884
+ * @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'.
885
+ * @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
886
+ */
637
887
  listLevels(node = this.root, nodeOrPropertyName = 'key') {
638
888
  if (!node)
639
889
  return [];
@@ -682,6 +932,11 @@ class AbstractBinaryTree {
682
932
  }
683
933
  return levelsNodes;
684
934
  }
935
+ /**
936
+ * The function returns the predecessor of a given node in a binary tree.
937
+ * @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
938
+ * @returns the predecessor of the given node in a binary tree.
939
+ */
685
940
  getPredecessor(node) {
686
941
  if (node.left) {
687
942
  let predecessor = node.left;
@@ -696,6 +951,12 @@ class AbstractBinaryTree {
696
951
  return node;
697
952
  }
698
953
  }
954
+ /**
955
+ * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
956
+ * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
957
+ * @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'.
958
+ * @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
959
+ */
699
960
  morris(pattern = 'in', nodeOrPropertyName = 'key') {
700
961
  if (this.root === null)
701
962
  return [];
@@ -780,8 +1041,20 @@ class AbstractBinaryTree {
780
1041
  }
781
1042
  return this._getResultByPropertyName(nodeOrPropertyName);
782
1043
  }
1044
+ /**
1045
+ * The function adds a new node to a binary tree if there is an available position.
1046
+ * @param {N | null} newNode - The `newNode` parameter is of type `N | null`, which means it can either be a node of
1047
+ * type `N` or `null`. It represents the node that you want to add to the binary tree.
1048
+ * @param {N} parent - The parent parameter is of type N, which represents a node in a binary tree.
1049
+ * @returns either the left or right child node of the parent node, depending on which child is available for adding
1050
+ * the new node. If a new node is added, the function also updates the size of the binary tree. If neither the left nor
1051
+ * right child is available, the function returns undefined. If the parent node is null, the function also returns
1052
+ * undefined.
1053
+ */
783
1054
  _addTo(newNode, parent) {
784
1055
  if (parent) {
1056
+ // When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
1057
+ // In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
785
1058
  if (parent.left === undefined) {
786
1059
  parent.left = newNode;
787
1060
  if (newNode) {
@@ -804,23 +1077,55 @@ class AbstractBinaryTree {
804
1077
  return;
805
1078
  }
806
1079
  }
1080
+ /**
1081
+ * The function sets the loop type for a protected variable.
1082
+ * @param {LoopType} value - The value parameter is of type LoopType.
1083
+ */
807
1084
  _setLoopType(value) {
808
1085
  this._loopType = value;
809
1086
  }
1087
+ /**
1088
+ * The function sets the root property of an object to a given value, and if the value is not null, it also sets the
1089
+ * parent property of the value to undefined.
1090
+ * @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of type `N` or `null`.
1091
+ */
810
1092
  _setRoot(v) {
811
1093
  if (v) {
812
1094
  v.parent = undefined;
813
1095
  }
814
1096
  this._root = v;
815
1097
  }
1098
+ /**
1099
+ * The function sets the size of a protected variable.
1100
+ * @param {number} v - number
1101
+ */
816
1102
  _setSize(v) {
817
1103
  this._size = v;
818
1104
  }
1105
+ /**
1106
+ * The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
1107
+ * properties.
1108
+ */
819
1109
  _clearResults() {
820
1110
  this.visitedKey = [];
821
1111
  this.visitedVal = [];
822
1112
  this.visitedNode = [];
823
1113
  }
1114
+ /**
1115
+ * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
1116
+ * a result array.
1117
+ * @param {N} cur - The current node being processed.
1118
+ * @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
1119
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeKey` or a `N`
1120
+ * type. It represents the property value that we are comparing against in the switch statement.
1121
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
1122
+ * specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'key'`
1123
+ * or `'val'`. If it is not provided or is not equal to `'key'` or `'val'`, the
1124
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
1125
+ * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
1126
+ * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
1127
+ * @returns a boolean value indicating whether only one matching node should be pushed into the result array.
1128
+ */
824
1129
  _pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName = 'key', onlyOne = false) {
825
1130
  switch (propertyName) {
826
1131
  case 'key':
@@ -843,6 +1148,13 @@ class AbstractBinaryTree {
843
1148
  break;
844
1149
  }
845
1150
  }
1151
+ /**
1152
+ * The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
1153
+ * @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
1154
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1155
+ * can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
1156
+ * specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
1157
+ */
846
1158
  _accumulatedByPropertyName(node, nodeOrPropertyName = 'key') {
847
1159
  switch (nodeOrPropertyName) {
848
1160
  case 'key':
@@ -859,6 +1171,17 @@ class AbstractBinaryTree {
859
1171
  break;
860
1172
  }
861
1173
  }
1174
+ /**
1175
+ * The time complexity of Morris traversal is O(n), it may slower than others
1176
+ * The space complexity Morris traversal is O(1) because no using stack
1177
+ */
1178
+ /**
1179
+ * The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
1180
+ * name.
1181
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1182
+ * can accept either a `NodeOrPropertyName` type or be undefined.
1183
+ * @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
1184
+ */
862
1185
  _getResultByPropertyName(nodeOrPropertyName = 'key') {
863
1186
  switch (nodeOrPropertyName) {
864
1187
  case 'key':