data-structure-typed 1.50.1 → 1.50.3

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 (196) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/README.md +30 -28
  3. package/README_zh-CN.md +1 -1
  4. package/SPECIFICATION.md +56 -0
  5. package/SPONSOR-zh-CN.md +90 -0
  6. package/SPONSOR.md +54 -0
  7. package/benchmark/report.html +13 -13
  8. package/benchmark/report.json +149 -161
  9. package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
  10. package/dist/cjs/data-structures/base/iterable-base.js +143 -7
  11. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  16. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  17. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  19. package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
  20. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
  22. package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
  23. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
  25. package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
  26. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  28. package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
  29. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
  30. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  31. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
  32. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  33. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
  34. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
  35. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  36. package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
  37. package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
  38. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  39. package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
  40. package/dist/cjs/data-structures/graph/map-graph.js +14 -0
  41. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  42. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
  43. package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
  44. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  45. package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
  46. package/dist/cjs/data-structures/hash/hash-map.js +347 -78
  47. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  48. package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
  49. package/dist/cjs/data-structures/heap/heap.js +95 -26
  50. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  51. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  52. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
  53. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  54. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  55. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
  56. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  57. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  58. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
  59. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  60. package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
  61. package/dist/cjs/data-structures/matrix/matrix.js +50 -11
  62. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  63. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  64. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  65. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  66. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  67. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  68. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  69. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  70. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
  71. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  72. package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
  73. package/dist/cjs/data-structures/queue/deque.js +200 -62
  74. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  75. package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
  76. package/dist/cjs/data-structures/queue/queue.js +111 -49
  77. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  78. package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
  79. package/dist/cjs/data-structures/stack/stack.js +58 -22
  80. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  81. package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
  82. package/dist/cjs/data-structures/tree/tree.js +77 -11
  83. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  84. package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
  85. package/dist/cjs/data-structures/trie/trie.js +153 -33
  86. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  87. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  88. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
  89. package/dist/cjs/types/utils/utils.d.ts +1 -0
  90. package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
  91. package/dist/mjs/data-structures/base/iterable-base.js +143 -7
  92. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
  93. package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
  94. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  95. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  96. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  97. package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
  98. package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
  99. package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
  100. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
  101. package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
  102. package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  103. package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
  104. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  105. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
  106. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
  107. package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
  108. package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
  109. package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
  110. package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
  111. package/dist/mjs/data-structures/graph/map-graph.js +14 -0
  112. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
  113. package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
  114. package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
  115. package/dist/mjs/data-structures/hash/hash-map.js +353 -84
  116. package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
  117. package/dist/mjs/data-structures/heap/heap.js +95 -26
  118. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  119. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
  120. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  121. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
  122. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  123. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
  124. package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
  125. package/dist/mjs/data-structures/matrix/matrix.js +50 -11
  126. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  127. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  128. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  129. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  130. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  131. package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
  132. package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
  133. package/dist/mjs/data-structures/queue/deque.js +205 -67
  134. package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
  135. package/dist/mjs/data-structures/queue/queue.js +111 -49
  136. package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
  137. package/dist/mjs/data-structures/stack/stack.js +58 -22
  138. package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
  139. package/dist/mjs/data-structures/tree/tree.js +80 -14
  140. package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
  141. package/dist/mjs/data-structures/trie/trie.js +156 -36
  142. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  143. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
  144. package/dist/mjs/types/utils/utils.d.ts +1 -0
  145. package/dist/umd/data-structure-typed.js +3121 -1583
  146. package/dist/umd/data-structure-typed.min.js +3 -3
  147. package/dist/umd/data-structure-typed.min.js.map +1 -1
  148. package/package.json +61 -55
  149. package/src/data-structures/base/iterable-base.ts +184 -19
  150. package/src/data-structures/binary-tree/avl-tree.ts +134 -100
  151. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  152. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  153. package/src/data-structures/binary-tree/bst.ts +127 -136
  154. package/src/data-structures/binary-tree/rb-tree.ts +199 -166
  155. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  156. package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
  157. package/src/data-structures/graph/abstract-graph.ts +4 -211
  158. package/src/data-structures/graph/directed-graph.ts +152 -0
  159. package/src/data-structures/graph/map-graph.ts +15 -0
  160. package/src/data-structures/graph/undirected-graph.ts +171 -19
  161. package/src/data-structures/hash/hash-map.ts +389 -96
  162. package/src/data-structures/heap/heap.ts +97 -26
  163. package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
  164. package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
  165. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  166. package/src/data-structures/matrix/matrix.ts +52 -12
  167. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  168. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  169. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  170. package/src/data-structures/queue/deque.ts +225 -70
  171. package/src/data-structures/queue/queue.ts +118 -49
  172. package/src/data-structures/stack/stack.ts +63 -23
  173. package/src/data-structures/tree/tree.ts +89 -15
  174. package/src/data-structures/trie/trie.ts +173 -38
  175. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  176. package/src/types/data-structures/hash/hash-map.ts +4 -3
  177. package/src/types/utils/utils.ts +2 -0
  178. package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
  179. package/test/performance/data-structures/queue/deque.test.ts +26 -25
  180. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
  181. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
  182. package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
  183. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
  184. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
  185. package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
  186. package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
  187. package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
  188. package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
  189. package/test/unit/data-structures/heap/heap.test.ts +18 -1
  190. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
  191. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
  192. package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
  193. package/test/unit/data-structures/queue/deque.test.ts +65 -5
  194. package/test/unit/data-structures/queue/queue.test.ts +24 -5
  195. package/test/unit/data-structures/stack/stack.test.ts +17 -0
  196. package/test/unit/data-structures/trie/trie.test.ts +17 -0
@@ -11,14 +11,16 @@ export class BSTNode extends BinaryTreeNode {
11
11
  }
12
12
  _left;
13
13
  /**
14
- * Get the left child node.
14
+ * The function returns the value of the `_left` property.
15
+ * @returns The `_left` property of the current object is being returned.
15
16
  */
16
17
  get left() {
17
18
  return this._left;
18
19
  }
19
20
  /**
20
- * Set the left child node.
21
- * @param {N | undefined} v - The left child node.
21
+ * The function sets the left child of a node and updates the parent reference of the child.
22
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an
23
+ * instance of the `NODE` class or `undefined`.
22
24
  */
23
25
  set left(v) {
24
26
  if (v) {
@@ -28,14 +30,17 @@ export class BSTNode extends BinaryTreeNode {
28
30
  }
29
31
  _right;
30
32
  /**
31
- * Get the right child node.
33
+ * The function returns the right node of a binary tree or undefined if there is no right node.
34
+ * @returns The method is returning the value of the `_right` property, which is of type `NODE` or
35
+ * `undefined`.
32
36
  */
33
37
  get right() {
34
38
  return this._right;
35
39
  }
36
40
  /**
37
- * Set the right child node.
38
- * @param {N | undefined} v - The right child node.
41
+ * The function sets the right child of a node and updates the parent reference of the child.
42
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a
43
+ * `NODE` object or `undefined`.
39
44
  */
40
45
  set right(v) {
41
46
  if (v) {
@@ -55,10 +60,10 @@ export class BSTNode extends BinaryTreeNode {
55
60
  */
56
61
  export class BST extends BinaryTree {
57
62
  /**
58
- * This is the constructor function for a binary search tree class in TypeScript, which initializes
59
- * the tree with optional keysOrNodesOrEntries and options.
60
- * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
61
- * binary search tree.
63
+ * This is the constructor function for a TypeScript class that initializes a binary search tree with
64
+ * optional keys or nodes or entries and options.
65
+ * @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
66
+ * to initialize the binary search tree with the provided keys, nodes, or entries.
62
67
  * @param [options] - The `options` parameter is an optional object that can contain additional
63
68
  * configuration options for the binary search tree. It can have the following properties:
64
69
  */
@@ -74,20 +79,28 @@ export class BST extends BinaryTree {
74
79
  this.addMany(keysOrNodesOrEntries);
75
80
  }
76
81
  _root;
82
+ /**
83
+ * The function returns the root node of a tree structure.
84
+ * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
85
+ */
77
86
  get root() {
78
87
  return this._root;
79
88
  }
80
89
  _variant = BSTVariant.STANDARD;
90
+ /**
91
+ * The function returns the value of the _variant property.
92
+ * @returns The value of the `_variant` property.
93
+ */
81
94
  get variant() {
82
95
  return this._variant;
83
96
  }
84
97
  /**
85
- * The function creates a new binary search tree node with the given key and value.
86
- * @param {K} key - The key parameter is the key value that will be associated with
87
- * the new node. It is used to determine the position of the node in the binary search tree.
88
- * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
89
- * represents the value associated with the node in a binary search tree.
90
- * @returns a new instance of the BSTNode class with the specified key and value.
98
+ * The function creates a new BSTNode with the given key and value and returns it.
99
+ * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
100
+ * being created.
101
+ * @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
102
+ * value associated with the key in the node being created.
103
+ * @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
91
104
  */
92
105
  createNode(key, value) {
93
106
  return new BSTNode(key, value);
@@ -95,9 +108,10 @@ export class BST extends BinaryTree {
95
108
  /**
96
109
  * The function creates a new binary search tree with the specified options.
97
110
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
98
- * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
99
- * that defines various options for creating a binary search tree.
100
- * @returns a new instance of the BST class with the specified options.
111
+ * behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
112
+ * partial object of type `BSTOptions<K>`.
113
+ * @returns a new instance of the BST class, with the provided options merged with the default
114
+ * options. The returned value is casted as TREE.
101
115
  */
102
116
  createTree(options) {
103
117
  return new BST([], {
@@ -107,14 +121,14 @@ export class BST extends BinaryTree {
107
121
  });
108
122
  }
109
123
  /**
110
- * The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
124
+ * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
111
125
  * otherwise it returns undefined.
112
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
126
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
113
127
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
114
- * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
115
- * @returns a node of type N or undefined.
128
+ * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
129
+ * @returns a node of type NODE or undefined.
116
130
  */
117
- exemplarToNode(keyOrNodeOrEntry, value) {
131
+ keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
118
132
  let node;
119
133
  if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
120
134
  return;
@@ -142,7 +156,6 @@ export class BST extends BinaryTree {
142
156
  /**
143
157
  * Time Complexity: O(log n)
144
158
  * Space Complexity: O(log n)
145
- * Average case for a balanced tree. Space for the recursive call stack in the worst case.
146
159
  */
147
160
  /**
148
161
  * Time Complexity: O(log n)
@@ -150,11 +163,11 @@ export class BST extends BinaryTree {
150
163
  *
151
164
  * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
152
165
  * otherwise it returns the key itself.
153
- * @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
166
+ * @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
154
167
  * `undefined`.
155
168
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
156
169
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
157
- * @returns either a node object (N) or undefined.
170
+ * @returns either a node object (NODE) or undefined.
158
171
  */
159
172
  ensureNode(keyOrNodeOrEntry, iterationType = IterationType.ITERATIVE) {
160
173
  let res;
@@ -173,7 +186,7 @@ export class BST extends BinaryTree {
173
186
  }
174
187
  /**
175
188
  * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
176
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
189
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
177
190
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
178
191
  */
179
192
  isNode(keyOrNodeOrEntry) {
@@ -182,7 +195,6 @@ export class BST extends BinaryTree {
182
195
  /**
183
196
  * Time Complexity: O(log n)
184
197
  * Space Complexity: O(1)
185
- * - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
186
198
  */
187
199
  /**
188
200
  * Time Complexity: O(log n)
@@ -197,7 +209,7 @@ export class BST extends BinaryTree {
197
209
  * node was not added.
198
210
  */
199
211
  add(keyOrNodeOrEntry, value) {
200
- const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
212
+ const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
201
213
  if (newNode === undefined)
202
214
  return false;
203
215
  if (this.root === undefined) {
@@ -221,7 +233,6 @@ export class BST extends BinaryTree {
221
233
  else if (this._compare(current.key, newNode.key) === CP.gt) {
222
234
  if (current.left === undefined) {
223
235
  current.left = newNode;
224
- newNode.parent = current;
225
236
  this._size++;
226
237
  return true;
227
238
  }
@@ -230,7 +241,6 @@ export class BST extends BinaryTree {
230
241
  else {
231
242
  if (current.right === undefined) {
232
243
  current.right = newNode;
233
- newNode.parent = current;
234
244
  this._size++;
235
245
  return true;
236
246
  }
@@ -241,12 +251,11 @@ export class BST extends BinaryTree {
241
251
  }
242
252
  /**
243
253
  * Time Complexity: O(k log n)
244
- * Space Complexity: O(k)
245
- * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
254
+ * Space Complexity: O(k + log n)
246
255
  */
247
256
  /**
248
257
  * Time Complexity: O(k log n)
249
- * Space Complexity: O(k)
258
+ * Space Complexity: O(k + log n)
250
259
  *
251
260
  * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
252
261
  * balancing the tree after each addition.
@@ -262,7 +271,7 @@ export class BST extends BinaryTree {
262
271
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
263
272
  * type of iteration to use when adding multiple keys or nodes. It has a default value of
264
273
  * `this.iterationType`, which suggests that it is a property of the current object.
265
- * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
274
+ * @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
266
275
  */
267
276
  addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
268
277
  const inserted = [];
@@ -353,7 +362,7 @@ export class BST extends BinaryTree {
353
362
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
354
363
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
355
364
  * values:
356
- * @returns The function `getNodeByKey` returns a node (`N`) if a node with the specified key is
365
+ * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
357
366
  * found in the binary tree. If no node is found, it returns `undefined`.
358
367
  */
359
368
  getNodeByKey(key, iterationType = IterationType.ITERATIVE) {
@@ -389,32 +398,31 @@ export class BST extends BinaryTree {
389
398
  }
390
399
  /**
391
400
  * Time Complexity: O(log n)
392
- * Space Complexity: O(log n)
393
- * Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
401
+ * Space Complexity: O(k + log n)
394
402
  * /
395
403
 
396
404
  /**
397
405
  * Time Complexity: O(log n)
398
- * Space Complexity: O(log n)
406
+ * Space Complexity: O(k + log n)
399
407
  *
400
408
  * The function `getNodes` returns an array of nodes that match a given identifier, using either a
401
409
  * recursive or iterative approach.
402
410
  * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
403
411
  * want to search for in the nodes of the binary tree. It can be of any type that is returned by the
404
412
  * callback function `C`.
405
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
413
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
406
414
  * argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
407
- * function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
415
+ * function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
408
416
  * @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
409
417
  * first node that matches the identifier. If set to true, the function will return an array
410
418
  * containing only the first matching node. If set to false (default), the function will continue
411
419
  * searching for all nodes that match the identifier and return an array containing
412
- * @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
420
+ * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
413
421
  * for the traversal. It can be either a key value or a node object. If it is undefined, the
414
422
  * traversal will start from the root of the tree.
415
423
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
416
424
  * performed on the binary tree. It can have two possible values:
417
- * @returns The method returns an array of nodes (`N[]`).
425
+ * @returns The method returns an array of nodes (`NODE[]`).
418
426
  */
419
427
  getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
420
428
  beginRoot = this.ensureNode(beginRoot);
@@ -472,25 +480,6 @@ export class BST extends BinaryTree {
472
480
  }
473
481
  return ans;
474
482
  }
475
- // /**
476
- // * The function overrides the subTreeTraverse method and returns the result of calling the super
477
- // * method with the provided arguments.
478
- // * @param {C} callback - The `callback` parameter is a function that will be called for each node in
479
- // * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
480
- // * the tree. The return type of the callback function can be any type.
481
- // * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
482
- // * can be either a key, a node, or an entry.
483
- // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
484
- // * be performed during the traversal of the subtree. It can have one of the following values:
485
- // * @returns The method is returning an array of the return type of the callback function.
486
- // */
487
- // override subTreeTraverse<C extends BTNCallback<N>>(
488
- // callback: C = this._defaultOneParamCallback as C,
489
- // beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
490
- // iterationType = this.iterationType
491
- // ): ReturnType<C>[] {
492
- // return super.subTreeTraverse(callback, beginRoot, iterationType, false);
493
- // }
494
483
  /**
495
484
  * Time complexity: O(n)
496
485
  * Space complexity: O(n)
@@ -552,7 +541,7 @@ export class BST extends BinaryTree {
552
541
  * The function overrides the listLevels method and returns an array of arrays containing the return
553
542
  * type of the callback function for each level of the tree.
554
543
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
555
- * `BTNCallback<N>`. It represents a callback function that will be called for each node in the tree
544
+ * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
556
545
  * during the level listing process.
557
546
  * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
558
547
  * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
@@ -567,18 +556,17 @@ export class BST extends BinaryTree {
567
556
  return super.listLevels(callback, beginRoot, iterationType, false);
568
557
  }
569
558
  /**
570
- * Time Complexity: O(n log n)
571
- * Space Complexity: O(n)
572
- * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
559
+ * Time Complexity: O(log n)
560
+ * Space Complexity: O(1)
573
561
  */
574
562
  /**
575
- * Time Complexity: O(n log n)
576
- * Space Complexity: O(n)
563
+ * Time Complexity: O(log n)
564
+ * Space Complexity: O(1)
577
565
  *
578
566
  * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
579
567
  * leftmost node if the comparison result is greater than.
580
- * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
581
- * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
568
+ * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
569
+ * type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
582
570
  * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
583
571
  * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
584
572
  * the key of the leftmost node if the comparison result is greater than, and the key of the
@@ -605,7 +593,6 @@ export class BST extends BinaryTree {
605
593
  /**
606
594
  * Time Complexity: O(log n)
607
595
  * Space Complexity: O(log n)
608
- * Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
609
596
  */
610
597
  /**
611
598
  * Time Complexity: O(log n)
@@ -615,12 +602,12 @@ export class BST extends BinaryTree {
615
602
  * are either lesser or greater than a target node, depending on the specified comparison type.
616
603
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
617
604
  * that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
618
- * parameter of type `N` (the node type) and returns a value of any type.
605
+ * parameter of type `NODE` (the node type) and returns a value of any type.
619
606
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
620
607
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
621
608
  * `CP`, which is a custom type representing the comparison operator. The possible values for
622
609
  * `lesserOrGreater` are
623
- * @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
610
+ * @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
624
611
  * binary tree that you want to traverse from. It can be specified either by its key, by the node
625
612
  * object itself, or it can be left undefined to start the traversal from the root of the tree.
626
613
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -729,8 +716,8 @@ export class BST extends BinaryTree {
729
716
  * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
730
717
  */
731
718
  /**
732
- * Time Complexity: O(n) - Building a balanced tree from a sorted array.
733
- * Space Complexity: O(n) - Additional space is required for the sorted array.
719
+ * Time Complexity: O(n)
720
+ * Space Complexity: O(log n)
734
721
  */
735
722
  /**
736
723
  * Time Complexity: O(n)
@@ -786,6 +773,11 @@ export class BST extends BinaryTree {
786
773
  }
787
774
  return balanced;
788
775
  }
776
+ /**
777
+ * The function sets the root property of an object and updates the parent property of the new root.
778
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
779
+ * can either be an object of type `NODE` or it can be `undefined`.
780
+ */
789
781
  _setRoot(v) {
790
782
  if (v) {
791
783
  v.parent = undefined;