undirected-graph-typed 1.51.8 → 1.52.1
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.
- package/dist/data-structures/base/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +333 -241
- package/dist/data-structures/binary-tree/binary-tree.js +478 -366
- package/dist/data-structures/binary-tree/bst.d.ts +202 -212
- package/dist/data-structures/binary-tree/bst.js +208 -250
- package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/graph/directed-graph.js +2 -1
- package/dist/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +28 -20
- package/dist/data-structures/queue/deque.js +45 -24
- package/dist/data-structures/queue/queue.d.ts +8 -29
- package/dist/data-structures/queue/queue.js +15 -32
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +19 -14
- package/dist/data-structures/trie/trie.js +27 -16
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +1 -23
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +7 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +4 -2
- package/dist/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +146 -97
- package/src/data-structures/binary-tree/avl-tree.ts +97 -70
- package/src/data-structures/binary-tree/binary-tree.ts +591 -455
- package/src/data-structures/binary-tree/bst.ts +266 -293
- package/src/data-structures/binary-tree/rb-tree.ts +124 -104
- package/src/data-structures/binary-tree/tree-multi-map.ts +128 -103
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/graph/directed-graph.ts +2 -1
- package/src/data-structures/hash/hash-map.ts +46 -53
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +52 -27
- package/src/data-structures/queue/queue.ts +23 -37
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +35 -20
- package/src/interfaces/binary-tree.ts +10 -10
- package/src/types/common.ts +2 -25
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +26 -6
- package/src/types/data-structures/binary-tree/bst.ts +11 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +6 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/utils/utils.ts +3 -3
|
@@ -14,13 +14,13 @@ import type {
|
|
|
14
14
|
BinaryTreePrintOptions,
|
|
15
15
|
BTNCallback,
|
|
16
16
|
BTNEntry,
|
|
17
|
-
|
|
17
|
+
BTNKeyOrNodeOrEntry,
|
|
18
18
|
DFSOrderPattern,
|
|
19
19
|
EntryCallback,
|
|
20
20
|
FamilyPosition,
|
|
21
21
|
IterationType,
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
NodeDisplayLayout,
|
|
23
|
+
OptBTNOrNull
|
|
24
24
|
} from '../../types';
|
|
25
25
|
import { IBinaryTree } from '../../interfaces';
|
|
26
26
|
import { trampoline } from '../../utils';
|
|
@@ -33,7 +33,7 @@ import { IterableEntryBase } from '../base';
|
|
|
33
33
|
* @template NODE - The type of the family relationship in the binary tree.
|
|
34
34
|
*/
|
|
35
35
|
export class BinaryTreeNode<
|
|
36
|
-
K
|
|
36
|
+
K = any,
|
|
37
37
|
V = any,
|
|
38
38
|
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>
|
|
39
39
|
> {
|
|
@@ -63,16 +63,16 @@ export class BinaryTreeNode<
|
|
|
63
63
|
* @returns The left node of the current node is being returned. It can be either a NODE object,
|
|
64
64
|
* null, or undefined.
|
|
65
65
|
*/
|
|
66
|
-
get left(): NODE
|
|
66
|
+
get left(): OptBTNOrNull<NODE> {
|
|
67
67
|
return this._left;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
/**
|
|
71
71
|
* The function sets the left child of a node and updates its parent reference.
|
|
72
|
-
* @param {NODE
|
|
72
|
+
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
73
73
|
* `undefined`.
|
|
74
74
|
*/
|
|
75
|
-
set left(v: NODE
|
|
75
|
+
set left(v: OptBTNOrNull<NODE>) {
|
|
76
76
|
if (v) {
|
|
77
77
|
v.parent = this as unknown as NODE;
|
|
78
78
|
}
|
|
@@ -86,16 +86,16 @@ export class BinaryTreeNode<
|
|
|
86
86
|
* @returns The method is returning the value of the `_right` property, which can be a `NODE` object,
|
|
87
87
|
* `null`, or `undefined`.
|
|
88
88
|
*/
|
|
89
|
-
get right(): NODE
|
|
89
|
+
get right(): OptBTNOrNull<NODE> {
|
|
90
90
|
return this._right;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
94
|
* The function sets the right child of a node and updates its parent.
|
|
95
|
-
* @param {NODE
|
|
95
|
+
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
96
96
|
* `undefined`.
|
|
97
97
|
*/
|
|
98
|
-
set right(v: NODE
|
|
98
|
+
set right(v: OptBTNOrNull<NODE>) {
|
|
99
99
|
if (v) {
|
|
100
100
|
v.parent = this as unknown as NODE;
|
|
101
101
|
}
|
|
@@ -131,48 +131,52 @@ export class BinaryTreeNode<
|
|
|
131
131
|
*/
|
|
132
132
|
|
|
133
133
|
export class BinaryTree<
|
|
134
|
-
K
|
|
134
|
+
K = any,
|
|
135
135
|
V = any,
|
|
136
|
+
R = BTNEntry<K, V>,
|
|
136
137
|
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
|
|
137
|
-
TREE extends BinaryTree<K, V, NODE, TREE> = BinaryTree<K, V, NODE, BinaryTreeNested<K, V, NODE>>
|
|
138
|
+
TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTree<K, V, R, NODE, BinaryTreeNested<K, V, R, NODE>>
|
|
138
139
|
>
|
|
139
140
|
extends IterableEntryBase<K, V | undefined>
|
|
140
|
-
implements IBinaryTree<K, V, NODE, TREE> {
|
|
141
|
+
implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
141
142
|
iterationType: IterationType = 'ITERATIVE';
|
|
142
143
|
|
|
143
144
|
/**
|
|
144
|
-
* The constructor function initializes a binary tree object with optional
|
|
145
|
-
* @param [
|
|
145
|
+
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
|
|
146
|
+
* @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of BTNKeyOrNodeOrEntry objects. These objects represent the
|
|
146
147
|
* nodes to be added to the binary tree.
|
|
147
148
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
148
149
|
* configuration options for the binary tree. In this case, it is of type
|
|
149
150
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
150
151
|
* required.
|
|
151
152
|
*/
|
|
152
|
-
constructor(
|
|
153
|
+
constructor(
|
|
154
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
155
|
+
options?: BinaryTreeOptions<K, V, R>
|
|
156
|
+
) {
|
|
153
157
|
super();
|
|
154
158
|
if (options) {
|
|
155
|
-
const { iterationType } = options;
|
|
159
|
+
const { iterationType, toEntryFn } = options;
|
|
156
160
|
if (iterationType) this.iterationType = iterationType;
|
|
161
|
+
if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn;
|
|
162
|
+
else if (toEntryFn) throw TypeError('toEntryFn must be a function type');
|
|
157
163
|
}
|
|
158
164
|
|
|
159
|
-
this.
|
|
160
|
-
|
|
161
|
-
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
165
|
+
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
162
166
|
}
|
|
163
167
|
|
|
164
|
-
protected _root?: NODE
|
|
168
|
+
protected _root?: OptBTNOrNull<NODE>;
|
|
165
169
|
|
|
166
170
|
/**
|
|
167
171
|
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
168
172
|
* @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
|
|
169
173
|
* `null`, or `undefined`.
|
|
170
174
|
*/
|
|
171
|
-
get root(): NODE
|
|
175
|
+
get root(): OptBTNOrNull<NODE> {
|
|
172
176
|
return this._root;
|
|
173
177
|
}
|
|
174
178
|
|
|
175
|
-
protected _size: number;
|
|
179
|
+
protected _size: number = 0;
|
|
176
180
|
|
|
177
181
|
/**
|
|
178
182
|
* The function returns the size of an object.
|
|
@@ -192,6 +196,16 @@ export class BinaryTree<
|
|
|
192
196
|
return this._NIL;
|
|
193
197
|
}
|
|
194
198
|
|
|
199
|
+
protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* The function returns the value of the _toEntryFn property.
|
|
203
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
204
|
+
*/
|
|
205
|
+
get toEntryFn() {
|
|
206
|
+
return this._toEntryFn;
|
|
207
|
+
}
|
|
208
|
+
|
|
195
209
|
/**
|
|
196
210
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
197
211
|
* @param {K} key - The key for the new node.
|
|
@@ -209,41 +223,46 @@ export class BinaryTree<
|
|
|
209
223
|
* you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
|
|
210
224
|
* @returns a new instance of a binary tree.
|
|
211
225
|
*/
|
|
212
|
-
createTree(options?: Partial<BinaryTreeOptions
|
|
213
|
-
return new BinaryTree<K, V, NODE, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
|
|
226
|
+
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE {
|
|
227
|
+
return new BinaryTree<K, V, R, NODE, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
|
|
214
228
|
}
|
|
215
229
|
|
|
216
230
|
/**
|
|
217
|
-
* The function `
|
|
218
|
-
*
|
|
231
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
|
|
232
|
+
* into a node object.
|
|
233
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
234
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
219
235
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
220
|
-
* `
|
|
221
|
-
*
|
|
222
|
-
* @returns
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
239
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
240
|
-
node = keyOrNodeOrEntry;
|
|
241
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
242
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
243
|
-
} else {
|
|
244
|
-
return;
|
|
236
|
+
* `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
|
|
237
|
+
* key-value pair. If provided, it will be used to create a node with the specified key and value.
|
|
238
|
+
* @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
|
|
239
|
+
* or `undefined`.
|
|
240
|
+
*/
|
|
241
|
+
keyValueOrEntryOrRawElementToNode(
|
|
242
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
243
|
+
value?: V
|
|
244
|
+
): OptBTNOrNull<NODE> {
|
|
245
|
+
if (keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
246
|
+
if (keyOrNodeOrEntryOrRawElement === null) return null;
|
|
247
|
+
|
|
248
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
249
|
+
|
|
250
|
+
if (this.toEntryFn) {
|
|
251
|
+
const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
|
|
252
|
+
if (key) return this.createNode(key, entryValue ?? value);
|
|
253
|
+
else return;
|
|
245
254
|
}
|
|
246
|
-
|
|
255
|
+
|
|
256
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
257
|
+
const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
258
|
+
if (key === undefined) return;
|
|
259
|
+
else if (key === null) return null;
|
|
260
|
+
else return this.createNode(key, value);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value);
|
|
264
|
+
|
|
265
|
+
return;
|
|
247
266
|
}
|
|
248
267
|
|
|
249
268
|
/**
|
|
@@ -255,81 +274,122 @@ export class BinaryTree<
|
|
|
255
274
|
* Time Complexity: O(n)
|
|
256
275
|
* Space Complexity: O(log n)
|
|
257
276
|
*
|
|
258
|
-
* The
|
|
259
|
-
*
|
|
260
|
-
* @param {
|
|
261
|
-
* `
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
277
|
+
* The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
|
|
278
|
+
* node if it is a key or entry.
|
|
279
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
280
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
|
|
281
|
+
* a raw element.
|
|
282
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
283
|
+
* parameter that specifies the type of iteration to be used when searching for a node. It has a
|
|
284
|
+
* default value of `'ITERATIVE'`.
|
|
285
|
+
* @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
|
|
267
286
|
*/
|
|
268
287
|
ensureNode(
|
|
269
|
-
|
|
288
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
270
289
|
iterationType: IterationType = 'ITERATIVE'
|
|
271
|
-
): NODE
|
|
272
|
-
if (
|
|
273
|
-
if (
|
|
274
|
-
|
|
290
|
+
): OptBTNOrNull<NODE> {
|
|
291
|
+
if (keyOrNodeOrEntryOrRawElement === null) return null;
|
|
292
|
+
if (keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
293
|
+
if (keyOrNodeOrEntryOrRawElement === this.NIL) return;
|
|
294
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
295
|
+
|
|
296
|
+
if (this.toEntryFn) {
|
|
297
|
+
const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
|
|
298
|
+
if (key) return this.getNodeByKey(key);
|
|
275
299
|
}
|
|
276
|
-
|
|
277
|
-
|
|
300
|
+
|
|
301
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
302
|
+
const key = keyOrNodeOrEntryOrRawElement[0];
|
|
278
303
|
if (key === null) return null;
|
|
279
304
|
if (key === undefined) return;
|
|
280
305
|
return this.getNodeByKey(key, iterationType);
|
|
281
306
|
}
|
|
282
|
-
|
|
283
|
-
if (
|
|
284
|
-
return
|
|
307
|
+
|
|
308
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
|
|
309
|
+
return;
|
|
285
310
|
}
|
|
286
311
|
|
|
287
312
|
/**
|
|
288
|
-
* The function checks if
|
|
289
|
-
* @param {
|
|
290
|
-
*
|
|
313
|
+
* The function checks if the input is an instance of the BinaryTreeNode class.
|
|
314
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
315
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
316
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
317
|
+
* an instance of the `BinaryTreeNode` class.
|
|
291
318
|
*/
|
|
292
|
-
|
|
293
|
-
return
|
|
319
|
+
isNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE {
|
|
320
|
+
return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
|
|
294
321
|
}
|
|
295
322
|
|
|
296
323
|
/**
|
|
297
|
-
* The function
|
|
298
|
-
* @param
|
|
299
|
-
*
|
|
324
|
+
* The function checks if a given node is a valid node in a binary search tree.
|
|
325
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
326
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
327
|
+
* @returns a boolean value.
|
|
300
328
|
*/
|
|
301
|
-
|
|
302
|
-
|
|
329
|
+
isRealNode(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE {
|
|
330
|
+
if (node === this.NIL || node === null || node === undefined) return false;
|
|
331
|
+
return this.isNode(node);
|
|
303
332
|
}
|
|
304
333
|
|
|
305
334
|
/**
|
|
306
|
-
* The function checks if a given node is a real node
|
|
307
|
-
*
|
|
308
|
-
*
|
|
335
|
+
* The function checks if a given node is a real node or null.
|
|
336
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
337
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
309
338
|
* @returns a boolean value.
|
|
310
339
|
*/
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
return this.isNode(node);
|
|
340
|
+
isNodeOrNull(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
|
|
341
|
+
return this.isRealNode(node) || node === null;
|
|
314
342
|
}
|
|
315
343
|
|
|
316
344
|
/**
|
|
317
|
-
* The function checks if a given node is
|
|
318
|
-
* @param {
|
|
345
|
+
* The function checks if a given node is equal to the NIL value.
|
|
346
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
347
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
319
348
|
* @returns a boolean value.
|
|
320
349
|
*/
|
|
321
|
-
isNIL(node:
|
|
350
|
+
isNIL(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>) {
|
|
322
351
|
return node === this.NIL;
|
|
323
352
|
}
|
|
324
353
|
|
|
325
354
|
/**
|
|
326
|
-
* The function checks if
|
|
327
|
-
*
|
|
328
|
-
*
|
|
355
|
+
* The function checks if the input is an array with two elements, indicating it is a binary tree
|
|
356
|
+
* node entry.
|
|
357
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
358
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
329
359
|
* @returns a boolean value.
|
|
330
360
|
*/
|
|
331
|
-
isEntry(
|
|
332
|
-
|
|
361
|
+
isEntry(
|
|
362
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
363
|
+
): keyOrNodeOrEntryOrRawElement is BTNEntry<K, V> {
|
|
364
|
+
return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* The function checks if a given value is a valid key by evaluating its type and value.
|
|
369
|
+
* @param {any} key - The `key` parameter can be of any type. It is the value that we want to check
|
|
370
|
+
* if it is a valid key.
|
|
371
|
+
* @param [isCheckValueOf=true] - The `isCheckValueOf` parameter is a boolean flag that determines
|
|
372
|
+
* whether the function should check the valueOf() method of an object when the key is of type
|
|
373
|
+
* 'object'. If `isCheckValueOf` is true, the function will recursively call itself with the value
|
|
374
|
+
* returned by key.valueOf().
|
|
375
|
+
* @returns a boolean value.
|
|
376
|
+
*/
|
|
377
|
+
isKey(key: any, isCheckValueOf = true): key is K {
|
|
378
|
+
if (key === null) return true;
|
|
379
|
+
const keyType = typeof key;
|
|
380
|
+
if (keyType === 'string' || keyType === 'bigint' || keyType === 'boolean') return true;
|
|
381
|
+
if (keyType === 'number') return !isNaN(key);
|
|
382
|
+
if (keyType === 'symbol' || keyType === 'undefined') return false;
|
|
383
|
+
if (keyType === 'function') return this.isKey(key());
|
|
384
|
+
if (keyType === 'object') {
|
|
385
|
+
if (typeof key.toString === 'function') return true;
|
|
386
|
+
if (isCheckValueOf && typeof key.valueOf === 'function') {
|
|
387
|
+
this.isKey(key.valueOf(), false);
|
|
388
|
+
}
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return false;
|
|
333
393
|
}
|
|
334
394
|
|
|
335
395
|
/**
|
|
@@ -341,14 +401,20 @@ export class BinaryTree<
|
|
|
341
401
|
* Time Complexity O(n)
|
|
342
402
|
* Space Complexity O(1)
|
|
343
403
|
*
|
|
344
|
-
* The `add` function
|
|
345
|
-
*
|
|
346
|
-
* @param
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
404
|
+
* The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
|
|
405
|
+
* and finding the appropriate insertion position.
|
|
406
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
407
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
|
|
408
|
+
* node, entry, or raw element to be added to the tree. It can also accept a value of type
|
|
409
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
410
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
411
|
+
* key being added to the tree. It represents the value that will be stored in the tree for the given
|
|
412
|
+
* key.
|
|
413
|
+
* @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
414
|
+
* insertion position cannot be found or if there are duplicate keys.
|
|
415
|
+
*/
|
|
416
|
+
add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
417
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
352
418
|
if (newNode === undefined) return false;
|
|
353
419
|
|
|
354
420
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -410,13 +476,20 @@ export class BinaryTree<
|
|
|
410
476
|
* Time Complexity: O(k * n)
|
|
411
477
|
* Space Complexity: O(1)
|
|
412
478
|
*
|
|
413
|
-
* The `addMany` function takes in
|
|
414
|
-
* adds each node with its corresponding value to
|
|
415
|
-
*
|
|
416
|
-
* @param
|
|
417
|
-
*
|
|
418
|
-
|
|
419
|
-
|
|
479
|
+
* The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
|
|
480
|
+
* optional iterable of values, and adds each key or node or entry with its corresponding value to a
|
|
481
|
+
* data structure, returning an array of booleans indicating whether each insertion was successful.
|
|
482
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
483
|
+
* elements. These elements will be added to the data structure.
|
|
484
|
+
* @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
|
|
485
|
+
* in the `keysOrNodesOrEntriesOrRawElements` parameter.
|
|
486
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
487
|
+
* successfully added to the data structure.
|
|
488
|
+
*/
|
|
489
|
+
addMany(
|
|
490
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
|
|
491
|
+
values?: Iterable<V | undefined>
|
|
492
|
+
): boolean[] {
|
|
420
493
|
// TODO not sure addMany not be run multi times
|
|
421
494
|
const inserted: boolean[] = [];
|
|
422
495
|
|
|
@@ -425,7 +498,7 @@ export class BinaryTree<
|
|
|
425
498
|
valuesIterator = values[Symbol.iterator]();
|
|
426
499
|
}
|
|
427
500
|
|
|
428
|
-
for (const
|
|
501
|
+
for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
|
|
429
502
|
let value: V | undefined | null = undefined;
|
|
430
503
|
|
|
431
504
|
if (valuesIterator) {
|
|
@@ -435,7 +508,7 @@ export class BinaryTree<
|
|
|
435
508
|
}
|
|
436
509
|
}
|
|
437
510
|
|
|
438
|
-
inserted.push(this.add(
|
|
511
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
|
|
439
512
|
}
|
|
440
513
|
|
|
441
514
|
return inserted;
|
|
@@ -451,23 +524,25 @@ export class BinaryTree<
|
|
|
451
524
|
* Time Complexity: O(k * n)
|
|
452
525
|
* Space Complexity: O(1)
|
|
453
526
|
*
|
|
454
|
-
* The `refill` function clears the current data and adds new
|
|
455
|
-
* @param
|
|
456
|
-
*
|
|
457
|
-
* @param [values] - The `values` parameter is an optional iterable
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
527
|
+
* The `refill` function clears the current data and adds new data to the collection.
|
|
528
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
|
|
529
|
+
* elements. These can be of any type (R) or a specific type (BTNKeyOrNodeOrEntry<K, V, NODE>).
|
|
530
|
+
* @param [values] - The `values` parameter is an optional iterable of values that will be associated
|
|
531
|
+
* with the keys or nodes being added. If provided, the values will be assigned to the corresponding
|
|
532
|
+
* keys or nodes. If not provided, the values will be set to `undefined`.
|
|
533
|
+
*/
|
|
534
|
+
refill(
|
|
535
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
|
|
536
|
+
values?: Iterable<V | undefined>
|
|
537
|
+
): void {
|
|
463
538
|
this.clear();
|
|
464
|
-
this.addMany(
|
|
539
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements, values);
|
|
465
540
|
}
|
|
466
541
|
|
|
467
542
|
delete<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
468
543
|
|
|
469
544
|
delete<C extends BTNCallback<NODE, NODE>>(
|
|
470
|
-
identifier: NODE
|
|
545
|
+
identifier: OptBTNOrNull<NODE>,
|
|
471
546
|
callback?: C
|
|
472
547
|
): BinaryTreeDeleteResult<NODE>[];
|
|
473
548
|
|
|
@@ -476,21 +551,21 @@ export class BinaryTree<
|
|
|
476
551
|
/**
|
|
477
552
|
* Time Complexity: O(n)
|
|
478
553
|
* Space Complexity: O(1)
|
|
479
|
-
|
|
554
|
+
*/
|
|
480
555
|
|
|
481
|
-
|
|
556
|
+
/**
|
|
482
557
|
* Time Complexity: O(n)
|
|
483
558
|
* Space Complexity: O(1)
|
|
484
559
|
*
|
|
485
|
-
* The function
|
|
486
|
-
*
|
|
487
|
-
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value
|
|
488
|
-
*
|
|
489
|
-
* the callback function
|
|
490
|
-
* specific node based on its value or object.
|
|
560
|
+
* The above function is a TypeScript implementation of deleting a node from a binary tree, returning
|
|
561
|
+
* the deleted node and the node that needs to be balanced.
|
|
562
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
563
|
+
* used to identify the node that needs to be deleted from the binary tree. It can be of any type
|
|
564
|
+
* that is returned by the callback function.
|
|
491
565
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
492
|
-
* identifier of the node to be deleted. It is
|
|
493
|
-
*
|
|
566
|
+
* identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
|
|
567
|
+
* interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
|
|
568
|
+
* type `NODE
|
|
494
569
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
495
570
|
*/
|
|
496
571
|
delete<C extends BTNCallback<NODE>>(
|
|
@@ -545,15 +620,15 @@ export class BinaryTree<
|
|
|
545
620
|
identifier: K,
|
|
546
621
|
callback?: C,
|
|
547
622
|
onlyOne?: boolean,
|
|
548
|
-
beginRoot?:
|
|
623
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
549
624
|
iterationType?: IterationType
|
|
550
625
|
): NODE[];
|
|
551
626
|
|
|
552
627
|
getNodes<C extends BTNCallback<NODE, NODE>>(
|
|
553
|
-
identifier: NODE
|
|
628
|
+
identifier: OptBTNOrNull<NODE>,
|
|
554
629
|
callback?: C,
|
|
555
630
|
onlyOne?: boolean,
|
|
556
|
-
beginRoot?:
|
|
631
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
557
632
|
iterationType?: IterationType
|
|
558
633
|
): NODE[];
|
|
559
634
|
|
|
@@ -561,7 +636,7 @@ export class BinaryTree<
|
|
|
561
636
|
identifier: ReturnType<C>,
|
|
562
637
|
callback: C,
|
|
563
638
|
onlyOne?: boolean,
|
|
564
|
-
beginRoot?:
|
|
639
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
565
640
|
iterationType?: IterationType
|
|
566
641
|
): NODE[];
|
|
567
642
|
|
|
@@ -572,34 +647,33 @@ export class BinaryTree<
|
|
|
572
647
|
|
|
573
648
|
/**
|
|
574
649
|
* Time Complexity: O(n)
|
|
575
|
-
* Space Complexity: O(k + log n)
|
|
650
|
+
* Space Complexity: O(k + log n)
|
|
576
651
|
*
|
|
577
|
-
* The function `getNodes`
|
|
578
|
-
*
|
|
652
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
653
|
+
* recursive or iterative approach.
|
|
579
654
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
580
|
-
* that
|
|
581
|
-
* callback function
|
|
582
|
-
*
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
* @returns an array of nodes of type `NODE`.
|
|
655
|
+
* that is used to identify the nodes. It can be of any type and is used to match against the result
|
|
656
|
+
* of the callback function for each node.
|
|
657
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
658
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
659
|
+
* `callback` function is optional and defaults to a default callback function
|
|
660
|
+
* (`this._DEFAULT_CALLBACK`) if not provided.
|
|
661
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
|
|
662
|
+
* the identifier or all nodes that match the identifier. If set to true, only the first matching
|
|
663
|
+
* node will be returned. If set to false, all matching nodes will be returned. The default value is
|
|
664
|
+
* false.
|
|
665
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
666
|
+
* point for the search. It can be either a node object, a key-value pair, or a key. If it is not
|
|
667
|
+
* provided, the `root` of the data structure is used as the starting point.
|
|
668
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
669
|
+
* iteration to be performed on the nodes of a binary tree. It can have two possible values:
|
|
670
|
+
* @returns an array of NODE objects.
|
|
597
671
|
*/
|
|
598
672
|
getNodes<C extends BTNCallback<NODE>>(
|
|
599
673
|
identifier: ReturnType<C> | null | undefined,
|
|
600
674
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
601
675
|
onlyOne = false,
|
|
602
|
-
beginRoot:
|
|
676
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
603
677
|
iterationType: IterationType = this.iterationType
|
|
604
678
|
): NODE[] {
|
|
605
679
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -641,23 +715,23 @@ export class BinaryTree<
|
|
|
641
715
|
getNode<C extends BTNCallback<NODE, K>>(
|
|
642
716
|
identifier: K,
|
|
643
717
|
callback?: C,
|
|
644
|
-
beginRoot?:
|
|
718
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
645
719
|
iterationType?: IterationType
|
|
646
|
-
): NODE
|
|
720
|
+
): OptBTNOrNull<NODE>;
|
|
647
721
|
|
|
648
722
|
getNode<C extends BTNCallback<NODE, NODE>>(
|
|
649
|
-
identifier: NODE
|
|
723
|
+
identifier: OptBTNOrNull<NODE>,
|
|
650
724
|
callback?: C,
|
|
651
|
-
beginRoot?:
|
|
725
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
652
726
|
iterationType?: IterationType
|
|
653
|
-
): NODE
|
|
727
|
+
): OptBTNOrNull<NODE>;
|
|
654
728
|
|
|
655
729
|
getNode<C extends BTNCallback<NODE>>(
|
|
656
730
|
identifier: ReturnType<C>,
|
|
657
731
|
callback: C,
|
|
658
|
-
beginRoot?:
|
|
732
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
659
733
|
iterationType?: IterationType
|
|
660
|
-
): NODE
|
|
734
|
+
): OptBTNOrNull<NODE>;
|
|
661
735
|
|
|
662
736
|
/**
|
|
663
737
|
* Time Complexity: O(n)
|
|
@@ -666,31 +740,28 @@ export class BinaryTree<
|
|
|
666
740
|
|
|
667
741
|
/**
|
|
668
742
|
* Time Complexity: O(n)
|
|
669
|
-
* Space Complexity: O(log n)
|
|
743
|
+
* Space Complexity: O(log n).
|
|
670
744
|
*
|
|
671
|
-
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
672
|
-
*
|
|
745
|
+
* The function `getNode` returns the first node that matches the given identifier and callback,
|
|
746
|
+
* starting from the specified root node and using the specified iteration type.
|
|
673
747
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
674
|
-
* used to identify the node you want to retrieve. It can be of any type that is
|
|
675
|
-
* callback function
|
|
676
|
-
*
|
|
677
|
-
*
|
|
678
|
-
*
|
|
679
|
-
*
|
|
680
|
-
*
|
|
681
|
-
*
|
|
682
|
-
*
|
|
683
|
-
* @
|
|
684
|
-
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
685
|
-
* nodes are visited during the search.
|
|
686
|
-
* @returns a value of type `NODE | null | undefined`.
|
|
748
|
+
* used to identify the node you want to retrieve. It can be of any type that is the return type of
|
|
749
|
+
* the `C` callback function, or it can be `null` or `undefined`.
|
|
750
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
751
|
+
* node matches the desired criteria. It should return a value that can be used to identify the node.
|
|
752
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
753
|
+
* point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
|
|
754
|
+
* a node entry. If not provided, the search will start from the root of the tree.
|
|
755
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
756
|
+
* of iteration to be performed when searching for nodes. It can have one of the following values:
|
|
757
|
+
* @returns The method is returning a NODE object, or null, or undefined.
|
|
687
758
|
*/
|
|
688
759
|
getNode<C extends BTNCallback<NODE>>(
|
|
689
760
|
identifier: ReturnType<C> | null | undefined,
|
|
690
761
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
691
|
-
beginRoot:
|
|
762
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
692
763
|
iterationType: IterationType = this.iterationType
|
|
693
|
-
): NODE
|
|
764
|
+
): OptBTNOrNull<NODE> {
|
|
694
765
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
695
766
|
}
|
|
696
767
|
|
|
@@ -703,38 +774,36 @@ export class BinaryTree<
|
|
|
703
774
|
* Time Complexity: O(n)
|
|
704
775
|
* Space Complexity: O(log n)
|
|
705
776
|
*
|
|
706
|
-
* The function `getNodeByKey`
|
|
707
|
-
*
|
|
708
|
-
*
|
|
709
|
-
*
|
|
710
|
-
*
|
|
711
|
-
*
|
|
712
|
-
*
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
*/
|
|
716
|
-
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined {
|
|
777
|
+
* The function `getNodeByKey` returns a node with a specific key value from a tree structure.
|
|
778
|
+
* @param {K} key - The key parameter is the value that you want to search for in the tree. It is
|
|
779
|
+
* used to find the node with the matching key value.
|
|
780
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
781
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
782
|
+
* It has a default value of `'ITERATIVE'`.
|
|
783
|
+
* @returns a value of type NODE, null, or undefined.
|
|
784
|
+
*/
|
|
785
|
+
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBTNOrNull<NODE> {
|
|
717
786
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
718
787
|
}
|
|
719
788
|
|
|
720
789
|
override get<C extends BTNCallback<NODE, K>>(
|
|
721
790
|
identifier: K,
|
|
722
791
|
callback?: C,
|
|
723
|
-
beginRoot?:
|
|
792
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
724
793
|
iterationType?: IterationType
|
|
725
794
|
): V | undefined;
|
|
726
795
|
|
|
727
796
|
override get<C extends BTNCallback<NODE, NODE>>(
|
|
728
|
-
identifier: NODE
|
|
797
|
+
identifier: OptBTNOrNull<NODE>,
|
|
729
798
|
callback?: C,
|
|
730
|
-
beginRoot?:
|
|
799
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
731
800
|
iterationType?: IterationType
|
|
732
801
|
): V | undefined;
|
|
733
802
|
|
|
734
803
|
override get<C extends BTNCallback<NODE>>(
|
|
735
804
|
identifier: ReturnType<C>,
|
|
736
805
|
callback: C,
|
|
737
|
-
beginRoot?:
|
|
806
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
738
807
|
iterationType?: IterationType
|
|
739
808
|
): V | undefined;
|
|
740
809
|
|
|
@@ -747,28 +816,27 @@ export class BinaryTree<
|
|
|
747
816
|
* Time Complexity: O(n)
|
|
748
817
|
* Space Complexity: O(log n)
|
|
749
818
|
*
|
|
750
|
-
* The function `get`
|
|
751
|
-
*
|
|
819
|
+
* The function `get` in TypeScript overrides the base class method and returns the value associated
|
|
820
|
+
* with the given identifier.
|
|
752
821
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
753
|
-
* used to identify the node in the binary tree. It can be of any type that is
|
|
822
|
+
* used to identify the node in the binary tree. It can be of any type that is returned by the
|
|
754
823
|
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
755
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
756
|
-
* the
|
|
757
|
-
*
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
*
|
|
761
|
-
*
|
|
762
|
-
*
|
|
763
|
-
*
|
|
764
|
-
*
|
|
765
|
-
*
|
|
766
|
-
* found, `undefined` is returned.
|
|
824
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
825
|
+
* node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
826
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
827
|
+
* point for the search in the binary tree. It can be either a root node of the tree or a key, node,
|
|
828
|
+
* or entry object that exists in the tree. If no specific starting point is provided, the search
|
|
829
|
+
* will begin from the root of the
|
|
830
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
831
|
+
* of iteration to be performed when searching for a node in the tree. It can have one of the
|
|
832
|
+
* following values:
|
|
833
|
+
* @returns The method is returning the value associated with the specified identifier in the binary
|
|
834
|
+
* tree.
|
|
767
835
|
*/
|
|
768
836
|
override get<C extends BTNCallback<NODE>>(
|
|
769
837
|
identifier: ReturnType<C> | null | undefined,
|
|
770
838
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
771
|
-
beginRoot:
|
|
839
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
772
840
|
iterationType: IterationType = this.iterationType
|
|
773
841
|
): V | undefined {
|
|
774
842
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
|
|
@@ -777,54 +845,53 @@ export class BinaryTree<
|
|
|
777
845
|
override has<C extends BTNCallback<NODE, K>>(
|
|
778
846
|
identifier: K,
|
|
779
847
|
callback?: C,
|
|
780
|
-
beginRoot?:
|
|
848
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
781
849
|
iterationType?: IterationType
|
|
782
850
|
): boolean;
|
|
783
851
|
|
|
784
852
|
override has<C extends BTNCallback<NODE, NODE>>(
|
|
785
|
-
identifier: NODE
|
|
853
|
+
identifier: OptBTNOrNull<NODE>,
|
|
786
854
|
callback?: C,
|
|
787
|
-
beginRoot?:
|
|
855
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
788
856
|
iterationType?: IterationType
|
|
789
857
|
): boolean;
|
|
790
858
|
|
|
791
859
|
override has<C extends BTNCallback<NODE>>(
|
|
792
860
|
identifier: ReturnType<C> | null | undefined,
|
|
793
861
|
callback: C,
|
|
794
|
-
beginRoot?:
|
|
862
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
795
863
|
iterationType?: IterationType
|
|
796
864
|
): boolean;
|
|
797
865
|
|
|
798
866
|
/**
|
|
799
867
|
* Time Complexity: O(n)
|
|
800
|
-
* Space Complexity: O(log n)
|
|
868
|
+
* Space Complexity: O(log n)
|
|
801
869
|
*/
|
|
802
870
|
|
|
803
871
|
/**
|
|
804
872
|
* Time Complexity: O(n)
|
|
805
|
-
* Space Complexity: O(log n)
|
|
873
|
+
* Space Complexity: O(log n)
|
|
806
874
|
*
|
|
807
|
-
* The function checks if a
|
|
875
|
+
* The `has` function checks if a given identifier exists in the data structure and returns a boolean
|
|
876
|
+
* value.
|
|
808
877
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
809
|
-
*
|
|
810
|
-
* callback function `C`. It can also be `null` or `undefined` if
|
|
811
|
-
*
|
|
812
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
813
|
-
*
|
|
814
|
-
*
|
|
815
|
-
*
|
|
816
|
-
*
|
|
817
|
-
*
|
|
818
|
-
*
|
|
819
|
-
*
|
|
820
|
-
*
|
|
821
|
-
* be performed in a pre-order, in-order, or post-order manner.
|
|
822
|
-
* @returns a boolean value.
|
|
878
|
+
* used to identify a specific node or entry in the data structure. It can be of any type that is
|
|
879
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if no specific
|
|
880
|
+
* identifier is provided.
|
|
881
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine
|
|
882
|
+
* whether a node should be included in the result or not. It is of type `C`, which extends the
|
|
883
|
+
* `BTNCallback<NODE>` type.
|
|
884
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
885
|
+
* point for the iteration in the data structure. It can be either a root node, a key-value pair, or
|
|
886
|
+
* a node entry. If not specified, it defaults to the root of the data structure.
|
|
887
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
888
|
+
* of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
|
|
889
|
+
* @returns The method is returning a boolean value.
|
|
823
890
|
*/
|
|
824
891
|
override has<C extends BTNCallback<NODE>>(
|
|
825
892
|
identifier: ReturnType<C> | null | undefined,
|
|
826
893
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
827
|
-
beginRoot:
|
|
894
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
828
895
|
iterationType: IterationType = this.iterationType
|
|
829
896
|
): boolean {
|
|
830
897
|
callback = this._ensureCallback(identifier, callback);
|
|
@@ -875,12 +942,13 @@ export class BinaryTree<
|
|
|
875
942
|
*
|
|
876
943
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
877
944
|
* height of the tree.
|
|
878
|
-
* @param {
|
|
879
|
-
*
|
|
880
|
-
*
|
|
945
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
|
|
946
|
+
* has a default value of `this.root`. It represents the starting point for checking if the tree is
|
|
947
|
+
* perfectly balanced. It can be either a root node (`R`), a key or node or entry
|
|
948
|
+
* (`BTNKeyOrNodeOrEntry<K, V, NODE
|
|
881
949
|
* @returns a boolean value.
|
|
882
950
|
*/
|
|
883
|
-
isPerfectlyBalanced(beginRoot:
|
|
951
|
+
isPerfectlyBalanced(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
|
|
884
952
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
885
953
|
}
|
|
886
954
|
|
|
@@ -893,16 +961,18 @@ export class BinaryTree<
|
|
|
893
961
|
* Time Complexity: O(n)
|
|
894
962
|
* Space Complexity: O(1)
|
|
895
963
|
*
|
|
896
|
-
* The function `
|
|
897
|
-
* @param {
|
|
898
|
-
*
|
|
899
|
-
*
|
|
900
|
-
*
|
|
901
|
-
*
|
|
964
|
+
* The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
|
|
965
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
966
|
+
* starting point for checking if a binary search tree (BST) is valid. It can be either a root node
|
|
967
|
+
* of the BST, a key value of a node in the BST, or an entry object containing both the key and value
|
|
968
|
+
* of a node in the BST
|
|
969
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
970
|
+
* of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
|
|
971
|
+
* two possible values:
|
|
902
972
|
* @returns a boolean value.
|
|
903
973
|
*/
|
|
904
974
|
isBST(
|
|
905
|
-
beginRoot:
|
|
975
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
906
976
|
iterationType: IterationType = this.iterationType
|
|
907
977
|
): boolean {
|
|
908
978
|
// TODO there is a bug
|
|
@@ -910,7 +980,7 @@ export class BinaryTree<
|
|
|
910
980
|
if (!beginRoot) return true;
|
|
911
981
|
|
|
912
982
|
if (iterationType === 'RECURSIVE') {
|
|
913
|
-
const dfs = (cur: NODE
|
|
983
|
+
const dfs = (cur: OptBTNOrNull<NODE>, min: number, max: number): boolean => {
|
|
914
984
|
if (!this.isRealNode(cur)) return true;
|
|
915
985
|
const numKey = Number(cur.key);
|
|
916
986
|
if (numKey <= min || numKey >= max) return false;
|
|
@@ -925,7 +995,7 @@ export class BinaryTree<
|
|
|
925
995
|
const stack = [];
|
|
926
996
|
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
927
997
|
// @ts-ignore
|
|
928
|
-
let curr: NODE
|
|
998
|
+
let curr: OptBTNOrNull<NODE> = beginRoot;
|
|
929
999
|
while (this.isRealNode(curr) || stack.length > 0) {
|
|
930
1000
|
while (this.isRealNode(curr)) {
|
|
931
1001
|
stack.push(curr);
|
|
@@ -954,16 +1024,20 @@ export class BinaryTree<
|
|
|
954
1024
|
* Time Complexity: O(n)
|
|
955
1025
|
* Space Complexity: O(1)
|
|
956
1026
|
*
|
|
957
|
-
* The function calculates the depth of a given node in a
|
|
958
|
-
* @param {
|
|
959
|
-
*
|
|
960
|
-
*
|
|
961
|
-
* @param {
|
|
962
|
-
* from which
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
|
|
966
|
-
|
|
1027
|
+
* The function calculates the depth of a given node or key in a tree-like data structure.
|
|
1028
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
|
|
1029
|
+
* (representing a root node), or a `BTNKeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
|
|
1030
|
+
* entry).
|
|
1031
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
|
|
1032
|
+
* represents the starting point from which to calculate the depth. It can be either a reference to a
|
|
1033
|
+
* node in the tree or a key-value pair or an entry object. If not provided, the default value is
|
|
1034
|
+
* `this.root`, which refers to the root node
|
|
1035
|
+
* @returns the depth of a node in a tree structure.
|
|
1036
|
+
*/
|
|
1037
|
+
getDepth(
|
|
1038
|
+
dist: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1039
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
|
|
1040
|
+
): number {
|
|
967
1041
|
let distEnsured = this.ensureNode(dist);
|
|
968
1042
|
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
969
1043
|
let depth = 0;
|
|
@@ -984,27 +1058,26 @@ export class BinaryTree<
|
|
|
984
1058
|
|
|
985
1059
|
/**
|
|
986
1060
|
* Time Complexity: O(n)
|
|
987
|
-
* Space Complexity: O(
|
|
1061
|
+
* Space Complexity: O(1)
|
|
988
1062
|
*
|
|
989
|
-
* The
|
|
990
|
-
* iterative
|
|
991
|
-
* @param {
|
|
992
|
-
* starting
|
|
993
|
-
*
|
|
994
|
-
* @param iterationType - The `iterationType` parameter
|
|
995
|
-
*
|
|
996
|
-
*
|
|
997
|
-
* @returns the height of the binary tree.
|
|
1063
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
1064
|
+
* or iterative approach.
|
|
1065
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1066
|
+
* starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
|
|
1067
|
+
* node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
|
|
1068
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1069
|
+
* iteration used to calculate the height of the tree. It can have two possible values:
|
|
1070
|
+
* @returns the maximum height of the binary tree.
|
|
998
1071
|
*/
|
|
999
1072
|
getHeight(
|
|
1000
|
-
beginRoot:
|
|
1073
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1001
1074
|
iterationType: IterationType = this.iterationType
|
|
1002
1075
|
): number {
|
|
1003
1076
|
beginRoot = this.ensureNode(beginRoot);
|
|
1004
1077
|
if (!this.isRealNode(beginRoot)) return -1;
|
|
1005
1078
|
|
|
1006
1079
|
if (iterationType === 'RECURSIVE') {
|
|
1007
|
-
const _getMaxHeight = (cur: NODE
|
|
1080
|
+
const _getMaxHeight = (cur: OptBTNOrNull<NODE>): number => {
|
|
1008
1081
|
if (!this.isRealNode(cur)) return -1;
|
|
1009
1082
|
const leftHeight = _getMaxHeight(cur.left);
|
|
1010
1083
|
const rightHeight = _getMaxHeight(cur.right);
|
|
@@ -1040,22 +1113,25 @@ export class BinaryTree<
|
|
|
1040
1113
|
*
|
|
1041
1114
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
1042
1115
|
* recursive or iterative approach.
|
|
1043
|
-
* @param {
|
|
1044
|
-
* starting
|
|
1045
|
-
*
|
|
1046
|
-
*
|
|
1047
|
-
*
|
|
1048
|
-
*
|
|
1116
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1117
|
+
* starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
|
|
1118
|
+
* key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
|
|
1119
|
+
* tree.
|
|
1120
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1121
|
+
* iteration to be used when calculating the minimum height of the tree. It can have two possible
|
|
1122
|
+
* values:
|
|
1123
|
+
* @returns The function `getMinHeight` returns a number, which represents the minimum height of the
|
|
1124
|
+
* binary tree.
|
|
1049
1125
|
*/
|
|
1050
1126
|
getMinHeight(
|
|
1051
|
-
beginRoot:
|
|
1127
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1052
1128
|
iterationType: IterationType = this.iterationType
|
|
1053
1129
|
): number {
|
|
1054
1130
|
beginRoot = this.ensureNode(beginRoot);
|
|
1055
1131
|
if (!beginRoot) return -1;
|
|
1056
1132
|
|
|
1057
1133
|
if (iterationType === 'RECURSIVE') {
|
|
1058
|
-
const _getMinHeight = (cur: NODE
|
|
1134
|
+
const _getMinHeight = (cur: OptBTNOrNull<NODE>): number => {
|
|
1059
1135
|
if (!this.isRealNode(cur)) return 0;
|
|
1060
1136
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return 0;
|
|
1061
1137
|
const leftMinHeight = _getMinHeight(cur.left);
|
|
@@ -1066,8 +1142,8 @@ export class BinaryTree<
|
|
|
1066
1142
|
return _getMinHeight(beginRoot);
|
|
1067
1143
|
} else {
|
|
1068
1144
|
const stack: NODE[] = [];
|
|
1069
|
-
let node: NODE
|
|
1070
|
-
last: NODE
|
|
1145
|
+
let node: OptBTNOrNull<NODE> = beginRoot,
|
|
1146
|
+
last: OptBTNOrNull<NODE> = null;
|
|
1071
1147
|
const depths: Map<NODE, number> = new Map();
|
|
1072
1148
|
|
|
1073
1149
|
while (stack.length > 0 || node) {
|
|
@@ -1096,24 +1172,22 @@ export class BinaryTree<
|
|
|
1096
1172
|
/**
|
|
1097
1173
|
* Time Complexity: O(log n)
|
|
1098
1174
|
* Space Complexity: O(log n)
|
|
1099
|
-
|
|
1175
|
+
*/
|
|
1100
1176
|
|
|
1101
|
-
|
|
1177
|
+
/**
|
|
1102
1178
|
* Time Complexity: O(log n)
|
|
1103
1179
|
* Space Complexity: O(log n)
|
|
1104
1180
|
*
|
|
1105
|
-
* The function `getPathToRoot` returns an array of nodes from a given node
|
|
1106
|
-
*
|
|
1107
|
-
* @param {
|
|
1108
|
-
*
|
|
1109
|
-
* `null`, or `undefined`.
|
|
1181
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
1182
|
+
* up to the root node, with an option to reverse the order of the nodes.
|
|
1183
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
|
|
1184
|
+
* type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
1110
1185
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
1111
1186
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
1112
|
-
* reversed before returning it. If `isReverse` is set to `false
|
|
1113
|
-
* @returns The function `getPathToRoot` returns an array of
|
|
1187
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
1188
|
+
* @returns The function `getPathToRoot` returns an array of `NODE` objects.
|
|
1114
1189
|
*/
|
|
1115
|
-
getPathToRoot(beginNode:
|
|
1116
|
-
// TODO to support get path through passing key
|
|
1190
|
+
getPathToRoot(beginNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
|
|
1117
1191
|
const result: NODE[] = [];
|
|
1118
1192
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
1119
1193
|
|
|
@@ -1121,7 +1195,6 @@ export class BinaryTree<
|
|
|
1121
1195
|
|
|
1122
1196
|
while (beginNodeEnsured.parent) {
|
|
1123
1197
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
1124
|
-
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
1125
1198
|
result.push(beginNodeEnsured);
|
|
1126
1199
|
beginNodeEnsured = beginNodeEnsured.parent;
|
|
1127
1200
|
}
|
|
@@ -1138,20 +1211,19 @@ export class BinaryTree<
|
|
|
1138
1211
|
* Time Complexity: O(log n)
|
|
1139
1212
|
* Space Complexity: O(1)
|
|
1140
1213
|
*
|
|
1141
|
-
* The
|
|
1142
|
-
*
|
|
1143
|
-
* @param {
|
|
1144
|
-
* for finding the leftmost node in a binary tree. It can be either a `
|
|
1145
|
-
*
|
|
1146
|
-
* @param iterationType - The `iterationType` parameter is used to
|
|
1147
|
-
*
|
|
1148
|
-
* @returns The function `getLeftMost` returns the leftmost node
|
|
1149
|
-
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
1214
|
+
* The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
|
|
1215
|
+
* iterative traversal.
|
|
1216
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1217
|
+
* starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
|
|
1218
|
+
* a key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1219
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1220
|
+
* of iteration to be performed. It can have two possible values:
|
|
1221
|
+
* @returns The function `getLeftMost` returns the leftmost node in a binary tree.
|
|
1150
1222
|
*/
|
|
1151
1223
|
getLeftMost(
|
|
1152
|
-
beginRoot:
|
|
1224
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1153
1225
|
iterationType: IterationType = this.iterationType
|
|
1154
|
-
): NODE
|
|
1226
|
+
): OptBTNOrNull<NODE> {
|
|
1155
1227
|
if (this.isNIL(beginRoot)) return beginRoot as NODE;
|
|
1156
1228
|
beginRoot = this.ensureNode(beginRoot);
|
|
1157
1229
|
|
|
@@ -1184,21 +1256,20 @@ export class BinaryTree<
|
|
|
1184
1256
|
* Time Complexity: O(log n)
|
|
1185
1257
|
* Space Complexity: O(1)
|
|
1186
1258
|
*
|
|
1187
|
-
* The
|
|
1259
|
+
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
|
|
1188
1260
|
* iteratively.
|
|
1189
|
-
* @param {
|
|
1190
|
-
* starting
|
|
1191
|
-
* `
|
|
1192
|
-
*
|
|
1193
|
-
*
|
|
1194
|
-
*
|
|
1195
|
-
* @returns The function `getRightMost` returns
|
|
1196
|
-
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1261
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1262
|
+
* starting point for finding the rightmost node in a binary tree. It can be either a root node
|
|
1263
|
+
* (`R`), a key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1264
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1265
|
+
* of iteration to be performed when finding the rightmost node in a binary tree. It can have two
|
|
1266
|
+
* possible values:
|
|
1267
|
+
* @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
|
|
1197
1268
|
*/
|
|
1198
1269
|
getRightMost(
|
|
1199
|
-
beginRoot:
|
|
1270
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1200
1271
|
iterationType: IterationType = this.iterationType
|
|
1201
|
-
): NODE
|
|
1272
|
+
): OptBTNOrNull<NODE> {
|
|
1202
1273
|
if (this.isNIL(beginRoot)) return beginRoot as NODE;
|
|
1203
1274
|
// TODO support get right most by passing key in
|
|
1204
1275
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1231,14 +1302,14 @@ export class BinaryTree<
|
|
|
1231
1302
|
* Time Complexity: O(log n)
|
|
1232
1303
|
* Space Complexity: O(1)
|
|
1233
1304
|
*
|
|
1234
|
-
* The function returns the predecessor of a given node in a tree.
|
|
1235
|
-
* @param {NODE} node - The parameter
|
|
1305
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
1306
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
|
|
1236
1307
|
* tree.
|
|
1237
|
-
* @returns the predecessor of the given
|
|
1308
|
+
* @returns the predecessor node of the given node.
|
|
1238
1309
|
*/
|
|
1239
1310
|
getPredecessor(node: NODE): NODE {
|
|
1240
1311
|
if (this.isRealNode(node.left)) {
|
|
1241
|
-
let predecessor: NODE
|
|
1312
|
+
let predecessor: OptBTNOrNull<NODE> = node.left;
|
|
1242
1313
|
while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
|
|
1243
1314
|
if (this.isRealNode(predecessor)) {
|
|
1244
1315
|
predecessor = predecessor.right;
|
|
@@ -1261,10 +1332,10 @@ export class BinaryTree<
|
|
|
1261
1332
|
*
|
|
1262
1333
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1263
1334
|
* @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
|
|
1264
|
-
* @returns
|
|
1265
|
-
*
|
|
1335
|
+
* @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
|
|
1336
|
+
* there is no successor, and `undefined` if the input `x` is not a valid node.
|
|
1266
1337
|
*/
|
|
1267
|
-
getSuccessor(x?: K | NODE | null): NODE
|
|
1338
|
+
getSuccessor(x?: K | NODE | null): OptBTNOrNull<NODE> {
|
|
1268
1339
|
x = this.ensureNode(x);
|
|
1269
1340
|
if (!this.isRealNode(x)) return undefined;
|
|
1270
1341
|
|
|
@@ -1272,7 +1343,7 @@ export class BinaryTree<
|
|
|
1272
1343
|
return this.getLeftMost(x.right);
|
|
1273
1344
|
}
|
|
1274
1345
|
|
|
1275
|
-
let y: NODE
|
|
1346
|
+
let y: OptBTNOrNull<NODE> = x.parent;
|
|
1276
1347
|
while (this.isRealNode(y) && x === y.right) {
|
|
1277
1348
|
x = y;
|
|
1278
1349
|
y = y.parent;
|
|
@@ -1283,7 +1354,7 @@ export class BinaryTree<
|
|
|
1283
1354
|
dfs<C extends BTNCallback<NODE>>(
|
|
1284
1355
|
callback?: C,
|
|
1285
1356
|
pattern?: DFSOrderPattern,
|
|
1286
|
-
beginRoot?:
|
|
1357
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1287
1358
|
iterationType?: IterationType,
|
|
1288
1359
|
includeNull?: false
|
|
1289
1360
|
): ReturnType<C>[];
|
|
@@ -1291,7 +1362,7 @@ export class BinaryTree<
|
|
|
1291
1362
|
dfs<C extends BTNCallback<NODE | null>>(
|
|
1292
1363
|
callback?: C,
|
|
1293
1364
|
pattern?: DFSOrderPattern,
|
|
1294
|
-
beginRoot?:
|
|
1365
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1295
1366
|
iterationType?: IterationType,
|
|
1296
1367
|
includeNull?: true
|
|
1297
1368
|
): ReturnType<C>[];
|
|
@@ -1299,35 +1370,35 @@ export class BinaryTree<
|
|
|
1299
1370
|
/**
|
|
1300
1371
|
* Time complexity: O(n)
|
|
1301
1372
|
* Space complexity: O(n)
|
|
1302
|
-
|
|
1373
|
+
*/
|
|
1303
1374
|
|
|
1304
|
-
|
|
1375
|
+
/**
|
|
1305
1376
|
* Time complexity: O(n)
|
|
1306
1377
|
* Space complexity: O(n)
|
|
1307
1378
|
*
|
|
1308
|
-
* The `dfs` function performs a depth-first search traversal on a binary tree
|
|
1309
|
-
*
|
|
1310
|
-
* callback function
|
|
1311
|
-
*
|
|
1312
|
-
*
|
|
1313
|
-
*
|
|
1314
|
-
*
|
|
1315
|
-
*
|
|
1316
|
-
*
|
|
1317
|
-
*
|
|
1318
|
-
*
|
|
1319
|
-
* @param {IterationType} iterationType - The `iterationType` parameter determines the
|
|
1320
|
-
* iteration to use
|
|
1379
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
1380
|
+
* function on each node according to a specified pattern and iteration type.
|
|
1381
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
1382
|
+
* visited during the depth-first search. It takes a node as an argument and returns a value. The
|
|
1383
|
+
* return type of the callback function is determined by the generic type `C`.
|
|
1384
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
|
|
1385
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
1386
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1387
|
+
* point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
|
|
1388
|
+
* is a key or key-value pair, the method will find the corresponding node in the tree and start the
|
|
1389
|
+
* search from there.
|
|
1390
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
|
|
1391
|
+
* type of iteration to use during the depth-first search. It can have two possible values:
|
|
1321
1392
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1322
|
-
* whether
|
|
1323
|
-
* `true`, null
|
|
1324
|
-
*
|
|
1325
|
-
* @returns an array of
|
|
1393
|
+
* whether or not to include null values in the depth-first search traversal. If `includeNull` is set
|
|
1394
|
+
* to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
|
|
1395
|
+
* values will
|
|
1396
|
+
* @returns an array of the return types of the callback function.
|
|
1326
1397
|
*/
|
|
1327
|
-
dfs<C extends BTNCallback<NODE
|
|
1398
|
+
dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1328
1399
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1329
1400
|
pattern: DFSOrderPattern = 'IN',
|
|
1330
|
-
beginRoot:
|
|
1401
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1331
1402
|
iterationType: IterationType = 'ITERATIVE',
|
|
1332
1403
|
includeNull = false
|
|
1333
1404
|
): ReturnType<C>[] {
|
|
@@ -1335,7 +1406,7 @@ export class BinaryTree<
|
|
|
1335
1406
|
if (!beginRoot) return [];
|
|
1336
1407
|
const ans: ReturnType<C>[] = [];
|
|
1337
1408
|
if (iterationType === 'RECURSIVE') {
|
|
1338
|
-
const dfs = (node: NODE
|
|
1409
|
+
const dfs = (node: OptBTNOrNull<NODE>) => {
|
|
1339
1410
|
switch (pattern) {
|
|
1340
1411
|
case 'IN':
|
|
1341
1412
|
if (includeNull) {
|
|
@@ -1377,7 +1448,7 @@ export class BinaryTree<
|
|
|
1377
1448
|
dfs(beginRoot);
|
|
1378
1449
|
} else {
|
|
1379
1450
|
// 0: visit, 1: print
|
|
1380
|
-
const stack: { opt: 0 | 1; node: NODE
|
|
1451
|
+
const stack: { opt: 0 | 1; node: OptBTNOrNull<NODE> }[] = [{ opt: 0, node: beginRoot }];
|
|
1381
1452
|
|
|
1382
1453
|
while (stack.length > 0) {
|
|
1383
1454
|
const cur = stack.pop();
|
|
@@ -1421,14 +1492,14 @@ export class BinaryTree<
|
|
|
1421
1492
|
|
|
1422
1493
|
bfs<C extends BTNCallback<NODE>>(
|
|
1423
1494
|
callback?: C,
|
|
1424
|
-
beginRoot?:
|
|
1495
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1425
1496
|
iterationType?: IterationType,
|
|
1426
1497
|
includeNull?: false
|
|
1427
1498
|
): ReturnType<C>[];
|
|
1428
1499
|
|
|
1429
1500
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1430
1501
|
callback?: C,
|
|
1431
|
-
beginRoot?:
|
|
1502
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1432
1503
|
iterationType?: IterationType,
|
|
1433
1504
|
includeNull?: true
|
|
1434
1505
|
): ReturnType<C>[];
|
|
@@ -1442,26 +1513,27 @@ export class BinaryTree<
|
|
|
1442
1513
|
* Time complexity: O(n)
|
|
1443
1514
|
* Space complexity: O(n)
|
|
1444
1515
|
*
|
|
1445
|
-
* The `bfs` function performs a breadth-first search
|
|
1446
|
-
*
|
|
1516
|
+
* The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
|
|
1517
|
+
* on each node and returning an array of the results.
|
|
1447
1518
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1448
|
-
* the breadth-first search traversal. It takes a single
|
|
1519
|
+
* the breadth-first search traversal. It takes a single argument, which is the current node being
|
|
1449
1520
|
* visited, and returns a value of any type.
|
|
1450
|
-
* @param {
|
|
1451
|
-
* starting
|
|
1452
|
-
* or
|
|
1453
|
-
*
|
|
1454
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
1455
|
-
*
|
|
1456
|
-
* @param [includeNull=false] - The `includeNull` parameter is a boolean
|
|
1457
|
-
* to include null values in the breadth-first search traversal. If
|
|
1458
|
-
* `true`, null values will be included in the traversal
|
|
1459
|
-
*
|
|
1460
|
-
*
|
|
1521
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1522
|
+
* starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
|
|
1523
|
+
* or entry object. If no value is provided, the `root` property of the class is used as the default
|
|
1524
|
+
* starting point.
|
|
1525
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1526
|
+
* iteration to be performed. It can have two possible values:
|
|
1527
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1528
|
+
* whether or not to include null values in the breadth-first search (BFS) traversal. If
|
|
1529
|
+
* `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
|
|
1530
|
+
* set to `false
|
|
1531
|
+
* @returns The function `bfs` returns an array of values that are the result of invoking the
|
|
1532
|
+
* `callback` function on each node in the breadth-first order traversal of the binary tree.
|
|
1461
1533
|
*/
|
|
1462
1534
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1463
1535
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1464
|
-
beginRoot:
|
|
1536
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1465
1537
|
iterationType: IterationType = this.iterationType,
|
|
1466
1538
|
includeNull = false
|
|
1467
1539
|
): ReturnType<C>[] {
|
|
@@ -1471,7 +1543,7 @@ export class BinaryTree<
|
|
|
1471
1543
|
const ans: ReturnType<BTNCallback<NODE>>[] = [];
|
|
1472
1544
|
|
|
1473
1545
|
if (iterationType === 'RECURSIVE') {
|
|
1474
|
-
const queue: Queue<NODE
|
|
1546
|
+
const queue: Queue<OptBTNOrNull<NODE>> = new Queue<OptBTNOrNull<NODE>>([beginRoot]);
|
|
1475
1547
|
|
|
1476
1548
|
const dfs = (level: number) => {
|
|
1477
1549
|
if (queue.size === 0) return;
|
|
@@ -1492,7 +1564,7 @@ export class BinaryTree<
|
|
|
1492
1564
|
|
|
1493
1565
|
dfs(0);
|
|
1494
1566
|
} else {
|
|
1495
|
-
const queue = new Queue<NODE
|
|
1567
|
+
const queue = new Queue<OptBTNOrNull<NODE>>([beginRoot]);
|
|
1496
1568
|
while (queue.size > 0) {
|
|
1497
1569
|
const levelSize = queue.size;
|
|
1498
1570
|
|
|
@@ -1515,14 +1587,14 @@ export class BinaryTree<
|
|
|
1515
1587
|
|
|
1516
1588
|
listLevels<C extends BTNCallback<NODE>>(
|
|
1517
1589
|
callback?: C,
|
|
1518
|
-
beginRoot?:
|
|
1590
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1519
1591
|
iterationType?: IterationType,
|
|
1520
1592
|
includeNull?: false
|
|
1521
1593
|
): ReturnType<C>[][];
|
|
1522
1594
|
|
|
1523
1595
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1524
1596
|
callback?: C,
|
|
1525
|
-
beginRoot?:
|
|
1597
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1526
1598
|
iterationType?: IterationType,
|
|
1527
1599
|
includeNull?: true
|
|
1528
1600
|
): ReturnType<C>[][];
|
|
@@ -1537,25 +1609,25 @@ export class BinaryTree<
|
|
|
1537
1609
|
* Space complexity: O(n)
|
|
1538
1610
|
*
|
|
1539
1611
|
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
1540
|
-
* a binary tree and contains the
|
|
1541
|
-
* level.
|
|
1612
|
+
* a binary tree and contains the results of applying a callback function to the nodes at that level.
|
|
1542
1613
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1543
|
-
* the tree. It takes a
|
|
1544
|
-
*
|
|
1545
|
-
* @param {
|
|
1546
|
-
* starting
|
|
1547
|
-
*
|
|
1548
|
-
*
|
|
1549
|
-
*
|
|
1614
|
+
* the tree. It takes a node as an argument and returns a value. The return type of the callback
|
|
1615
|
+
* function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
|
|
1616
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1617
|
+
* starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
|
|
1618
|
+
* entry. If no value is provided, the `root` property of the class is used as the default starting
|
|
1619
|
+
* point.
|
|
1620
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1621
|
+
* iteration to be performed on the binary tree. It can have two possible values:
|
|
1550
1622
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1551
|
-
* whether to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1623
|
+
* whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1552
1624
|
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
1553
1625
|
* be excluded
|
|
1554
1626
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1555
1627
|
*/
|
|
1556
1628
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1557
1629
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1558
|
-
beginRoot:
|
|
1630
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1559
1631
|
iterationType: IterationType = this.iterationType,
|
|
1560
1632
|
includeNull = false
|
|
1561
1633
|
): ReturnType<C>[][] {
|
|
@@ -1612,31 +1684,31 @@ export class BinaryTree<
|
|
|
1612
1684
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1613
1685
|
* algorithm.
|
|
1614
1686
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1615
|
-
* the tree. It takes a single
|
|
1616
|
-
*
|
|
1617
|
-
*
|
|
1618
|
-
*
|
|
1687
|
+
* the tree. It takes a single argument, which is the current node, and can return any value. The
|
|
1688
|
+
* return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
|
|
1689
|
+
* the return
|
|
1690
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
|
|
1691
|
+
* to specify the order in which the nodes of a binary tree are traversed. It can take one of the
|
|
1619
1692
|
* following values:
|
|
1620
|
-
* @param {
|
|
1621
|
-
* for the traversal. It can be
|
|
1622
|
-
* the root of the tree
|
|
1623
|
-
* @returns The function `morris` returns an array of values that are the
|
|
1624
|
-
*
|
|
1625
|
-
* by the return type of the `callback` function.
|
|
1693
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1694
|
+
* point for the traversal. It can be either a node object, a key, or an entry object. If no value is
|
|
1695
|
+
* provided, the `root` of the tree is used as the starting point.
|
|
1696
|
+
* @returns The function `morris` returns an array of values that are the return values of the
|
|
1697
|
+
* callback function `callback`.
|
|
1626
1698
|
*/
|
|
1627
1699
|
morris<C extends BTNCallback<NODE>>(
|
|
1628
1700
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1629
1701
|
pattern: DFSOrderPattern = 'IN',
|
|
1630
|
-
beginRoot:
|
|
1702
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
|
|
1631
1703
|
): ReturnType<C>[] {
|
|
1632
1704
|
beginRoot = this.ensureNode(beginRoot);
|
|
1633
1705
|
if (beginRoot === null) return [];
|
|
1634
1706
|
const ans: ReturnType<BTNCallback<NODE>>[] = [];
|
|
1635
1707
|
|
|
1636
|
-
let cur: NODE
|
|
1637
|
-
const _reverseEdge = (node: NODE
|
|
1638
|
-
let pre: NODE
|
|
1639
|
-
let next: NODE
|
|
1708
|
+
let cur: OptBTNOrNull<NODE> = beginRoot;
|
|
1709
|
+
const _reverseEdge = (node: OptBTNOrNull<NODE>) => {
|
|
1710
|
+
let pre: OptBTNOrNull<NODE> = null;
|
|
1711
|
+
let next: OptBTNOrNull<NODE> = null;
|
|
1640
1712
|
while (node) {
|
|
1641
1713
|
next = node.right;
|
|
1642
1714
|
node.right = pre;
|
|
@@ -1645,9 +1717,9 @@ export class BinaryTree<
|
|
|
1645
1717
|
}
|
|
1646
1718
|
return pre;
|
|
1647
1719
|
};
|
|
1648
|
-
const _printEdge = (node: NODE
|
|
1649
|
-
const tail: NODE
|
|
1650
|
-
let cur: NODE
|
|
1720
|
+
const _printEdge = (node: OptBTNOrNull<NODE>) => {
|
|
1721
|
+
const tail: OptBTNOrNull<NODE> = _reverseEdge(node);
|
|
1722
|
+
let cur: OptBTNOrNull<NODE> = tail;
|
|
1651
1723
|
while (cur) {
|
|
1652
1724
|
ans.push(callback(cur));
|
|
1653
1725
|
cur = cur.right;
|
|
@@ -1719,8 +1791,7 @@ export class BinaryTree<
|
|
|
1719
1791
|
* Time complexity: O(n)
|
|
1720
1792
|
* Space complexity: O(n)
|
|
1721
1793
|
*
|
|
1722
|
-
* The `clone` function creates a
|
|
1723
|
-
* the new tree.
|
|
1794
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
1724
1795
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
1725
1796
|
*/
|
|
1726
1797
|
clone(): TREE {
|
|
@@ -1746,16 +1817,16 @@ export class BinaryTree<
|
|
|
1746
1817
|
* Time Complexity: O(n)
|
|
1747
1818
|
* Space Complexity: O(n)
|
|
1748
1819
|
*
|
|
1749
|
-
* The `filter` function creates a new tree
|
|
1750
|
-
*
|
|
1751
|
-
*
|
|
1752
|
-
*
|
|
1753
|
-
*
|
|
1754
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1755
|
-
*
|
|
1756
|
-
*
|
|
1757
|
-
* @returns The `filter` method is returning a new tree object that contains the
|
|
1758
|
-
*
|
|
1820
|
+
* The `filter` function creates a new tree with entries that pass a given predicate function.
|
|
1821
|
+
* @param predicate - The `predicate` parameter is a callback function that is used to test each
|
|
1822
|
+
* element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
|
|
1823
|
+
* represents the value of the current element being processed, the `key` argument represents the key
|
|
1824
|
+
* of the
|
|
1825
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1826
|
+
* specify the value of `this` within the `predicate` function. When the `predicate` function is
|
|
1827
|
+
* called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
|
|
1828
|
+
* @returns The `filter` method is returning a new tree object that contains the entries that pass
|
|
1829
|
+
* the given predicate function.
|
|
1759
1830
|
*/
|
|
1760
1831
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
|
|
1761
1832
|
const newTree = this.createTree();
|
|
@@ -1777,15 +1848,15 @@ export class BinaryTree<
|
|
|
1777
1848
|
* Time Complexity: O(n)
|
|
1778
1849
|
* Space Complexity: O(n)
|
|
1779
1850
|
*
|
|
1780
|
-
* The `map` function creates a new tree by applying a callback function to each
|
|
1781
|
-
*
|
|
1782
|
-
* @param callback - The callback parameter is a function that will be called for each
|
|
1783
|
-
*
|
|
1784
|
-
* the
|
|
1785
|
-
*
|
|
1786
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1787
|
-
*
|
|
1788
|
-
*
|
|
1851
|
+
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
1852
|
+
* tree.
|
|
1853
|
+
* @param callback - The callback parameter is a function that will be called for each entry in the
|
|
1854
|
+
* tree. It takes three arguments: value, key, and index. The value argument represents the value of
|
|
1855
|
+
* the current entry, the key argument represents the key of the current entry, and the index
|
|
1856
|
+
* argument represents the index of the
|
|
1857
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1858
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
1859
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
1789
1860
|
* @returns The `map` method is returning a new tree object.
|
|
1790
1861
|
*/
|
|
1791
1862
|
map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
|
|
@@ -1816,13 +1887,17 @@ export class BinaryTree<
|
|
|
1816
1887
|
* Time Complexity: O(n)
|
|
1817
1888
|
* Space Complexity: O(n)
|
|
1818
1889
|
*
|
|
1819
|
-
* The `print` function
|
|
1820
|
-
* @param {
|
|
1821
|
-
*
|
|
1822
|
-
*
|
|
1823
|
-
*
|
|
1824
|
-
|
|
1825
|
-
|
|
1890
|
+
* The `print` function in TypeScript prints the binary tree structure with customizable options.
|
|
1891
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1892
|
+
* point for printing the binary tree. It can be either a node of the binary tree or a key or entry
|
|
1893
|
+
* that exists in the binary tree. If no value is provided, the root of the binary tree will be used
|
|
1894
|
+
* as the starting point.
|
|
1895
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
|
|
1896
|
+
* allows you to customize the printing behavior. It has the following properties:
|
|
1897
|
+
* @returns Nothing is being returned. The function has a return type of `void`, which means it does
|
|
1898
|
+
* not return any value.
|
|
1899
|
+
*/
|
|
1900
|
+
override print(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
|
|
1826
1901
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1827
1902
|
beginRoot = this.ensureNode(beginRoot);
|
|
1828
1903
|
if (!beginRoot) return;
|
|
@@ -1837,7 +1912,7 @@ export class BinaryTree<
|
|
|
1837
1912
|
console.log(`S for Sentinel Node(NIL)
|
|
1838
1913
|
`);
|
|
1839
1914
|
|
|
1840
|
-
const display = (root: NODE
|
|
1915
|
+
const display = (root: OptBTNOrNull<NODE>): void => {
|
|
1841
1916
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
1842
1917
|
for (const line of lines) {
|
|
1843
1918
|
console.log(line);
|
|
@@ -1848,20 +1923,26 @@ export class BinaryTree<
|
|
|
1848
1923
|
}
|
|
1849
1924
|
|
|
1850
1925
|
/**
|
|
1851
|
-
*
|
|
1852
|
-
*
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
*
|
|
1857
|
-
*
|
|
1926
|
+
* Time Complexity: O(1)
|
|
1927
|
+
* Space Complexity: O(1)
|
|
1928
|
+
*/
|
|
1929
|
+
|
|
1930
|
+
/**
|
|
1931
|
+
* Time Complexity: O(1)
|
|
1932
|
+
* Space Complexity: O(1)
|
|
1933
|
+
*
|
|
1934
|
+
* The function `_getIterator` is a generator function that returns an iterator for the key-value
|
|
1935
|
+
* pairs in a binary search tree.
|
|
1936
|
+
* @param node - The `node` parameter represents the current node in the binary search tree. It is
|
|
1937
|
+
* initially set to the root node of the tree.
|
|
1938
|
+
* @returns an IterableIterator<[K, V | undefined]>.
|
|
1858
1939
|
*/
|
|
1859
1940
|
protected* _getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
|
|
1860
1941
|
if (!node) return;
|
|
1861
1942
|
|
|
1862
1943
|
if (this.iterationType === 'ITERATIVE') {
|
|
1863
|
-
const stack:
|
|
1864
|
-
let current: NODE
|
|
1944
|
+
const stack: OptBTNOrNull<NODE>[] = [];
|
|
1945
|
+
let current: OptBTNOrNull<NODE> = node;
|
|
1865
1946
|
|
|
1866
1947
|
while (current || stack.length > 0) {
|
|
1867
1948
|
while (this.isRealNode(current)) {
|
|
@@ -1888,9 +1969,17 @@ export class BinaryTree<
|
|
|
1888
1969
|
}
|
|
1889
1970
|
|
|
1890
1971
|
/**
|
|
1972
|
+
* Time Complexity: O(n)
|
|
1973
|
+
* Space Complexity: O(n)
|
|
1974
|
+
*/
|
|
1975
|
+
|
|
1976
|
+
/**
|
|
1977
|
+
* Time Complexity: O(n)
|
|
1978
|
+
* Space Complexity: O(n)
|
|
1979
|
+
*
|
|
1891
1980
|
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
1892
1981
|
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
1893
|
-
* @param {NODE
|
|
1982
|
+
* @param {OptBTNOrNull<NODE>} node - The `node` parameter represents a node in a binary tree.
|
|
1894
1983
|
* It can be of type `NODE`, `null`, or `undefined`.
|
|
1895
1984
|
* @param {BinaryTreePrintOptions} options - The `options` parameter is an object that contains the
|
|
1896
1985
|
* following properties:
|
|
@@ -1901,7 +1990,7 @@ export class BinaryTree<
|
|
|
1901
1990
|
* 3. `totalHeight`: The total height of the node display.
|
|
1902
1991
|
* 4. `middleIndex`: The index of the middle character
|
|
1903
1992
|
*/
|
|
1904
|
-
protected _displayAux(node: NODE
|
|
1993
|
+
protected _displayAux(node: OptBTNOrNull<NODE>, options: BinaryTreePrintOptions): NodeDisplayLayout {
|
|
1905
1994
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1906
1995
|
const emptyDisplayLayout = <NodeDisplayLayout>[['─'], 1, 0, 0];
|
|
1907
1996
|
|
|
@@ -1916,7 +2005,7 @@ export class BinaryTree<
|
|
|
1916
2005
|
// Display logic of normal nodes
|
|
1917
2006
|
|
|
1918
2007
|
const key = node.key,
|
|
1919
|
-
line = this.isNIL(node) ? 'S' : key
|
|
2008
|
+
line = this.isNIL(node) ? 'S' : String(key),
|
|
1920
2009
|
width = line.length;
|
|
1921
2010
|
|
|
1922
2011
|
return _buildNodeDisplay(
|
|
@@ -1927,7 +2016,7 @@ export class BinaryTree<
|
|
|
1927
2016
|
);
|
|
1928
2017
|
} else {
|
|
1929
2018
|
// For cases where none of the conditions are met, null, undefined, and NaN nodes are not displayed
|
|
1930
|
-
const line = node === undefined ? 'U' : '
|
|
2019
|
+
const line = node === undefined ? 'U' : 'N',
|
|
1931
2020
|
width = line.length;
|
|
1932
2021
|
|
|
1933
2022
|
return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);
|
|
@@ -1969,17 +2058,29 @@ export class BinaryTree<
|
|
|
1969
2058
|
}
|
|
1970
2059
|
}
|
|
1971
2060
|
|
|
1972
|
-
protected _DEFAULT_CALLBACK = (node: NODE
|
|
2061
|
+
protected _DEFAULT_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
|
|
1973
2062
|
|
|
1974
2063
|
/**
|
|
1975
|
-
*
|
|
1976
|
-
*
|
|
1977
|
-
|
|
1978
|
-
|
|
2064
|
+
* Time Complexity: O(1)
|
|
2065
|
+
* Space Complexity: O(1)
|
|
2066
|
+
*/
|
|
2067
|
+
|
|
2068
|
+
/**
|
|
2069
|
+
* Time Complexity: O(1)
|
|
2070
|
+
* Space Complexity: O(1)
|
|
2071
|
+
*
|
|
2072
|
+
* The function `_swapProperties` swaps the key-value properties between two nodes.
|
|
2073
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
|
|
2074
|
+
* destination node. It can be either an instance of the class `R`, or an object of type
|
|
2075
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
2076
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
|
|
2077
|
+
* the properties will be swapped with the `srcNode`.
|
|
2078
|
+
* @returns either the `destNode` object with its properties swapped with the `srcNode` object's
|
|
2079
|
+
* properties, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
1979
2080
|
*/
|
|
1980
2081
|
protected _swapProperties(
|
|
1981
|
-
srcNode:
|
|
1982
|
-
destNode:
|
|
2082
|
+
srcNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
2083
|
+
destNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
1983
2084
|
): NODE | undefined {
|
|
1984
2085
|
srcNode = this.ensureNode(srcNode);
|
|
1985
2086
|
destNode = this.ensureNode(destNode);
|
|
@@ -2002,12 +2103,21 @@ export class BinaryTree<
|
|
|
2002
2103
|
}
|
|
2003
2104
|
|
|
2004
2105
|
/**
|
|
2005
|
-
*
|
|
2106
|
+
* Time Complexity: O(1)
|
|
2107
|
+
* Space Complexity: O(1)
|
|
2108
|
+
*/
|
|
2109
|
+
|
|
2110
|
+
/**
|
|
2111
|
+
* Time Complexity: O(1)
|
|
2112
|
+
* Space Complexity: O(1)
|
|
2113
|
+
*
|
|
2114
|
+
* The function replaces a node in a binary tree with a new node, updating the parent, left child,
|
|
2115
|
+
* right child, and root if necessary.
|
|
2006
2116
|
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
2007
2117
|
* tree.
|
|
2008
2118
|
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
2009
2119
|
* tree.
|
|
2010
|
-
* @returns
|
|
2120
|
+
* @returns the newNode.
|
|
2011
2121
|
*/
|
|
2012
2122
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
2013
2123
|
if (oldNode.parent) {
|
|
@@ -2028,18 +2138,44 @@ export class BinaryTree<
|
|
|
2028
2138
|
}
|
|
2029
2139
|
|
|
2030
2140
|
/**
|
|
2031
|
-
*
|
|
2032
|
-
*
|
|
2033
|
-
* @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`, which means it can either be of
|
|
2034
|
-
* type `NODE` or `null`.
|
|
2141
|
+
* Time Complexity: O(1)
|
|
2142
|
+
* Space Complexity: O(1)
|
|
2035
2143
|
*/
|
|
2036
|
-
|
|
2144
|
+
|
|
2145
|
+
/**
|
|
2146
|
+
* Time Complexity: O(1)
|
|
2147
|
+
* Space Complexity: O(1)
|
|
2148
|
+
*
|
|
2149
|
+
* The function sets the root property of an object to the provided value, and also updates the
|
|
2150
|
+
* parent property of the new root.
|
|
2151
|
+
* @param {OptBTNOrNull<NODE>} v - The parameter `v` is of type `OptBTNOrNull<NODE>`. This
|
|
2152
|
+
* means that it can accept a value of type `NODE`, `null`, or `undefined`.
|
|
2153
|
+
*/
|
|
2154
|
+
protected _setRoot(v: OptBTNOrNull<NODE>) {
|
|
2037
2155
|
if (v) {
|
|
2038
2156
|
v.parent = undefined;
|
|
2039
2157
|
}
|
|
2040
2158
|
this._root = v;
|
|
2041
2159
|
}
|
|
2042
2160
|
|
|
2161
|
+
/**
|
|
2162
|
+
* Time Complexity: O(1)
|
|
2163
|
+
* Space Complexity: O(1)
|
|
2164
|
+
*/
|
|
2165
|
+
|
|
2166
|
+
/**
|
|
2167
|
+
* Time Complexity: O(1)
|
|
2168
|
+
* Space Complexity: O(1)
|
|
2169
|
+
*
|
|
2170
|
+
* The function `_ensureCallback` ensures that a callback function is provided and returns it.
|
|
2171
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
|
|
2172
|
+
* `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
|
|
2173
|
+
* the generic type `C`, or it can be `null` or `undefined`.
|
|
2174
|
+
* @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
|
|
2175
|
+
* and returns a value. It is of type `C`, which is a generic type that extends the
|
|
2176
|
+
* `BTNCallback<NODE>` type.
|
|
2177
|
+
* @returns the callback parameter.
|
|
2178
|
+
*/
|
|
2043
2179
|
protected _ensureCallback<C extends BTNCallback<NODE>>(
|
|
2044
2180
|
identifier: ReturnType<C> | null | undefined,
|
|
2045
2181
|
callback: C = this._DEFAULT_CALLBACK as C
|