bst-typed 1.52.6 → 1.52.8
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/constants/index.d.ts +4 -0
- package/dist/constants/index.js +8 -0
- package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-element-base.js +10 -1
- package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/data-structures/binary-tree/binary-tree.js +668 -597
- package/dist/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/data-structures/binary-tree/bst.js +115 -113
- package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.js +7 -4
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +4 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/heap/heap.js +3 -3
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.js +4 -2
- package/dist/data-structures/queue/deque.d.ts +3 -3
- package/dist/data-structures/queue/deque.js +29 -29
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/stack/stack.d.ts +2 -2
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/package.json +2 -2
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +697 -725
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +2 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
@@ -15,18 +15,20 @@ import type {
|
|
|
15
15
|
BTNCallback,
|
|
16
16
|
BTNEntry,
|
|
17
17
|
BTNKeyOrNodeOrEntry,
|
|
18
|
+
BTNPredicate,
|
|
18
19
|
DFSOrderPattern,
|
|
20
|
+
DFSStackItem,
|
|
19
21
|
EntryCallback,
|
|
20
22
|
FamilyPosition,
|
|
21
23
|
IterationType,
|
|
22
24
|
NodeDisplayLayout,
|
|
23
25
|
OptBTNOrNull
|
|
24
26
|
} from '../../types';
|
|
25
|
-
import { DFSOperation, DFSStackItem } from '../../types';
|
|
26
27
|
import { IBinaryTree } from '../../interfaces';
|
|
27
28
|
import { isComparable, trampoline } from '../../utils';
|
|
28
29
|
import { Queue } from '../queue';
|
|
29
30
|
import { IterableEntryBase } from '../base';
|
|
31
|
+
import { DFSOperation } from '../../constants';
|
|
30
32
|
|
|
31
33
|
/**
|
|
32
34
|
* Represents a node in a binary tree.
|
|
@@ -44,13 +46,6 @@ export class BinaryTreeNode<
|
|
|
44
46
|
|
|
45
47
|
parent?: NODE;
|
|
46
48
|
|
|
47
|
-
/**
|
|
48
|
-
* The constructor function initializes an object with a key and an optional value.
|
|
49
|
-
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
|
|
50
|
-
* constructor. It is used to set the key property of the object being created.
|
|
51
|
-
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
52
|
-
* value associated with the key in the constructor.
|
|
53
|
-
*/
|
|
54
49
|
constructor(key: K, value?: V) {
|
|
55
50
|
this.key = key;
|
|
56
51
|
this.value = value;
|
|
@@ -58,21 +53,10 @@ export class BinaryTreeNode<
|
|
|
58
53
|
|
|
59
54
|
protected _left?: OptBTNOrNull<NODE>;
|
|
60
55
|
|
|
61
|
-
/**
|
|
62
|
-
* The function returns the value of the `_left` property, which can be of type `NODE`, `null`, or
|
|
63
|
-
* `undefined`.
|
|
64
|
-
* @returns The left node of the current node is being returned. It can be either a NODE object,
|
|
65
|
-
* null, or undefined.
|
|
66
|
-
*/
|
|
67
56
|
get left(): OptBTNOrNull<NODE> {
|
|
68
57
|
return this._left;
|
|
69
58
|
}
|
|
70
59
|
|
|
71
|
-
/**
|
|
72
|
-
* The function sets the left child of a node and updates its parent reference.
|
|
73
|
-
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
74
|
-
* `undefined`.
|
|
75
|
-
*/
|
|
76
60
|
set left(v: OptBTNOrNull<NODE>) {
|
|
77
61
|
if (v) {
|
|
78
62
|
v.parent = this as unknown as NODE;
|
|
@@ -82,20 +66,10 @@ export class BinaryTreeNode<
|
|
|
82
66
|
|
|
83
67
|
protected _right?: OptBTNOrNull<NODE>;
|
|
84
68
|
|
|
85
|
-
/**
|
|
86
|
-
* The function returns the right node of a binary tree or null if it doesn't exist.
|
|
87
|
-
* @returns The method is returning the value of the `_right` property, which can be a `NODE` object,
|
|
88
|
-
* `null`, or `undefined`.
|
|
89
|
-
*/
|
|
90
69
|
get right(): OptBTNOrNull<NODE> {
|
|
91
70
|
return this._right;
|
|
92
71
|
}
|
|
93
72
|
|
|
94
|
-
/**
|
|
95
|
-
* The function sets the right child of a node and updates its parent.
|
|
96
|
-
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
97
|
-
* `undefined`.
|
|
98
|
-
*/
|
|
99
73
|
set right(v: OptBTNOrNull<NODE>) {
|
|
100
74
|
if (v) {
|
|
101
75
|
v.parent = this as unknown as NODE;
|
|
@@ -103,10 +77,6 @@ export class BinaryTreeNode<
|
|
|
103
77
|
this._right = v;
|
|
104
78
|
}
|
|
105
79
|
|
|
106
|
-
/**
|
|
107
|
-
* Get the position of the node within its family.
|
|
108
|
-
* @returns {FamilyPosition} - The family position of the node.
|
|
109
|
-
*/
|
|
110
80
|
get familyPosition(): FamilyPosition {
|
|
111
81
|
const that = this as unknown as NODE;
|
|
112
82
|
if (!this.parent) {
|
|
@@ -130,7 +100,6 @@ export class BinaryTreeNode<
|
|
|
130
100
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
131
101
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
132
102
|
*/
|
|
133
|
-
|
|
134
103
|
export class BinaryTree<
|
|
135
104
|
K = any,
|
|
136
105
|
V = any,
|
|
@@ -144,16 +113,16 @@ export class BinaryTree<
|
|
|
144
113
|
iterationType: IterationType = 'ITERATIVE';
|
|
145
114
|
|
|
146
115
|
/**
|
|
147
|
-
* The constructor
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* `
|
|
153
|
-
*
|
|
116
|
+
* The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or
|
|
117
|
+
* raw data if provided.
|
|
118
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter in the constructor
|
|
119
|
+
* is an iterable that can contain elements of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It is
|
|
120
|
+
* initialized with an empty array `[]` by default.
|
|
121
|
+
* @param [options] - The `options` parameter in the constructor is an object that can contain the
|
|
122
|
+
* following properties:
|
|
154
123
|
*/
|
|
155
124
|
constructor(
|
|
156
|
-
|
|
125
|
+
keysOrNodesOrEntriesOrRaws: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE> | R> = [],
|
|
157
126
|
options?: BinaryTreeOptions<K, V, R>
|
|
158
127
|
) {
|
|
159
128
|
super();
|
|
@@ -164,105 +133,100 @@ export class BinaryTree<
|
|
|
164
133
|
else if (toEntryFn) throw TypeError('toEntryFn must be a function type');
|
|
165
134
|
}
|
|
166
135
|
|
|
167
|
-
if (
|
|
136
|
+
if (keysOrNodesOrEntriesOrRaws) this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
168
137
|
}
|
|
169
138
|
|
|
170
139
|
protected _root?: OptBTNOrNull<NODE>;
|
|
171
140
|
|
|
172
|
-
/**
|
|
173
|
-
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
174
|
-
* @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
|
|
175
|
-
* `null`, or `undefined`.
|
|
176
|
-
*/
|
|
177
141
|
get root(): OptBTNOrNull<NODE> {
|
|
178
142
|
return this._root;
|
|
179
143
|
}
|
|
180
144
|
|
|
181
145
|
protected _size: number = 0;
|
|
182
146
|
|
|
183
|
-
/**
|
|
184
|
-
* The function returns the size of an object.
|
|
185
|
-
* @returns The size of the object, which is a number.
|
|
186
|
-
*/
|
|
187
147
|
get size(): number {
|
|
188
148
|
return this._size;
|
|
189
149
|
}
|
|
190
150
|
|
|
191
151
|
protected _NIL: NODE = new BinaryTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
192
152
|
|
|
193
|
-
/**
|
|
194
|
-
* The function returns the value of the _NIL property.
|
|
195
|
-
* @returns The method is returning the value of the `_NIL` property.
|
|
196
|
-
*/
|
|
197
153
|
get NIL(): NODE {
|
|
198
154
|
return this._NIL;
|
|
199
155
|
}
|
|
200
156
|
|
|
201
157
|
protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
|
|
202
158
|
|
|
203
|
-
/**
|
|
204
|
-
* The function returns the value of the _toEntryFn property.
|
|
205
|
-
* @returns The function being returned is `this._toEntryFn`.
|
|
206
|
-
*/
|
|
207
159
|
get toEntryFn() {
|
|
208
160
|
return this._toEntryFn;
|
|
209
161
|
}
|
|
210
162
|
|
|
211
163
|
/**
|
|
212
|
-
*
|
|
213
|
-
* @param {K} key - The key
|
|
214
|
-
* @param {V} value - The value
|
|
215
|
-
*
|
|
164
|
+
* The function creates a new binary tree node with a specified key and optional value.
|
|
165
|
+
* @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
|
|
166
|
+
* @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
|
|
167
|
+
* not required to be provided when calling the function. If a `value` is provided, it should be of
|
|
168
|
+
* type `V`, which is the type of the value associated with the node.
|
|
169
|
+
* @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
|
|
170
|
+
* as NODE.
|
|
216
171
|
*/
|
|
217
172
|
createNode(key: K, value?: V): NODE {
|
|
218
173
|
return new BinaryTreeNode<K, V, NODE>(key, value) as NODE;
|
|
219
174
|
}
|
|
220
175
|
|
|
221
176
|
/**
|
|
222
|
-
* The function creates a binary tree with the
|
|
223
|
-
* @param [options] - The `options` parameter
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
177
|
+
* The function creates a binary tree with the specified options.
|
|
178
|
+
* @param [options] - The `options` parameter in the `createTree` function is an optional parameter
|
|
179
|
+
* that allows you to provide partial configuration options for creating a binary tree. It is of type
|
|
180
|
+
* `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
|
|
181
|
+
* of properties
|
|
182
|
+
* @returns A new instance of a binary tree with the specified options is being returned.
|
|
227
183
|
*/
|
|
228
|
-
createTree(options?:
|
|
229
|
-
return new BinaryTree<K, V, R, NODE, TREE>([], {
|
|
184
|
+
createTree(options?: BinaryTreeOptions<K, V, R>): TREE {
|
|
185
|
+
return new BinaryTree<K, V, R, NODE, TREE>([], {
|
|
186
|
+
iterationType: this.iterationType,
|
|
187
|
+
toEntryFn: this._toEntryFn,
|
|
188
|
+
...options
|
|
189
|
+
}) as TREE;
|
|
230
190
|
}
|
|
231
191
|
|
|
232
192
|
/**
|
|
233
|
-
* The function `keyValueOrEntryOrRawElementToNode` converts
|
|
234
|
-
*
|
|
235
|
-
* @param {
|
|
236
|
-
* `
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
193
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts various input types into a node object
|
|
194
|
+
* or returns null.
|
|
195
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
|
|
196
|
+
* `keyValueOrEntryOrRawElementToNode` function takes in a parameter `keyOrNodeOrEntryOrRaw`, which
|
|
197
|
+
* can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
198
|
+
* node, an entry
|
|
199
|
+
* @param {V} [value] - The `value` parameter in the `keyValueOrEntryOrRawElementToNode` function is
|
|
200
|
+
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
201
|
+
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
202
|
+
* @returns The `keyValueOrEntryOrRawElementToNode` function returns an optional node
|
|
203
|
+
* (`OptBTNOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
204
|
+
* input parameter (`keyOrNodeOrEntryOrRaw`) and processes it accordingly to return a node or null
|
|
205
|
+
* value.
|
|
242
206
|
*/
|
|
243
207
|
keyValueOrEntryOrRawElementToNode(
|
|
244
|
-
|
|
208
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
245
209
|
value?: V
|
|
246
210
|
): OptBTNOrNull<NODE> {
|
|
247
|
-
if (
|
|
248
|
-
if (
|
|
211
|
+
if (keyOrNodeOrEntryOrRaw === undefined) return;
|
|
212
|
+
if (keyOrNodeOrEntryOrRaw === null) return null;
|
|
249
213
|
|
|
250
|
-
if (this.isNode(
|
|
214
|
+
if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
|
|
251
215
|
|
|
252
|
-
if (this.isEntry(
|
|
253
|
-
const [key, entryValue] =
|
|
216
|
+
if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
217
|
+
const [key, entryValue] = keyOrNodeOrEntryOrRaw;
|
|
254
218
|
if (key === undefined) return;
|
|
255
219
|
else if (key === null) return null;
|
|
256
220
|
if (this.isKey(key)) return this.createNode(key, value ?? entryValue);
|
|
257
221
|
}
|
|
258
222
|
|
|
259
|
-
if (this.
|
|
260
|
-
const [key, entryValue] = this.
|
|
223
|
+
if (this._toEntryFn) {
|
|
224
|
+
const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
|
|
261
225
|
if (this.isKey(key)) return this.createNode(key, value ?? entryValue);
|
|
262
226
|
else return;
|
|
263
227
|
}
|
|
264
228
|
|
|
265
|
-
if (this.isKey(
|
|
229
|
+
if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.createNode(keyOrNodeOrEntryOrRaw, value);
|
|
266
230
|
|
|
267
231
|
return;
|
|
268
232
|
}
|
|
@@ -271,110 +235,126 @@ export class BinaryTree<
|
|
|
271
235
|
* Time Complexity: O(n)
|
|
272
236
|
* Space Complexity: O(log n)
|
|
273
237
|
*
|
|
274
|
-
* The `ensureNode`
|
|
275
|
-
*
|
|
276
|
-
* @param {
|
|
277
|
-
* `
|
|
278
|
-
* a raw
|
|
279
|
-
* @param {IterationType}
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
* @returns The
|
|
238
|
+
* The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
|
|
239
|
+
* value and returns the corresponding node or null.
|
|
240
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
241
|
+
* parameter in the `ensureNode` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It
|
|
242
|
+
* is used to determine whether the input is a key, node, entry, or raw data. The
|
|
243
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
|
|
244
|
+
* is used to specify the type of iteration to be performed. It has a default value of
|
|
245
|
+
* `this.iterationType` if not explicitly provided.
|
|
246
|
+
* @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
|
|
247
|
+
* conditions specified in the code snippet.
|
|
283
248
|
*/
|
|
284
249
|
ensureNode(
|
|
285
|
-
|
|
250
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
286
251
|
iterationType: IterationType = this.iterationType
|
|
287
252
|
): OptBTNOrNull<NODE> {
|
|
288
|
-
if (
|
|
289
|
-
if (
|
|
290
|
-
if (
|
|
291
|
-
if (this.isNode(
|
|
253
|
+
if (keyOrNodeOrEntryOrRaw === null) return null;
|
|
254
|
+
if (keyOrNodeOrEntryOrRaw === undefined) return;
|
|
255
|
+
if (keyOrNodeOrEntryOrRaw === this._NIL) return;
|
|
256
|
+
if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
|
|
292
257
|
|
|
293
|
-
if (this.
|
|
294
|
-
const
|
|
295
|
-
if (this.isKey(key)) return this.getNodeByKey(key);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
299
|
-
const key = keyOrNodeOrEntryOrRawElement[0];
|
|
258
|
+
if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
259
|
+
const key = keyOrNodeOrEntryOrRaw[0];
|
|
300
260
|
if (key === null) return null;
|
|
301
261
|
if (key === undefined) return;
|
|
302
262
|
return this.getNodeByKey(key, iterationType);
|
|
303
263
|
}
|
|
304
264
|
|
|
305
|
-
if (this.
|
|
265
|
+
if (this._toEntryFn) {
|
|
266
|
+
const [key] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
|
|
267
|
+
if (this.isKey(key)) return this.getNodeByKey(key);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.getNodeByKey(keyOrNodeOrEntryOrRaw, iterationType);
|
|
306
271
|
return;
|
|
307
272
|
}
|
|
308
273
|
|
|
309
274
|
/**
|
|
310
|
-
* The function checks if the input is an instance of
|
|
311
|
-
* @param {
|
|
312
|
-
* `
|
|
313
|
-
*
|
|
314
|
-
*
|
|
275
|
+
* The function isNode checks if the input is an instance of BinaryTreeNode.
|
|
276
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
277
|
+
* `keyOrNodeOrEntryOrRaw` can be either a key, a node, an entry, or raw data. The function is
|
|
278
|
+
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
|
|
279
|
+
* accordingly.
|
|
280
|
+
* @returns The function `isNode` is checking if the input `keyOrNodeOrEntryOrRaw` is an instance of
|
|
281
|
+
* `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
|
|
282
|
+
* it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
|
|
283
|
+
* is not a node.
|
|
315
284
|
*/
|
|
316
|
-
isNode(
|
|
317
|
-
return
|
|
285
|
+
isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
|
|
286
|
+
return keyOrNodeOrEntryOrRaw instanceof BinaryTreeNode;
|
|
318
287
|
}
|
|
319
288
|
|
|
320
289
|
/**
|
|
321
|
-
* The function checks if a given
|
|
322
|
-
* @param {
|
|
323
|
-
* `BTNKeyOrNodeOrEntry<K, V, NODE
|
|
324
|
-
*
|
|
290
|
+
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
291
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
292
|
+
* parameter in the `isRealNode` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`.
|
|
293
|
+
* The function checks if the input parameter is a `NODE` type by verifying if it is not equal
|
|
294
|
+
* @returns The function `isRealNode` is checking if the input `keyOrNodeOrEntryOrRaw` is a valid
|
|
295
|
+
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
|
|
296
|
+
* values, it then calls the `isNode` method to further determine if the input is a node. The
|
|
297
|
+
* function will return a boolean value indicating whether the
|
|
325
298
|
*/
|
|
326
|
-
isRealNode(
|
|
327
|
-
if (
|
|
328
|
-
|
|
299
|
+
isRealNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
|
|
300
|
+
if (keyOrNodeOrEntryOrRaw === this._NIL || keyOrNodeOrEntryOrRaw === null || keyOrNodeOrEntryOrRaw === undefined)
|
|
301
|
+
return false;
|
|
302
|
+
return this.isNode(keyOrNodeOrEntryOrRaw);
|
|
329
303
|
}
|
|
330
304
|
|
|
331
305
|
/**
|
|
332
|
-
* The function checks if a given
|
|
333
|
-
* @param {
|
|
334
|
-
* `BTNKeyOrNodeOrEntry<K,
|
|
335
|
-
*
|
|
306
|
+
* The function checks if a given input is a valid node or null.
|
|
307
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
308
|
+
* `keyOrNodeOrEntryOrRaw` in the `isRealNodeOrNull` function can be of type `BTNKeyOrNodeOrEntry<K,
|
|
309
|
+
* V, NODE>` or `R`. It is a union type that can either be a key, a node, an entry, or
|
|
310
|
+
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
|
|
311
|
+
* `keyOrNodeOrEntryOrRaw` is either `null` or a real node, and returns `true` if it is a node or
|
|
312
|
+
* `null`, and `false` otherwise.
|
|
336
313
|
*/
|
|
337
|
-
isRealNodeOrNull(
|
|
338
|
-
return
|
|
314
|
+
isRealNodeOrNull(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE | null {
|
|
315
|
+
return keyOrNodeOrEntryOrRaw === null || this.isRealNode(keyOrNodeOrEntryOrRaw);
|
|
339
316
|
}
|
|
340
317
|
|
|
341
318
|
/**
|
|
342
|
-
* The function checks if a given node is equal to the
|
|
343
|
-
* @param {
|
|
344
|
-
*
|
|
345
|
-
* @returns
|
|
319
|
+
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
|
|
320
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - BTNKeyOrNodeOrEntry<K, V,
|
|
321
|
+
* NODE> | R
|
|
322
|
+
* @returns The function is checking if the `keyOrNodeOrEntryOrRaw` parameter is equal to the `_NIL`
|
|
323
|
+
* property of the current object and returning a boolean value based on that comparison.
|
|
346
324
|
*/
|
|
347
|
-
isNIL(
|
|
348
|
-
return
|
|
325
|
+
isNIL(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): boolean {
|
|
326
|
+
return keyOrNodeOrEntryOrRaw === this._NIL;
|
|
349
327
|
}
|
|
350
328
|
|
|
351
329
|
/**
|
|
352
|
-
* The function
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
* returns `
|
|
330
|
+
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
331
|
+
* tree.
|
|
332
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
333
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It represents a
|
|
334
|
+
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
|
|
335
|
+
* provided
|
|
336
|
+
* @returns The function `isLeaf` returns a boolean value indicating whether the input
|
|
337
|
+
* `keyOrNodeOrEntryOrRaw` is a leaf node in a binary tree.
|
|
359
338
|
*/
|
|
360
|
-
isLeaf(
|
|
361
|
-
|
|
362
|
-
if (
|
|
363
|
-
if (
|
|
364
|
-
return !this.isRealNode(
|
|
339
|
+
isLeaf(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): boolean {
|
|
340
|
+
keyOrNodeOrEntryOrRaw = this.ensureNode(keyOrNodeOrEntryOrRaw);
|
|
341
|
+
if (keyOrNodeOrEntryOrRaw === undefined) return false;
|
|
342
|
+
if (keyOrNodeOrEntryOrRaw === null) return true;
|
|
343
|
+
return !this.isRealNode(keyOrNodeOrEntryOrRaw.left) && !this.isRealNode(keyOrNodeOrEntryOrRaw.right);
|
|
365
344
|
}
|
|
366
345
|
|
|
367
346
|
/**
|
|
368
|
-
* The function checks if the input is
|
|
369
|
-
*
|
|
370
|
-
* @param {
|
|
371
|
-
* `
|
|
372
|
-
*
|
|
347
|
+
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
|
|
348
|
+
* with a length of 2.
|
|
349
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
350
|
+
* parameter in the `isEntry` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or type `R`.
|
|
351
|
+
* The function checks if the provided `keyOrNodeOrEntryOrRaw` is of type `BTN
|
|
352
|
+
* @returns The `isEntry` function is checking if the `keyOrNodeOrEntryOrRaw` parameter is an array
|
|
353
|
+
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
|
|
354
|
+
* `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
|
|
373
355
|
*/
|
|
374
|
-
isEntry(
|
|
375
|
-
|
|
376
|
-
): keyOrNodeOrEntryOrRawElement is BTNEntry<K, V> {
|
|
377
|
-
return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
|
|
356
|
+
isEntry(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is BTNEntry<K, V> {
|
|
357
|
+
return Array.isArray(keyOrNodeOrEntryOrRaw) && keyOrNodeOrEntryOrRaw.length === 2;
|
|
378
358
|
}
|
|
379
359
|
|
|
380
360
|
/**
|
|
@@ -397,30 +377,31 @@ export class BinaryTree<
|
|
|
397
377
|
* Time Complexity O(n)
|
|
398
378
|
* Space Complexity O(1)
|
|
399
379
|
*
|
|
400
|
-
* The `add` function
|
|
401
|
-
* and finding the
|
|
402
|
-
* @param {
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
*
|
|
409
|
-
* @returns a boolean value. It returns `true` if the insertion
|
|
410
|
-
* insertion position
|
|
380
|
+
* The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
|
|
381
|
+
* and finding the correct insertion position.
|
|
382
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `add` method you provided
|
|
383
|
+
* seems to be for adding a new node to a binary tree structure. The `keyOrNodeOrEntryOrRaw`
|
|
384
|
+
* parameter in the method can accept different types of values:
|
|
385
|
+
* @param {V} [value] - The `value` parameter in the `add` method represents the value associated
|
|
386
|
+
* with the key that you want to add to the binary tree. When adding a key-value pair to the binary
|
|
387
|
+
* tree, you provide the key and its corresponding value. The `add` method then creates a new node
|
|
388
|
+
* with this
|
|
389
|
+
* @returns The `add` method returns a boolean value. It returns `true` if the insertion of the new
|
|
390
|
+
* node was successful, and `false` if the insertion position could not be found or if a duplicate
|
|
391
|
+
* key was found and the node was replaced instead of inserted.
|
|
411
392
|
*/
|
|
412
|
-
add(
|
|
413
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
393
|
+
add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean {
|
|
394
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
|
|
414
395
|
if (newNode === undefined) return false;
|
|
415
396
|
|
|
416
397
|
// If the tree is empty, directly set the new node as the root node
|
|
417
|
-
if (!this.
|
|
418
|
-
this.
|
|
398
|
+
if (!this._root) {
|
|
399
|
+
this._setRoot(newNode);
|
|
419
400
|
this._size = 1;
|
|
420
401
|
return true;
|
|
421
402
|
}
|
|
422
403
|
|
|
423
|
-
const queue = new Queue<NODE>([this.
|
|
404
|
+
const queue = new Queue<NODE>([this._root]);
|
|
424
405
|
let potentialParent: NODE | undefined; // Record the parent node of the potential insertion location
|
|
425
406
|
|
|
426
407
|
while (queue.size > 0) {
|
|
@@ -441,10 +422,10 @@ export class BinaryTree<
|
|
|
441
422
|
|
|
442
423
|
// Continue traversing the left and right subtrees
|
|
443
424
|
if (cur.left !== null) {
|
|
444
|
-
cur.left
|
|
425
|
+
if (cur.left) queue.push(cur.left);
|
|
445
426
|
}
|
|
446
427
|
if (cur.right !== null) {
|
|
447
|
-
cur.right
|
|
428
|
+
if (cur.right) queue.push(cur.right);
|
|
448
429
|
}
|
|
449
430
|
}
|
|
450
431
|
|
|
@@ -466,18 +447,22 @@ export class BinaryTree<
|
|
|
466
447
|
* Time Complexity: O(k * n)
|
|
467
448
|
* Space Complexity: O(1)
|
|
468
449
|
*
|
|
469
|
-
* The `addMany` function takes in
|
|
470
|
-
* optional
|
|
471
|
-
*
|
|
472
|
-
* @param
|
|
473
|
-
*
|
|
474
|
-
*
|
|
475
|
-
* in the `
|
|
476
|
-
*
|
|
477
|
-
*
|
|
450
|
+
* The `addMany` function takes in multiple keys or nodes or entries or raw values along with
|
|
451
|
+
* optional values, and adds them to a data structure while returning an array indicating whether
|
|
452
|
+
* each insertion was successful.
|
|
453
|
+
* @param keysOrNodesOrEntriesOrRaws - `keysOrNodesOrEntriesOrRaws` is an iterable that can contain a
|
|
454
|
+
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
|
|
455
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`.
|
|
456
|
+
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
|
|
457
|
+
* accepts an iterable of values. These values correspond to the keys or nodes being added in the
|
|
458
|
+
* `keysOrNodesOrEntriesOrRaws` parameter. If provided, the function will iterate over the values and
|
|
459
|
+
* assign them
|
|
460
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each key,
|
|
461
|
+
* node, entry, or raw value was successfully added to the data structure. Each boolean value
|
|
462
|
+
* corresponds to the success of adding the corresponding key or value in the input iterable.
|
|
478
463
|
*/
|
|
479
464
|
addMany(
|
|
480
|
-
|
|
465
|
+
keysOrNodesOrEntriesOrRaws: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE> | R>,
|
|
481
466
|
values?: Iterable<V | undefined>
|
|
482
467
|
): boolean[] {
|
|
483
468
|
// TODO not sure addMany not be run multi times
|
|
@@ -488,7 +473,7 @@ export class BinaryTree<
|
|
|
488
473
|
valuesIterator = values[Symbol.iterator]();
|
|
489
474
|
}
|
|
490
475
|
|
|
491
|
-
for (const
|
|
476
|
+
for (const keyOrNodeOrEntryOrRaw of keysOrNodesOrEntriesOrRaws) {
|
|
492
477
|
let value: V | undefined | null = undefined;
|
|
493
478
|
|
|
494
479
|
if (valuesIterator) {
|
|
@@ -498,7 +483,7 @@ export class BinaryTree<
|
|
|
498
483
|
}
|
|
499
484
|
}
|
|
500
485
|
|
|
501
|
-
inserted.push(this.add(
|
|
486
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRaw, value));
|
|
502
487
|
}
|
|
503
488
|
|
|
504
489
|
return inserted;
|
|
@@ -508,54 +493,44 @@ export class BinaryTree<
|
|
|
508
493
|
* Time Complexity: O(k * n)
|
|
509
494
|
* Space Complexity: O(1)
|
|
510
495
|
*
|
|
511
|
-
* The `refill` function clears the
|
|
512
|
-
*
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
496
|
+
* The `refill` function clears the existing data structure and then adds new key-value pairs based
|
|
497
|
+
* on the provided input.
|
|
498
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter in the `refill`
|
|
499
|
+
* method can accept an iterable containing a mix of `BTNKeyOrNodeOrEntry<K, V, NODE>` objects or `R`
|
|
500
|
+
* objects.
|
|
501
|
+
* @param [values] - The `values` parameter in the `refill` method is an optional parameter that
|
|
502
|
+
* accepts an iterable of values of type `V` or `undefined`.
|
|
517
503
|
*/
|
|
518
504
|
refill(
|
|
519
|
-
|
|
505
|
+
keysOrNodesOrEntriesOrRaws: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE> | R>,
|
|
520
506
|
values?: Iterable<V | undefined>
|
|
521
507
|
): void {
|
|
522
508
|
this.clear();
|
|
523
|
-
this.addMany(
|
|
509
|
+
this.addMany(keysOrNodesOrEntriesOrRaws, values);
|
|
524
510
|
}
|
|
525
511
|
|
|
526
|
-
delete<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
527
|
-
|
|
528
|
-
delete<C extends BTNCallback<NODE, NODE>>(
|
|
529
|
-
identifier: OptBTNOrNull<NODE>,
|
|
530
|
-
callback?: C
|
|
531
|
-
): BinaryTreeDeleteResult<NODE>[];
|
|
532
|
-
|
|
533
|
-
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback: C): BinaryTreeDeleteResult<NODE>[];
|
|
534
|
-
|
|
535
512
|
/**
|
|
536
513
|
* Time Complexity: O(n)
|
|
537
514
|
* Space Complexity: O(1)
|
|
538
515
|
*
|
|
539
|
-
* The
|
|
540
|
-
* the deleted node
|
|
541
|
-
* @param {
|
|
542
|
-
*
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
516
|
+
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
517
|
+
* the deleted node along with information for tree balancing.
|
|
518
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
519
|
+
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
520
|
+
* node, entry, raw data, or a custom predicate. The method returns an array of
|
|
521
|
+
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
522
|
+
* balancing is needed.
|
|
523
|
+
* @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
|
|
524
|
+
* the array contains information about the node that was deleted (`deleted`) and the node that may
|
|
525
|
+
* need to be balanced (`needBalanced`).
|
|
549
526
|
*/
|
|
550
|
-
delete
|
|
551
|
-
|
|
552
|
-
callback: C = this._DEFAULT_CALLBACK as C
|
|
527
|
+
delete(
|
|
528
|
+
keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>
|
|
553
529
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
554
530
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
555
|
-
if (!this.
|
|
556
|
-
callback = this._ensureCallback(identifier, callback);
|
|
531
|
+
if (!this._root) return deletedResult;
|
|
557
532
|
|
|
558
|
-
const curr = this.getNode(
|
|
533
|
+
const curr = this.getNode(keyOrNodeOrEntryOrRawOrPredicate);
|
|
559
534
|
if (!curr) return deletedResult;
|
|
560
535
|
|
|
561
536
|
const parent: NODE | undefined = curr?.parent;
|
|
@@ -589,84 +564,56 @@ export class BinaryTree<
|
|
|
589
564
|
curr.right = undefined;
|
|
590
565
|
}
|
|
591
566
|
|
|
592
|
-
this._size = this.
|
|
567
|
+
this._size = this._size - 1;
|
|
593
568
|
|
|
594
569
|
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
595
570
|
return deletedResult;
|
|
596
571
|
}
|
|
597
572
|
|
|
598
|
-
getNodes<C extends BTNCallback<NODE, K>>(
|
|
599
|
-
identifier: K,
|
|
600
|
-
callback?: C,
|
|
601
|
-
onlyOne?: boolean,
|
|
602
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
603
|
-
iterationType?: IterationType
|
|
604
|
-
): NODE[];
|
|
605
|
-
|
|
606
|
-
getNodes<C extends BTNCallback<NODE, NODE>>(
|
|
607
|
-
identifier: OptBTNOrNull<NODE>,
|
|
608
|
-
callback?: C,
|
|
609
|
-
onlyOne?: boolean,
|
|
610
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
611
|
-
iterationType?: IterationType
|
|
612
|
-
): NODE[];
|
|
613
|
-
|
|
614
|
-
getNodes<C extends BTNCallback<NODE>>(
|
|
615
|
-
identifier: ReturnType<C>,
|
|
616
|
-
callback: C,
|
|
617
|
-
onlyOne?: boolean,
|
|
618
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
619
|
-
iterationType?: IterationType
|
|
620
|
-
): NODE[];
|
|
621
|
-
|
|
622
573
|
/**
|
|
623
574
|
* Time Complexity: O(n)
|
|
624
575
|
* Space Complexity: O(k + log n)
|
|
625
576
|
*
|
|
626
|
-
* The function `getNodes`
|
|
627
|
-
* recursive or iterative
|
|
628
|
-
* @param {
|
|
629
|
-
*
|
|
630
|
-
*
|
|
631
|
-
*
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
643
|
-
* iteration to be performed on the nodes of a binary tree. It can have two possible values:
|
|
644
|
-
* @returns an array of NODE objects.
|
|
577
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
578
|
+
* or predicate, with options for recursive or iterative traversal.
|
|
579
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
580
|
+
* - The `getNodes` function you provided takes several parameters:
|
|
581
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
582
|
+
* determines whether to return only the first node that matches the criteria specified by the
|
|
583
|
+
* `keyOrNodeOrEntryOrRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
584
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
585
|
+
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
586
|
+
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
587
|
+
* not provided, the default value is set to `this._root
|
|
588
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
589
|
+
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
590
|
+
* can have two possible values:
|
|
591
|
+
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
592
|
+
* based on the input parameters and the iteration type specified.
|
|
645
593
|
*/
|
|
646
|
-
getNodes
|
|
647
|
-
|
|
648
|
-
callback: C = this._DEFAULT_CALLBACK as C,
|
|
594
|
+
getNodes(
|
|
595
|
+
keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
649
596
|
onlyOne = false,
|
|
650
|
-
beginRoot:
|
|
597
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
651
598
|
iterationType: IterationType = this.iterationType
|
|
652
599
|
): NODE[] {
|
|
653
|
-
if (
|
|
654
|
-
if (
|
|
600
|
+
if (keyOrNodeOrEntryOrRawOrPredicate === undefined) return [];
|
|
601
|
+
if (keyOrNodeOrEntryOrRawOrPredicate === null) return [];
|
|
655
602
|
beginRoot = this.ensureNode(beginRoot);
|
|
656
603
|
if (!beginRoot) return [];
|
|
657
|
-
callback = this.
|
|
604
|
+
const callback = this._ensurePredicate(keyOrNodeOrEntryOrRawOrPredicate);
|
|
658
605
|
|
|
659
606
|
const ans: NODE[] = [];
|
|
660
607
|
|
|
661
608
|
if (iterationType === 'RECURSIVE') {
|
|
662
609
|
const dfs = (cur: NODE) => {
|
|
663
|
-
if (callback(cur)
|
|
610
|
+
if (callback(cur)) {
|
|
664
611
|
ans.push(cur);
|
|
665
612
|
if (onlyOne) return;
|
|
666
613
|
}
|
|
667
614
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
668
|
-
this.isRealNode(cur.left)
|
|
669
|
-
this.isRealNode(cur.right)
|
|
615
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
616
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
670
617
|
};
|
|
671
618
|
|
|
672
619
|
dfs(beginRoot);
|
|
@@ -675,12 +622,12 @@ export class BinaryTree<
|
|
|
675
622
|
while (stack.length > 0) {
|
|
676
623
|
const cur = stack.pop();
|
|
677
624
|
if (this.isRealNode(cur)) {
|
|
678
|
-
if (callback(cur)
|
|
625
|
+
if (callback(cur)) {
|
|
679
626
|
ans.push(cur);
|
|
680
627
|
if (onlyOne) return ans;
|
|
681
628
|
}
|
|
682
|
-
this.isRealNode(cur.left)
|
|
683
|
-
this.isRealNode(cur.right)
|
|
629
|
+
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
630
|
+
if (this.isRealNode(cur.right)) stack.push(cur.right);
|
|
684
631
|
}
|
|
685
632
|
}
|
|
686
633
|
}
|
|
@@ -688,178 +635,115 @@ export class BinaryTree<
|
|
|
688
635
|
return ans;
|
|
689
636
|
}
|
|
690
637
|
|
|
691
|
-
getNode<C extends BTNCallback<NODE, K>>(
|
|
692
|
-
identifier: K,
|
|
693
|
-
callback?: C,
|
|
694
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
695
|
-
iterationType?: IterationType
|
|
696
|
-
): OptBTNOrNull<NODE>;
|
|
697
|
-
|
|
698
|
-
getNode<C extends BTNCallback<NODE, NODE>>(
|
|
699
|
-
identifier: OptBTNOrNull<NODE>,
|
|
700
|
-
callback?: C,
|
|
701
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
702
|
-
iterationType?: IterationType
|
|
703
|
-
): OptBTNOrNull<NODE>;
|
|
704
|
-
|
|
705
|
-
getNode<C extends BTNCallback<NODE>>(
|
|
706
|
-
identifier: ReturnType<C>,
|
|
707
|
-
callback: C,
|
|
708
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
709
|
-
iterationType?: IterationType
|
|
710
|
-
): OptBTNOrNull<NODE>;
|
|
711
|
-
|
|
712
638
|
/**
|
|
713
639
|
* Time Complexity: O(n)
|
|
714
640
|
* Space Complexity: O(log n).
|
|
715
641
|
*
|
|
716
|
-
* The
|
|
717
|
-
*
|
|
718
|
-
* @param {
|
|
719
|
-
*
|
|
720
|
-
*
|
|
721
|
-
* @param {
|
|
722
|
-
*
|
|
723
|
-
*
|
|
724
|
-
*
|
|
725
|
-
*
|
|
726
|
-
*
|
|
727
|
-
* of
|
|
728
|
-
*
|
|
642
|
+
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
643
|
+
* predicate.
|
|
644
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
645
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `getNode` function can accept a key,
|
|
646
|
+
* node, entry, raw data, or a predicate function.
|
|
647
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
648
|
+
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
649
|
+
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
650
|
+
* is typically the root node of the binary tree.
|
|
651
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is
|
|
652
|
+
* used to specify the type of iteration to be performed when searching for a node. It has a default
|
|
653
|
+
* value of `this.iterationType`, which means it will use the iteration type defined in the current
|
|
654
|
+
* context if no specific value is provided
|
|
655
|
+
* @returns The `getNode` function is returning the first node that matches the specified criteria,
|
|
656
|
+
* or `null` if no matching node is found.
|
|
729
657
|
*/
|
|
730
|
-
getNode
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
658
|
+
getNode(
|
|
659
|
+
keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
660
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
734
661
|
iterationType: IterationType = this.iterationType
|
|
735
662
|
): OptBTNOrNull<NODE> {
|
|
736
|
-
return this.getNodes(
|
|
663
|
+
return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType)[0] ?? null;
|
|
737
664
|
}
|
|
738
665
|
|
|
739
666
|
/**
|
|
740
667
|
* Time Complexity: O(n)
|
|
741
668
|
* Space Complexity: O(log n)
|
|
742
669
|
*
|
|
743
|
-
* The function `getNodeByKey`
|
|
744
|
-
* @param {K} key - The key parameter is the value
|
|
745
|
-
*
|
|
746
|
-
* @param {IterationType}
|
|
747
|
-
*
|
|
748
|
-
*
|
|
749
|
-
* @returns
|
|
670
|
+
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
671
|
+
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
672
|
+
* structure.
|
|
673
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
674
|
+
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
675
|
+
* is an optional parameter with a default value of `this.iterationType`.
|
|
676
|
+
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
677
|
+
* (`OptBTNOrNull<NODE>`).
|
|
750
678
|
*/
|
|
751
679
|
getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptBTNOrNull<NODE> {
|
|
752
|
-
return this.getNode(key, this.
|
|
680
|
+
return this.getNode(key, this._root, iterationType);
|
|
753
681
|
}
|
|
754
682
|
|
|
755
|
-
override get<C extends BTNCallback<NODE, K>>(
|
|
756
|
-
identifier: K,
|
|
757
|
-
callback?: C,
|
|
758
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
759
|
-
iterationType?: IterationType
|
|
760
|
-
): V | undefined;
|
|
761
|
-
|
|
762
|
-
override get<C extends BTNCallback<NODE, NODE>>(
|
|
763
|
-
identifier: OptBTNOrNull<NODE>,
|
|
764
|
-
callback?: C,
|
|
765
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
766
|
-
iterationType?: IterationType
|
|
767
|
-
): V | undefined;
|
|
768
|
-
|
|
769
|
-
override get<C extends BTNCallback<NODE>>(
|
|
770
|
-
identifier: ReturnType<C>,
|
|
771
|
-
callback: C,
|
|
772
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
773
|
-
iterationType?: IterationType
|
|
774
|
-
): V | undefined;
|
|
775
|
-
|
|
776
683
|
/**
|
|
777
684
|
* Time Complexity: O(n)
|
|
778
685
|
* Space Complexity: O(log n)
|
|
779
686
|
*
|
|
780
|
-
*
|
|
781
|
-
*
|
|
782
|
-
* @param {
|
|
783
|
-
*
|
|
784
|
-
*
|
|
785
|
-
* @param {
|
|
786
|
-
*
|
|
787
|
-
*
|
|
788
|
-
*
|
|
789
|
-
*
|
|
790
|
-
*
|
|
791
|
-
*
|
|
792
|
-
*
|
|
793
|
-
*
|
|
794
|
-
*
|
|
795
|
-
*
|
|
687
|
+
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
688
|
+
* node, entry, raw data, or predicate in a data structure.
|
|
689
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
690
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `get` method can accept one of the
|
|
691
|
+
* following types:
|
|
692
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `get`
|
|
693
|
+
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
694
|
+
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
695
|
+
* tree (`this._root`).
|
|
696
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `get` method is used
|
|
697
|
+
* to specify the type of iteration to be performed when searching for a key in the binary tree. It
|
|
698
|
+
* is an optional parameter with a default value of `this.iterationType`, which means it will use the
|
|
699
|
+
* iteration type defined in the
|
|
700
|
+
* @returns The `get` method is returning the value associated with the specified key, node, entry,
|
|
701
|
+
* raw data, or predicate in the binary tree map. If the specified key or node is found in the tree,
|
|
702
|
+
* the method returns the corresponding value. If the key or node is not found, it returns
|
|
703
|
+
* `undefined`.
|
|
796
704
|
*/
|
|
797
|
-
override get
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
705
|
+
override get(
|
|
706
|
+
keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
707
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
801
708
|
iterationType: IterationType = this.iterationType
|
|
802
709
|
): V | undefined {
|
|
803
|
-
return this.getNode(
|
|
710
|
+
return this.getNode(keyOrNodeOrEntryOrRawOrPredicate, beginRoot, iterationType)?.value;
|
|
804
711
|
}
|
|
805
712
|
|
|
806
|
-
override has<C extends BTNCallback<NODE, K>>(
|
|
807
|
-
identifier: K,
|
|
808
|
-
callback?: C,
|
|
809
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
810
|
-
iterationType?: IterationType
|
|
811
|
-
): boolean;
|
|
812
|
-
|
|
813
|
-
override has<C extends BTNCallback<NODE, NODE>>(
|
|
814
|
-
identifier: OptBTNOrNull<NODE>,
|
|
815
|
-
callback?: C,
|
|
816
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
817
|
-
iterationType?: IterationType
|
|
818
|
-
): boolean;
|
|
819
|
-
|
|
820
|
-
override has<C extends BTNCallback<NODE>>(
|
|
821
|
-
identifier: ReturnType<C> | null | undefined,
|
|
822
|
-
callback: C,
|
|
823
|
-
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
824
|
-
iterationType?: IterationType
|
|
825
|
-
): boolean;
|
|
826
|
-
|
|
827
713
|
/**
|
|
828
714
|
* Time Complexity: O(n)
|
|
829
715
|
* Space Complexity: O(log n)
|
|
830
716
|
*
|
|
831
|
-
* The `has` function checks if a
|
|
832
|
-
*
|
|
833
|
-
* @param {
|
|
834
|
-
*
|
|
835
|
-
*
|
|
836
|
-
*
|
|
837
|
-
*
|
|
838
|
-
*
|
|
839
|
-
* `
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
*
|
|
843
|
-
* @
|
|
844
|
-
*
|
|
845
|
-
*
|
|
717
|
+
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
718
|
+
* exists in the data structure.
|
|
719
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
720
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `override has` method can accept one of
|
|
721
|
+
* the following types:
|
|
722
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
723
|
+
* `override` method is used to specify the starting point for the search operation within the data
|
|
724
|
+
* structure. It defaults to `this._root` if not provided explicitly.
|
|
725
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
726
|
+
* is used to specify the type of iteration to be performed. It has a default value of
|
|
727
|
+
* `this.iterationType`, which means it will use the iteration type defined in the current context if
|
|
728
|
+
* no value is provided when calling the method.
|
|
729
|
+
* @returns The `override has` method is returning a boolean value. It checks if there are any nodes
|
|
730
|
+
* that match the provided key, node, entry, raw data, or predicate in the tree structure. If there
|
|
731
|
+
* are matching nodes, it returns `true`, indicating that the tree contains the specified element.
|
|
732
|
+
* Otherwise, it returns `false`.
|
|
846
733
|
*/
|
|
847
|
-
override has
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
734
|
+
override has(
|
|
735
|
+
keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
736
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
851
737
|
iterationType: IterationType = this.iterationType
|
|
852
738
|
): boolean {
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
739
|
+
return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType).length > 0;
|
|
856
740
|
}
|
|
857
741
|
|
|
858
742
|
/**
|
|
859
743
|
* Time Complexity: O(1)
|
|
860
744
|
* Space Complexity: O(1)
|
|
861
745
|
*
|
|
862
|
-
*
|
|
746
|
+
* The `clear` function resets the root node and size of a data structure to empty.
|
|
863
747
|
*/
|
|
864
748
|
clear() {
|
|
865
749
|
this._setRoot(undefined);
|
|
@@ -870,26 +754,31 @@ export class BinaryTree<
|
|
|
870
754
|
* Time Complexity: O(1)
|
|
871
755
|
* Space Complexity: O(1)
|
|
872
756
|
*
|
|
873
|
-
*
|
|
874
|
-
*
|
|
757
|
+
* The `isEmpty` function in TypeScript checks if a data structure has no elements and returns a
|
|
758
|
+
* boolean value.
|
|
759
|
+
* @returns The `isEmpty()` method is returning a boolean value, specifically `true` if the `_size`
|
|
760
|
+
* property is equal to 0, indicating that the data structure is empty, and `false` otherwise.
|
|
875
761
|
*/
|
|
876
762
|
isEmpty(): boolean {
|
|
877
|
-
return this.
|
|
763
|
+
return this._size === 0;
|
|
878
764
|
}
|
|
879
765
|
|
|
880
766
|
/**
|
|
881
767
|
* Time Complexity: O(n)
|
|
882
768
|
* Space Complexity: O(log n)
|
|
883
769
|
*
|
|
884
|
-
* The function checks if a binary tree is perfectly balanced by comparing
|
|
885
|
-
* height
|
|
886
|
-
* @param {
|
|
887
|
-
*
|
|
888
|
-
*
|
|
889
|
-
*
|
|
890
|
-
*
|
|
770
|
+
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
771
|
+
* its height.
|
|
772
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
773
|
+
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
774
|
+
* binary tree or a specific node from which the balance check should begin.
|
|
775
|
+
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
776
|
+
* the tree starting from the `beginRoot` node is perfectly balanced or not. The return value is
|
|
777
|
+
* determined by comparing the minimum height of the tree with the height of the tree. If the minimum
|
|
778
|
+
* height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
|
|
779
|
+
* balanced and
|
|
891
780
|
*/
|
|
892
|
-
isPerfectlyBalanced(beginRoot:
|
|
781
|
+
isPerfectlyBalanced(beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root): boolean {
|
|
893
782
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
894
783
|
}
|
|
895
784
|
|
|
@@ -897,18 +786,22 @@ export class BinaryTree<
|
|
|
897
786
|
* Time Complexity: O(n)
|
|
898
787
|
* Space Complexity: O(1)
|
|
899
788
|
*
|
|
900
|
-
* The function `isBST` checks if a binary search tree is valid
|
|
901
|
-
*
|
|
902
|
-
*
|
|
903
|
-
*
|
|
904
|
-
*
|
|
905
|
-
*
|
|
906
|
-
*
|
|
907
|
-
*
|
|
908
|
-
*
|
|
789
|
+
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
790
|
+
* or iterative methods.
|
|
791
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `isBST`
|
|
792
|
+
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
793
|
+
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
794
|
+
* provided, the function will default to
|
|
795
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `isBST` function
|
|
796
|
+
* determines whether the function should use a recursive approach or an iterative approach to check
|
|
797
|
+
* if the binary search tree (BST) is valid.
|
|
798
|
+
* @returns The `isBST` method is returning a boolean value, which indicates whether the binary
|
|
799
|
+
* search tree (BST) represented by the given root node is a valid BST or not. The method checks if
|
|
800
|
+
* the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
|
|
801
|
+
* less than the node's key, and all nodes in its right subtree have keys greater than the node's
|
|
909
802
|
*/
|
|
910
803
|
isBST(
|
|
911
|
-
beginRoot:
|
|
804
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
912
805
|
iterationType: IterationType = this.iterationType
|
|
913
806
|
): boolean {
|
|
914
807
|
// TODO there is a bug
|
|
@@ -955,19 +848,21 @@ export class BinaryTree<
|
|
|
955
848
|
* Time Complexity: O(n)
|
|
956
849
|
* Space Complexity: O(1)
|
|
957
850
|
*
|
|
958
|
-
* The function calculates the depth
|
|
959
|
-
* @param {
|
|
960
|
-
*
|
|
961
|
-
*
|
|
962
|
-
* @param {
|
|
963
|
-
* represents the starting point from which to calculate the depth
|
|
964
|
-
* node
|
|
965
|
-
* `
|
|
966
|
-
* @returns the depth of a node
|
|
851
|
+
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
852
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} dist - The `dist` parameter in the `getDepth`
|
|
853
|
+
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
854
|
+
* It is the target node for which you want to calculate the depth from the `beginRoot` node.
|
|
855
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
856
|
+
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
857
|
+
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
858
|
+
* for `beginRoot` is set to the root of the binary
|
|
859
|
+
* @returns The `getDepth` method returns the depth of a given node `dist` relative to the
|
|
860
|
+
* `beginRoot` node in a binary tree. If the `dist` node is not found in the path to the `beginRoot`
|
|
861
|
+
* node, it returns the depth of the `dist` node from the root of the tree.
|
|
967
862
|
*/
|
|
968
863
|
getDepth(
|
|
969
|
-
dist:
|
|
970
|
-
beginRoot:
|
|
864
|
+
dist: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
865
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root
|
|
971
866
|
): number {
|
|
972
867
|
let distEnsured = this.ensureNode(dist);
|
|
973
868
|
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
@@ -987,16 +882,20 @@ export class BinaryTree<
|
|
|
987
882
|
* Space Complexity: O(1)
|
|
988
883
|
*
|
|
989
884
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
990
|
-
* or iterative approach.
|
|
991
|
-
* @param {
|
|
992
|
-
*
|
|
993
|
-
*
|
|
994
|
-
*
|
|
995
|
-
*
|
|
996
|
-
*
|
|
885
|
+
* or iterative approach in TypeScript.
|
|
886
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
887
|
+
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
888
|
+
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
889
|
+
* binary tree data structure.
|
|
890
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
891
|
+
* of iteration to be performed while calculating the height of the binary tree. It can have two
|
|
892
|
+
* possible values:
|
|
893
|
+
* @returns The `getHeight` method returns the height of the binary tree starting from the specified
|
|
894
|
+
* root node. The height is calculated based on the maximum depth of the tree, considering either a
|
|
895
|
+
* recursive approach or an iterative approach depending on the `iterationType` parameter.
|
|
997
896
|
*/
|
|
998
897
|
getHeight(
|
|
999
|
-
beginRoot:
|
|
898
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1000
899
|
iterationType: IterationType = this.iterationType
|
|
1001
900
|
): number {
|
|
1002
901
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1033,19 +932,21 @@ export class BinaryTree<
|
|
|
1033
932
|
* Space Complexity: O(log n)
|
|
1034
933
|
*
|
|
1035
934
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
1036
|
-
* recursive or iterative approach.
|
|
1037
|
-
* @param {
|
|
1038
|
-
* starting
|
|
1039
|
-
*
|
|
1040
|
-
* tree.
|
|
1041
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
1042
|
-
* iteration to
|
|
1043
|
-
* values:
|
|
1044
|
-
* @returns The
|
|
1045
|
-
*
|
|
935
|
+
* recursive or iterative approach in TypeScript.
|
|
936
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
937
|
+
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
938
|
+
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
939
|
+
* tree. If not provided, the default value is the root
|
|
940
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getMinHeight` method
|
|
941
|
+
* specifies the type of iteration to use when calculating the minimum height of a binary tree. It
|
|
942
|
+
* can have two possible values:
|
|
943
|
+
* @returns The `getMinHeight` method returns the minimum height of the binary tree starting from the
|
|
944
|
+
* specified root node. The height is calculated based on the shortest path from the root node to a
|
|
945
|
+
* leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
|
|
946
|
+
* a stack) based on the `iterationType` parameter.
|
|
1046
947
|
*/
|
|
1047
948
|
getMinHeight(
|
|
1048
|
-
beginRoot:
|
|
949
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1049
950
|
iterationType: IterationType = this.iterationType
|
|
1050
951
|
): number {
|
|
1051
952
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1076,8 +977,8 @@ export class BinaryTree<
|
|
|
1076
977
|
if (!this.isRealNode(node.right) || last === node.right) {
|
|
1077
978
|
node = stack.pop();
|
|
1078
979
|
if (this.isRealNode(node)) {
|
|
1079
|
-
const leftMinHeight = this.isRealNode(node.left) ?
|
|
1080
|
-
const rightMinHeight = this.isRealNode(node.right) ?
|
|
980
|
+
const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left)! : -1;
|
|
981
|
+
const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right)! : -1;
|
|
1081
982
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
1082
983
|
last = node;
|
|
1083
984
|
node = null;
|
|
@@ -1086,7 +987,7 @@ export class BinaryTree<
|
|
|
1086
987
|
}
|
|
1087
988
|
}
|
|
1088
989
|
|
|
1089
|
-
return depths.get(beginRoot)
|
|
990
|
+
return depths.get(beginRoot)!;
|
|
1090
991
|
}
|
|
1091
992
|
}
|
|
1092
993
|
|
|
@@ -1094,27 +995,38 @@ export class BinaryTree<
|
|
|
1094
995
|
* Time Complexity: O(log n)
|
|
1095
996
|
* Space Complexity: O(log n)
|
|
1096
997
|
*
|
|
1097
|
-
* The function `getPathToRoot`
|
|
1098
|
-
*
|
|
1099
|
-
* @param {
|
|
1100
|
-
*
|
|
1101
|
-
*
|
|
1102
|
-
*
|
|
1103
|
-
*
|
|
1104
|
-
*
|
|
998
|
+
* The function `getPathToRoot` in TypeScript retrieves the path from a given node to the root of a
|
|
999
|
+
* tree structure, applying a specified callback function along the way.
|
|
1000
|
+
* @param {C} callback - The `callback` parameter is a function that is used to process each node in
|
|
1001
|
+
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
1002
|
+
* a value based on that node. The return type of the callback function is determined by the generic
|
|
1003
|
+
* type `C
|
|
1004
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginNode - The `beginNode` parameter in the
|
|
1005
|
+
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
1006
|
+
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
1007
|
+
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
1008
|
+
* not. If `isReverse` is set to `true`, the path will be reversed before being returned. If `is
|
|
1009
|
+
* @returns The function `getPathToRoot` returns an array of the return values of the callback
|
|
1010
|
+
* function `callback` applied to each node in the path from the `beginNode` to the root node. The
|
|
1011
|
+
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
1012
|
+
* parameter.
|
|
1105
1013
|
*/
|
|
1106
|
-
getPathToRoot
|
|
1107
|
-
|
|
1014
|
+
getPathToRoot<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1015
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1016
|
+
beginNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1017
|
+
isReverse = true
|
|
1018
|
+
): ReturnType<C>[] {
|
|
1019
|
+
const result: ReturnType<C>[] = [];
|
|
1108
1020
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
1109
1021
|
|
|
1110
1022
|
if (!beginNodeEnsured) return result;
|
|
1111
1023
|
|
|
1112
1024
|
while (beginNodeEnsured.parent) {
|
|
1113
1025
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
1114
|
-
result.push(beginNodeEnsured);
|
|
1026
|
+
result.push(callback(beginNodeEnsured));
|
|
1115
1027
|
beginNodeEnsured = beginNodeEnsured.parent;
|
|
1116
1028
|
}
|
|
1117
|
-
result.push(beginNodeEnsured);
|
|
1029
|
+
result.push(callback(beginNodeEnsured));
|
|
1118
1030
|
return isReverse ? result.reverse() : result;
|
|
1119
1031
|
}
|
|
1120
1032
|
|
|
@@ -1125,23 +1037,23 @@ export class BinaryTree<
|
|
|
1125
1037
|
* The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
|
|
1126
1038
|
* tail-recursive iteration.
|
|
1127
1039
|
* @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
|
|
1128
|
-
* node of a binary tree or
|
|
1129
|
-
* if not
|
|
1130
|
-
* @param {
|
|
1040
|
+
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
1041
|
+
* value of `_DEFAULT_BTN_CALLBACK` if not specified.
|
|
1042
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1131
1043
|
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
1132
|
-
* tree. It can be either a
|
|
1133
|
-
* the
|
|
1044
|
+
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1045
|
+
* starting point is provided, the function will default
|
|
1134
1046
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getLeftMost` function
|
|
1135
1047
|
* specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
|
|
1136
1048
|
* possible values:
|
|
1137
1049
|
* @returns The `getLeftMost` function returns the result of the callback function `C` applied to the
|
|
1138
|
-
* leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` is
|
|
1139
|
-
* it returns the result of the callback function applied to `undefined`. If the `beginRoot`
|
|
1140
|
-
* real node, it returns the result of the callback
|
|
1050
|
+
* leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` node is
|
|
1051
|
+
* `NIL`, it returns the result of the callback function applied to `undefined`. If the `beginRoot`
|
|
1052
|
+
* node is not a real node, it returns the result of the callback
|
|
1141
1053
|
*/
|
|
1142
1054
|
getLeftMost<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1143
|
-
callback: C = this.
|
|
1144
|
-
beginRoot:
|
|
1055
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1056
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1145
1057
|
iterationType: IterationType = this.iterationType
|
|
1146
1058
|
): ReturnType<C> {
|
|
1147
1059
|
if (this.isNIL(beginRoot)) return callback(undefined);
|
|
@@ -1174,24 +1086,24 @@ export class BinaryTree<
|
|
|
1174
1086
|
* The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
|
|
1175
1087
|
* or iterative traversal methods.
|
|
1176
1088
|
* @param {C} callback - The `callback` parameter is a function that will be called with the result
|
|
1177
|
-
* of the
|
|
1178
|
-
*
|
|
1179
|
-
*
|
|
1089
|
+
* of finding the rightmost node in a binary tree. It is of type `BTNCallback<OptBTNOrNull<NODE>>`,
|
|
1090
|
+
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
1091
|
+
* as
|
|
1092
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1180
1093
|
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
1181
|
-
* tree. It can be either a
|
|
1182
|
-
*
|
|
1094
|
+
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1095
|
+
* starting point is provided, the function will default
|
|
1183
1096
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getRightMost`
|
|
1184
|
-
* function specifies the type of iteration to be used when
|
|
1185
|
-
*
|
|
1186
|
-
* @returns The `getRightMost` function returns the result of the callback function `C
|
|
1187
|
-
*
|
|
1188
|
-
*
|
|
1189
|
-
*
|
|
1190
|
-
* rightmost node is returned
|
|
1097
|
+
* function specifies the type of iteration to be used when traversing the binary tree nodes. It can
|
|
1098
|
+
* have two possible values:
|
|
1099
|
+
* @returns The `getRightMost` function returns the result of the callback function `C`, which is
|
|
1100
|
+
* passed as a parameter to the function. The callback function is called with the rightmost node in
|
|
1101
|
+
* the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
|
|
1102
|
+
* other).
|
|
1191
1103
|
*/
|
|
1192
1104
|
getRightMost<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1193
|
-
callback: C = this.
|
|
1194
|
-
beginRoot:
|
|
1105
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1106
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1195
1107
|
iterationType: IterationType = this.iterationType
|
|
1196
1108
|
): ReturnType<C> {
|
|
1197
1109
|
if (this.isNIL(beginRoot)) return callback(undefined);
|
|
@@ -1221,10 +1133,14 @@ export class BinaryTree<
|
|
|
1221
1133
|
* Time Complexity: O(log n)
|
|
1222
1134
|
* Space Complexity: O(1)
|
|
1223
1135
|
*
|
|
1224
|
-
* The function returns the predecessor node of a given node in a
|
|
1225
|
-
*
|
|
1226
|
-
*
|
|
1227
|
-
*
|
|
1136
|
+
* The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
|
|
1137
|
+
* binary tree.
|
|
1138
|
+
* @param {NODE} node - The `getPredecessor` function you provided seems to be attempting to find the
|
|
1139
|
+
* predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
|
|
1140
|
+
* while loop condition that might cause an infinite loop.
|
|
1141
|
+
* @returns The `getPredecessor` function returns the predecessor node of the input `NODE` parameter.
|
|
1142
|
+
* If the left child of the input node exists, it traverses to the rightmost node of the left subtree
|
|
1143
|
+
* to find the predecessor. If the left child does not exist, it returns the input node itself.
|
|
1228
1144
|
*/
|
|
1229
1145
|
getPredecessor(node: NODE): NODE {
|
|
1230
1146
|
if (this.isRealNode(node.left)) {
|
|
@@ -1244,10 +1160,14 @@ export class BinaryTree<
|
|
|
1244
1160
|
* Time Complexity: O(log n)
|
|
1245
1161
|
* Space Complexity: O(1)
|
|
1246
1162
|
*
|
|
1247
|
-
* The function `getSuccessor` returns the next node in
|
|
1248
|
-
*
|
|
1249
|
-
* @
|
|
1250
|
-
*
|
|
1163
|
+
* The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
|
|
1164
|
+
* binary tree.
|
|
1165
|
+
* @param {K | NODE | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
|
|
1166
|
+
* type `K`, `NODE`, or `null`.
|
|
1167
|
+
* @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
|
|
1168
|
+
* a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
|
|
1169
|
+
* have a right child, the function traverses up the parent nodes until it finds a node that is not
|
|
1170
|
+
* the right child of its parent, and returns that node
|
|
1251
1171
|
*/
|
|
1252
1172
|
getSuccessor(x?: K | NODE | null): OptBTNOrNull<NODE> {
|
|
1253
1173
|
x = this.ensureNode(x);
|
|
@@ -1268,14 +1188,14 @@ export class BinaryTree<
|
|
|
1268
1188
|
dfs<C extends BTNCallback<NODE>>(
|
|
1269
1189
|
callback?: C,
|
|
1270
1190
|
pattern?: DFSOrderPattern,
|
|
1271
|
-
beginRoot?:
|
|
1191
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1272
1192
|
iterationType?: IterationType
|
|
1273
1193
|
): ReturnType<C>[];
|
|
1274
1194
|
|
|
1275
1195
|
dfs<C extends BTNCallback<NODE | null>>(
|
|
1276
1196
|
callback?: C,
|
|
1277
1197
|
pattern?: DFSOrderPattern,
|
|
1278
|
-
beginRoot?:
|
|
1198
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1279
1199
|
iterationType?: IterationType,
|
|
1280
1200
|
includeNull?: boolean
|
|
1281
1201
|
): ReturnType<C>[];
|
|
@@ -1284,29 +1204,32 @@ export class BinaryTree<
|
|
|
1284
1204
|
* Time complexity: O(n)
|
|
1285
1205
|
* Space complexity: O(n)
|
|
1286
1206
|
*
|
|
1287
|
-
* The `dfs`
|
|
1288
|
-
*
|
|
1289
|
-
* @param {C} callback - The `callback` parameter is a
|
|
1290
|
-
*
|
|
1291
|
-
*
|
|
1292
|
-
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter
|
|
1293
|
-
*
|
|
1294
|
-
*
|
|
1295
|
-
*
|
|
1296
|
-
* is
|
|
1297
|
-
*
|
|
1298
|
-
*
|
|
1299
|
-
*
|
|
1300
|
-
*
|
|
1301
|
-
*
|
|
1302
|
-
*
|
|
1303
|
-
* values
|
|
1304
|
-
*
|
|
1207
|
+
* The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
|
|
1208
|
+
* specified parameters.
|
|
1209
|
+
* @param {C} callback - The `callback` parameter is a generic type `C` that extends the
|
|
1210
|
+
* `BTNCallback` interface with a type parameter of `OptBTNOrNull<NODE>`. It has a default value of
|
|
1211
|
+
* `this._DEFAULT_BTN_CALLBACK as C`.
|
|
1212
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
|
|
1213
|
+
* order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
|
|
1214
|
+
* possible values for the `pattern` parameter are:
|
|
1215
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `dfs`
|
|
1216
|
+
* method is used to specify the starting point for the Depth-First Search traversal. It can be
|
|
1217
|
+
* either a `BTNKeyOrNodeOrEntry` object representing a key, node, or entry in the binary tree map,
|
|
1218
|
+
* or it can be a
|
|
1219
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` method specifies
|
|
1220
|
+
* the type of iteration to be performed during the depth-first search traversal. It is used to
|
|
1221
|
+
* determine the order in which nodes are visited during the traversal.
|
|
1222
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `dfs` method is a boolean flag
|
|
1223
|
+
* that determines whether null values should be included in the traversal or not. If `includeNull`
|
|
1224
|
+
* is set to `true`, then null values will be included in the traversal process. If it is set to
|
|
1225
|
+
* `false`,
|
|
1226
|
+
* @returns The `dfs` method is returning an array of the return type specified by the generic type
|
|
1227
|
+
* parameter `C`. The return type is determined by the callback function provided to the method.
|
|
1305
1228
|
*/
|
|
1306
1229
|
dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1307
|
-
callback: C = this.
|
|
1230
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1308
1231
|
pattern: DFSOrderPattern = 'IN',
|
|
1309
|
-
beginRoot:
|
|
1232
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1310
1233
|
iterationType: IterationType = this.iterationType,
|
|
1311
1234
|
includeNull = false
|
|
1312
1235
|
): ReturnType<C>[] {
|
|
@@ -1317,14 +1240,14 @@ export class BinaryTree<
|
|
|
1317
1240
|
|
|
1318
1241
|
bfs<C extends BTNCallback<NODE>>(
|
|
1319
1242
|
callback?: C,
|
|
1320
|
-
beginRoot?:
|
|
1243
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1321
1244
|
iterationType?: IterationType,
|
|
1322
1245
|
includeNull?: false
|
|
1323
1246
|
): ReturnType<C>[];
|
|
1324
1247
|
|
|
1325
1248
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1326
1249
|
callback?: C,
|
|
1327
|
-
beginRoot?:
|
|
1250
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1328
1251
|
iterationType?: IterationType,
|
|
1329
1252
|
includeNull?: true
|
|
1330
1253
|
): ReturnType<C>[];
|
|
@@ -1333,27 +1256,28 @@ export class BinaryTree<
|
|
|
1333
1256
|
* Time complexity: O(n)
|
|
1334
1257
|
* Space complexity: O(n)
|
|
1335
1258
|
*
|
|
1336
|
-
* The `bfs` function performs a breadth-first search on a binary tree
|
|
1337
|
-
*
|
|
1338
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1339
|
-
* the breadth-first search traversal. It
|
|
1340
|
-
*
|
|
1341
|
-
* @param {
|
|
1342
|
-
* starting point
|
|
1343
|
-
*
|
|
1344
|
-
*
|
|
1345
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
1346
|
-
* iteration to be performed. It can have two
|
|
1347
|
-
*
|
|
1348
|
-
*
|
|
1349
|
-
* `
|
|
1350
|
-
* set to `
|
|
1351
|
-
*
|
|
1352
|
-
* `
|
|
1259
|
+
* The `bfs` function performs a breadth-first search traversal on a binary tree or binary search
|
|
1260
|
+
* tree, executing a specified callback function on each node visited.
|
|
1261
|
+
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
1262
|
+
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
1263
|
+
* that extends the `BTNCallback` type, which takes a parameter of type `NODE` or `null`.
|
|
1264
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `bfs`
|
|
1265
|
+
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
1266
|
+
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
1267
|
+
* default value is the root node of the binary
|
|
1268
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `bfs` function
|
|
1269
|
+
* determines the type of iteration to be performed on the binary tree nodes. It can have two
|
|
1270
|
+
* possible values:
|
|
1271
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `bfs` function determines whether
|
|
1272
|
+
* to include `null` values in the breadth-first search traversal of a binary tree. If `includeNull`
|
|
1273
|
+
* is set to `true`, the traversal will include `null` values for nodes that do not have children
|
|
1274
|
+
* (left
|
|
1275
|
+
* @returns The `bfs` function returns an array of values that are the result of applying the
|
|
1276
|
+
* provided callback function to each node in the binary tree in a breadth-first search manner.
|
|
1353
1277
|
*/
|
|
1354
1278
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1355
|
-
callback: C = this.
|
|
1356
|
-
beginRoot:
|
|
1279
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1280
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1357
1281
|
iterationType: IterationType = this.iterationType,
|
|
1358
1282
|
includeNull = false
|
|
1359
1283
|
): ReturnType<C>[] {
|
|
@@ -1409,39 +1333,37 @@ export class BinaryTree<
|
|
|
1409
1333
|
* Time complexity: O(n)
|
|
1410
1334
|
* Space complexity: O(n)
|
|
1411
1335
|
*
|
|
1412
|
-
* The `leaves` function in TypeScript
|
|
1413
|
-
*
|
|
1336
|
+
* The `leaves` function in TypeScript returns an array of values from leaf nodes in a binary tree
|
|
1337
|
+
* structure based on a specified callback and iteration type.
|
|
1414
1338
|
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
|
|
1415
|
-
* in the binary tree. It is
|
|
1416
|
-
*
|
|
1417
|
-
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the `leaves`
|
|
1339
|
+
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
1340
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `leaves`
|
|
1418
1341
|
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
1419
|
-
* tree. It
|
|
1420
|
-
*
|
|
1342
|
+
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
1343
|
+
* explicitly provided, the default value
|
|
1421
1344
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
|
|
1422
1345
|
* specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
|
|
1423
1346
|
* can have two possible values:
|
|
1424
1347
|
* @returns The `leaves` method returns an array of values that are the result of applying the
|
|
1425
|
-
* provided callback function to
|
|
1348
|
+
* provided callback function to each leaf node in the binary tree.
|
|
1426
1349
|
*/
|
|
1427
1350
|
leaves<C extends BTNCallback<NODE | null>>(
|
|
1428
|
-
callback: C = this.
|
|
1429
|
-
beginRoot:
|
|
1351
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1352
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1430
1353
|
iterationType: IterationType = this.iterationType
|
|
1431
1354
|
): ReturnType<C>[] {
|
|
1432
1355
|
beginRoot = this.ensureNode(beginRoot);
|
|
1433
1356
|
const leaves: ReturnType<BTNCallback<NODE>>[] = [];
|
|
1434
|
-
if (!this.isRealNode(beginRoot))
|
|
1435
|
-
|
|
1436
|
-
}
|
|
1357
|
+
if (!this.isRealNode(beginRoot)) return [];
|
|
1358
|
+
|
|
1437
1359
|
if (iterationType === 'RECURSIVE') {
|
|
1438
1360
|
const dfs = (cur: NODE) => {
|
|
1439
1361
|
if (this.isLeaf(cur)) {
|
|
1440
1362
|
leaves.push(callback(cur));
|
|
1441
1363
|
}
|
|
1442
1364
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
1443
|
-
this.isRealNode(cur.left)
|
|
1444
|
-
this.isRealNode(cur.right)
|
|
1365
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
1366
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
1445
1367
|
};
|
|
1446
1368
|
|
|
1447
1369
|
dfs(beginRoot);
|
|
@@ -1453,8 +1375,8 @@ export class BinaryTree<
|
|
|
1453
1375
|
if (this.isLeaf(cur)) {
|
|
1454
1376
|
leaves.push(callback(cur));
|
|
1455
1377
|
}
|
|
1456
|
-
this.isRealNode(cur.left)
|
|
1457
|
-
this.isRealNode(cur.right)
|
|
1378
|
+
if (this.isRealNode(cur.left)) queue.push(cur.left);
|
|
1379
|
+
if (this.isRealNode(cur.right)) queue.push(cur.right);
|
|
1458
1380
|
}
|
|
1459
1381
|
}
|
|
1460
1382
|
}
|
|
@@ -1464,14 +1386,14 @@ export class BinaryTree<
|
|
|
1464
1386
|
|
|
1465
1387
|
listLevels<C extends BTNCallback<NODE>>(
|
|
1466
1388
|
callback?: C,
|
|
1467
|
-
beginRoot?:
|
|
1389
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1468
1390
|
iterationType?: IterationType,
|
|
1469
1391
|
includeNull?: false
|
|
1470
1392
|
): ReturnType<C>[][];
|
|
1471
1393
|
|
|
1472
1394
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1473
1395
|
callback?: C,
|
|
1474
|
-
beginRoot?:
|
|
1396
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1475
1397
|
iterationType?: IterationType,
|
|
1476
1398
|
includeNull?: true
|
|
1477
1399
|
): ReturnType<C>[][];
|
|
@@ -1480,26 +1402,29 @@ export class BinaryTree<
|
|
|
1480
1402
|
* Time complexity: O(n)
|
|
1481
1403
|
* Space complexity: O(n)
|
|
1482
1404
|
*
|
|
1483
|
-
* The `listLevels` function
|
|
1484
|
-
*
|
|
1485
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1486
|
-
* the tree. It
|
|
1487
|
-
*
|
|
1488
|
-
* @param {
|
|
1489
|
-
* starting point for traversing the tree. It can be
|
|
1490
|
-
*
|
|
1491
|
-
*
|
|
1492
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
1493
|
-
* iteration to be performed on the binary tree. It can have two
|
|
1494
|
-
*
|
|
1495
|
-
*
|
|
1496
|
-
*
|
|
1497
|
-
*
|
|
1498
|
-
*
|
|
1405
|
+
* The `listLevels` function in TypeScript generates a list of nodes at each level of a binary tree,
|
|
1406
|
+
* using either recursive or iterative traversal based on the specified iteration type.
|
|
1407
|
+
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
1408
|
+
* the binary tree during the traversal. It is used to process each node and determine what
|
|
1409
|
+
* information to include in the output for each level of the tree.
|
|
1410
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1411
|
+
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
1412
|
+
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
1413
|
+
* root of the binary tree.
|
|
1414
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `listLevels` function
|
|
1415
|
+
* determines the type of iteration to be performed on the binary tree nodes. It can have two
|
|
1416
|
+
* possible values:
|
|
1417
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `listLevels` method determines
|
|
1418
|
+
* whether or not to include null nodes in the traversal of the binary tree. If `includeNull` is set
|
|
1419
|
+
* to `true`, the traversal will include null nodes in the levels of the tree. If set to `false`,
|
|
1420
|
+
* null
|
|
1421
|
+
* @returns The `listLevels` method returns an array of arrays, where each inner array represents a
|
|
1422
|
+
* level in a binary tree. Each inner array contains the return value of the provided callback
|
|
1423
|
+
* function applied to the nodes at that level.
|
|
1499
1424
|
*/
|
|
1500
1425
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1501
|
-
callback: C = this.
|
|
1502
|
-
beginRoot:
|
|
1426
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1427
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1503
1428
|
iterationType: IterationType = this.iterationType,
|
|
1504
1429
|
includeNull = false
|
|
1505
1430
|
): ReturnType<C>[][] {
|
|
@@ -1548,28 +1473,29 @@ export class BinaryTree<
|
|
|
1548
1473
|
* Time complexity: O(n)
|
|
1549
1474
|
* Space complexity: O(n)
|
|
1550
1475
|
*
|
|
1551
|
-
* The `morris` function performs a
|
|
1552
|
-
* algorithm.
|
|
1553
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1554
|
-
*
|
|
1555
|
-
*
|
|
1556
|
-
* the
|
|
1557
|
-
*
|
|
1558
|
-
*
|
|
1559
|
-
*
|
|
1560
|
-
*
|
|
1561
|
-
*
|
|
1562
|
-
*
|
|
1563
|
-
* @returns The
|
|
1564
|
-
* callback function
|
|
1476
|
+
* The `morris` function in TypeScript performs a Depth-First Search traversal on a binary tree using
|
|
1477
|
+
* Morris Traversal algorithm with different order patterns.
|
|
1478
|
+
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
1479
|
+
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
1480
|
+
* `BTNCallback<NODE>` type. The default value for `callback` is `this._DEFAULT
|
|
1481
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
1482
|
+
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
1483
|
+
* values for the `pattern` parameter are:
|
|
1484
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `morris`
|
|
1485
|
+
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
1486
|
+
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
1487
|
+
* key, a node, an entry, or a reference
|
|
1488
|
+
* @returns The `morris` function is returning an array of values that are the result of applying the
|
|
1489
|
+
* provided callback function to each node in the binary tree in the specified order pattern (IN,
|
|
1490
|
+
* PRE, or POST).
|
|
1565
1491
|
*/
|
|
1566
1492
|
morris<C extends BTNCallback<NODE>>(
|
|
1567
|
-
callback: C = this.
|
|
1493
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1568
1494
|
pattern: DFSOrderPattern = 'IN',
|
|
1569
|
-
beginRoot:
|
|
1495
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root
|
|
1570
1496
|
): ReturnType<C>[] {
|
|
1571
1497
|
beginRoot = this.ensureNode(beginRoot);
|
|
1572
|
-
if (beginRoot
|
|
1498
|
+
if (!beginRoot) return [];
|
|
1573
1499
|
const ans: ReturnType<BTNCallback<NODE>>[] = [];
|
|
1574
1500
|
|
|
1575
1501
|
let cur: OptBTNOrNull<NODE> = beginRoot;
|
|
@@ -1653,8 +1579,12 @@ export class BinaryTree<
|
|
|
1653
1579
|
* Time complexity: O(n)
|
|
1654
1580
|
* Space complexity: O(n)
|
|
1655
1581
|
*
|
|
1656
|
-
* The `clone` function creates a deep copy of a tree
|
|
1657
|
-
*
|
|
1582
|
+
* The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first
|
|
1583
|
+
* search.
|
|
1584
|
+
* @returns The `clone()` method is returning a cloned copy of the tree with the same structure and
|
|
1585
|
+
* values as the original tree. The method creates a new tree, iterates over the nodes of the
|
|
1586
|
+
* original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
|
|
1587
|
+
* the original tree is null, a null node is added to the cloned tree. If a node
|
|
1658
1588
|
*/
|
|
1659
1589
|
clone(): TREE {
|
|
1660
1590
|
const cloned = this.createTree();
|
|
@@ -1663,7 +1593,7 @@ export class BinaryTree<
|
|
|
1663
1593
|
if (node === null) cloned.add(null);
|
|
1664
1594
|
else cloned.add([node.key, node.value]);
|
|
1665
1595
|
},
|
|
1666
|
-
this.
|
|
1596
|
+
this._root,
|
|
1667
1597
|
this.iterationType,
|
|
1668
1598
|
true
|
|
1669
1599
|
);
|
|
@@ -1674,16 +1604,17 @@ export class BinaryTree<
|
|
|
1674
1604
|
* Time Complexity: O(n)
|
|
1675
1605
|
* Space Complexity: O(n)
|
|
1676
1606
|
*
|
|
1677
|
-
* The `filter` function
|
|
1678
|
-
*
|
|
1679
|
-
*
|
|
1680
|
-
*
|
|
1681
|
-
* of the
|
|
1682
|
-
* @param {any} [thisArg] - The `thisArg` parameter
|
|
1683
|
-
*
|
|
1684
|
-
*
|
|
1685
|
-
*
|
|
1686
|
-
*
|
|
1607
|
+
* The `filter` function iterates over key-value pairs in a tree data structure and creates a new
|
|
1608
|
+
* tree with elements that satisfy a given predicate.
|
|
1609
|
+
* @param predicate - The `predicate` parameter in the `filter` method is a function that will be
|
|
1610
|
+
* called with four arguments: the `value` of the current entry, the `key` of the current entry, the
|
|
1611
|
+
* `index` of the current entry in the iteration, and the reference to the tree itself (`
|
|
1612
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `filter` method allows you to specify the
|
|
1613
|
+
* value of `this` that should be used when executing the `predicate` function. This is useful when
|
|
1614
|
+
* the `predicate` function relies on the context of a specific object or value. By providing a
|
|
1615
|
+
* `thisArg
|
|
1616
|
+
* @returns The `filter` method is returning a new tree that contains entries that pass the provided
|
|
1617
|
+
* predicate function.
|
|
1687
1618
|
*/
|
|
1688
1619
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
|
|
1689
1620
|
const newTree = this.createTree();
|
|
@@ -1700,16 +1631,16 @@ export class BinaryTree<
|
|
|
1700
1631
|
* Time Complexity: O(n)
|
|
1701
1632
|
* Space Complexity: O(n)
|
|
1702
1633
|
*
|
|
1703
|
-
* The `map` function
|
|
1704
|
-
* tree.
|
|
1705
|
-
* @param callback - The callback parameter is a function that will be called
|
|
1706
|
-
* tree. It takes
|
|
1707
|
-
*
|
|
1708
|
-
*
|
|
1709
|
-
*
|
|
1710
|
-
*
|
|
1711
|
-
*
|
|
1712
|
-
*
|
|
1634
|
+
* The `map` function iterates over key-value pairs in a tree data structure, applies a callback
|
|
1635
|
+
* function to each value, and returns a new tree with the updated values.
|
|
1636
|
+
* @param callback - The `callback` parameter in the `map` method is a function that will be called
|
|
1637
|
+
* on each entry in the tree. It takes four arguments:
|
|
1638
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is an optional parameter
|
|
1639
|
+
* that specifies the value to be passed as `this` when executing the callback function. If provided,
|
|
1640
|
+
* the `thisArg` value will be used as the `this` value within the callback function. If `thisArg
|
|
1641
|
+
* @returns The `map` method is returning a new tree with the entries modified by the provided
|
|
1642
|
+
* callback function. Each entry in the original tree is passed to the callback function, and the
|
|
1643
|
+
* result of the callback function is added to the new tree.
|
|
1713
1644
|
*/
|
|
1714
1645
|
map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
|
|
1715
1646
|
const newTree = this.createTree();
|
|
@@ -1734,17 +1665,24 @@ export class BinaryTree<
|
|
|
1734
1665
|
* Time Complexity: O(n)
|
|
1735
1666
|
* Space Complexity: O(n)
|
|
1736
1667
|
*
|
|
1737
|
-
* The `
|
|
1738
|
-
*
|
|
1739
|
-
*
|
|
1740
|
-
*
|
|
1741
|
-
*
|
|
1742
|
-
*
|
|
1743
|
-
*
|
|
1744
|
-
*
|
|
1745
|
-
*
|
|
1668
|
+
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
1669
|
+
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
1670
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1671
|
+
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
1672
|
+
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
1673
|
+
* the default is set to the root
|
|
1674
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter in the `toVisual` method is an
|
|
1675
|
+
* object that contains the following properties:
|
|
1676
|
+
* @returns The `override toVisual` method returns a string that represents the visual display of the
|
|
1677
|
+
* binary tree based on the provided options for showing undefined, null, and Red-Black NIL nodes.
|
|
1678
|
+
* The method constructs the visual representation by calling the `_displayAux` method and appending
|
|
1679
|
+
* the lines to the output string. The final output string contains the visual representation of the
|
|
1680
|
+
* binary tree with the specified options.
|
|
1746
1681
|
*/
|
|
1747
|
-
override
|
|
1682
|
+
override toVisual(
|
|
1683
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1684
|
+
options?: BinaryTreePrintOptions
|
|
1685
|
+
): string {
|
|
1748
1686
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1749
1687
|
beginRoot = this.ensureNode(beginRoot);
|
|
1750
1688
|
let output = '';
|
|
@@ -1776,14 +1714,14 @@ export class BinaryTree<
|
|
|
1776
1714
|
protected _dfs<C extends BTNCallback<NODE>>(
|
|
1777
1715
|
callback?: C,
|
|
1778
1716
|
pattern?: DFSOrderPattern,
|
|
1779
|
-
beginRoot?:
|
|
1717
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1780
1718
|
iterationType?: IterationType
|
|
1781
1719
|
): ReturnType<C>[];
|
|
1782
1720
|
|
|
1783
1721
|
protected _dfs<C extends BTNCallback<NODE | null>>(
|
|
1784
1722
|
callback?: C,
|
|
1785
1723
|
pattern?: DFSOrderPattern,
|
|
1786
|
-
beginRoot?:
|
|
1724
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1787
1725
|
iterationType?: IterationType,
|
|
1788
1726
|
includeNull?: boolean
|
|
1789
1727
|
): ReturnType<C>[];
|
|
@@ -1792,49 +1730,48 @@ export class BinaryTree<
|
|
|
1792
1730
|
* Time complexity: O(n)
|
|
1793
1731
|
* Space complexity: O(n)
|
|
1794
1732
|
*
|
|
1795
|
-
* The
|
|
1733
|
+
* The `_dfs` function performs a depth-first search traversal on a binary tree structure based on
|
|
1796
1734
|
* the specified order pattern and callback function.
|
|
1797
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1798
|
-
* visited during the depth-first search. It is of type `C`, which
|
|
1799
|
-
* `BTNCallback<OptBTNOrNull<NODE>>`. The default value
|
|
1800
|
-
* provided.
|
|
1735
|
+
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
1736
|
+
* called on each node visited during the depth-first search traversal. It is of type `C`, which
|
|
1737
|
+
* extends `BTNCallback<OptBTNOrNull<NODE>>`. The default value for this parameter is `this._DEFAULT
|
|
1801
1738
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
1802
|
-
* order in which the Depth-First Search
|
|
1803
|
-
*
|
|
1804
|
-
* @param {
|
|
1739
|
+
* order in which the nodes are visited during the Depth-First Search traversal. It can have one of
|
|
1740
|
+
* the following values:
|
|
1741
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `_dfs`
|
|
1805
1742
|
* method is used to specify the starting point for the depth-first search traversal in a binary
|
|
1806
|
-
* tree. It can be provided as either
|
|
1807
|
-
*
|
|
1743
|
+
* tree. It can be provided as either a `BTNKeyOrNodeOrEntry` object or a reference to the root node
|
|
1744
|
+
* of the tree. If no specific
|
|
1808
1745
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
|
|
1809
|
-
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal
|
|
1810
|
-
* can have two possible values:
|
|
1746
|
+
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
|
|
1747
|
+
* binary tree. It can have two possible values:
|
|
1811
1748
|
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag
|
|
1812
1749
|
* that determines whether null nodes should be included in the depth-first search traversal. If
|
|
1813
|
-
* `includeNull` is set to `true`,
|
|
1814
|
-
*
|
|
1750
|
+
* `includeNull` is set to `true`, null nodes will be considered during the traversal process. If it
|
|
1751
|
+
* is set to `false`,
|
|
1815
1752
|
* @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input
|
|
1816
1753
|
* and returns a boolean value. It is used to determine whether the left child of a node should be
|
|
1817
1754
|
* visited during the depth-first search traversal. By default, it checks if the node is truthy (not
|
|
1818
1755
|
* null or undefined
|
|
1819
|
-
* @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as
|
|
1820
|
-
*
|
|
1756
|
+
* @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as an
|
|
1757
|
+
* argument and returns a boolean value. It is used to determine whether the right child of a node
|
|
1821
1758
|
* should be visited during the depth-first search traversal. The default implementation checks if
|
|
1822
|
-
* the node is truthy before visiting the right child
|
|
1759
|
+
* the node is truthy before visiting the right child
|
|
1823
1760
|
* @param shouldVisitRoot - The `shouldVisitRoot` parameter is a function that takes a node as an
|
|
1824
|
-
* argument and returns a boolean value. It is used to determine whether
|
|
1761
|
+
* argument and returns a boolean value. It is used to determine whether the root node should be
|
|
1825
1762
|
* visited during the depth-first search traversal based on certain conditions. The default
|
|
1826
1763
|
* implementation checks if the node is a real node or null based
|
|
1827
|
-
* @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as
|
|
1828
|
-
*
|
|
1829
|
-
* depth-first search traversal. The default implementation
|
|
1830
|
-
*
|
|
1831
|
-
* @returns The `_dfs`
|
|
1764
|
+
* @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as an
|
|
1765
|
+
* argument and returns a boolean value indicating whether the node should be processed during the
|
|
1766
|
+
* depth-first search traversal. The default implementation checks if the node is a real node or null
|
|
1767
|
+
* based on the `includeNull` flag. If `
|
|
1768
|
+
* @returns The function `_dfs` returns an array of the return type of the callback function provided
|
|
1832
1769
|
* as input.
|
|
1833
1770
|
*/
|
|
1834
1771
|
protected _dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1835
|
-
callback: C = this.
|
|
1772
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1836
1773
|
pattern: DFSOrderPattern = 'IN',
|
|
1837
|
-
beginRoot:
|
|
1774
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1838
1775
|
iterationType: IterationType = this.iterationType,
|
|
1839
1776
|
includeNull = false,
|
|
1840
1777
|
shouldVisitLeft: (node: OptBTNOrNull<NODE>) => boolean = node => !!node,
|
|
@@ -1928,13 +1865,18 @@ export class BinaryTree<
|
|
|
1928
1865
|
* Time Complexity: O(1)
|
|
1929
1866
|
* Space Complexity: O(1)
|
|
1930
1867
|
*
|
|
1931
|
-
* The function `_getIterator`
|
|
1932
|
-
*
|
|
1933
|
-
* @param node - The `node` parameter
|
|
1934
|
-
* initially set to the root node of the
|
|
1935
|
-
*
|
|
1868
|
+
* The function `_getIterator` returns an iterable iterator for a binary tree data structure, either
|
|
1869
|
+
* using an iterative approach or a recursive approach based on the specified iteration type.
|
|
1870
|
+
* @param node - The `node` parameter in the `_getIterator` method represents the current node being
|
|
1871
|
+
* processed during iteration. It is initially set to the root node of the data structure (or the
|
|
1872
|
+
* node passed as an argument), and then it is traversed through the data structure based on the
|
|
1873
|
+
* iteration type specified (`ITER
|
|
1874
|
+
* @returns The `_getIterator` method returns an IterableIterator containing key-value pairs of nodes
|
|
1875
|
+
* in a binary tree structure. The method uses an iterative approach to traverse the tree based on
|
|
1876
|
+
* the `iterationType` property. If the `iterationType` is set to 'ITERATIVE', the method uses a
|
|
1877
|
+
* stack to perform an in-order traversal of the tree. If the `iterationType` is not 'ITERATIVE
|
|
1936
1878
|
*/
|
|
1937
|
-
protected *_getIterator(node = this.
|
|
1879
|
+
protected *_getIterator(node = this._root): IterableIterator<[K, V | undefined]> {
|
|
1938
1880
|
if (!node) return;
|
|
1939
1881
|
|
|
1940
1882
|
if (this.iterationType === 'ITERATIVE') {
|
|
@@ -1969,18 +1911,16 @@ export class BinaryTree<
|
|
|
1969
1911
|
* Time Complexity: O(n)
|
|
1970
1912
|
* Space Complexity: O(n)
|
|
1971
1913
|
*
|
|
1972
|
-
* The `_displayAux`
|
|
1973
|
-
*
|
|
1974
|
-
* @param
|
|
1975
|
-
* It can be
|
|
1976
|
-
*
|
|
1977
|
-
*
|
|
1978
|
-
*
|
|
1979
|
-
*
|
|
1980
|
-
*
|
|
1981
|
-
*
|
|
1982
|
-
* 3. `totalHeight`: The total height of the node display.
|
|
1983
|
-
* 4. `middleIndex`: The index of the middle character
|
|
1914
|
+
* The function `_displayAux` in TypeScript is responsible for generating the display layout of nodes
|
|
1915
|
+
* in a binary tree based on specified options.
|
|
1916
|
+
* @param node - The `node` parameter in the `_displayAux` function represents a node in a binary
|
|
1917
|
+
* tree. It can be either a valid node containing a key or a special type of node like null,
|
|
1918
|
+
* undefined, or a Red-Black tree NIL node. The function checks the type of the node and its
|
|
1919
|
+
* @param {BinaryTreePrintOptions} options - The `options` parameter in the `_displayAux` function
|
|
1920
|
+
* contains the following properties:
|
|
1921
|
+
* @returns The `_displayAux` function returns a `NodeDisplayLayout`, which is an array containing
|
|
1922
|
+
* information about how to display a node in a binary tree. The `NodeDisplayLayout` consists of four
|
|
1923
|
+
* elements:
|
|
1984
1924
|
*/
|
|
1985
1925
|
protected _displayAux(node: OptBTNOrNull<NODE>, options: BinaryTreePrintOptions): NodeDisplayLayout {
|
|
1986
1926
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
@@ -2050,24 +1990,26 @@ export class BinaryTree<
|
|
|
2050
1990
|
}
|
|
2051
1991
|
}
|
|
2052
1992
|
|
|
2053
|
-
protected
|
|
1993
|
+
protected _DEFAULT_BTN_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
|
|
2054
1994
|
|
|
2055
1995
|
/**
|
|
2056
1996
|
* Time Complexity: O(1)
|
|
2057
1997
|
* Space Complexity: O(1)
|
|
2058
1998
|
*
|
|
2059
|
-
* The function
|
|
2060
|
-
* @param {
|
|
2061
|
-
*
|
|
2062
|
-
*
|
|
2063
|
-
* @param {
|
|
2064
|
-
* the properties will be swapped with
|
|
2065
|
-
*
|
|
2066
|
-
*
|
|
1999
|
+
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
2000
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} srcNode - The `srcNode` parameter in the
|
|
2001
|
+
* `_swapProperties` method can be either a BTNKeyOrNodeOrEntry object containing key and value
|
|
2002
|
+
* properties, or it can be of type R.
|
|
2003
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} destNode - The `destNode` parameter in the
|
|
2004
|
+
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
2005
|
+
* the `srcNode`. It can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. The method ensures that
|
|
2006
|
+
* both `srcNode
|
|
2007
|
+
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
2008
|
+
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
2067
2009
|
*/
|
|
2068
2010
|
protected _swapProperties(
|
|
2069
|
-
srcNode:
|
|
2070
|
-
destNode:
|
|
2011
|
+
srcNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
2012
|
+
destNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R
|
|
2071
2013
|
): NODE | undefined {
|
|
2072
2014
|
srcNode = this.ensureNode(srcNode);
|
|
2073
2015
|
destNode = this.ensureNode(destNode);
|
|
@@ -2093,13 +2035,15 @@ export class BinaryTree<
|
|
|
2093
2035
|
* Time Complexity: O(1)
|
|
2094
2036
|
* Space Complexity: O(1)
|
|
2095
2037
|
*
|
|
2096
|
-
* The function replaces
|
|
2097
|
-
*
|
|
2098
|
-
*
|
|
2099
|
-
*
|
|
2100
|
-
*
|
|
2101
|
-
*
|
|
2102
|
-
*
|
|
2038
|
+
* The _replaceNode function replaces an old node with a new node in a binary tree structure.
|
|
2039
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that you want to replace in a
|
|
2040
|
+
* tree data structure.
|
|
2041
|
+
* @param {NODE} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
|
|
2042
|
+
* that will replace the `oldNode` in a tree data structure. This function is responsible for
|
|
2043
|
+
* updating the parent, left child, right child, and root (if necessary) references when replacing a
|
|
2044
|
+
* node in the tree.
|
|
2045
|
+
* @returns The method `_replaceNode` is returning the `newNode` that was passed as a parameter after
|
|
2046
|
+
* replacing the `oldNode` with it in the binary tree structure.
|
|
2103
2047
|
*/
|
|
2104
2048
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
2105
2049
|
if (oldNode.parent) {
|
|
@@ -2112,8 +2056,8 @@ export class BinaryTree<
|
|
|
2112
2056
|
newNode.left = oldNode.left;
|
|
2113
2057
|
newNode.right = oldNode.right;
|
|
2114
2058
|
newNode.parent = oldNode.parent;
|
|
2115
|
-
if (this.
|
|
2116
|
-
this.
|
|
2059
|
+
if (this._root === oldNode) {
|
|
2060
|
+
this._setRoot(newNode);
|
|
2117
2061
|
}
|
|
2118
2062
|
|
|
2119
2063
|
return newNode;
|
|
@@ -2123,10 +2067,10 @@ export class BinaryTree<
|
|
|
2123
2067
|
* Time Complexity: O(1)
|
|
2124
2068
|
* Space Complexity: O(1)
|
|
2125
2069
|
*
|
|
2126
|
-
* The function sets the root
|
|
2127
|
-
*
|
|
2128
|
-
* @param
|
|
2129
|
-
*
|
|
2070
|
+
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
2071
|
+
* of the previous root node.
|
|
2072
|
+
* @param v - The parameter `v` in the `_setRoot` method is of type `OptBTNOrNull<NODE>`, which means
|
|
2073
|
+
* it can either be an optional `NODE` type or `null`.
|
|
2130
2074
|
*/
|
|
2131
2075
|
protected _setRoot(v: OptBTNOrNull<NODE>) {
|
|
2132
2076
|
if (v) {
|
|
@@ -2139,23 +2083,51 @@ export class BinaryTree<
|
|
|
2139
2083
|
* Time Complexity: O(1)
|
|
2140
2084
|
* Space Complexity: O(1)
|
|
2141
2085
|
*
|
|
2142
|
-
* The function `
|
|
2143
|
-
*
|
|
2144
|
-
*
|
|
2145
|
-
* the
|
|
2146
|
-
*
|
|
2147
|
-
*
|
|
2148
|
-
*
|
|
2149
|
-
* @returns the callback parameter.
|
|
2086
|
+
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
2087
|
+
* predicate function for a binary tree node.
|
|
2088
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrEntryOrRawOrPredicate - The
|
|
2089
|
+
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
2090
|
+
* parameter `keyOrEntryOrRawOrPredicate` is transformed into a valid predicate function that can be
|
|
2091
|
+
* used for filtering nodes in a binary tree.
|
|
2092
|
+
* @returns A BTNPredicate<NODE> function is being returned.
|
|
2150
2093
|
*/
|
|
2151
|
-
protected
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2094
|
+
protected _ensurePredicate(
|
|
2095
|
+
keyOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>
|
|
2096
|
+
): BTNPredicate<NODE> {
|
|
2097
|
+
if (keyOrEntryOrRawOrPredicate === null || keyOrEntryOrRawOrPredicate === undefined)
|
|
2098
|
+
return (node: NODE) => (node ? false : false);
|
|
2099
|
+
|
|
2100
|
+
if (this._isPredicated(keyOrEntryOrRawOrPredicate)) return keyOrEntryOrRawOrPredicate;
|
|
2101
|
+
|
|
2102
|
+
if (this.isRealNode(keyOrEntryOrRawOrPredicate)) return (node: NODE) => node === keyOrEntryOrRawOrPredicate;
|
|
2103
|
+
|
|
2104
|
+
if (this.isEntry(keyOrEntryOrRawOrPredicate)) {
|
|
2105
|
+
const [key] = keyOrEntryOrRawOrPredicate;
|
|
2106
|
+
return (node: NODE) => node.key === key;
|
|
2157
2107
|
}
|
|
2158
2108
|
|
|
2159
|
-
return
|
|
2109
|
+
if (this.isKey(keyOrEntryOrRawOrPredicate)) return (node: NODE) => node.key === keyOrEntryOrRawOrPredicate;
|
|
2110
|
+
|
|
2111
|
+
if (this._toEntryFn) {
|
|
2112
|
+
const [key] = this._toEntryFn(keyOrEntryOrRawOrPredicate);
|
|
2113
|
+
return (node: NODE) => node.key === key;
|
|
2114
|
+
}
|
|
2115
|
+
return (node: NODE) => node.key === keyOrEntryOrRawOrPredicate;
|
|
2116
|
+
}
|
|
2117
|
+
|
|
2118
|
+
/**
|
|
2119
|
+
* Time Complexity: O(1)
|
|
2120
|
+
* Space Complexity: O(1)
|
|
2121
|
+
*
|
|
2122
|
+
* The function `_isPredicated` checks if a given parameter is a function.
|
|
2123
|
+
* @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
|
|
2124
|
+
* of value. In this context, the function `_isPredicated` is checking if `p` is a function that
|
|
2125
|
+
* satisfies the type `BTNPredicate<NODE>`.
|
|
2126
|
+
* @returns The function is checking if the input `p` is a function and returning a boolean value
|
|
2127
|
+
* based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
|
|
2128
|
+
* predicate function for a binary tree node. If `p` is not a function, it will return `false`.
|
|
2129
|
+
*/
|
|
2130
|
+
protected _isPredicated(p: any): p is BTNPredicate<NODE> {
|
|
2131
|
+
return typeof p === 'function';
|
|
2160
2132
|
}
|
|
2161
2133
|
}
|