linked-list-typed 1.52.6 → 1.52.9
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 +591 -402
- package/dist/data-structures/binary-tree/binary-tree.js +690 -604
- package/dist/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/data-structures/binary-tree/bst.js +122 -125
- package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/data-structures/binary-tree/rb-tree.js +42 -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 +27 -31
- package/dist/data-structures/binary-tree/tree-multi-map.js +45 -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 +46 -50
- package/src/data-structures/binary-tree/avl-tree.ts +68 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +716 -748
- package/src/data-structures/binary-tree/bst.ts +137 -146
- package/src/data-structures/binary-tree/rb-tree.ts +46 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +49 -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,42 @@ 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} keyOrNodeOrEntryOrRaw
|
|
519
|
+
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
520
|
+
* node, entry or raw data. 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
|
-
identifier: ReturnType<C> | null | undefined,
|
|
552
|
-
callback: C = this._DEFAULT_CALLBACK as C
|
|
553
|
-
): BinaryTreeDeleteResult<NODE>[] {
|
|
527
|
+
delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
|
|
554
528
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
555
|
-
if (!this.
|
|
556
|
-
callback = this._ensureCallback(identifier, callback);
|
|
529
|
+
if (!this._root) return deletedResult;
|
|
557
530
|
|
|
558
|
-
const curr = this.getNode(
|
|
531
|
+
const curr = this.getNode(keyOrNodeOrEntryOrRaw);
|
|
559
532
|
if (!curr) return deletedResult;
|
|
560
533
|
|
|
561
534
|
const parent: NODE | undefined = curr?.parent;
|
|
@@ -589,84 +562,56 @@ export class BinaryTree<
|
|
|
589
562
|
curr.right = undefined;
|
|
590
563
|
}
|
|
591
564
|
|
|
592
|
-
this._size = this.
|
|
565
|
+
this._size = this._size - 1;
|
|
593
566
|
|
|
594
567
|
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
595
568
|
return deletedResult;
|
|
596
569
|
}
|
|
597
570
|
|
|
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
571
|
/**
|
|
623
572
|
* Time Complexity: O(n)
|
|
624
573
|
* Space Complexity: O(k + log n)
|
|
625
574
|
*
|
|
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.
|
|
575
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
576
|
+
* or predicate, with options for recursive or iterative traversal.
|
|
577
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
578
|
+
* - The `getNodes` function you provided takes several parameters:
|
|
579
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
580
|
+
* determines whether to return only the first node that matches the criteria specified by the
|
|
581
|
+
* `keyOrNodeOrEntryOrRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
582
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
583
|
+
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
584
|
+
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
585
|
+
* not provided, the default value is set to `this._root
|
|
586
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
587
|
+
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
588
|
+
* can have two possible values:
|
|
589
|
+
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
590
|
+
* based on the input parameters and the iteration type specified.
|
|
645
591
|
*/
|
|
646
|
-
getNodes
|
|
647
|
-
|
|
648
|
-
callback: C = this._DEFAULT_CALLBACK as C,
|
|
592
|
+
getNodes(
|
|
593
|
+
keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
649
594
|
onlyOne = false,
|
|
650
|
-
beginRoot:
|
|
595
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
651
596
|
iterationType: IterationType = this.iterationType
|
|
652
597
|
): NODE[] {
|
|
653
|
-
if (
|
|
654
|
-
if (
|
|
598
|
+
if (keyOrNodeOrEntryOrRawOrPredicate === undefined) return [];
|
|
599
|
+
if (keyOrNodeOrEntryOrRawOrPredicate === null) return [];
|
|
655
600
|
beginRoot = this.ensureNode(beginRoot);
|
|
656
601
|
if (!beginRoot) return [];
|
|
657
|
-
callback = this.
|
|
602
|
+
const callback = this._ensurePredicate(keyOrNodeOrEntryOrRawOrPredicate);
|
|
658
603
|
|
|
659
604
|
const ans: NODE[] = [];
|
|
660
605
|
|
|
661
606
|
if (iterationType === 'RECURSIVE') {
|
|
662
607
|
const dfs = (cur: NODE) => {
|
|
663
|
-
if (callback(cur)
|
|
608
|
+
if (callback(cur)) {
|
|
664
609
|
ans.push(cur);
|
|
665
610
|
if (onlyOne) return;
|
|
666
611
|
}
|
|
667
612
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
668
|
-
this.isRealNode(cur.left)
|
|
669
|
-
this.isRealNode(cur.right)
|
|
613
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
614
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
670
615
|
};
|
|
671
616
|
|
|
672
617
|
dfs(beginRoot);
|
|
@@ -675,12 +620,12 @@ export class BinaryTree<
|
|
|
675
620
|
while (stack.length > 0) {
|
|
676
621
|
const cur = stack.pop();
|
|
677
622
|
if (this.isRealNode(cur)) {
|
|
678
|
-
if (callback(cur)
|
|
623
|
+
if (callback(cur)) {
|
|
679
624
|
ans.push(cur);
|
|
680
625
|
if (onlyOne) return ans;
|
|
681
626
|
}
|
|
682
|
-
this.isRealNode(cur.left)
|
|
683
|
-
this.isRealNode(cur.right)
|
|
627
|
+
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
628
|
+
if (this.isRealNode(cur.right)) stack.push(cur.right);
|
|
684
629
|
}
|
|
685
630
|
}
|
|
686
631
|
}
|
|
@@ -688,178 +633,115 @@ export class BinaryTree<
|
|
|
688
633
|
return ans;
|
|
689
634
|
}
|
|
690
635
|
|
|
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
636
|
/**
|
|
713
637
|
* Time Complexity: O(n)
|
|
714
638
|
* Space Complexity: O(log n).
|
|
715
639
|
*
|
|
716
|
-
* The
|
|
717
|
-
*
|
|
718
|
-
* @param {
|
|
719
|
-
*
|
|
720
|
-
*
|
|
721
|
-
* @param {
|
|
722
|
-
*
|
|
723
|
-
*
|
|
724
|
-
*
|
|
725
|
-
*
|
|
726
|
-
*
|
|
727
|
-
* of
|
|
728
|
-
*
|
|
640
|
+
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
641
|
+
* predicate.
|
|
642
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
643
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `getNode` function can accept a key,
|
|
644
|
+
* node, entry, raw data, or a predicate function.
|
|
645
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
646
|
+
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
647
|
+
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
648
|
+
* is typically the root node of the binary tree.
|
|
649
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is
|
|
650
|
+
* used to specify the type of iteration to be performed when searching for a node. It has a default
|
|
651
|
+
* value of `this.iterationType`, which means it will use the iteration type defined in the current
|
|
652
|
+
* context if no specific value is provided
|
|
653
|
+
* @returns The `getNode` function is returning the first node that matches the specified criteria,
|
|
654
|
+
* or `null` if no matching node is found.
|
|
729
655
|
*/
|
|
730
|
-
getNode
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
656
|
+
getNode(
|
|
657
|
+
keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
658
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
734
659
|
iterationType: IterationType = this.iterationType
|
|
735
660
|
): OptBTNOrNull<NODE> {
|
|
736
|
-
return this.getNodes(
|
|
661
|
+
return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType)[0] ?? null;
|
|
737
662
|
}
|
|
738
663
|
|
|
739
664
|
/**
|
|
740
665
|
* Time Complexity: O(n)
|
|
741
666
|
* Space Complexity: O(log n)
|
|
742
667
|
*
|
|
743
|
-
* The function `getNodeByKey`
|
|
744
|
-
* @param {K} key - The key parameter is the value
|
|
745
|
-
*
|
|
746
|
-
* @param {IterationType}
|
|
747
|
-
*
|
|
748
|
-
*
|
|
749
|
-
* @returns
|
|
668
|
+
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
669
|
+
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
670
|
+
* structure.
|
|
671
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
672
|
+
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
673
|
+
* is an optional parameter with a default value of `this.iterationType`.
|
|
674
|
+
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
675
|
+
* (`OptBTNOrNull<NODE>`).
|
|
750
676
|
*/
|
|
751
677
|
getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptBTNOrNull<NODE> {
|
|
752
|
-
return this.getNode(key, this.
|
|
678
|
+
return this.getNode(key, this._root, iterationType);
|
|
753
679
|
}
|
|
754
680
|
|
|
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
681
|
/**
|
|
777
682
|
* Time Complexity: O(n)
|
|
778
683
|
* Space Complexity: O(log n)
|
|
779
684
|
*
|
|
780
|
-
*
|
|
781
|
-
*
|
|
782
|
-
* @param {
|
|
783
|
-
*
|
|
784
|
-
*
|
|
785
|
-
* @param {
|
|
786
|
-
*
|
|
787
|
-
*
|
|
788
|
-
*
|
|
789
|
-
*
|
|
790
|
-
*
|
|
791
|
-
*
|
|
792
|
-
*
|
|
793
|
-
*
|
|
794
|
-
*
|
|
795
|
-
*
|
|
685
|
+
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
686
|
+
* node, entry, raw data, or predicate in a data structure.
|
|
687
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
688
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `get` method can accept one of the
|
|
689
|
+
* following types:
|
|
690
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `get`
|
|
691
|
+
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
692
|
+
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
693
|
+
* tree (`this._root`).
|
|
694
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `get` method is used
|
|
695
|
+
* to specify the type of iteration to be performed when searching for a key in the binary tree. It
|
|
696
|
+
* is an optional parameter with a default value of `this.iterationType`, which means it will use the
|
|
697
|
+
* iteration type defined in the
|
|
698
|
+
* @returns The `get` method is returning the value associated with the specified key, node, entry,
|
|
699
|
+
* raw data, or predicate in the binary tree map. If the specified key or node is found in the tree,
|
|
700
|
+
* the method returns the corresponding value. If the key or node is not found, it returns
|
|
701
|
+
* `undefined`.
|
|
796
702
|
*/
|
|
797
|
-
override get
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
703
|
+
override get(
|
|
704
|
+
keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
705
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
801
706
|
iterationType: IterationType = this.iterationType
|
|
802
707
|
): V | undefined {
|
|
803
|
-
return this.getNode(
|
|
708
|
+
return this.getNode(keyOrNodeOrEntryOrRawOrPredicate, beginRoot, iterationType)?.value;
|
|
804
709
|
}
|
|
805
710
|
|
|
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
711
|
/**
|
|
828
712
|
* Time Complexity: O(n)
|
|
829
713
|
* Space Complexity: O(log n)
|
|
830
714
|
*
|
|
831
|
-
* The `has` function checks if a
|
|
832
|
-
*
|
|
833
|
-
* @param {
|
|
834
|
-
*
|
|
835
|
-
*
|
|
836
|
-
*
|
|
837
|
-
*
|
|
838
|
-
*
|
|
839
|
-
* `
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
*
|
|
843
|
-
* @
|
|
844
|
-
*
|
|
845
|
-
*
|
|
715
|
+
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
716
|
+
* exists in the data structure.
|
|
717
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
718
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `override has` method can accept one of
|
|
719
|
+
* the following types:
|
|
720
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
721
|
+
* `override` method is used to specify the starting point for the search operation within the data
|
|
722
|
+
* structure. It defaults to `this._root` if not provided explicitly.
|
|
723
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
724
|
+
* is used to specify the type of iteration to be performed. It has a default value of
|
|
725
|
+
* `this.iterationType`, which means it will use the iteration type defined in the current context if
|
|
726
|
+
* no value is provided when calling the method.
|
|
727
|
+
* @returns The `override has` method is returning a boolean value. It checks if there are any nodes
|
|
728
|
+
* that match the provided key, node, entry, raw data, or predicate in the tree structure. If there
|
|
729
|
+
* are matching nodes, it returns `true`, indicating that the tree contains the specified element.
|
|
730
|
+
* Otherwise, it returns `false`.
|
|
846
731
|
*/
|
|
847
|
-
override has
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
732
|
+
override has(
|
|
733
|
+
keyOrNodeOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
734
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
851
735
|
iterationType: IterationType = this.iterationType
|
|
852
736
|
): boolean {
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
737
|
+
return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType).length > 0;
|
|
856
738
|
}
|
|
857
739
|
|
|
858
740
|
/**
|
|
859
741
|
* Time Complexity: O(1)
|
|
860
742
|
* Space Complexity: O(1)
|
|
861
743
|
*
|
|
862
|
-
*
|
|
744
|
+
* The `clear` function resets the root node and size of a data structure to empty.
|
|
863
745
|
*/
|
|
864
746
|
clear() {
|
|
865
747
|
this._setRoot(undefined);
|
|
@@ -870,26 +752,31 @@ export class BinaryTree<
|
|
|
870
752
|
* Time Complexity: O(1)
|
|
871
753
|
* Space Complexity: O(1)
|
|
872
754
|
*
|
|
873
|
-
*
|
|
874
|
-
*
|
|
755
|
+
* The `isEmpty` function in TypeScript checks if a data structure has no elements and returns a
|
|
756
|
+
* boolean value.
|
|
757
|
+
* @returns The `isEmpty()` method is returning a boolean value, specifically `true` if the `_size`
|
|
758
|
+
* property is equal to 0, indicating that the data structure is empty, and `false` otherwise.
|
|
875
759
|
*/
|
|
876
760
|
isEmpty(): boolean {
|
|
877
|
-
return this.
|
|
761
|
+
return this._size === 0;
|
|
878
762
|
}
|
|
879
763
|
|
|
880
764
|
/**
|
|
881
765
|
* Time Complexity: O(n)
|
|
882
766
|
* Space Complexity: O(log n)
|
|
883
767
|
*
|
|
884
|
-
* The function checks if a binary tree is perfectly balanced by comparing
|
|
885
|
-
* height
|
|
886
|
-
* @param {
|
|
887
|
-
*
|
|
888
|
-
*
|
|
889
|
-
*
|
|
890
|
-
*
|
|
768
|
+
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
769
|
+
* its height.
|
|
770
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
771
|
+
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
772
|
+
* binary tree or a specific node from which the balance check should begin.
|
|
773
|
+
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
774
|
+
* the tree starting from the `beginRoot` node is perfectly balanced or not. The return value is
|
|
775
|
+
* determined by comparing the minimum height of the tree with the height of the tree. If the minimum
|
|
776
|
+
* height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
|
|
777
|
+
* balanced and
|
|
891
778
|
*/
|
|
892
|
-
isPerfectlyBalanced(beginRoot:
|
|
779
|
+
isPerfectlyBalanced(beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root): boolean {
|
|
893
780
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
894
781
|
}
|
|
895
782
|
|
|
@@ -897,18 +784,22 @@ export class BinaryTree<
|
|
|
897
784
|
* Time Complexity: O(n)
|
|
898
785
|
* Space Complexity: O(1)
|
|
899
786
|
*
|
|
900
|
-
* The function `isBST` checks if a binary search tree is valid
|
|
901
|
-
*
|
|
902
|
-
*
|
|
903
|
-
*
|
|
904
|
-
*
|
|
905
|
-
*
|
|
906
|
-
*
|
|
907
|
-
*
|
|
908
|
-
*
|
|
787
|
+
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
788
|
+
* or iterative methods.
|
|
789
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `isBST`
|
|
790
|
+
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
791
|
+
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
792
|
+
* provided, the function will default to
|
|
793
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `isBST` function
|
|
794
|
+
* determines whether the function should use a recursive approach or an iterative approach to check
|
|
795
|
+
* if the binary search tree (BST) is valid.
|
|
796
|
+
* @returns The `isBST` method is returning a boolean value, which indicates whether the binary
|
|
797
|
+
* search tree (BST) represented by the given root node is a valid BST or not. The method checks if
|
|
798
|
+
* the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
|
|
799
|
+
* less than the node's key, and all nodes in its right subtree have keys greater than the node's
|
|
909
800
|
*/
|
|
910
801
|
isBST(
|
|
911
|
-
beginRoot:
|
|
802
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
912
803
|
iterationType: IterationType = this.iterationType
|
|
913
804
|
): boolean {
|
|
914
805
|
// TODO there is a bug
|
|
@@ -955,19 +846,21 @@ export class BinaryTree<
|
|
|
955
846
|
* Time Complexity: O(n)
|
|
956
847
|
* Space Complexity: O(1)
|
|
957
848
|
*
|
|
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
|
|
849
|
+
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
850
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} dist - The `dist` parameter in the `getDepth`
|
|
851
|
+
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
852
|
+
* It is the target node for which you want to calculate the depth from the `beginRoot` node.
|
|
853
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
854
|
+
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
855
|
+
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
856
|
+
* for `beginRoot` is set to the root of the binary
|
|
857
|
+
* @returns The `getDepth` method returns the depth of a given node `dist` relative to the
|
|
858
|
+
* `beginRoot` node in a binary tree. If the `dist` node is not found in the path to the `beginRoot`
|
|
859
|
+
* node, it returns the depth of the `dist` node from the root of the tree.
|
|
967
860
|
*/
|
|
968
861
|
getDepth(
|
|
969
|
-
dist:
|
|
970
|
-
beginRoot:
|
|
862
|
+
dist: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
863
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root
|
|
971
864
|
): number {
|
|
972
865
|
let distEnsured = this.ensureNode(dist);
|
|
973
866
|
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
@@ -987,16 +880,20 @@ export class BinaryTree<
|
|
|
987
880
|
* Space Complexity: O(1)
|
|
988
881
|
*
|
|
989
882
|
* 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
|
-
*
|
|
883
|
+
* or iterative approach in TypeScript.
|
|
884
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
885
|
+
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
886
|
+
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
887
|
+
* binary tree data structure.
|
|
888
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
889
|
+
* of iteration to be performed while calculating the height of the binary tree. It can have two
|
|
890
|
+
* possible values:
|
|
891
|
+
* @returns The `getHeight` method returns the height of the binary tree starting from the specified
|
|
892
|
+
* root node. The height is calculated based on the maximum depth of the tree, considering either a
|
|
893
|
+
* recursive approach or an iterative approach depending on the `iterationType` parameter.
|
|
997
894
|
*/
|
|
998
895
|
getHeight(
|
|
999
|
-
beginRoot:
|
|
896
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1000
897
|
iterationType: IterationType = this.iterationType
|
|
1001
898
|
): number {
|
|
1002
899
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1033,19 +930,21 @@ export class BinaryTree<
|
|
|
1033
930
|
* Space Complexity: O(log n)
|
|
1034
931
|
*
|
|
1035
932
|
* 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
|
-
*
|
|
933
|
+
* recursive or iterative approach in TypeScript.
|
|
934
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
935
|
+
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
936
|
+
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
937
|
+
* tree. If not provided, the default value is the root
|
|
938
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getMinHeight` method
|
|
939
|
+
* specifies the type of iteration to use when calculating the minimum height of a binary tree. It
|
|
940
|
+
* can have two possible values:
|
|
941
|
+
* @returns The `getMinHeight` method returns the minimum height of the binary tree starting from the
|
|
942
|
+
* specified root node. The height is calculated based on the shortest path from the root node to a
|
|
943
|
+
* leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
|
|
944
|
+
* a stack) based on the `iterationType` parameter.
|
|
1046
945
|
*/
|
|
1047
946
|
getMinHeight(
|
|
1048
|
-
beginRoot:
|
|
947
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1049
948
|
iterationType: IterationType = this.iterationType
|
|
1050
949
|
): number {
|
|
1051
950
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1076,8 +975,8 @@ export class BinaryTree<
|
|
|
1076
975
|
if (!this.isRealNode(node.right) || last === node.right) {
|
|
1077
976
|
node = stack.pop();
|
|
1078
977
|
if (this.isRealNode(node)) {
|
|
1079
|
-
const leftMinHeight = this.isRealNode(node.left) ?
|
|
1080
|
-
const rightMinHeight = this.isRealNode(node.right) ?
|
|
978
|
+
const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left)! : -1;
|
|
979
|
+
const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right)! : -1;
|
|
1081
980
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
1082
981
|
last = node;
|
|
1083
982
|
node = null;
|
|
@@ -1086,7 +985,7 @@ export class BinaryTree<
|
|
|
1086
985
|
}
|
|
1087
986
|
}
|
|
1088
987
|
|
|
1089
|
-
return depths.get(beginRoot)
|
|
988
|
+
return depths.get(beginRoot)!;
|
|
1090
989
|
}
|
|
1091
990
|
}
|
|
1092
991
|
|
|
@@ -1094,27 +993,38 @@ export class BinaryTree<
|
|
|
1094
993
|
* Time Complexity: O(log n)
|
|
1095
994
|
* Space Complexity: O(log n)
|
|
1096
995
|
*
|
|
1097
|
-
* The function `getPathToRoot`
|
|
1098
|
-
*
|
|
1099
|
-
* @param {
|
|
1100
|
-
*
|
|
1101
|
-
*
|
|
1102
|
-
*
|
|
1103
|
-
*
|
|
1104
|
-
*
|
|
996
|
+
* The function `getPathToRoot` in TypeScript retrieves the path from a given node to the root of a
|
|
997
|
+
* tree structure, applying a specified callback function along the way.
|
|
998
|
+
* @param {C} callback - The `callback` parameter is a function that is used to process each node in
|
|
999
|
+
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
1000
|
+
* a value based on that node. The return type of the callback function is determined by the generic
|
|
1001
|
+
* type `C
|
|
1002
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginNode - The `beginNode` parameter in the
|
|
1003
|
+
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
1004
|
+
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
1005
|
+
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
1006
|
+
* not. If `isReverse` is set to `true`, the path will be reversed before being returned. If `is
|
|
1007
|
+
* @returns The function `getPathToRoot` returns an array of the return values of the callback
|
|
1008
|
+
* function `callback` applied to each node in the path from the `beginNode` to the root node. The
|
|
1009
|
+
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
1010
|
+
* parameter.
|
|
1105
1011
|
*/
|
|
1106
|
-
getPathToRoot
|
|
1107
|
-
|
|
1012
|
+
getPathToRoot<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1013
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1014
|
+
beginNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1015
|
+
isReverse = true
|
|
1016
|
+
): ReturnType<C>[] {
|
|
1017
|
+
const result: ReturnType<C>[] = [];
|
|
1108
1018
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
1109
1019
|
|
|
1110
1020
|
if (!beginNodeEnsured) return result;
|
|
1111
1021
|
|
|
1112
1022
|
while (beginNodeEnsured.parent) {
|
|
1113
1023
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
1114
|
-
result.push(beginNodeEnsured);
|
|
1024
|
+
result.push(callback(beginNodeEnsured));
|
|
1115
1025
|
beginNodeEnsured = beginNodeEnsured.parent;
|
|
1116
1026
|
}
|
|
1117
|
-
result.push(beginNodeEnsured);
|
|
1027
|
+
result.push(callback(beginNodeEnsured));
|
|
1118
1028
|
return isReverse ? result.reverse() : result;
|
|
1119
1029
|
}
|
|
1120
1030
|
|
|
@@ -1125,23 +1035,23 @@ export class BinaryTree<
|
|
|
1125
1035
|
* The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
|
|
1126
1036
|
* tail-recursive iteration.
|
|
1127
1037
|
* @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 {
|
|
1038
|
+
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
1039
|
+
* value of `_DEFAULT_BTN_CALLBACK` if not specified.
|
|
1040
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1131
1041
|
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
1132
|
-
* tree. It can be either a
|
|
1133
|
-
* the
|
|
1042
|
+
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1043
|
+
* starting point is provided, the function will default
|
|
1134
1044
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getLeftMost` function
|
|
1135
1045
|
* specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
|
|
1136
1046
|
* possible values:
|
|
1137
1047
|
* @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
|
|
1048
|
+
* leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` node is
|
|
1049
|
+
* `NIL`, it returns the result of the callback function applied to `undefined`. If the `beginRoot`
|
|
1050
|
+
* node is not a real node, it returns the result of the callback
|
|
1141
1051
|
*/
|
|
1142
1052
|
getLeftMost<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1143
|
-
callback: C = this.
|
|
1144
|
-
beginRoot:
|
|
1053
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1054
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1145
1055
|
iterationType: IterationType = this.iterationType
|
|
1146
1056
|
): ReturnType<C> {
|
|
1147
1057
|
if (this.isNIL(beginRoot)) return callback(undefined);
|
|
@@ -1174,24 +1084,24 @@ export class BinaryTree<
|
|
|
1174
1084
|
* The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
|
|
1175
1085
|
* or iterative traversal methods.
|
|
1176
1086
|
* @param {C} callback - The `callback` parameter is a function that will be called with the result
|
|
1177
|
-
* of the
|
|
1178
|
-
*
|
|
1179
|
-
*
|
|
1087
|
+
* of finding the rightmost node in a binary tree. It is of type `BTNCallback<OptBTNOrNull<NODE>>`,
|
|
1088
|
+
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
1089
|
+
* as
|
|
1090
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1180
1091
|
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
1181
|
-
* tree. It can be either a
|
|
1182
|
-
*
|
|
1092
|
+
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1093
|
+
* starting point is provided, the function will default
|
|
1183
1094
|
* @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
|
|
1095
|
+
* function specifies the type of iteration to be used when traversing the binary tree nodes. It can
|
|
1096
|
+
* have two possible values:
|
|
1097
|
+
* @returns The `getRightMost` function returns the result of the callback function `C`, which is
|
|
1098
|
+
* passed as a parameter to the function. The callback function is called with the rightmost node in
|
|
1099
|
+
* the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
|
|
1100
|
+
* other).
|
|
1191
1101
|
*/
|
|
1192
1102
|
getRightMost<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1193
|
-
callback: C = this.
|
|
1194
|
-
beginRoot:
|
|
1103
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1104
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1195
1105
|
iterationType: IterationType = this.iterationType
|
|
1196
1106
|
): ReturnType<C> {
|
|
1197
1107
|
if (this.isNIL(beginRoot)) return callback(undefined);
|
|
@@ -1221,10 +1131,14 @@ export class BinaryTree<
|
|
|
1221
1131
|
* Time Complexity: O(log n)
|
|
1222
1132
|
* Space Complexity: O(1)
|
|
1223
1133
|
*
|
|
1224
|
-
* The function returns the predecessor node of a given node in a
|
|
1225
|
-
*
|
|
1226
|
-
*
|
|
1227
|
-
*
|
|
1134
|
+
* The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
|
|
1135
|
+
* binary tree.
|
|
1136
|
+
* @param {NODE} node - The `getPredecessor` function you provided seems to be attempting to find the
|
|
1137
|
+
* predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
|
|
1138
|
+
* while loop condition that might cause an infinite loop.
|
|
1139
|
+
* @returns The `getPredecessor` function returns the predecessor node of the input `NODE` parameter.
|
|
1140
|
+
* If the left child of the input node exists, it traverses to the rightmost node of the left subtree
|
|
1141
|
+
* to find the predecessor. If the left child does not exist, it returns the input node itself.
|
|
1228
1142
|
*/
|
|
1229
1143
|
getPredecessor(node: NODE): NODE {
|
|
1230
1144
|
if (this.isRealNode(node.left)) {
|
|
@@ -1244,10 +1158,14 @@ export class BinaryTree<
|
|
|
1244
1158
|
* Time Complexity: O(log n)
|
|
1245
1159
|
* Space Complexity: O(1)
|
|
1246
1160
|
*
|
|
1247
|
-
* The function `getSuccessor` returns the next node in
|
|
1248
|
-
*
|
|
1249
|
-
* @
|
|
1250
|
-
*
|
|
1161
|
+
* The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
|
|
1162
|
+
* binary tree.
|
|
1163
|
+
* @param {K | NODE | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
|
|
1164
|
+
* type `K`, `NODE`, or `null`.
|
|
1165
|
+
* @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
|
|
1166
|
+
* a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
|
|
1167
|
+
* have a right child, the function traverses up the parent nodes until it finds a node that is not
|
|
1168
|
+
* the right child of its parent, and returns that node
|
|
1251
1169
|
*/
|
|
1252
1170
|
getSuccessor(x?: K | NODE | null): OptBTNOrNull<NODE> {
|
|
1253
1171
|
x = this.ensureNode(x);
|
|
@@ -1268,14 +1186,14 @@ export class BinaryTree<
|
|
|
1268
1186
|
dfs<C extends BTNCallback<NODE>>(
|
|
1269
1187
|
callback?: C,
|
|
1270
1188
|
pattern?: DFSOrderPattern,
|
|
1271
|
-
beginRoot?:
|
|
1189
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1272
1190
|
iterationType?: IterationType
|
|
1273
1191
|
): ReturnType<C>[];
|
|
1274
1192
|
|
|
1275
1193
|
dfs<C extends BTNCallback<NODE | null>>(
|
|
1276
1194
|
callback?: C,
|
|
1277
1195
|
pattern?: DFSOrderPattern,
|
|
1278
|
-
beginRoot?:
|
|
1196
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1279
1197
|
iterationType?: IterationType,
|
|
1280
1198
|
includeNull?: boolean
|
|
1281
1199
|
): ReturnType<C>[];
|
|
@@ -1284,29 +1202,32 @@ export class BinaryTree<
|
|
|
1284
1202
|
* Time complexity: O(n)
|
|
1285
1203
|
* Space complexity: O(n)
|
|
1286
1204
|
*
|
|
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
|
-
*
|
|
1205
|
+
* The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
|
|
1206
|
+
* specified parameters.
|
|
1207
|
+
* @param {C} callback - The `callback` parameter is a generic type `C` that extends the
|
|
1208
|
+
* `BTNCallback` interface with a type parameter of `OptBTNOrNull<NODE>`. It has a default value of
|
|
1209
|
+
* `this._DEFAULT_BTN_CALLBACK as C`.
|
|
1210
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
|
|
1211
|
+
* order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
|
|
1212
|
+
* possible values for the `pattern` parameter are:
|
|
1213
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `dfs`
|
|
1214
|
+
* method is used to specify the starting point for the Depth-First Search traversal. It can be
|
|
1215
|
+
* either a `BTNKeyOrNodeOrEntry` object representing a key, node, or entry in the binary tree map,
|
|
1216
|
+
* or it can be a
|
|
1217
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` method specifies
|
|
1218
|
+
* the type of iteration to be performed during the depth-first search traversal. It is used to
|
|
1219
|
+
* determine the order in which nodes are visited during the traversal.
|
|
1220
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `dfs` method is a boolean flag
|
|
1221
|
+
* that determines whether null values should be included in the traversal or not. If `includeNull`
|
|
1222
|
+
* is set to `true`, then null values will be included in the traversal process. If it is set to
|
|
1223
|
+
* `false`,
|
|
1224
|
+
* @returns The `dfs` method is returning an array of the return type specified by the generic type
|
|
1225
|
+
* parameter `C`. The return type is determined by the callback function provided to the method.
|
|
1305
1226
|
*/
|
|
1306
1227
|
dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1307
|
-
callback: C = this.
|
|
1228
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1308
1229
|
pattern: DFSOrderPattern = 'IN',
|
|
1309
|
-
beginRoot:
|
|
1230
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1310
1231
|
iterationType: IterationType = this.iterationType,
|
|
1311
1232
|
includeNull = false
|
|
1312
1233
|
): ReturnType<C>[] {
|
|
@@ -1317,14 +1238,14 @@ export class BinaryTree<
|
|
|
1317
1238
|
|
|
1318
1239
|
bfs<C extends BTNCallback<NODE>>(
|
|
1319
1240
|
callback?: C,
|
|
1320
|
-
beginRoot?:
|
|
1241
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1321
1242
|
iterationType?: IterationType,
|
|
1322
1243
|
includeNull?: false
|
|
1323
1244
|
): ReturnType<C>[];
|
|
1324
1245
|
|
|
1325
1246
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1326
1247
|
callback?: C,
|
|
1327
|
-
beginRoot?:
|
|
1248
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1328
1249
|
iterationType?: IterationType,
|
|
1329
1250
|
includeNull?: true
|
|
1330
1251
|
): ReturnType<C>[];
|
|
@@ -1333,27 +1254,28 @@ export class BinaryTree<
|
|
|
1333
1254
|
* Time complexity: O(n)
|
|
1334
1255
|
* Space complexity: O(n)
|
|
1335
1256
|
*
|
|
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
|
-
* `
|
|
1257
|
+
* The `bfs` function performs a breadth-first search traversal on a binary tree or binary search
|
|
1258
|
+
* tree, executing a specified callback function on each node visited.
|
|
1259
|
+
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
1260
|
+
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
1261
|
+
* that extends the `BTNCallback` type, which takes a parameter of type `NODE` or `null`.
|
|
1262
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `bfs`
|
|
1263
|
+
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
1264
|
+
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
1265
|
+
* default value is the root node of the binary
|
|
1266
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `bfs` function
|
|
1267
|
+
* determines the type of iteration to be performed on the binary tree nodes. It can have two
|
|
1268
|
+
* possible values:
|
|
1269
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `bfs` function determines whether
|
|
1270
|
+
* to include `null` values in the breadth-first search traversal of a binary tree. If `includeNull`
|
|
1271
|
+
* is set to `true`, the traversal will include `null` values for nodes that do not have children
|
|
1272
|
+
* (left
|
|
1273
|
+
* @returns The `bfs` function returns an array of values that are the result of applying the
|
|
1274
|
+
* provided callback function to each node in the binary tree in a breadth-first search manner.
|
|
1353
1275
|
*/
|
|
1354
1276
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1355
|
-
callback: C = this.
|
|
1356
|
-
beginRoot:
|
|
1277
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1278
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1357
1279
|
iterationType: IterationType = this.iterationType,
|
|
1358
1280
|
includeNull = false
|
|
1359
1281
|
): ReturnType<C>[] {
|
|
@@ -1409,39 +1331,37 @@ export class BinaryTree<
|
|
|
1409
1331
|
* Time complexity: O(n)
|
|
1410
1332
|
* Space complexity: O(n)
|
|
1411
1333
|
*
|
|
1412
|
-
* The `leaves` function in TypeScript
|
|
1413
|
-
*
|
|
1334
|
+
* The `leaves` function in TypeScript returns an array of values from leaf nodes in a binary tree
|
|
1335
|
+
* structure based on a specified callback and iteration type.
|
|
1414
1336
|
* @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`
|
|
1337
|
+
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
1338
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `leaves`
|
|
1418
1339
|
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
1419
|
-
* tree. It
|
|
1420
|
-
*
|
|
1340
|
+
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
1341
|
+
* explicitly provided, the default value
|
|
1421
1342
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
|
|
1422
1343
|
* specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
|
|
1423
1344
|
* can have two possible values:
|
|
1424
1345
|
* @returns The `leaves` method returns an array of values that are the result of applying the
|
|
1425
|
-
* provided callback function to
|
|
1346
|
+
* provided callback function to each leaf node in the binary tree.
|
|
1426
1347
|
*/
|
|
1427
1348
|
leaves<C extends BTNCallback<NODE | null>>(
|
|
1428
|
-
callback: C = this.
|
|
1429
|
-
beginRoot:
|
|
1349
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1350
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1430
1351
|
iterationType: IterationType = this.iterationType
|
|
1431
1352
|
): ReturnType<C>[] {
|
|
1432
1353
|
beginRoot = this.ensureNode(beginRoot);
|
|
1433
1354
|
const leaves: ReturnType<BTNCallback<NODE>>[] = [];
|
|
1434
|
-
if (!this.isRealNode(beginRoot))
|
|
1435
|
-
|
|
1436
|
-
}
|
|
1355
|
+
if (!this.isRealNode(beginRoot)) return [];
|
|
1356
|
+
|
|
1437
1357
|
if (iterationType === 'RECURSIVE') {
|
|
1438
1358
|
const dfs = (cur: NODE) => {
|
|
1439
1359
|
if (this.isLeaf(cur)) {
|
|
1440
1360
|
leaves.push(callback(cur));
|
|
1441
1361
|
}
|
|
1442
1362
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
1443
|
-
this.isRealNode(cur.left)
|
|
1444
|
-
this.isRealNode(cur.right)
|
|
1363
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
1364
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
1445
1365
|
};
|
|
1446
1366
|
|
|
1447
1367
|
dfs(beginRoot);
|
|
@@ -1453,8 +1373,8 @@ export class BinaryTree<
|
|
|
1453
1373
|
if (this.isLeaf(cur)) {
|
|
1454
1374
|
leaves.push(callback(cur));
|
|
1455
1375
|
}
|
|
1456
|
-
this.isRealNode(cur.left)
|
|
1457
|
-
this.isRealNode(cur.right)
|
|
1376
|
+
if (this.isRealNode(cur.left)) queue.push(cur.left);
|
|
1377
|
+
if (this.isRealNode(cur.right)) queue.push(cur.right);
|
|
1458
1378
|
}
|
|
1459
1379
|
}
|
|
1460
1380
|
}
|
|
@@ -1464,14 +1384,14 @@ export class BinaryTree<
|
|
|
1464
1384
|
|
|
1465
1385
|
listLevels<C extends BTNCallback<NODE>>(
|
|
1466
1386
|
callback?: C,
|
|
1467
|
-
beginRoot?:
|
|
1387
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1468
1388
|
iterationType?: IterationType,
|
|
1469
1389
|
includeNull?: false
|
|
1470
1390
|
): ReturnType<C>[][];
|
|
1471
1391
|
|
|
1472
1392
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1473
1393
|
callback?: C,
|
|
1474
|
-
beginRoot?:
|
|
1394
|
+
beginRoot?: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
1475
1395
|
iterationType?: IterationType,
|
|
1476
1396
|
includeNull?: true
|
|
1477
1397
|
): ReturnType<C>[][];
|
|
@@ -1480,26 +1400,29 @@ export class BinaryTree<
|
|
|
1480
1400
|
* Time complexity: O(n)
|
|
1481
1401
|
* Space complexity: O(n)
|
|
1482
1402
|
*
|
|
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
|
-
*
|
|
1403
|
+
* The `listLevels` function in TypeScript generates a list of nodes at each level of a binary tree,
|
|
1404
|
+
* using either recursive or iterative traversal based on the specified iteration type.
|
|
1405
|
+
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
1406
|
+
* the binary tree during the traversal. It is used to process each node and determine what
|
|
1407
|
+
* information to include in the output for each level of the tree.
|
|
1408
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1409
|
+
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
1410
|
+
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
1411
|
+
* root of the binary tree.
|
|
1412
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `listLevels` function
|
|
1413
|
+
* determines the type of iteration to be performed on the binary tree nodes. It can have two
|
|
1414
|
+
* possible values:
|
|
1415
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `listLevels` method determines
|
|
1416
|
+
* whether or not to include null nodes in the traversal of the binary tree. If `includeNull` is set
|
|
1417
|
+
* to `true`, the traversal will include null nodes in the levels of the tree. If set to `false`,
|
|
1418
|
+
* null
|
|
1419
|
+
* @returns The `listLevels` method returns an array of arrays, where each inner array represents a
|
|
1420
|
+
* level in a binary tree. Each inner array contains the return value of the provided callback
|
|
1421
|
+
* function applied to the nodes at that level.
|
|
1499
1422
|
*/
|
|
1500
1423
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1501
|
-
callback: C = this.
|
|
1502
|
-
beginRoot:
|
|
1424
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1425
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1503
1426
|
iterationType: IterationType = this.iterationType,
|
|
1504
1427
|
includeNull = false
|
|
1505
1428
|
): ReturnType<C>[][] {
|
|
@@ -1548,28 +1471,29 @@ export class BinaryTree<
|
|
|
1548
1471
|
* Time complexity: O(n)
|
|
1549
1472
|
* Space complexity: O(n)
|
|
1550
1473
|
*
|
|
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
|
|
1474
|
+
* The `morris` function in TypeScript performs a Depth-First Search traversal on a binary tree using
|
|
1475
|
+
* Morris Traversal algorithm with different order patterns.
|
|
1476
|
+
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
1477
|
+
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
1478
|
+
* `BTNCallback<NODE>` type. The default value for `callback` is `this._DEFAULT
|
|
1479
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
1480
|
+
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
1481
|
+
* values for the `pattern` parameter are:
|
|
1482
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `morris`
|
|
1483
|
+
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
1484
|
+
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
1485
|
+
* key, a node, an entry, or a reference
|
|
1486
|
+
* @returns The `morris` function is returning an array of values that are the result of applying the
|
|
1487
|
+
* provided callback function to each node in the binary tree in the specified order pattern (IN,
|
|
1488
|
+
* PRE, or POST).
|
|
1565
1489
|
*/
|
|
1566
1490
|
morris<C extends BTNCallback<NODE>>(
|
|
1567
|
-
callback: C = this.
|
|
1491
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1568
1492
|
pattern: DFSOrderPattern = 'IN',
|
|
1569
|
-
beginRoot:
|
|
1493
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root
|
|
1570
1494
|
): ReturnType<C>[] {
|
|
1571
1495
|
beginRoot = this.ensureNode(beginRoot);
|
|
1572
|
-
if (beginRoot
|
|
1496
|
+
if (!beginRoot) return [];
|
|
1573
1497
|
const ans: ReturnType<BTNCallback<NODE>>[] = [];
|
|
1574
1498
|
|
|
1575
1499
|
let cur: OptBTNOrNull<NODE> = beginRoot;
|
|
@@ -1653,8 +1577,12 @@ export class BinaryTree<
|
|
|
1653
1577
|
* Time complexity: O(n)
|
|
1654
1578
|
* Space complexity: O(n)
|
|
1655
1579
|
*
|
|
1656
|
-
* The `clone` function creates a deep copy of a tree
|
|
1657
|
-
*
|
|
1580
|
+
* The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first
|
|
1581
|
+
* search.
|
|
1582
|
+
* @returns The `clone()` method is returning a cloned copy of the tree with the same structure and
|
|
1583
|
+
* values as the original tree. The method creates a new tree, iterates over the nodes of the
|
|
1584
|
+
* original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
|
|
1585
|
+
* the original tree is null, a null node is added to the cloned tree. If a node
|
|
1658
1586
|
*/
|
|
1659
1587
|
clone(): TREE {
|
|
1660
1588
|
const cloned = this.createTree();
|
|
@@ -1663,7 +1591,7 @@ export class BinaryTree<
|
|
|
1663
1591
|
if (node === null) cloned.add(null);
|
|
1664
1592
|
else cloned.add([node.key, node.value]);
|
|
1665
1593
|
},
|
|
1666
|
-
this.
|
|
1594
|
+
this._root,
|
|
1667
1595
|
this.iterationType,
|
|
1668
1596
|
true
|
|
1669
1597
|
);
|
|
@@ -1674,16 +1602,17 @@ export class BinaryTree<
|
|
|
1674
1602
|
* Time Complexity: O(n)
|
|
1675
1603
|
* Space Complexity: O(n)
|
|
1676
1604
|
*
|
|
1677
|
-
* The `filter` function
|
|
1678
|
-
*
|
|
1679
|
-
*
|
|
1680
|
-
*
|
|
1681
|
-
* of the
|
|
1682
|
-
* @param {any} [thisArg] - The `thisArg` parameter
|
|
1683
|
-
*
|
|
1684
|
-
*
|
|
1685
|
-
*
|
|
1686
|
-
*
|
|
1605
|
+
* The `filter` function iterates over key-value pairs in a tree data structure and creates a new
|
|
1606
|
+
* tree with elements that satisfy a given predicate.
|
|
1607
|
+
* @param predicate - The `predicate` parameter in the `filter` method is a function that will be
|
|
1608
|
+
* called with four arguments: the `value` of the current entry, the `key` of the current entry, the
|
|
1609
|
+
* `index` of the current entry in the iteration, and the reference to the tree itself (`
|
|
1610
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `filter` method allows you to specify the
|
|
1611
|
+
* value of `this` that should be used when executing the `predicate` function. This is useful when
|
|
1612
|
+
* the `predicate` function relies on the context of a specific object or value. By providing a
|
|
1613
|
+
* `thisArg
|
|
1614
|
+
* @returns The `filter` method is returning a new tree that contains entries that pass the provided
|
|
1615
|
+
* predicate function.
|
|
1687
1616
|
*/
|
|
1688
1617
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
|
|
1689
1618
|
const newTree = this.createTree();
|
|
@@ -1700,16 +1629,16 @@ export class BinaryTree<
|
|
|
1700
1629
|
* Time Complexity: O(n)
|
|
1701
1630
|
* Space Complexity: O(n)
|
|
1702
1631
|
*
|
|
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
|
-
*
|
|
1632
|
+
* The `map` function iterates over key-value pairs in a tree data structure, applies a callback
|
|
1633
|
+
* function to each value, and returns a new tree with the updated values.
|
|
1634
|
+
* @param callback - The `callback` parameter in the `map` method is a function that will be called
|
|
1635
|
+
* on each entry in the tree. It takes four arguments:
|
|
1636
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is an optional parameter
|
|
1637
|
+
* that specifies the value to be passed as `this` when executing the callback function. If provided,
|
|
1638
|
+
* the `thisArg` value will be used as the `this` value within the callback function. If `thisArg
|
|
1639
|
+
* @returns The `map` method is returning a new tree with the entries modified by the provided
|
|
1640
|
+
* callback function. Each entry in the original tree is passed to the callback function, and the
|
|
1641
|
+
* result of the callback function is added to the new tree.
|
|
1713
1642
|
*/
|
|
1714
1643
|
map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
|
|
1715
1644
|
const newTree = this.createTree();
|
|
@@ -1734,31 +1663,32 @@ export class BinaryTree<
|
|
|
1734
1663
|
* Time Complexity: O(n)
|
|
1735
1664
|
* Space Complexity: O(n)
|
|
1736
1665
|
*
|
|
1737
|
-
* The `
|
|
1738
|
-
*
|
|
1739
|
-
*
|
|
1740
|
-
*
|
|
1741
|
-
*
|
|
1742
|
-
*
|
|
1743
|
-
*
|
|
1744
|
-
*
|
|
1745
|
-
*
|
|
1666
|
+
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
1667
|
+
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
1668
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1669
|
+
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
1670
|
+
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
1671
|
+
* the default is set to the root
|
|
1672
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter in the `toVisual` method is an
|
|
1673
|
+
* object that contains the following properties:
|
|
1674
|
+
* @returns The `override toVisual` method returns a string that represents the visual display of the
|
|
1675
|
+
* binary tree based on the provided options for showing undefined, null, and Red-Black NIL nodes.
|
|
1676
|
+
* The method constructs the visual representation by calling the `_displayAux` method and appending
|
|
1677
|
+
* the lines to the output string. The final output string contains the visual representation of the
|
|
1678
|
+
* binary tree with the specified options.
|
|
1746
1679
|
*/
|
|
1747
|
-
override
|
|
1748
|
-
|
|
1680
|
+
override toVisual(
|
|
1681
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1682
|
+
options?: BinaryTreePrintOptions
|
|
1683
|
+
): string {
|
|
1684
|
+
const opts = { isShowUndefined: false, isShowNull: true, isShowRedBlackNIL: false, ...options };
|
|
1749
1685
|
beginRoot = this.ensureNode(beginRoot);
|
|
1750
1686
|
let output = '';
|
|
1751
1687
|
if (!beginRoot) return output;
|
|
1752
1688
|
|
|
1753
|
-
if (opts.isShowUndefined)
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
if (opts.isShowNull)
|
|
1757
|
-
output += `N for null
|
|
1758
|
-
`;
|
|
1759
|
-
if (opts.isShowRedBlackNIL)
|
|
1760
|
-
output += `S for Sentinel Node(NIL)
|
|
1761
|
-
`;
|
|
1689
|
+
if (opts.isShowUndefined) output += `U for undefined\n`;
|
|
1690
|
+
if (opts.isShowNull) output += `N for null\n`;
|
|
1691
|
+
if (opts.isShowRedBlackNIL) output += `S for Sentinel Node(NIL)\n`;
|
|
1762
1692
|
|
|
1763
1693
|
const display = (root: OptBTNOrNull<NODE>): void => {
|
|
1764
1694
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
@@ -1773,68 +1703,71 @@ export class BinaryTree<
|
|
|
1773
1703
|
return output;
|
|
1774
1704
|
}
|
|
1775
1705
|
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1706
|
+
/**
|
|
1707
|
+
* Time Complexity: O(n)
|
|
1708
|
+
* Space Complexity: O(n)
|
|
1709
|
+
*
|
|
1710
|
+
* The function `print` in TypeScript overrides the default print behavior to log a visual
|
|
1711
|
+
* representation of the binary tree to the console.
|
|
1712
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is used to specify the
|
|
1713
|
+
* printing options for the binary tree. It is an optional parameter that allows you to customize how
|
|
1714
|
+
* the binary tree is printed, such as choosing between different traversal orders or formatting
|
|
1715
|
+
* options.
|
|
1716
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1717
|
+
* `override print` method is used to specify the starting point for printing the binary tree. It can
|
|
1718
|
+
* be either a key, a node, an entry, or the root of the tree. If no specific starting point is
|
|
1719
|
+
* provided, the default value is set to
|
|
1720
|
+
*/
|
|
1721
|
+
override print(options?: BinaryTreePrintOptions, beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root) {
|
|
1722
|
+
console.log(this.toVisual(beginRoot, options));
|
|
1723
|
+
}
|
|
1790
1724
|
|
|
1791
1725
|
/**
|
|
1792
1726
|
* Time complexity: O(n)
|
|
1793
1727
|
* Space complexity: O(n)
|
|
1794
1728
|
*
|
|
1795
|
-
* The
|
|
1729
|
+
* The `_dfs` function performs a depth-first search traversal on a binary tree structure based on
|
|
1796
1730
|
* 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.
|
|
1731
|
+
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
1732
|
+
* called on each node visited during the depth-first search traversal. It is of type `C`, which
|
|
1733
|
+
* extends `BTNCallback<OptBTNOrNull<NODE>>`. The default value for this parameter is `this._DEFAULT
|
|
1801
1734
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
1802
|
-
* order in which the Depth-First Search
|
|
1803
|
-
*
|
|
1804
|
-
* @param {
|
|
1735
|
+
* order in which the nodes are visited during the Depth-First Search traversal. It can have one of
|
|
1736
|
+
* the following values:
|
|
1737
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `_dfs`
|
|
1805
1738
|
* 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
|
-
*
|
|
1739
|
+
* tree. It can be provided as either a `BTNKeyOrNodeOrEntry` object or a reference to the root node
|
|
1740
|
+
* of the tree. If no specific
|
|
1808
1741
|
* @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:
|
|
1742
|
+
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
|
|
1743
|
+
* binary tree. It can have two possible values:
|
|
1811
1744
|
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag
|
|
1812
1745
|
* that determines whether null nodes should be included in the depth-first search traversal. If
|
|
1813
|
-
* `includeNull` is set to `true`,
|
|
1814
|
-
*
|
|
1746
|
+
* `includeNull` is set to `true`, null nodes will be considered during the traversal process. If it
|
|
1747
|
+
* is set to `false`,
|
|
1815
1748
|
* @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input
|
|
1816
1749
|
* and returns a boolean value. It is used to determine whether the left child of a node should be
|
|
1817
1750
|
* visited during the depth-first search traversal. By default, it checks if the node is truthy (not
|
|
1818
1751
|
* null or undefined
|
|
1819
|
-
* @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as
|
|
1820
|
-
*
|
|
1752
|
+
* @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as an
|
|
1753
|
+
* argument and returns a boolean value. It is used to determine whether the right child of a node
|
|
1821
1754
|
* should be visited during the depth-first search traversal. The default implementation checks if
|
|
1822
|
-
* the node is truthy before visiting the right child
|
|
1755
|
+
* the node is truthy before visiting the right child
|
|
1823
1756
|
* @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
|
|
1757
|
+
* argument and returns a boolean value. It is used to determine whether the root node should be
|
|
1825
1758
|
* visited during the depth-first search traversal based on certain conditions. The default
|
|
1826
1759
|
* 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`
|
|
1760
|
+
* @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as an
|
|
1761
|
+
* argument and returns a boolean value indicating whether the node should be processed during the
|
|
1762
|
+
* depth-first search traversal. The default implementation checks if the node is a real node or null
|
|
1763
|
+
* based on the `includeNull` flag. If `
|
|
1764
|
+
* @returns The function `_dfs` returns an array of the return type of the callback function provided
|
|
1832
1765
|
* as input.
|
|
1833
1766
|
*/
|
|
1834
1767
|
protected _dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1835
|
-
callback: C = this.
|
|
1768
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
1836
1769
|
pattern: DFSOrderPattern = 'IN',
|
|
1837
|
-
beginRoot:
|
|
1770
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
1838
1771
|
iterationType: IterationType = this.iterationType,
|
|
1839
1772
|
includeNull = false,
|
|
1840
1773
|
shouldVisitLeft: (node: OptBTNOrNull<NODE>) => boolean = node => !!node,
|
|
@@ -1928,13 +1861,18 @@ export class BinaryTree<
|
|
|
1928
1861
|
* Time Complexity: O(1)
|
|
1929
1862
|
* Space Complexity: O(1)
|
|
1930
1863
|
*
|
|
1931
|
-
* The function `_getIterator`
|
|
1932
|
-
*
|
|
1933
|
-
* @param node - The `node` parameter
|
|
1934
|
-
* initially set to the root node of the
|
|
1935
|
-
*
|
|
1864
|
+
* The function `_getIterator` returns an iterable iterator for a binary tree data structure, either
|
|
1865
|
+
* using an iterative approach or a recursive approach based on the specified iteration type.
|
|
1866
|
+
* @param node - The `node` parameter in the `_getIterator` method represents the current node being
|
|
1867
|
+
* processed during iteration. It is initially set to the root node of the data structure (or the
|
|
1868
|
+
* node passed as an argument), and then it is traversed through the data structure based on the
|
|
1869
|
+
* iteration type specified (`ITER
|
|
1870
|
+
* @returns The `_getIterator` method returns an IterableIterator containing key-value pairs of nodes
|
|
1871
|
+
* in a binary tree structure. The method uses an iterative approach to traverse the tree based on
|
|
1872
|
+
* the `iterationType` property. If the `iterationType` is set to 'ITERATIVE', the method uses a
|
|
1873
|
+
* stack to perform an in-order traversal of the tree. If the `iterationType` is not 'ITERATIVE
|
|
1936
1874
|
*/
|
|
1937
|
-
protected *_getIterator(node = this.
|
|
1875
|
+
protected *_getIterator(node = this._root): IterableIterator<[K, V | undefined]> {
|
|
1938
1876
|
if (!node) return;
|
|
1939
1877
|
|
|
1940
1878
|
if (this.iterationType === 'ITERATIVE') {
|
|
@@ -1969,18 +1907,16 @@ export class BinaryTree<
|
|
|
1969
1907
|
* Time Complexity: O(n)
|
|
1970
1908
|
* Space Complexity: O(n)
|
|
1971
1909
|
*
|
|
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
|
|
1910
|
+
* The function `_displayAux` in TypeScript is responsible for generating the display layout of nodes
|
|
1911
|
+
* in a binary tree based on specified options.
|
|
1912
|
+
* @param node - The `node` parameter in the `_displayAux` function represents a node in a binary
|
|
1913
|
+
* tree. It can be either a valid node containing a key or a special type of node like null,
|
|
1914
|
+
* undefined, or a Red-Black tree NIL node. The function checks the type of the node and its
|
|
1915
|
+
* @param {BinaryTreePrintOptions} options - The `options` parameter in the `_displayAux` function
|
|
1916
|
+
* contains the following properties:
|
|
1917
|
+
* @returns The `_displayAux` function returns a `NodeDisplayLayout`, which is an array containing
|
|
1918
|
+
* information about how to display a node in a binary tree. The `NodeDisplayLayout` consists of four
|
|
1919
|
+
* elements:
|
|
1984
1920
|
*/
|
|
1985
1921
|
protected _displayAux(node: OptBTNOrNull<NODE>, options: BinaryTreePrintOptions): NodeDisplayLayout {
|
|
1986
1922
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
@@ -2050,24 +1986,26 @@ export class BinaryTree<
|
|
|
2050
1986
|
}
|
|
2051
1987
|
}
|
|
2052
1988
|
|
|
2053
|
-
protected
|
|
1989
|
+
protected _DEFAULT_BTN_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
|
|
2054
1990
|
|
|
2055
1991
|
/**
|
|
2056
1992
|
* Time Complexity: O(1)
|
|
2057
1993
|
* Space Complexity: O(1)
|
|
2058
1994
|
*
|
|
2059
|
-
* The function
|
|
2060
|
-
* @param {
|
|
2061
|
-
*
|
|
2062
|
-
*
|
|
2063
|
-
* @param {
|
|
2064
|
-
* the properties will be swapped with
|
|
2065
|
-
*
|
|
2066
|
-
*
|
|
1995
|
+
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
1996
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} srcNode - The `srcNode` parameter in the
|
|
1997
|
+
* `_swapProperties` method can be either a BTNKeyOrNodeOrEntry object containing key and value
|
|
1998
|
+
* properties, or it can be of type R.
|
|
1999
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} destNode - The `destNode` parameter in the
|
|
2000
|
+
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
2001
|
+
* the `srcNode`. It can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. The method ensures that
|
|
2002
|
+
* both `srcNode
|
|
2003
|
+
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
2004
|
+
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
2067
2005
|
*/
|
|
2068
2006
|
protected _swapProperties(
|
|
2069
|
-
srcNode:
|
|
2070
|
-
destNode:
|
|
2007
|
+
srcNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
2008
|
+
destNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R
|
|
2071
2009
|
): NODE | undefined {
|
|
2072
2010
|
srcNode = this.ensureNode(srcNode);
|
|
2073
2011
|
destNode = this.ensureNode(destNode);
|
|
@@ -2093,13 +2031,15 @@ export class BinaryTree<
|
|
|
2093
2031
|
* Time Complexity: O(1)
|
|
2094
2032
|
* Space Complexity: O(1)
|
|
2095
2033
|
*
|
|
2096
|
-
* The function replaces
|
|
2097
|
-
*
|
|
2098
|
-
*
|
|
2099
|
-
*
|
|
2100
|
-
*
|
|
2101
|
-
*
|
|
2102
|
-
*
|
|
2034
|
+
* The _replaceNode function replaces an old node with a new node in a binary tree structure.
|
|
2035
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that you want to replace in a
|
|
2036
|
+
* tree data structure.
|
|
2037
|
+
* @param {NODE} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
|
|
2038
|
+
* that will replace the `oldNode` in a tree data structure. This function is responsible for
|
|
2039
|
+
* updating the parent, left child, right child, and root (if necessary) references when replacing a
|
|
2040
|
+
* node in the tree.
|
|
2041
|
+
* @returns The method `_replaceNode` is returning the `newNode` that was passed as a parameter after
|
|
2042
|
+
* replacing the `oldNode` with it in the binary tree structure.
|
|
2103
2043
|
*/
|
|
2104
2044
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
2105
2045
|
if (oldNode.parent) {
|
|
@@ -2112,8 +2052,8 @@ export class BinaryTree<
|
|
|
2112
2052
|
newNode.left = oldNode.left;
|
|
2113
2053
|
newNode.right = oldNode.right;
|
|
2114
2054
|
newNode.parent = oldNode.parent;
|
|
2115
|
-
if (this.
|
|
2116
|
-
this.
|
|
2055
|
+
if (this._root === oldNode) {
|
|
2056
|
+
this._setRoot(newNode);
|
|
2117
2057
|
}
|
|
2118
2058
|
|
|
2119
2059
|
return newNode;
|
|
@@ -2123,10 +2063,10 @@ export class BinaryTree<
|
|
|
2123
2063
|
* Time Complexity: O(1)
|
|
2124
2064
|
* Space Complexity: O(1)
|
|
2125
2065
|
*
|
|
2126
|
-
* The function sets the root
|
|
2127
|
-
*
|
|
2128
|
-
* @param
|
|
2129
|
-
*
|
|
2066
|
+
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
2067
|
+
* of the previous root node.
|
|
2068
|
+
* @param v - The parameter `v` in the `_setRoot` method is of type `OptBTNOrNull<NODE>`, which means
|
|
2069
|
+
* it can either be an optional `NODE` type or `null`.
|
|
2130
2070
|
*/
|
|
2131
2071
|
protected _setRoot(v: OptBTNOrNull<NODE>) {
|
|
2132
2072
|
if (v) {
|
|
@@ -2139,23 +2079,51 @@ export class BinaryTree<
|
|
|
2139
2079
|
* Time Complexity: O(1)
|
|
2140
2080
|
* Space Complexity: O(1)
|
|
2141
2081
|
*
|
|
2142
|
-
* The function `
|
|
2143
|
-
*
|
|
2144
|
-
*
|
|
2145
|
-
* the
|
|
2146
|
-
*
|
|
2147
|
-
*
|
|
2148
|
-
*
|
|
2149
|
-
* @returns the callback parameter.
|
|
2082
|
+
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
2083
|
+
* predicate function for a binary tree node.
|
|
2084
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrEntryOrRawOrPredicate - The
|
|
2085
|
+
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
2086
|
+
* parameter `keyOrEntryOrRawOrPredicate` is transformed into a valid predicate function that can be
|
|
2087
|
+
* used for filtering nodes in a binary tree.
|
|
2088
|
+
* @returns A BTNPredicate<NODE> function is being returned.
|
|
2150
2089
|
*/
|
|
2151
|
-
protected
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2090
|
+
protected _ensurePredicate(
|
|
2091
|
+
keyOrEntryOrRawOrPredicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>
|
|
2092
|
+
): BTNPredicate<NODE> {
|
|
2093
|
+
if (keyOrEntryOrRawOrPredicate === null || keyOrEntryOrRawOrPredicate === undefined)
|
|
2094
|
+
return (node: NODE) => (node ? false : false);
|
|
2095
|
+
|
|
2096
|
+
if (this._isPredicated(keyOrEntryOrRawOrPredicate)) return keyOrEntryOrRawOrPredicate;
|
|
2097
|
+
|
|
2098
|
+
if (this.isRealNode(keyOrEntryOrRawOrPredicate)) return (node: NODE) => node === keyOrEntryOrRawOrPredicate;
|
|
2099
|
+
|
|
2100
|
+
if (this.isEntry(keyOrEntryOrRawOrPredicate)) {
|
|
2101
|
+
const [key] = keyOrEntryOrRawOrPredicate;
|
|
2102
|
+
return (node: NODE) => node.key === key;
|
|
2157
2103
|
}
|
|
2158
2104
|
|
|
2159
|
-
return
|
|
2105
|
+
if (this.isKey(keyOrEntryOrRawOrPredicate)) return (node: NODE) => node.key === keyOrEntryOrRawOrPredicate;
|
|
2106
|
+
|
|
2107
|
+
if (this._toEntryFn) {
|
|
2108
|
+
const [key] = this._toEntryFn(keyOrEntryOrRawOrPredicate);
|
|
2109
|
+
return (node: NODE) => node.key === key;
|
|
2110
|
+
}
|
|
2111
|
+
return (node: NODE) => node.key === keyOrEntryOrRawOrPredicate;
|
|
2112
|
+
}
|
|
2113
|
+
|
|
2114
|
+
/**
|
|
2115
|
+
* Time Complexity: O(1)
|
|
2116
|
+
* Space Complexity: O(1)
|
|
2117
|
+
*
|
|
2118
|
+
* The function `_isPredicated` checks if a given parameter is a function.
|
|
2119
|
+
* @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
|
|
2120
|
+
* of value. In this context, the function `_isPredicated` is checking if `p` is a function that
|
|
2121
|
+
* satisfies the type `BTNPredicate<NODE>`.
|
|
2122
|
+
* @returns The function is checking if the input `p` is a function and returning a boolean value
|
|
2123
|
+
* based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
|
|
2124
|
+
* predicate function for a binary tree node. If `p` is not a function, it will return `false`.
|
|
2125
|
+
*/
|
|
2126
|
+
protected _isPredicated(p: any): p is BTNPredicate<NODE> {
|
|
2127
|
+
return typeof p === 'function';
|
|
2160
2128
|
}
|
|
2161
2129
|
}
|