avl-tree-typed 2.0.4 → 2.1.0

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 (104) hide show
  1. package/dist/common/index.js +1 -1
  2. package/dist/constants/index.js +1 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  4. package/dist/data-structures/base/iterable-element-base.js +149 -107
  5. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  6. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  7. package/dist/data-structures/base/linear-base.d.ts +250 -192
  8. package/dist/data-structures/base/linear-base.js +137 -274
  9. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  10. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  11. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  12. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  13. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  14. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  15. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  16. package/dist/data-structures/binary-tree/binary-tree.js +612 -879
  17. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  18. package/dist/data-structures/binary-tree/bst.js +505 -481
  19. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  20. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  21. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  22. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  23. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  24. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  25. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  26. package/dist/data-structures/graph/abstract-graph.js +267 -237
  27. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  28. package/dist/data-structures/graph/directed-graph.js +146 -233
  29. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  30. package/dist/data-structures/graph/map-graph.js +56 -59
  31. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  32. package/dist/data-structures/graph/undirected-graph.js +129 -149
  33. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  34. package/dist/data-structures/hash/hash-map.js +270 -457
  35. package/dist/data-structures/heap/heap.d.ts +214 -289
  36. package/dist/data-structures/heap/heap.js +340 -349
  37. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  38. package/dist/data-structures/heap/max-heap.js +11 -66
  39. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  40. package/dist/data-structures/heap/min-heap.js +11 -66
  41. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  42. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  43. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  44. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  45. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  46. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  47. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  48. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  49. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  50. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  51. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  52. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  53. package/dist/data-structures/queue/deque.d.ts +227 -254
  54. package/dist/data-structures/queue/deque.js +309 -348
  55. package/dist/data-structures/queue/queue.d.ts +180 -201
  56. package/dist/data-structures/queue/queue.js +265 -248
  57. package/dist/data-structures/stack/stack.d.ts +124 -102
  58. package/dist/data-structures/stack/stack.js +181 -125
  59. package/dist/data-structures/trie/trie.d.ts +164 -165
  60. package/dist/data-structures/trie/trie.js +189 -172
  61. package/dist/interfaces/binary-tree.d.ts +56 -6
  62. package/dist/interfaces/graph.d.ts +16 -0
  63. package/dist/types/data-structures/base/base.d.ts +1 -1
  64. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  65. package/dist/types/utils/utils.d.ts +6 -6
  66. package/dist/utils/number.js +1 -2
  67. package/dist/utils/utils.d.ts +110 -49
  68. package/dist/utils/utils.js +149 -74
  69. package/package.json +15 -15
  70. package/src/data-structures/base/iterable-element-base.ts +238 -115
  71. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  72. package/src/data-structures/base/linear-base.ts +271 -277
  73. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  74. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  75. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  76. package/src/data-structures/binary-tree/binary-tree.ts +681 -905
  77. package/src/data-structures/binary-tree/bst.ts +568 -570
  78. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  79. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  80. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  81. package/src/data-structures/graph/abstract-graph.ts +339 -264
  82. package/src/data-structures/graph/directed-graph.ts +146 -236
  83. package/src/data-structures/graph/map-graph.ts +63 -60
  84. package/src/data-structures/graph/undirected-graph.ts +129 -152
  85. package/src/data-structures/hash/hash-map.ts +274 -496
  86. package/src/data-structures/heap/heap.ts +389 -402
  87. package/src/data-structures/heap/max-heap.ts +12 -76
  88. package/src/data-structures/heap/min-heap.ts +13 -76
  89. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  90. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  91. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  92. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  93. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  94. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  95. package/src/data-structures/queue/deque.ts +381 -357
  96. package/src/data-structures/queue/queue.ts +310 -264
  97. package/src/data-structures/stack/stack.ts +217 -131
  98. package/src/data-structures/trie/trie.ts +240 -175
  99. package/src/interfaces/binary-tree.ts +240 -6
  100. package/src/interfaces/graph.ts +37 -0
  101. package/src/types/data-structures/base/base.ts +5 -5
  102. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  103. package/src/types/utils/utils.ts +9 -5
  104. package/src/utils/utils.ts +152 -86
@@ -5,46 +5,119 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeDeleteResult, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, RBTNColor, ToEntryFn } from '../../types';
8
+ import type { BinaryTreeDeleteResult, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, RBTNColor, ToEntryFn } from '../../types';
9
9
  import { IBinaryTree } from '../../interfaces';
10
10
  import { IterableEntryBase } from '../base';
11
11
  import { Range } from '../../common';
12
12
  /**
13
- * Represents a node in a binary tree.
14
- * @template V - The type of data stored in the node.
15
- * @template BinaryTreeNode<K, V> - The type of the family relationship in the binary tree.
13
+ * @template K - The type of the key.
14
+ * @template V - The type of the value.
16
15
  */
17
16
  export declare class BinaryTreeNode<K = any, V = any> {
18
17
  key: K;
19
18
  value?: V;
20
19
  parent?: BinaryTreeNode<K, V>;
21
20
  /**
22
- * The constructor function initializes an object with a key and an optional value in TypeScript.
23
- * @param {K} key - The `key` parameter in the constructor function is used to store the key value
24
- * for the key-value pair.
25
- * @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
26
- * have to be provided when creating an instance of the class. If a `value` is not provided, it will
27
- * default to `undefined`.
21
+ * Creates an instance of BinaryTreeNode.
22
+ * @remarks Time O(1), Space O(1)
23
+ *
24
+ * @param key - The key of the node.
25
+ * @param [value] - The value associated with the key.
28
26
  */
29
27
  constructor(key: K, value?: V);
30
28
  _left?: BinaryTreeNode<K, V> | null | undefined;
29
+ /**
30
+ * Gets the left child of the node.
31
+ * @remarks Time O(1), Space O(1)
32
+ *
33
+ * @returns The left child.
34
+ */
31
35
  get left(): BinaryTreeNode<K, V> | null | undefined;
36
+ /**
37
+ * Sets the left child of the node and updates its parent reference.
38
+ * @remarks Time O(1), Space O(1)
39
+ *
40
+ * @param v - The node to set as the left child.
41
+ */
32
42
  set left(v: BinaryTreeNode<K, V> | null | undefined);
33
43
  _right?: BinaryTreeNode<K, V> | null | undefined;
44
+ /**
45
+ * Gets the right child of the node.
46
+ * @remarks Time O(1), Space O(1)
47
+ *
48
+ * @returns The right child.
49
+ */
34
50
  get right(): BinaryTreeNode<K, V> | null | undefined;
51
+ /**
52
+ * Sets the right child of the node and updates its parent reference.
53
+ * @remarks Time O(1), Space O(1)
54
+ *
55
+ * @param v - The node to set as the right child.
56
+ */
35
57
  set right(v: BinaryTreeNode<K, V> | null | undefined);
36
58
  _height: number;
59
+ /**
60
+ * Gets the height of the node (used in self-balancing trees).
61
+ * @remarks Time O(1), Space O(1)
62
+ *
63
+ * @returns The height.
64
+ */
37
65
  get height(): number;
66
+ /**
67
+ * Sets the height of the node.
68
+ * @remarks Time O(1), Space O(1)
69
+ *
70
+ * @param value - The new height.
71
+ */
38
72
  set height(value: number);
39
73
  _color: RBTNColor;
74
+ /**
75
+ * Gets the color of the node (used in Red-Black trees).
76
+ * @remarks Time O(1), Space O(1)
77
+ *
78
+ * @returns The node's color.
79
+ */
40
80
  get color(): RBTNColor;
81
+ /**
82
+ * Sets the color of the node.
83
+ * @remarks Time O(1), Space O(1)
84
+ *
85
+ * @param value - The new color.
86
+ */
41
87
  set color(value: RBTNColor);
42
88
  _count: number;
89
+ /**
90
+ * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
91
+ * @remarks Time O(1), Space O(1)
92
+ *
93
+ * @returns The subtree node count.
94
+ */
43
95
  get count(): number;
96
+ /**
97
+ * Sets the count of nodes in the subtree.
98
+ * @remarks Time O(1), Space O(1)
99
+ *
100
+ * @param value - The new count.
101
+ */
44
102
  set count(value: number);
103
+ /**
104
+ * Gets the position of the node relative to its parent.
105
+ * @remarks Time O(1), Space O(1)
106
+ *
107
+ * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
108
+ */
45
109
  get familyPosition(): FamilyPosition;
46
110
  }
47
111
  /**
112
+ * A general Binary Tree implementation.
113
+ *
114
+ * @remarks
115
+ * This class implements a basic Binary Tree, not a Binary Search Tree.
116
+ * The `add` operation inserts nodes level-by-level (BFS) into the first available slot.
117
+ *
118
+ * @template K - The type of the key.
119
+ * @template V - The type of the value.
120
+ * @template R - The type of the raw data object (if using `toEntryFn`).
48
121
  * 1. Two Children Maximum: Each node has at most two children.
49
122
  * 2. Left and Right Children: Nodes have distinct left and right children.
50
123
  * 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
@@ -113,536 +186,368 @@ export declare class BinaryTreeNode<K = any, V = any> {
113
186
  *
114
187
  * console.log(evaluate(expressionTree.root)); // -27
115
188
  */
116
- export declare class BinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R, MK, MV, MR> {
189
+ export declare class BinaryTree<K = any, V = any, R extends object = object> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R> {
117
190
  iterationType: IterationType;
118
191
  /**
119
- * This TypeScript constructor function initializes a binary tree with optional options and adds
120
- * elements based on the provided input.
121
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
122
- * iterable that can contain either objects of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
123
- * is used to initialize the binary tree with keys, nodes, entries, or raw data.
124
- * @param [options] - The `options` parameter in the constructor is an optional object that can
125
- * contain the following properties:
192
+ * Creates an instance of BinaryTree.
193
+ * @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `add` operation). Space O(N) for storing the nodes.
194
+ *
195
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
196
+ * @param [options] - Configuration options for the tree.
126
197
  */
127
198
  constructor(keysNodesEntriesOrRaws?: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BinaryTreeOptions<K, V, R>);
128
199
  protected _isMapMode: boolean;
200
+ /**
201
+ * Gets whether the tree is in Map mode.
202
+ * @remarks In Map mode (default), values are stored in an external Map, and nodes only hold keys. If false, values are stored directly on the nodes. Time O(1)
203
+ *
204
+ * @returns True if in Map mode, false otherwise.
205
+ */
129
206
  get isMapMode(): boolean;
130
207
  protected _isDuplicate: boolean;
208
+ /**
209
+ * Gets whether the tree allows duplicate keys.
210
+ * @remarks Time O(1)
211
+ *
212
+ * @returns True if duplicates are allowed, false otherwise.
213
+ */
131
214
  get isDuplicate(): boolean;
132
215
  protected _store: Map<K, V | undefined>;
216
+ /**
217
+ * Gets the external value store (used in Map mode).
218
+ * @remarks Time O(1)
219
+ *
220
+ * @returns The map storing key-value pairs.
221
+ */
133
222
  get store(): Map<K, V | undefined>;
134
223
  protected _root?: BinaryTreeNode<K, V> | null | undefined;
224
+ /**
225
+ * Gets the root node of the tree.
226
+ * @remarks Time O(1)
227
+ *
228
+ * @returns The root node.
229
+ */
135
230
  get root(): BinaryTreeNode<K, V> | null | undefined;
136
231
  protected _size: number;
232
+ /**
233
+ * Gets the number of nodes in the tree.
234
+ * @remarks Time O(1)
235
+ *
236
+ * @returns The size of the tree.
237
+ */
137
238
  get size(): number;
138
239
  protected _NIL: BinaryTreeNode<K, V>;
240
+ /**
241
+ * Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
242
+ * @remarks Time O(1)
243
+ *
244
+ * @returns The NIL node.
245
+ */
139
246
  get NIL(): BinaryTreeNode<K, V>;
140
247
  protected _toEntryFn?: ToEntryFn<K, V, R>;
248
+ /**
249
+ * Gets the function used to convert raw data objects (R) into [key, value] entries.
250
+ * @remarks Time O(1)
251
+ *
252
+ * @returns The conversion function.
253
+ */
141
254
  get toEntryFn(): ToEntryFn<K, V, R> | undefined;
142
255
  /**
143
- * Time Complexity: O(1)
144
- * Space Complexity: O(1)
145
- *
146
- * The function creates a new binary tree node with a specified key and optional value.
147
- * @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
148
- * @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
149
- * not required to be provided when calling the function. If a `value` is provided, it should be of
150
- * type `V`, which is the type of the value associated with the node.
151
- * @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
152
- * as BinaryTreeNode<K, V>.
153
- */
154
- createNode(key: K, value?: V): BinaryTreeNode<K, V>;
155
- /**
156
- * Time Complexity: O(1)
157
- * Space Complexity: O(1)
158
- *
159
- * The function creates a binary tree with the specified options.
160
- * @param [options] - The `options` parameter in the `createTree` function is an optional parameter
161
- * that allows you to provide partial configuration options for creating a binary tree. It is of type
162
- * `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
163
- * of properties
164
- * @returns A new instance of a binary tree with the specified options is being returned.
165
- */
166
- createTree(options?: BinaryTreeOptions<K, V, R>): BinaryTree<K, V, R, MK, MV, MR>;
167
- /**
168
- * Time Complexity: O(n)
169
- * Space Complexity: O(log n)
170
- *
171
- * The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
172
- * value and returns the corresponding node or null.
173
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
174
- * parameter in the `ensureNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
175
- * is used to determine whether the input is a key, node, entry, or raw data. The
176
- * @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
177
- * is used to specify the type of iteration to be performed. It has a default value of
178
- * `this.iterationType` if not explicitly provided.
179
- * @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
180
- * conditions specified in the code snippet.
256
+ * (Protected) Creates a new node.
257
+ * @remarks Time O(1), Space O(1)
258
+ *
259
+ * @param key - The key for the new node.
260
+ * @param [value] - The value for the new node (used if not in Map mode).
261
+ * @returns The newly created node.
262
+ */
263
+ _createNode(key: K, value?: V): BinaryTreeNode<K, V>;
264
+ /**
265
+ * Creates a new, empty tree of the same type and configuration.
266
+ * @remarks Time O(1) (excluding options cloning), Space O(1)
267
+ *
268
+ * @param [options] - Optional overrides for the new tree's options.
269
+ * @returns A new, empty tree instance.
270
+ */
271
+ createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): this;
272
+ /**
273
+ * Ensures the input is a node. If it's a key or entry, it searches for the node.
274
+ * @remarks Time O(1) if a node is passed. O(N) if a key or entry is passed (due to `getNode` performing a full search). Space O(1) if iterative search, O(H) if recursive (where H is height, O(N) worst-case).
275
+ *
276
+ * @param keyNodeOrEntry - The item to resolve to a node.
277
+ * @param [iterationType=this.iterationType] - The traversal method to use if searching.
278
+ * @returns The resolved node, or null/undefined if not found or input is null/undefined.
181
279
  */
182
280
  ensureNode(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V> | null | undefined;
183
281
  /**
184
- * Time Complexity: O(1)
185
- * Space Complexity: O(1)
282
+ * Checks if the given item is a `BinaryTreeNode` instance.
283
+ * @remarks Time O(1), Space O(1)
186
284
  *
187
- * The function isNode checks if the input is an instance of BinaryTreeNode.
188
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
189
- * `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is
190
- * checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
191
- * accordingly.
192
- * @returns The function `isNode` is checking if the input `keyNodeOrEntry` is an instance of
193
- * `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
194
- * it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
195
- * is not a node.
285
+ * @param keyNodeOrEntry - The item to check.
286
+ * @returns True if it's a node, false otherwise.
196
287
  */
197
288
  isNode(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BinaryTreeNode<K, V>;
198
289
  /**
199
- * Time Complexity: O(1)
200
- * Space Complexity: O(1)
290
+ * Checks if the given item is a raw data object (R) that needs conversion via `toEntryFn`.
291
+ * @remarks Time O(1), Space O(1)
201
292
  *
202
- * The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
203
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R} keyNodeEntryOrRaw - K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
204
- * @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
205
- * checking if it is an object. If the parameter is an object, the function will return `true`,
206
- * indicating that it is of type `R`.
293
+ * @param keyNodeEntryOrRaw - The item to check.
294
+ * @returns True if it's a raw object, false otherwise.
207
295
  */
208
296
  isRaw(keyNodeEntryOrRaw: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R): keyNodeEntryOrRaw is R;
209
297
  /**
210
- * Time Complexity: O(1)
211
- * Space Complexity: O(1)
298
+ * Checks if the given item is a "real" node (i.e., not null, undefined, or NIL).
299
+ * @remarks Time O(1), Space O(1)
212
300
  *
213
- * The function `isRealNode` checks if a given input is a valid node in a binary tree.
214
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
215
- * parameter in the `isRealNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
216
- * The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal
217
- * @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid
218
- * node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
219
- * values, it then calls the `isNode` method to further determine if the input is a node. The
220
- * function will return a boolean value indicating whether the
301
+ * @param keyNodeOrEntry - The item to check.
302
+ * @returns True if it's a real node, false otherwise.
221
303
  */
222
304
  isRealNode(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BinaryTreeNode<K, V>;
223
305
  /**
224
- * Time Complexity: O(1)
225
- * Space Complexity: O(1)
306
+ * Checks if the given item is either a "real" node or null.
307
+ * @remarks Time O(1), Space O(1)
226
308
  *
227
- * The function checks if a given input is a valid node or null.
228
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
229
- * `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
230
- * V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or
231
- * @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
232
- * `keyNodeOrEntry` is either `null` or a real node, and returns `true` if it is a node or
233
- * `null`, and `false` otherwise.
309
+ * @param keyNodeOrEntry - The item to check.
310
+ * @returns True if it's a real node or null, false otherwise.
234
311
  */
235
312
  isRealNodeOrNull(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BinaryTreeNode<K, V> | null;
236
313
  /**
237
- * Time Complexity: O(1)
238
- * Space Complexity: O(1)
314
+ * Checks if the given item is the sentinel NIL node.
315
+ * @remarks Time O(1), Space O(1)
239
316
  *
240
- * The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
241
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - BTNRep<K, V,
242
- * BinaryTreeNode<K, V>>
243
- * @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL`
244
- * property of the current object and returning a boolean value based on that comparison.
317
+ * @param keyNodeOrEntry - The item to check.
318
+ * @returns True if it's the NIL node, false otherwise.
245
319
  */
246
320
  isNIL(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
247
321
  /**
248
- * Time Complexity: O(1)
249
- * Space Complexity: O(1)
322
+ * Checks if the given item is a `Range` object.
323
+ * @remarks Time O(1), Space O(1)
250
324
  *
251
- * The function `isRange` checks if the input parameter is an instance of the `Range` class.
252
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>} keyNodeEntryOrPredicate
253
- * - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
254
- * of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `NodePredicate<BinaryTreeNode<K, V>>`, or
255
- * `Range<K>`. The function checks if the `keyNodeEntry
256
- * @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an
257
- * instance of the `Range` class. If it is an instance of `Range`, the function will return `true`,
258
- * indicating that the parameter is a `Range<K>`. If it is not an instance of `Range`, the function
259
- * will return `false`.
325
+ * @param keyNodeEntryOrPredicate - The item to check.
326
+ * @returns True if it's a Range, false otherwise.
260
327
  */
261
328
  isRange(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>): keyNodeEntryOrPredicate is Range<K>;
262
329
  /**
263
- * Time Complexity: O(1)
264
- * Space Complexity: O(1)
330
+ * Checks if a node is a leaf (has no real children).
331
+ * @remarks Time O(N) if a key/entry is passed (due to `ensureNode`). O(1) if a node is passed. Space O(1) or O(H) (from `ensureNode`).
265
332
  *
266
- * The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
267
- * tree.
268
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
269
- * `keyNodeOrEntry` can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It represents a
270
- * key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
271
- * provided
272
- * @returns The function `isLeaf` returns a boolean value indicating whether the input
273
- * `keyNodeOrEntry` is a leaf node in a binary tree.
333
+ * @param keyNodeOrEntry - The node to check.
334
+ * @returns True if the node is a leaf, false otherwise.
274
335
  */
275
336
  isLeaf(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
276
337
  /**
277
- * Time Complexity: O(1)
278
- * Space Complexity: O(1)
338
+ * Checks if the given item is a [key, value] entry pair.
339
+ * @remarks Time O(1), Space O(1)
279
340
  *
280
- * The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
281
- * with a length of 2.
282
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
283
- * parameter in the `isEntry` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or type `R`.
284
- * The function checks if the provided `keyNodeOrEntry` is of type `BTN
285
- * @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array
286
- * with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
287
- * `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
341
+ * @param keyNodeOrEntry - The item to check.
342
+ * @returns True if it's an entry, false otherwise.
288
343
  */
289
344
  isEntry(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BTNEntry<K, V>;
290
345
  /**
291
- * Time Complexity O(1)
292
- * Space Complexity O(1)
346
+ * Checks if the given key is valid (comparable or null).
347
+ * @remarks Time O(1), Space O(1)
293
348
  *
294
- * The function `isValidKey` checks if a given key is comparable.
295
- * @param {any} key - The `key` parameter is of type `any`, which means it can be any data type in
296
- * TypeScript.
297
- * @returns The function `isValidKey` is checking if the `key` parameter is `null` or if it is comparable.
298
- * If the `key` is `null`, the function returns `true`. Otherwise, it returns the result of the
299
- * `isComparable` function, which is not provided in the code snippet.
349
+ * @param key - The key to validate.
350
+ * @returns True if the key is valid, false otherwise.
300
351
  */
301
352
  isValidKey(key: any): key is K;
302
353
  /**
303
- * Time Complexity O(n)
304
- * Space Complexity O(1)
305
- *
306
- * The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
307
- * and finding the correct insertion position.
308
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `add` method you provided
309
- * seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry`
310
- * parameter in the method can accept different types of values:
311
- * @param {V} [value] - The `value` parameter in the `add` method represents the value associated
312
- * with the key that you want to add to the binary tree. When adding a key-value pair to the binary
313
- * tree, you provide the key and its corresponding value. The `add` method then creates a new node
314
- * with this
315
- * @returns The `add` method returns a boolean value. It returns `true` if the insertion of the new
316
- * node was successful, and `false` if the insertion position could not be found or if a duplicate
317
- * key was found and the node was replaced instead of inserted.
354
+ * Adds a new node to the tree.
355
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
356
+ *
357
+ * @param keyNodeOrEntry - The key, node, or entry to add.
358
+ * @param [value] - The value, if providing just a key.
359
+ * @returns True if the addition was successful, false otherwise.
318
360
  */
319
361
  add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
320
362
  /**
321
- * Time Complexity: O(k * n)
322
- * Space Complexity: O(k)
323
- *
324
- * The `addMany` function takes in multiple keys or nodes or entries or raw values along with
325
- * optional values, and adds them to a data structure while returning an array indicating whether
326
- * each insertion was successful.
327
- * @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
328
- * mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
329
- * `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
330
- * @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
331
- * accepts an iterable of values. These values correspond to the keys or nodes being added in the
332
- * `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
333
- * assign them
334
- * @returns The `addMany` method returns an array of boolean values indicating whether each key,
335
- * node, entry, or raw value was successfully added to the data structure. Each boolean value
336
- * corresponds to the success of adding the corresponding key or value in the input iterable.
363
+ * Adds multiple items to the tree.
364
+ * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
365
+ *
366
+ * @param keysNodesEntriesOrRaws - An iterable of items to add.
367
+ * @param [values] - An optional parallel iterable of values.
368
+ * @returns An array of booleans indicating the success of each individual `add` operation.
337
369
  */
338
370
  addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
339
371
  /**
340
- * Time Complexity: O(k * n)
341
- * Space Complexity: O(1)
372
+ * Merges another tree into this one by adding all its nodes.
373
+ * @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
342
374
  *
343
- * The `merge` function in TypeScript merges another binary tree into the current tree by adding all
344
- * elements from the other tree.
345
- * @param anotherTree - BinaryTree<K, V, R, MK, MV, MR>
375
+ * @param anotherTree - The tree to merge.
346
376
  */
347
- merge(anotherTree: BinaryTree<K, V, R, MK, MV, MR>): void;
377
+ merge(anotherTree: BinaryTree<K, V, R>): void;
348
378
  /**
349
- * Time Complexity: O(k * n)
350
- * Space Complexity: O(1)
379
+ * Clears the tree and refills it with new items.
380
+ * @remarks Time O(N) (for `clear`) + O(N * M) (for `addMany`) = O(N * M). Space O(M) (from `addMany`).
351
381
  *
352
- * The `refill` function clears the existing data structure and then adds new key-value pairs based
353
- * on the provided input.
354
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
355
- * method can accept an iterable containing a mix of `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R`
356
- * objects.
357
- * @param [values] - The `values` parameter in the `refill` method is an optional parameter that
358
- * accepts an iterable of values of type `V` or `undefined`.
382
+ * @param keysNodesEntriesOrRaws - An iterable of items to add.
383
+ * @param [values] - An optional parallel iterable of values.
359
384
  */
360
385
  refill(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): void;
361
386
  /**
362
- * Time Complexity: O(n)
363
- * Space Complexity: O(1)
387
+ * Deletes a node from the tree.
388
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
364
389
  *
365
- * The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
366
- * the deleted node along with information for tree balancing.
367
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry
368
- * - The `delete` method you provided is used to delete a node from a binary tree based on the key,
369
- * node, entry or raw data. The method returns an array of
370
- * `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
371
- * balancing is needed.
372
- * @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
373
- * the array contains information about the node that was deleted (`deleted`) and the node that may
374
- * need to be balanced (`needBalanced`).
390
+ * @param keyNodeOrEntry - The node to delete.
391
+ * @returns An array containing deletion results (for compatibility with self-balancing trees).
375
392
  */
376
393
  delete(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
377
394
  /**
378
- * Time Complexity: O(n)
379
- * Space Complexity: O(k + log n)
380
- *
381
- * The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
382
- * structure based on a given predicate or key, with options to return multiple results or just one.
383
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
384
- * `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values:
385
- * @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
386
- * determines whether the search should stop after finding the first matching node. If `onlyOne` is
387
- * set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
388
- * @param {C} callback - The `callback` parameter in the `search` function is a callback function
389
- * that will be called on each node that matches the search criteria. It is of type `C`, which
390
- * extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
391
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `search` function is
392
- * used to specify the node from which the search operation should begin. It represents the starting
393
- * point in the binary tree where the search will be performed. If no specific `startNode` is
394
- * provided, the search operation will start from the root
395
- * @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
396
- * specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
397
- * two possible values:
398
- * @returns The `search` function returns an array of values that match the provided criteria based
399
- * on the search algorithm implemented within the function.
395
+ * Searches the tree for nodes matching a predicate.
396
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Performs a full DFS (pre-order) scan of the tree. Time O(N), as it may visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
397
+ *
398
+ * @template C - The type of the callback function.
399
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
400
+ * @param [onlyOne=false] - If true, stops after finding the first match.
401
+ * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
402
+ * @param [startNode=this._root] - The node to start the search from.
403
+ * @param [iterationType=this.iterationType] - Whether to use 'RECURSIVE' or 'ITERATIVE' search.
404
+ * @returns An array of results from the callback function for each matching node.
400
405
  */
401
406
  search<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean, callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
407
+ /**
408
+ * Gets all nodes matching a predicate.
409
+ * @remarks Time O(N) (via `search`). Space O(H) or O(N) (via `search`).
410
+ *
411
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
412
+ * @param [onlyOne=false] - If true, stops after finding the first match.
413
+ * @param [startNode=this._root] - The node to start the search from.
414
+ * @param [iterationType=this.iterationType] - The traversal method.
415
+ * @returns An array of matching nodes.
416
+ */
402
417
  getNodes(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V>[];
403
418
  /**
404
- * Time Complexity: O(n)
405
- * Space Complexity: O(log n)
406
- *
407
- * The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
408
- * predicate.
409
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
410
- * - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key,
411
- * node, entry, raw data, or a predicate function.
412
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
413
- * `getNode` function is used to specify the starting point for searching for a node in a binary
414
- * tree. If no specific starting point is provided, the default value is set to `this._root`, which
415
- * is typically the root node of the binary tree.
416
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is
417
- * used to specify the type of iteration to be performed when searching for a node. It has a default
418
- * value of `this.iterationType`, which means it will use the iteration type defined in the current
419
- * context if no specific value is provided
420
- * @returns The `getNode` function is returning the first node that matches the specified criteria,
421
- * or `null` if no matching node is found.
419
+ * Gets the first node matching a predicate.
420
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
421
+ *
422
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
423
+ * @param [startNode=this._root] - The node to start the search from.
424
+ * @param [iterationType=this.iterationType] - The traversal method.
425
+ * @returns The first matching node, or undefined if not found.
422
426
  */
423
427
  getNode(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V> | null | undefined;
424
428
  /**
425
- * Time Complexity: O(n)
426
- * Space Complexity: O(log n)
427
- *
428
- * This function overrides the `get` method to retrieve the value associated with a specified key,
429
- * node, entry, raw data, or predicate in a data structure.
430
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
431
- * - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the
432
- * following types:
433
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `get`
434
- * method is used to specify the starting point for searching for a key or node in the binary tree.
435
- * If no specific starting point is provided, the default starting point is the root of the binary
436
- * tree (`this._root`).
437
- * @param {IterationType} iterationType - The `iterationType` parameter in the `get` method is used
438
- * to specify the type of iteration to be performed when searching for a key in the binary tree. It
439
- * is an optional parameter with a default value of `this.iterationType`, which means it will use the
440
- * iteration type defined in the
441
- * @returns The `get` method is returning the value associated with the specified key, node, entry,
442
- * raw data, or predicate in the binary tree map. If the specified key or node is found in the tree,
443
- * the method returns the corresponding value. If the key or node is not found, it returns
444
- * `undefined`.
429
+ * Gets the value associated with a key.
430
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(1) if in Map mode. O(N) if not in Map mode (uses `getNode`). Space O(1) if in Map mode. O(H) or O(N) otherwise.
431
+ *
432
+ * @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
433
+ * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
434
+ * @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
435
+ * @returns The associated value, or undefined.
445
436
  */
446
437
  get(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): V | undefined;
447
- has(keyNodeEntryOrPredicate?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
448
438
  /**
449
- * Time Complexity: O(1)
450
- * Space Complexity: O(1)
439
+ * Checks if a node matching the predicate exists in the tree.
440
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
451
441
  *
452
- * The clear function removes nodes and values in map mode.
442
+ * @param [keyNodeEntryOrPredicate] - The key, node, entry, or predicate to check for.
443
+ * @param [startNode] - The node to start the search from.
444
+ * @param [iterationType] - The traversal method.
445
+ * @returns True if a matching node exists, false otherwise.
446
+ */
447
+ has(keyNodeEntryOrPredicate?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
448
+ /**
449
+ * Clears the tree of all nodes and values.
450
+ * @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
453
451
  */
454
452
  clear(): void;
455
453
  /**
456
- * Time Complexity: O(1)
457
- * Space Complexity: O(1)
454
+ * Checks if the tree is empty.
455
+ * @remarks Time O(1), Space O(1)
458
456
  *
459
- * The `isEmpty` function in TypeScript checks if a data structure has no elements and returns a
460
- * boolean value.
461
- * @returns The `isEmpty()` method is returning a boolean value, specifically `true` if the `_size`
462
- * property is equal to 0, indicating that the data structure is empty, and `false` otherwise.
457
+ * @returns True if the tree has no nodes, false otherwise.
463
458
  */
464
459
  isEmpty(): boolean;
465
460
  /**
466
- * Time Complexity: O(n)
467
- * Space Complexity: O(log n)
461
+ * Checks if the tree is perfectly balanced.
462
+ * @remarks A tree is perfectly balanced if the difference between min and max height is at most 1. Time O(N), as it requires two full traversals (`getMinHeight` and `getHeight`). Space O(H) or O(N) (from height calculation).
468
463
  *
469
- * The function checks if a binary tree is perfectly balanced by comparing its minimum height with
470
- * its height.
471
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
472
- * point for checking if the binary tree is perfectly balanced. It represents the root node of the
473
- * binary tree or a specific node from which the balance check should begin.
474
- * @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
475
- * the tree starting from the `startNode` node is perfectly balanced or not. The return value is
476
- * determined by comparing the minimum height of the tree with the height of the tree. If the minimum
477
- * height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
478
- * balanced and
464
+ * @param [startNode=this._root] - The node to start checking from.
465
+ * @returns True if perfectly balanced, false otherwise.
479
466
  */
480
467
  isPerfectlyBalanced(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
481
468
  /**
482
- * Time Complexity: O(n)
483
- * Space Complexity: O(log n)
484
- *
485
- * The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
486
- * or iterative methods.
487
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `isBST`
488
- * function represents the starting point for checking whether a binary search tree (BST) is valid.
489
- * It can be a node in the BST or a reference to the root of the BST. If no specific node is
490
- * provided, the function will default to
491
- * @param {IterationType} iterationType - The `iterationType` parameter in the `isBST` function
492
- * determines whether the function should use a recursive approach or an iterative approach to check
493
- * if the binary search tree (BST) is valid.
494
- * @returns The `isBST` method is returning a boolean value, which indicates whether the binary
495
- * search tree (BST) represented by the given root node is a valid BST or not. The method checks if
496
- * the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
497
- * less than the node's key, and all nodes in its right subtree have keys greater than the node's
469
+ * Checks if the tree is a valid Binary Search Tree (BST).
470
+ * @remarks Time O(N), as it must visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
471
+ *
472
+ * @param [startNode=this._root] - The node to start checking from.
473
+ * @param [iterationType=this.iterationType] - The traversal method.
474
+ * @returns True if it's a valid BST, false otherwise.
498
475
  */
499
476
  isBST(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
500
477
  /**
501
- * Time Complexity: O(n)
502
- * Space Complexity: O(log n)
503
- *
504
- * The `getDepth` function calculates the depth between two nodes in a binary tree.
505
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } dist - The `dist` parameter in the `getDepth`
506
- * function represents the node or entry in a binary tree map, or a reference to a node in the tree.
507
- * It is the target node for which you want to calculate the depth from the `startNode` node.
508
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
509
- * `getDepth` function represents the starting point from which you want to calculate the depth of a
510
- * given node or entry in a binary tree. If no specific starting point is provided, the default value
511
- * for `startNode` is set to the root of the binary
512
- * @returns The `getDepth` method returns the depth of a given node `dist` relative to the
513
- * `startNode` node in a binary tree. If the `dist` node is not found in the path to the `startNode`
514
- * node, it returns the depth of the `dist` node from the root of the tree.
478
+ * Gets the depth of a node (distance from `startNode`).
479
+ * @remarks Time O(H), where H is the depth of the `dist` node relative to `startNode`. O(N) worst-case. Space O(1).
480
+ *
481
+ * @param dist - The node to find the depth of.
482
+ * @param [startNode=this._root] - The node to measure depth from (defaults to root).
483
+ * @returns The depth (0 if `dist` is `startNode`).
515
484
  */
516
485
  getDepth(dist: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): number;
517
486
  /**
518
- * Time Complexity: O(n)
519
- * Space Complexity: O(log n)
520
- *
521
- * The `getHeight` function calculates the maximum height of a binary tree using either a recursive
522
- * or iterative approach in TypeScript.
523
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
524
- * point from which the height of the binary tree will be calculated. It can be a node in the binary
525
- * tree or a reference to the root of the tree. If not provided, it defaults to the root of the
526
- * binary tree data structure.
527
- * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
528
- * of iteration to be performed while calculating the height of the binary tree. It can have two
529
- * possible values:
530
- * @returns The `getHeight` method returns the height of the binary tree starting from the specified
531
- * root node. The height is calculated based on the maximum depth of the tree, considering either a
532
- * recursive approach or an iterative approach depending on the `iterationType` parameter.
487
+ * Gets the maximum height of the tree (longest path from startNode to a leaf).
488
+ * @remarks Time O(N), as it must visit every node. Space O(H) for recursive stack (O(N) worst-case) or O(N) for iterative stack (storing node + depth).
489
+ *
490
+ * @param [startNode=this._root] - The node to start measuring from.
491
+ * @param [iterationType=this.iterationType] - The traversal method.
492
+ * @returns The height ( -1 for an empty tree, 0 for a single-node tree).
533
493
  */
534
494
  getHeight(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): number;
535
495
  /**
536
- * Time Complexity: O(n)
537
- * Space Complexity: O(log n)
538
- *
539
- * The `getMinHeight` function calculates the minimum height of a binary tree using either a
540
- * recursive or iterative approach in TypeScript.
541
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
542
- * `getMinHeight` function represents the starting node from which the minimum height of the binary
543
- * tree will be calculated. It is either a node in the binary tree or a reference to the root of the
544
- * tree. If not provided, the default value is the root
545
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getMinHeight` method
546
- * specifies the type of iteration to use when calculating the minimum height of a binary tree. It
547
- * can have two possible values:
548
- * @returns The `getMinHeight` method returns the minimum height of the binary tree starting from the
549
- * specified root node. The height is calculated based on the shortest path from the root node to a
550
- * leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
551
- * a stack) based on the `iterationType` parameter.
496
+ * Gets the minimum height of the tree (shortest path from startNode to a leaf).
497
+ * @remarks Time O(N), as it must visit every node. Space O(H) for recursive stack (O(N) worst-case) or O(N) for iterative (due to `depths` Map).
498
+ *
499
+ * @param [startNode=this._root] - The node to start measuring from.
500
+ * @param [iterationType=this.iterationType] - The traversal method.
501
+ * @returns The minimum height (-1 for empty, 0 for single node).
552
502
  */
553
503
  getMinHeight(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): number;
554
504
  /**
555
- * Time Complexity: O(log n)
556
- * Space Complexity: O(log n)
557
- *
558
- * The function `getPathToRoot` in TypeScript retrieves the path from a given node to the root of a
559
- * tree structure, applying a specified callback function along the way.
560
- * @param {C} callback - The `callback` parameter is a function that is used to process each node in
561
- * the path to the root. It is expected to be a function that takes a node as an argument and returns
562
- * a value based on that node. The return type of the callback function is determined by the generic
563
- * type `C
564
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } beginNode - The `beginNode` parameter in the
565
- * `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
566
- * @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
567
- * whether the resulting path from the given `beginNode` to the root should be in reverse order or
568
- * not. If `isReverse` is set to `true`, the path will be reversed before being returned. If `is
569
- * @returns The function `getPathToRoot` returns an array of the return values of the callback
570
- * function `callback` applied to each node in the path from the `beginNode` to the root node. The
571
- * array is either in reverse order or in the original order based on the value of the `isReverse`
572
- * parameter.
573
- */
574
- getPathToRoot<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(beginNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, callback?: C, isReverse?: boolean): ReturnType<C>[];
575
- /**
576
- * Time Complexity: O(log n)
577
- * Space Complexity: O(log n)
578
- *
579
- * The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
580
- * tail-recursive iteration.
581
- * @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
582
- * node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
583
- * value of `_DEFAULT_NODE_CALLBACK` if not specified.
584
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
585
- * `getLeftMost` function represents the starting point for finding the leftmost node in a binary
586
- * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
587
- * starting point is provided, the function will default
588
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getLeftMost` function
589
- * specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
590
- * possible values:
591
- * @returns The `getLeftMost` function returns the result of the callback function `C` applied to the
592
- * leftmost node in the binary tree starting from the `startNode` node. If the `startNode` node is
593
- * `NIL`, it returns the result of the callback function applied to `undefined`. If the `startNode`
594
- * node is not a real node, it returns the result of the callback
595
- */
596
- getLeftMost<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>;
597
- /**
598
- * Time Complexity: O(log n)
599
- * Space Complexity: O(log n)
600
- *
601
- * The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
602
- * or iterative traversal methods.
603
- * @param {C} callback - The `callback` parameter is a function that will be called with the result
604
- * of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`,
605
- * which means it is a callback function that can accept either an optional binary tree node or null
606
- * as
607
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
608
- * `getRightMost` function represents the starting point for finding the rightmost node in a binary
609
- * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
610
- * starting point is provided, the function will default
611
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getRightMost`
612
- * function specifies the type of iteration to be used when traversing the binary tree nodes. It can
613
- * have two possible values:
614
- * @returns The `getRightMost` function returns the result of the callback function `C`, which is
615
- * passed as a parameter to the function. The callback function is called with the rightmost node in
616
- * the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
617
- * other).
618
- */
619
- getRightMost<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>;
620
- /**
621
- * Time Complexity: O(log n)
622
- * Space Complexity: O(log n)
623
- *
624
- * The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
625
- * binary tree.
626
- * @param {BinaryTreeNode<K, V>} node - The `getPredecessor` function you provided seems to be attempting to find the
627
- * predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
628
- * while loop condition that might cause an infinite loop.
629
- * @returns The `getPredecessor` function returns the predecessor node of the input `BinaryTreeNode<K, V>` parameter.
630
- * If the left child of the input node exists, it traverses to the rightmost node of the left subtree
631
- * to find the predecessor. If the left child does not exist, it returns the input node itself.
505
+ * Gets the path from a given node up to the root.
506
+ * @remarks Time O(H), where H is the depth of the `beginNode`. O(N) worst-case. Space O(H) for the result array.
507
+ *
508
+ * @template C - The type of the callback function.
509
+ * @param beginNode - The node to start the path from.
510
+ * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on each node in the path.
511
+ * @param [isReverse=false] - If true, returns the path from root-to-node.
512
+ * @returns An array of callback results.
513
+ */
514
+ getPathToRoot<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(beginNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, callback?: C, isReverse?: boolean): ReturnType<C>[];
515
+ /**
516
+ * Finds the leftmost node in a subtree (the node with the smallest key in a BST).
517
+ * @remarks Time O(H), where H is the height of the left spine. O(N) worst-case. Space O(H) for recursive/trampoline stack.
518
+ *
519
+ * @template C - The type of the callback function.
520
+ * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on the leftmost node.
521
+ * @param [startNode=this._root] - The subtree root to search from.
522
+ * @param [iterationType=this.iterationType] - The traversal method.
523
+ * @returns The callback result for the leftmost node.
524
+ */
525
+ getLeftMost<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>;
526
+ /**
527
+ * Finds the rightmost node in a subtree (the node with the largest key in a BST).
528
+ * @remarks Time O(H), where H is the height of the right spine. O(N) worst-case. Space O(H) for recursive/trampoline stack.
529
+ *
530
+ * @template C - The type of the callback function.
531
+ * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on the rightmost node.
532
+ * @param [startNode=this._root] - The subtree root to search from.
533
+ * @param [iterationType=this.iterationType] - The traversal method.
534
+ * @returns The callback result for the rightmost node.
535
+ */
536
+ getRightMost<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>;
537
+ /**
538
+ * Gets the Morris traversal predecessor (rightmost node in the left subtree, or node itself).
539
+ * @remarks This is primarily a helper for Morris traversal. Time O(H), where H is the height of the left subtree. O(N) worst-case. Space O(1).
540
+ *
541
+ * @param node - The node to find the predecessor for.
542
+ * @returns The Morris predecessor.
632
543
  */
633
544
  getPredecessor(node: BinaryTreeNode<K, V>): BinaryTreeNode<K, V>;
634
545
  /**
635
- * Time Complexity: O(log n)
636
- * Space Complexity: O(log n)
546
+ * Gets the in-order successor of a node in a BST.
547
+ * @remarks Time O(H), where H is the tree height. O(N) worst-case. Space O(H) (due to `getLeftMost` stack).
637
548
  *
638
- * The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
639
- * binary tree.
640
- * @param {K | BinaryTreeNode<K, V> | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
641
- * type `K`, `BinaryTreeNode<K, V>`, or `null`.
642
- * @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
643
- * a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
644
- * have a right child, the function traverses up the parent nodes until it finds a node that is not
645
- * the right child of its parent, and returns that node
549
+ * @param [x] - The node to find the successor of.
550
+ * @returns The successor node, or null/undefined if none exists.
646
551
  */
647
552
  getSuccessor(x?: K | BinaryTreeNode<K, V> | null): BinaryTreeNode<K, V> | null | undefined;
648
553
  dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
@@ -650,261 +555,200 @@ export declare class BinaryTree<K = any, V = any, R = object, MK = any, MV = any
650
555
  bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
651
556
  bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
652
557
  /**
653
- * Time complexity: O(n)
654
- * Space complexity: O(n)
655
- *
656
- * The `leaves` function in TypeScript returns an array of values from leaf nodes in a binary tree
657
- * structure based on a specified callback and iteration type.
658
- * @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
659
- * in the binary tree. It is optional and defaults to a default callback function if not provided.
660
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `leaves`
661
- * method is used to specify the starting point for finding and processing the leaves of a binary
662
- * tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
663
- * explicitly provided, the default value
664
- * @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
665
- * specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
666
- * can have two possible values:
667
- * @returns The `leaves` method returns an array of values that are the result of applying the
668
- * provided callback function to each leaf node in the binary tree.
558
+ * Finds all leaf nodes in the tree.
559
+ * @remarks Time O(N), visits every node. Space O(H) for recursive stack or O(N) for iterative queue.
560
+ *
561
+ * @template C - The type of the callback function.
562
+ * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each leaf node.
563
+ * @param [startNode=this._root] - The node to start from.
564
+ * @param [iterationType=this.iterationType] - The traversal method.
565
+ * @returns An array of callback results.
669
566
  */
670
567
  leaves<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
671
568
  listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
672
569
  listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
673
570
  morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): ReturnType<C>[];
674
571
  /**
675
- * Time complexity: O(n)
676
- * Space complexity: O(n)
677
- *
678
- * The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first
679
- * search.
680
- * @returns The `clone()` method is returning a cloned copy of the tree with the same structure and
681
- * values as the original tree. The method creates a new tree, iterates over the nodes of the
682
- * original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
683
- * the original tree is null, a null node is added to the cloned tree. If a node
684
- */
685
- clone(): BinaryTree<K, V, R, MK, MV, MR>;
686
- /**
687
- * Time Complexity: O(n)
688
- * Space Complexity: O(n)
689
- *
690
- * The `filter` function iterates over key-value pairs in a tree data structure and creates a new
691
- * tree with elements that satisfy a given predicate.
692
- * @param predicate - The `predicate` parameter in the `filter` method is a function that will be
693
- * called with four arguments: the `value` of the current entry, the `key` of the current entry, the
694
- * `index` of the current entry in the iteration, and the reference to the tree itself (`
695
- * @param {any} [thisArg] - The `thisArg` parameter in the `filter` method allows you to specify the
696
- * value of `this` that should be used when executing the `predicate` function. This is useful when
697
- * the `predicate` function relies on the context of a specific object or value. By providing a
698
- * `thisArg
699
- * @returns The `filter` method is returning a new tree that contains entries that pass the provided
700
- * predicate function.
701
- */
702
- filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any): BinaryTree<K, V, R, MK, MV, MR>;
703
- /**
704
- * Time Complexity: O(n)
705
- * Space Complexity: O(n)
706
- *
707
- * The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
708
- * entry in the original BinaryTree.
709
- * @param callback - A function that will be called for each entry in the current binary tree. It
710
- * takes the key, value (which can be undefined), and an array containing the mapped key and value as
711
- * arguments.
712
- * @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
713
- * MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
714
- * tree being created during the mapping process. These options could include things like custom
715
- * comparators, initial
716
- * @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
717
- * of `this` when executing the `callback` function. It allows you to set the context (value of
718
- * `this`) within the callback function. If `thisArg` is provided, it will be passed
719
- * @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
720
- * the result of applying the provided `callback` function to each entry in the original tree.
721
- */
722
- map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BinaryTreeOptions<MK, MV, MR>, thisArg?: any): BinaryTree<MK, MV, MR>;
723
- /**
724
- * Time Complexity: O(n)
725
- * Space Complexity: O(n)
726
- *
727
- * The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
728
- * customizable options for displaying undefined, null, and sentinel nodes.
729
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
730
- * `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
731
- * It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
732
- * the default is set to the root
733
- * @param {BinaryTreePrintOptions} [options] - The `options` parameter in the `toVisual` method is an
734
- * object that contains the following properties:
735
- * @returns The `override toVisual` method returns a string that represents the visual display of the
736
- * binary tree based on the provided options for showing undefined, null, and Red-Black NIL nodes.
737
- * The method constructs the visual representation by calling the `_displayAux` method and appending
738
- * the lines to the output string. The final output string contains the visual representation of the
739
- * binary tree with the specified options.
572
+ * Clones the tree.
573
+ * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `add`, and `add` is O(M)). Space O(N) for the new tree and the BFS queue.
574
+ *
575
+ * @returns A new, cloned instance of the tree.
576
+ */
577
+ clone(): this;
578
+ /**
579
+ * Creates a new tree containing only the entries that satisfy the predicate.
580
+ * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `add` for each item). Space O(N) for the new tree.
581
+ *
582
+ * @param predicate - A function to test each [key, value] pair.
583
+ * @param [thisArg] - `this` context for the predicate.
584
+ * @returns A new, filtered tree.
585
+ */
586
+ filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
587
+ /**
588
+ * Creates a new tree by mapping each [key, value] pair to a new entry.
589
+ * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion. Space O(N) for the new tree.
590
+ *
591
+ * @template MK - New key type.
592
+ * @template MV - New value type.
593
+ * @template MR - New raw type.
594
+ * @param cb - A function to map each [key, value] pair.
595
+ * @param [options] - Options for the new tree.
596
+ * @param [thisArg] - `this` context for the callback.
597
+ * @returns A new, mapped tree.
598
+ */
599
+ map<MK = K, MV = V, MR extends object = object>(cb: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): BinaryTree<MK, MV, MR>;
600
+ /**
601
+ * Generates a string representation of the tree for visualization.
602
+ * @remarks Time O(N), visits every node. Space O(N*H) or O(N^2) in the worst case, as the string width can grow significantly.
603
+ *
604
+ * @param [startNode=this._root] - The node to start printing from.
605
+ * @param [options] - Options to control the output (e.g., show nulls).
606
+ * @returns The string representation of the tree.
740
607
  */
741
608
  toVisual(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, options?: BinaryTreePrintOptions): string;
742
609
  /**
743
- * Time Complexity: O(n)
744
- * Space Complexity: O(n)
610
+ * Prints a visual representation of the tree to the console.
611
+ * @remarks Time O(N) (via `toVisual`). Space O(N*H) or O(N^2) (via `toVisual`).
745
612
  *
746
- * The function `print` in TypeScript overrides the default print behavior to log a visual
747
- * representation of the binary tree to the console.
748
- * @param {BinaryTreePrintOptions} [options] - The `options` parameter is used to specify the
749
- * printing options for the binary tree. It is an optional parameter that allows you to customize how
750
- * the binary tree is printed, such as choosing between different traversal orders or formatting
751
- * options.
752
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
753
- * `override print` method is used to specify the starting point for printing the binary tree. It can
754
- * be either a key, a node, an entry, or the root of the tree. If no specific starting point is
755
- * provided, the default value is set to
613
+ * @param [options] - Options to control the output.
614
+ * @param [startNode=this._root] - The node to start printing from.
756
615
  */
757
616
  print(options?: BinaryTreePrintOptions, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void;
758
- protected _clone(cloned: BinaryTree<K, V, R, MK, MV, MR>): void;
759
- /**
760
- * Time Complexity: O(1)
761
- * Space Complexity: O(1)
762
- *
763
- * The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
764
- * or returns null.
765
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The
766
- * `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which
767
- * can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. This parameter represents either a key, a
768
- * node, an entry
769
- * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
770
- * an optional parameter of type `V`. It represents the value associated with the key in the node
771
- * being created. If a `value` is provided, it will be used when creating the node. If
772
- * @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
773
- * (`BinaryTreeNode<K, V> | null | undefined`) based on the input parameters provided. The function checks the type of the
774
- * input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null
775
- * value.
776
- */
777
- protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): [BinaryTreeNode<K, V> | null | undefined, V | undefined];
778
617
  protected _dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean, shouldVisitLeft?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRight?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldProcessRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean): ReturnType<C>[];
779
618
  /**
780
- * Time Complexity: O(1)
781
- * Space Complexity: O(1)
619
+ * (Protected) Gets the iterator for the tree (default in-order).
620
+ * @remarks Time O(N) for full iteration. O(H) to get the first element. Space O(H) for the iterative stack. O(H) for recursive stack.
782
621
  *
783
- * The function `_getIterator` returns an iterable iterator for a binary tree data structure, either
784
- * using an iterative approach or a recursive approach based on the specified iteration type.
785
- * @param node - The `node` parameter in the `_getIterator` method represents the current node being
786
- * processed during iteration. It is initially set to the root node of the data structure (or the
787
- * node passed as an argument), and then it is traversed through the data structure based on the
788
- * iteration type specified (`ITER
789
- * @returns The `_getIterator` method returns an IterableIterator containing key-value pairs of nodes
790
- * in a binary tree structure. The method uses an iterative approach to traverse the tree based on
791
- * the `iterationType` property. If the `iterationType` is set to 'ITERATIVE', the method uses a
792
- * stack to perform an in-order traversal of the tree. If the `iterationType` is not 'ITERATIVE
622
+ * @param [node=this._root] - The node to start iteration from.
623
+ * @returns An iterator for [key, value] pairs.
793
624
  */
794
625
  protected _getIterator(node?: BinaryTreeNode<K, V> | null | undefined): IterableIterator<[K, V | undefined]>;
795
626
  /**
796
- * Time Complexity: O(n)
797
- * Space Complexity: O(n)
627
+ * (Protected) Default callback function, returns the node's key.
628
+ * @remarks Time O(1)
798
629
  *
799
- * The function `_displayAux` in TypeScript is responsible for generating the display layout of nodes
800
- * in a binary tree based on specified options.
801
- * @param node - The `node` parameter in the `_displayAux` function represents a node in a binary
802
- * tree. It can be either a valid node containing a key or a special type of node like null,
803
- * undefined, or a Red-Black tree NIL node. The function checks the type of the node and its
804
- * @param {BinaryTreePrintOptions} options - The `options` parameter in the `_displayAux` function
805
- * contains the following properties:
806
- * @returns The `_displayAux` function returns a `NodeDisplayLayout`, which is an array containing
807
- * information about how to display a node in a binary tree. The `NodeDisplayLayout` consists of four
808
- * elements:
630
+ * @param node - The node.
631
+ * @returns The node's key or undefined.
809
632
  */
810
- protected _displayAux(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
811
633
  protected _DEFAULT_NODE_CALLBACK: (node: BinaryTreeNode<K, V> | null | undefined) => K | undefined;
812
634
  /**
813
- * Time Complexity: O(1)
814
- * Space Complexity: O(1)
635
+ * (Protected) Snapshots the current tree's configuration options.
636
+ * @remarks Time O(1)
637
+ *
638
+ * @template TK, TV, TR - Generic types for the options.
639
+ * @returns The options object.
640
+ */
641
+ protected _snapshotOptions<TK = K, TV = V, TR extends object = R>(): BinaryTreeOptions<TK, TV, TR>;
642
+ /**
643
+ * (Protected) Creates a new, empty instance of the same tree constructor.
644
+ * @remarks Time O(1)
645
+ *
646
+ * @template TK, TV, TR - Generic types for the new instance.
647
+ * @param [options] - Options for the new tree.
648
+ * @returns A new, empty tree.
649
+ */
650
+ protected _createInstance<TK = K, TV = V, TR extends object = R>(options?: Partial<BinaryTreeOptions<TK, TV, TR>>): this;
651
+ /**
652
+ * (Protected) Creates a new instance of the same tree constructor, potentially with different generic types.
653
+ * @remarks Time O(N) (or as per constructor) due to processing the iterable.
654
+ *
655
+ * @template TK, TV, TR - Generic types for the new instance.
656
+ * @param [iter=[]] - An iterable to populate the new tree.
657
+ * @param [options] - Options for the new tree.
658
+ * @returns A new tree.
659
+ */
660
+ protected _createLike<TK = K, TV = V, TR extends object = R>(iter?: Iterable<TK | BinaryTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<BinaryTreeOptions<TK, TV, TR>>): BinaryTree<TK, TV, TR>;
661
+ /**
662
+ * (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
663
+ * @remarks Time O(1)
815
664
  *
816
- * The _swapProperties function swaps key and value properties between two nodes in a binary tree.
817
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } srcNode - The `srcNode` parameter in the
818
- * `_swapProperties` method can be either a BTNRep object containing key and value
819
- * properties, or it can be of type R.
820
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } destNode - The `destNode` parameter in the
821
- * `_swapProperties` method represents the node or entry where the properties will be swapped with
822
- * the `srcNode`. It can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. The method ensures that
823
- * both `srcNode
824
- * @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
825
- * with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
665
+ * @param keyNodeOrEntry - The input item.
666
+ * @param [value] - An optional value (used if input is just a key).
667
+ * @returns A tuple of [node, value].
668
+ */
669
+ protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): [BinaryTreeNode<K, V> | null | undefined, V | undefined];
670
+ /**
671
+ * (Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
672
+ * @remarks Time O(N * M) (O(N) BFS + O(M) `add` for each node).
673
+ *
674
+ * @param cloned - The new, empty tree instance to populate.
675
+ */
676
+ protected _clone(cloned: BinaryTree<K, V, R>): void;
677
+ /**
678
+ * (Protected) Recursive helper for `toVisual`.
679
+ * @remarks Time O(N), Space O(N*H) or O(N^2)
680
+ *
681
+ * @param node - The current node.
682
+ * @param options - Print options.
683
+ * @returns Layout information for this subtree.
684
+ */
685
+ protected _displayAux(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
686
+ /**
687
+ * (Protected) Swaps the key/value properties of two nodes.
688
+ * @remarks Time O(1)
689
+ *
690
+ * @param srcNode - The source node.
691
+ * @param destNode - The destination node.
692
+ * @returns The `destNode` (now holding `srcNode`'s properties).
826
693
  */
827
694
  protected _swapProperties(srcNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, destNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeNode<K, V> | undefined;
828
695
  /**
829
- * Time Complexity: O(1)
830
- * Space Complexity: O(1)
696
+ * (Protected) Replaces a node in the tree with a new node, maintaining children and parent links.
697
+ * @remarks Time O(1)
831
698
  *
832
- * The _replaceNode function replaces an old node with a new node in a binary tree structure.
833
- * @param {BinaryTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that you want to replace in a
834
- * tree data structure.
835
- * @param {BinaryTreeNode<K, V>} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
836
- * that will replace the `oldNode` in a tree data structure. This function is responsible for
837
- * updating the parent, left child, right child, and root (if necessary) references when replacing a
838
- * node in the tree.
839
- * @returns The method `_replaceNode` is returning the `newNode` that was passed as a parameter after
840
- * replacing the `oldNode` with it in the binary tree structure.
699
+ * @param oldNode - The node to be replaced.
700
+ * @param newNode - The node to insert.
701
+ * @returns The `newNode`.
841
702
  */
842
703
  protected _replaceNode(oldNode: BinaryTreeNode<K, V>, newNode: BinaryTreeNode<K, V>): BinaryTreeNode<K, V>;
843
704
  /**
844
- * Time Complexity: O(1)
845
- * Space Complexity: O(1)
705
+ * (Protected) Sets the root node and clears its parent reference.
706
+ * @remarks Time O(1)
846
707
  *
847
- * The function _setRoot sets the root node of a data structure while updating the parent reference
848
- * of the previous root node.
849
- * @param v - The parameter `v` in the `_setRoot` method is of type `BinaryTreeNode<K, V> | null | undefined`, which means
850
- * it can either be an optional `BinaryTreeNode<K, V>` type or `null`.
708
+ * @param v - The node to set as root.
851
709
  */
852
710
  protected _setRoot(v: BinaryTreeNode<K, V> | null | undefined): void;
711
+ /**
712
+ * (Protected) Converts a key, node, entry, or predicate into a standardized predicate function.
713
+ * @remarks Time O(1)
714
+ *
715
+ * @param keyNodeEntryOrPredicate - The item to convert.
716
+ * @returns A predicate function.
717
+ */
853
718
  protected _ensurePredicate(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>): NodePredicate<BinaryTreeNode<K, V>>;
854
719
  /**
855
- * Time Complexity: O(1)
856
- * Space Complexity: O(1)
720
+ * (Protected) Checks if an item is a predicate function.
721
+ * @remarks Time O(1)
857
722
  *
858
- * The function `_isPredicate` checks if a given parameter is a function.
859
- * @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
860
- * of value. In this context, the function `_isPredicate` is checking if `p` is a function that
861
- * satisfies the type `NodePredicate<BinaryTreeNode<K, V>>`.
862
- * @returns The function is checking if the input `p` is a function and returning a boolean value
863
- * based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
864
- * predicate function for a binary tree node. If `p` is not a function, it will return `false`.
723
+ * @param p - The item to check.
724
+ * @returns True if it's a function.
865
725
  */
866
726
  protected _isPredicate(p: any): p is NodePredicate<BinaryTreeNode<K, V>>;
867
727
  /**
868
- * Time Complexity: O(1)
869
- * Space Complexity: O(1)
728
+ * (Protected) Extracts the key from a key, node, or entry.
729
+ * @remarks Time O(1)
870
730
  *
871
- * The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
872
- * entry, raw data, or null/undefined.
873
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `_extractKey` method you provided is a
874
- * TypeScript method that takes in a parameter `keyNodeOrEntry` of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `,
875
- * where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and `
876
- * @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry`
877
- * parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
878
- * the conditions checked in the method.
731
+ * @param keyNodeOrEntry - The item.
732
+ * @returns The extracted key.
879
733
  */
880
734
  protected _extractKey(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): K | null | undefined;
881
735
  /**
882
- * Time Complexity: O(1)
883
- * Space Complexity: O(1)
736
+ * (Protected) Sets a value in the external store (Map mode).
737
+ * @remarks Time O(1) (average for Map.set).
884
738
  *
885
- * The function `_setValue` sets a value in a store based on a key, handling cases where the key or
886
- * value is null or undefined.
887
- * @param {K | null | undefined} key - The `key` parameter can be of type `K`, `null`, or
888
- * `undefined`.
889
- * @param {V | undefined} value - The `value` parameter in the `_setValue` method can be of type `V`
890
- * or `undefined`.
891
- * @returns The method `_setValue` returns `false` if either the `key` is `null` or `undefined`, or
892
- * if the `value` is `undefined`. Otherwise, it returns the result of calling the `set` method on the
893
- * `_store` object with the `key` and `value` arguments.
739
+ * @param key - The key.
740
+ * @param value - The value.
741
+ * @returns True if successful.
894
742
  */
895
743
  protected _setValue(key: K | null | undefined, value: V | undefined): false | Map<K, V | undefined>;
896
744
  /**
897
- * Time Complexity: O(1)
898
- * Space Complexity: O(1)
899
- *
900
- * The _clearNodes function sets the root node to undefined and resets the size to 0.
745
+ * (Protected) Clears all nodes from the tree.
746
+ * @remarks Time O(1)
901
747
  */
902
748
  protected _clearNodes(): void;
903
749
  /**
904
- * Time Complexity: O(1)
905
- * Space Complexity: O(1)
906
- *
907
- * The _clearValues function clears all values stored in the _store object.
750
+ * (Protected) Clears all values from the external store.
751
+ * @remarks Time O(N)
908
752
  */
909
753
  protected _clearValues(): void;
910
754
  }