linked-list-typed 1.52.5 → 1.52.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants/index.d.ts +4 -0
- package/dist/constants/index.js +8 -0
- package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-element-base.js +10 -1
- package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/data-structures/binary-tree/binary-tree.js +669 -598
- package/dist/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/data-structures/binary-tree/bst.js +115 -113
- package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.js +7 -4
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +4 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/heap/heap.js +3 -3
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.js +4 -2
- package/dist/data-structures/queue/deque.d.ts +3 -3
- package/dist/data-structures/queue/deque.js +29 -29
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/stack/stack.d.ts +2 -2
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/package.json +2 -2
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +698 -726
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +2 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
|
@@ -2,76 +2,44 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* data-structure-typed
|
|
4
4
|
*
|
|
5
|
-
* @author
|
|
6
|
-
* @copyright Copyright (c) 2022
|
|
5
|
+
* @author Pablo Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
7
|
* @license MIT License
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.BinaryTree = exports.BinaryTreeNode = void 0;
|
|
11
|
-
const types_1 = require("../../types");
|
|
12
11
|
const utils_1 = require("../../utils");
|
|
13
12
|
const queue_1 = require("../queue");
|
|
14
13
|
const base_1 = require("../base");
|
|
14
|
+
const constants_1 = require("../../constants");
|
|
15
15
|
/**
|
|
16
16
|
* Represents a node in a binary tree.
|
|
17
17
|
* @template V - The type of data stored in the node.
|
|
18
18
|
* @template NODE - The type of the family relationship in the binary tree.
|
|
19
19
|
*/
|
|
20
20
|
class BinaryTreeNode {
|
|
21
|
-
/**
|
|
22
|
-
* The constructor function initializes an object with a key and an optional value.
|
|
23
|
-
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
|
|
24
|
-
* constructor. It is used to set the key property of the object being created.
|
|
25
|
-
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
26
|
-
* value associated with the key in the constructor.
|
|
27
|
-
*/
|
|
28
21
|
constructor(key, value) {
|
|
29
22
|
this.key = key;
|
|
30
23
|
this.value = value;
|
|
31
24
|
}
|
|
32
|
-
/**
|
|
33
|
-
* The function returns the value of the `_left` property, which can be of type `NODE`, `null`, or
|
|
34
|
-
* `undefined`.
|
|
35
|
-
* @returns The left node of the current node is being returned. It can be either a NODE object,
|
|
36
|
-
* null, or undefined.
|
|
37
|
-
*/
|
|
38
25
|
get left() {
|
|
39
26
|
return this._left;
|
|
40
27
|
}
|
|
41
|
-
/**
|
|
42
|
-
* The function sets the left child of a node and updates its parent reference.
|
|
43
|
-
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
44
|
-
* `undefined`.
|
|
45
|
-
*/
|
|
46
28
|
set left(v) {
|
|
47
29
|
if (v) {
|
|
48
30
|
v.parent = this;
|
|
49
31
|
}
|
|
50
32
|
this._left = v;
|
|
51
33
|
}
|
|
52
|
-
/**
|
|
53
|
-
* The function returns the right node of a binary tree or null if it doesn't exist.
|
|
54
|
-
* @returns The method is returning the value of the `_right` property, which can be a `NODE` object,
|
|
55
|
-
* `null`, or `undefined`.
|
|
56
|
-
*/
|
|
57
34
|
get right() {
|
|
58
35
|
return this._right;
|
|
59
36
|
}
|
|
60
|
-
/**
|
|
61
|
-
* The function sets the right child of a node and updates its parent.
|
|
62
|
-
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
63
|
-
* `undefined`.
|
|
64
|
-
*/
|
|
65
37
|
set right(v) {
|
|
66
38
|
if (v) {
|
|
67
39
|
v.parent = this;
|
|
68
40
|
}
|
|
69
41
|
this._right = v;
|
|
70
42
|
}
|
|
71
|
-
/**
|
|
72
|
-
* Get the position of the node within its family.
|
|
73
|
-
* @returns {FamilyPosition} - The family position of the node.
|
|
74
|
-
*/
|
|
75
43
|
get familyPosition() {
|
|
76
44
|
const that = this;
|
|
77
45
|
if (!this.parent) {
|
|
@@ -96,20 +64,20 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
96
64
|
*/
|
|
97
65
|
class BinaryTree extends base_1.IterableEntryBase {
|
|
98
66
|
/**
|
|
99
|
-
* The constructor
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
* `
|
|
105
|
-
*
|
|
67
|
+
* The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or
|
|
68
|
+
* raw data if provided.
|
|
69
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter in the constructor
|
|
70
|
+
* is an iterable that can contain elements of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It is
|
|
71
|
+
* initialized with an empty array `[]` by default.
|
|
72
|
+
* @param [options] - The `options` parameter in the constructor is an object that can contain the
|
|
73
|
+
* following properties:
|
|
106
74
|
*/
|
|
107
|
-
constructor(
|
|
75
|
+
constructor(keysOrNodesOrEntriesOrRaws = [], options) {
|
|
108
76
|
super();
|
|
109
77
|
this.iterationType = 'ITERATIVE';
|
|
110
78
|
this._size = 0;
|
|
111
79
|
this._NIL = new BinaryTreeNode(NaN);
|
|
112
|
-
this.
|
|
80
|
+
this._DEFAULT_BTN_CALLBACK = (node) => (node ? node.key : undefined);
|
|
113
81
|
if (options) {
|
|
114
82
|
const { iterationType, toEntryFn } = options;
|
|
115
83
|
if (iterationType)
|
|
@@ -119,77 +87,68 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
119
87
|
else if (toEntryFn)
|
|
120
88
|
throw TypeError('toEntryFn must be a function type');
|
|
121
89
|
}
|
|
122
|
-
if (
|
|
123
|
-
this.addMany(
|
|
90
|
+
if (keysOrNodesOrEntriesOrRaws)
|
|
91
|
+
this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
124
92
|
}
|
|
125
|
-
/**
|
|
126
|
-
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
127
|
-
* @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
|
|
128
|
-
* `null`, or `undefined`.
|
|
129
|
-
*/
|
|
130
93
|
get root() {
|
|
131
94
|
return this._root;
|
|
132
95
|
}
|
|
133
|
-
/**
|
|
134
|
-
* The function returns the size of an object.
|
|
135
|
-
* @returns The size of the object, which is a number.
|
|
136
|
-
*/
|
|
137
96
|
get size() {
|
|
138
97
|
return this._size;
|
|
139
98
|
}
|
|
140
|
-
/**
|
|
141
|
-
* The function returns the value of the _NIL property.
|
|
142
|
-
* @returns The method is returning the value of the `_NIL` property.
|
|
143
|
-
*/
|
|
144
99
|
get NIL() {
|
|
145
100
|
return this._NIL;
|
|
146
101
|
}
|
|
147
|
-
/**
|
|
148
|
-
* The function returns the value of the _toEntryFn property.
|
|
149
|
-
* @returns The function being returned is `this._toEntryFn`.
|
|
150
|
-
*/
|
|
151
102
|
get toEntryFn() {
|
|
152
103
|
return this._toEntryFn;
|
|
153
104
|
}
|
|
154
105
|
/**
|
|
155
|
-
*
|
|
156
|
-
* @param {K} key - The key
|
|
157
|
-
* @param {V} value - The value
|
|
158
|
-
*
|
|
106
|
+
* The function creates a new binary tree node with a specified key and optional value.
|
|
107
|
+
* @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
|
|
108
|
+
* @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
|
|
109
|
+
* not required to be provided when calling the function. If a `value` is provided, it should be of
|
|
110
|
+
* type `V`, which is the type of the value associated with the node.
|
|
111
|
+
* @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
|
|
112
|
+
* as NODE.
|
|
159
113
|
*/
|
|
160
114
|
createNode(key, value) {
|
|
161
115
|
return new BinaryTreeNode(key, value);
|
|
162
116
|
}
|
|
163
117
|
/**
|
|
164
|
-
* The function creates a binary tree with the
|
|
165
|
-
* @param [options] - The `options` parameter
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
118
|
+
* The function creates a binary tree with the specified options.
|
|
119
|
+
* @param [options] - The `options` parameter in the `createTree` function is an optional parameter
|
|
120
|
+
* that allows you to provide partial configuration options for creating a binary tree. It is of type
|
|
121
|
+
* `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
|
|
122
|
+
* of properties
|
|
123
|
+
* @returns A new instance of a binary tree with the specified options is being returned.
|
|
169
124
|
*/
|
|
170
125
|
createTree(options) {
|
|
171
|
-
return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
126
|
+
return new BinaryTree([], Object.assign({ iterationType: this.iterationType, toEntryFn: this._toEntryFn }, options));
|
|
172
127
|
}
|
|
173
128
|
/**
|
|
174
|
-
* The function `keyValueOrEntryOrRawElementToNode` converts
|
|
175
|
-
*
|
|
176
|
-
* @param {
|
|
177
|
-
* `
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
129
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts various input types into a node object
|
|
130
|
+
* or returns null.
|
|
131
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
|
|
132
|
+
* `keyValueOrEntryOrRawElementToNode` function takes in a parameter `keyOrNodeOrEntryOrRaw`, which
|
|
133
|
+
* can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
134
|
+
* node, an entry
|
|
135
|
+
* @param {V} [value] - The `value` parameter in the `keyValueOrEntryOrRawElementToNode` function is
|
|
136
|
+
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
137
|
+
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
138
|
+
* @returns The `keyValueOrEntryOrRawElementToNode` function returns an optional node
|
|
139
|
+
* (`OptBTNOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
140
|
+
* input parameter (`keyOrNodeOrEntryOrRaw`) and processes it accordingly to return a node or null
|
|
141
|
+
* value.
|
|
183
142
|
*/
|
|
184
|
-
keyValueOrEntryOrRawElementToNode(
|
|
185
|
-
if (
|
|
143
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value) {
|
|
144
|
+
if (keyOrNodeOrEntryOrRaw === undefined)
|
|
186
145
|
return;
|
|
187
|
-
if (
|
|
146
|
+
if (keyOrNodeOrEntryOrRaw === null)
|
|
188
147
|
return null;
|
|
189
|
-
if (this.isNode(
|
|
190
|
-
return
|
|
191
|
-
if (this.isEntry(
|
|
192
|
-
const [key, entryValue] =
|
|
148
|
+
if (this.isNode(keyOrNodeOrEntryOrRaw))
|
|
149
|
+
return keyOrNodeOrEntryOrRaw;
|
|
150
|
+
if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
151
|
+
const [key, entryValue] = keyOrNodeOrEntryOrRaw;
|
|
193
152
|
if (key === undefined)
|
|
194
153
|
return;
|
|
195
154
|
else if (key === null)
|
|
@@ -197,122 +156,139 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
197
156
|
if (this.isKey(key))
|
|
198
157
|
return this.createNode(key, value !== null && value !== void 0 ? value : entryValue);
|
|
199
158
|
}
|
|
200
|
-
if (this.
|
|
201
|
-
const [key, entryValue] = this.
|
|
159
|
+
if (this._toEntryFn) {
|
|
160
|
+
const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw);
|
|
202
161
|
if (this.isKey(key))
|
|
203
162
|
return this.createNode(key, value !== null && value !== void 0 ? value : entryValue);
|
|
204
163
|
else
|
|
205
164
|
return;
|
|
206
165
|
}
|
|
207
|
-
if (this.isKey(
|
|
208
|
-
return this.createNode(
|
|
166
|
+
if (this.isKey(keyOrNodeOrEntryOrRaw))
|
|
167
|
+
return this.createNode(keyOrNodeOrEntryOrRaw, value);
|
|
209
168
|
return;
|
|
210
169
|
}
|
|
211
170
|
/**
|
|
212
171
|
* Time Complexity: O(n)
|
|
213
172
|
* Space Complexity: O(log n)
|
|
214
173
|
*
|
|
215
|
-
* The `ensureNode`
|
|
216
|
-
*
|
|
217
|
-
* @param {
|
|
218
|
-
* `
|
|
219
|
-
* a raw
|
|
220
|
-
* @param {IterationType}
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
* @returns The
|
|
174
|
+
* The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
|
|
175
|
+
* value and returns the corresponding node or null.
|
|
176
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
177
|
+
* parameter in the `ensureNode` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It
|
|
178
|
+
* is used to determine whether the input is a key, node, entry, or raw data. The
|
|
179
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
|
|
180
|
+
* is used to specify the type of iteration to be performed. It has a default value of
|
|
181
|
+
* `this.iterationType` if not explicitly provided.
|
|
182
|
+
* @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
|
|
183
|
+
* conditions specified in the code snippet.
|
|
224
184
|
*/
|
|
225
|
-
ensureNode(
|
|
226
|
-
if (
|
|
185
|
+
ensureNode(keyOrNodeOrEntryOrRaw, iterationType = this.iterationType) {
|
|
186
|
+
if (keyOrNodeOrEntryOrRaw === null)
|
|
227
187
|
return null;
|
|
228
|
-
if (
|
|
188
|
+
if (keyOrNodeOrEntryOrRaw === undefined)
|
|
229
189
|
return;
|
|
230
|
-
if (
|
|
190
|
+
if (keyOrNodeOrEntryOrRaw === this._NIL)
|
|
231
191
|
return;
|
|
232
|
-
if (this.isNode(
|
|
233
|
-
return
|
|
234
|
-
if (this.
|
|
235
|
-
const
|
|
236
|
-
if (this.isKey(key))
|
|
237
|
-
return this.getNodeByKey(key);
|
|
238
|
-
}
|
|
239
|
-
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
240
|
-
const key = keyOrNodeOrEntryOrRawElement[0];
|
|
192
|
+
if (this.isNode(keyOrNodeOrEntryOrRaw))
|
|
193
|
+
return keyOrNodeOrEntryOrRaw;
|
|
194
|
+
if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
195
|
+
const key = keyOrNodeOrEntryOrRaw[0];
|
|
241
196
|
if (key === null)
|
|
242
197
|
return null;
|
|
243
198
|
if (key === undefined)
|
|
244
199
|
return;
|
|
245
200
|
return this.getNodeByKey(key, iterationType);
|
|
246
201
|
}
|
|
247
|
-
if (this.
|
|
248
|
-
|
|
202
|
+
if (this._toEntryFn) {
|
|
203
|
+
const [key] = this._toEntryFn(keyOrNodeOrEntryOrRaw);
|
|
204
|
+
if (this.isKey(key))
|
|
205
|
+
return this.getNodeByKey(key);
|
|
206
|
+
}
|
|
207
|
+
if (this.isKey(keyOrNodeOrEntryOrRaw))
|
|
208
|
+
return this.getNodeByKey(keyOrNodeOrEntryOrRaw, iterationType);
|
|
249
209
|
return;
|
|
250
210
|
}
|
|
251
211
|
/**
|
|
252
|
-
* The function checks if the input is an instance of
|
|
253
|
-
* @param {
|
|
254
|
-
* `
|
|
255
|
-
*
|
|
256
|
-
*
|
|
212
|
+
* The function isNode checks if the input is an instance of BinaryTreeNode.
|
|
213
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
214
|
+
* `keyOrNodeOrEntryOrRaw` can be either a key, a node, an entry, or raw data. The function is
|
|
215
|
+
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
|
|
216
|
+
* accordingly.
|
|
217
|
+
* @returns The function `isNode` is checking if the input `keyOrNodeOrEntryOrRaw` is an instance of
|
|
218
|
+
* `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
|
|
219
|
+
* it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
|
|
220
|
+
* is not a node.
|
|
257
221
|
*/
|
|
258
|
-
isNode(
|
|
259
|
-
return
|
|
222
|
+
isNode(keyOrNodeOrEntryOrRaw) {
|
|
223
|
+
return keyOrNodeOrEntryOrRaw instanceof BinaryTreeNode;
|
|
260
224
|
}
|
|
261
225
|
/**
|
|
262
|
-
* The function checks if a given
|
|
263
|
-
* @param {
|
|
264
|
-
* `BTNKeyOrNodeOrEntry<K, V, NODE
|
|
265
|
-
*
|
|
226
|
+
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
227
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
228
|
+
* parameter in the `isRealNode` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`.
|
|
229
|
+
* The function checks if the input parameter is a `NODE` type by verifying if it is not equal
|
|
230
|
+
* @returns The function `isRealNode` is checking if the input `keyOrNodeOrEntryOrRaw` is a valid
|
|
231
|
+
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
|
|
232
|
+
* values, it then calls the `isNode` method to further determine if the input is a node. The
|
|
233
|
+
* function will return a boolean value indicating whether the
|
|
266
234
|
*/
|
|
267
|
-
isRealNode(
|
|
268
|
-
if (
|
|
235
|
+
isRealNode(keyOrNodeOrEntryOrRaw) {
|
|
236
|
+
if (keyOrNodeOrEntryOrRaw === this._NIL || keyOrNodeOrEntryOrRaw === null || keyOrNodeOrEntryOrRaw === undefined)
|
|
269
237
|
return false;
|
|
270
|
-
return this.isNode(
|
|
238
|
+
return this.isNode(keyOrNodeOrEntryOrRaw);
|
|
271
239
|
}
|
|
272
240
|
/**
|
|
273
|
-
* The function checks if a given
|
|
274
|
-
* @param {
|
|
275
|
-
* `BTNKeyOrNodeOrEntry<K,
|
|
276
|
-
*
|
|
241
|
+
* The function checks if a given input is a valid node or null.
|
|
242
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
243
|
+
* `keyOrNodeOrEntryOrRaw` in the `isRealNodeOrNull` function can be of type `BTNKeyOrNodeOrEntry<K,
|
|
244
|
+
* V, NODE>` or `R`. It is a union type that can either be a key, a node, an entry, or
|
|
245
|
+
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
|
|
246
|
+
* `keyOrNodeOrEntryOrRaw` is either `null` or a real node, and returns `true` if it is a node or
|
|
247
|
+
* `null`, and `false` otherwise.
|
|
277
248
|
*/
|
|
278
|
-
isRealNodeOrNull(
|
|
279
|
-
return this.isRealNode(
|
|
249
|
+
isRealNodeOrNull(keyOrNodeOrEntryOrRaw) {
|
|
250
|
+
return keyOrNodeOrEntryOrRaw === null || this.isRealNode(keyOrNodeOrEntryOrRaw);
|
|
280
251
|
}
|
|
281
252
|
/**
|
|
282
|
-
* The function checks if a given node is equal to the
|
|
283
|
-
* @param {
|
|
284
|
-
*
|
|
285
|
-
* @returns
|
|
253
|
+
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
|
|
254
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - BTNKeyOrNodeOrEntry<K, V,
|
|
255
|
+
* NODE> | R
|
|
256
|
+
* @returns The function is checking if the `keyOrNodeOrEntryOrRaw` parameter is equal to the `_NIL`
|
|
257
|
+
* property of the current object and returning a boolean value based on that comparison.
|
|
286
258
|
*/
|
|
287
|
-
isNIL(
|
|
288
|
-
return
|
|
259
|
+
isNIL(keyOrNodeOrEntryOrRaw) {
|
|
260
|
+
return keyOrNodeOrEntryOrRaw === this._NIL;
|
|
289
261
|
}
|
|
290
262
|
/**
|
|
291
|
-
* The function
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
* returns `
|
|
263
|
+
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
264
|
+
* tree.
|
|
265
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
266
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It represents a
|
|
267
|
+
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
|
|
268
|
+
* provided
|
|
269
|
+
* @returns The function `isLeaf` returns a boolean value indicating whether the input
|
|
270
|
+
* `keyOrNodeOrEntryOrRaw` is a leaf node in a binary tree.
|
|
298
271
|
*/
|
|
299
|
-
isLeaf(
|
|
300
|
-
|
|
301
|
-
if (
|
|
272
|
+
isLeaf(keyOrNodeOrEntryOrRaw) {
|
|
273
|
+
keyOrNodeOrEntryOrRaw = this.ensureNode(keyOrNodeOrEntryOrRaw);
|
|
274
|
+
if (keyOrNodeOrEntryOrRaw === undefined)
|
|
302
275
|
return false;
|
|
303
|
-
if (
|
|
276
|
+
if (keyOrNodeOrEntryOrRaw === null)
|
|
304
277
|
return true;
|
|
305
|
-
return !this.isRealNode(
|
|
278
|
+
return !this.isRealNode(keyOrNodeOrEntryOrRaw.left) && !this.isRealNode(keyOrNodeOrEntryOrRaw.right);
|
|
306
279
|
}
|
|
307
280
|
/**
|
|
308
|
-
* The function checks if the input is
|
|
309
|
-
*
|
|
310
|
-
* @param {
|
|
311
|
-
* `
|
|
312
|
-
*
|
|
281
|
+
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
|
|
282
|
+
* with a length of 2.
|
|
283
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
284
|
+
* parameter in the `isEntry` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or type `R`.
|
|
285
|
+
* The function checks if the provided `keyOrNodeOrEntryOrRaw` is of type `BTN
|
|
286
|
+
* @returns The `isEntry` function is checking if the `keyOrNodeOrEntryOrRaw` parameter is an array
|
|
287
|
+
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
|
|
288
|
+
* `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
|
|
313
289
|
*/
|
|
314
|
-
isEntry(
|
|
315
|
-
return Array.isArray(
|
|
290
|
+
isEntry(keyOrNodeOrEntryOrRaw) {
|
|
291
|
+
return Array.isArray(keyOrNodeOrEntryOrRaw) && keyOrNodeOrEntryOrRaw.length === 2;
|
|
316
292
|
}
|
|
317
293
|
/**
|
|
318
294
|
* Time Complexity O(1)
|
|
@@ -334,29 +310,30 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
334
310
|
* Time Complexity O(n)
|
|
335
311
|
* Space Complexity O(1)
|
|
336
312
|
*
|
|
337
|
-
* The `add` function
|
|
338
|
-
* and finding the
|
|
339
|
-
* @param {
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
* @returns a boolean value. It returns `true` if the insertion
|
|
347
|
-
* insertion position
|
|
313
|
+
* The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
|
|
314
|
+
* and finding the correct insertion position.
|
|
315
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `add` method you provided
|
|
316
|
+
* seems to be for adding a new node to a binary tree structure. The `keyOrNodeOrEntryOrRaw`
|
|
317
|
+
* parameter in the method can accept different types of values:
|
|
318
|
+
* @param {V} [value] - The `value` parameter in the `add` method represents the value associated
|
|
319
|
+
* with the key that you want to add to the binary tree. When adding a key-value pair to the binary
|
|
320
|
+
* tree, you provide the key and its corresponding value. The `add` method then creates a new node
|
|
321
|
+
* with this
|
|
322
|
+
* @returns The `add` method returns a boolean value. It returns `true` if the insertion of the new
|
|
323
|
+
* node was successful, and `false` if the insertion position could not be found or if a duplicate
|
|
324
|
+
* key was found and the node was replaced instead of inserted.
|
|
348
325
|
*/
|
|
349
|
-
add(
|
|
350
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
326
|
+
add(keyOrNodeOrEntryOrRaw, value) {
|
|
327
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
|
|
351
328
|
if (newNode === undefined)
|
|
352
329
|
return false;
|
|
353
330
|
// If the tree is empty, directly set the new node as the root node
|
|
354
|
-
if (!this.
|
|
355
|
-
this.
|
|
331
|
+
if (!this._root) {
|
|
332
|
+
this._setRoot(newNode);
|
|
356
333
|
this._size = 1;
|
|
357
334
|
return true;
|
|
358
335
|
}
|
|
359
|
-
const queue = new queue_1.Queue([this.
|
|
336
|
+
const queue = new queue_1.Queue([this._root]);
|
|
360
337
|
let potentialParent; // Record the parent node of the potential insertion location
|
|
361
338
|
while (queue.size > 0) {
|
|
362
339
|
const cur = queue.shift();
|
|
@@ -373,10 +350,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
373
350
|
}
|
|
374
351
|
// Continue traversing the left and right subtrees
|
|
375
352
|
if (cur.left !== null) {
|
|
376
|
-
|
|
353
|
+
if (cur.left)
|
|
354
|
+
queue.push(cur.left);
|
|
377
355
|
}
|
|
378
356
|
if (cur.right !== null) {
|
|
379
|
-
|
|
357
|
+
if (cur.right)
|
|
358
|
+
queue.push(cur.right);
|
|
380
359
|
}
|
|
381
360
|
}
|
|
382
361
|
// At the end of the traversal, if the insertion position is found, insert
|
|
@@ -396,24 +375,28 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
396
375
|
* Time Complexity: O(k * n)
|
|
397
376
|
* Space Complexity: O(1)
|
|
398
377
|
*
|
|
399
|
-
* The `addMany` function takes in
|
|
400
|
-
* optional
|
|
401
|
-
*
|
|
402
|
-
* @param
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
* in the `
|
|
406
|
-
*
|
|
407
|
-
*
|
|
378
|
+
* The `addMany` function takes in multiple keys or nodes or entries or raw values along with
|
|
379
|
+
* optional values, and adds them to a data structure while returning an array indicating whether
|
|
380
|
+
* each insertion was successful.
|
|
381
|
+
* @param keysOrNodesOrEntriesOrRaws - `keysOrNodesOrEntriesOrRaws` is an iterable that can contain a
|
|
382
|
+
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
|
|
383
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`.
|
|
384
|
+
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
|
|
385
|
+
* accepts an iterable of values. These values correspond to the keys or nodes being added in the
|
|
386
|
+
* `keysOrNodesOrEntriesOrRaws` parameter. If provided, the function will iterate over the values and
|
|
387
|
+
* assign them
|
|
388
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each key,
|
|
389
|
+
* node, entry, or raw value was successfully added to the data structure. Each boolean value
|
|
390
|
+
* corresponds to the success of adding the corresponding key or value in the input iterable.
|
|
408
391
|
*/
|
|
409
|
-
addMany(
|
|
392
|
+
addMany(keysOrNodesOrEntriesOrRaws, values) {
|
|
410
393
|
// TODO not sure addMany not be run multi times
|
|
411
394
|
const inserted = [];
|
|
412
395
|
let valuesIterator;
|
|
413
396
|
if (values) {
|
|
414
397
|
valuesIterator = values[Symbol.iterator]();
|
|
415
398
|
}
|
|
416
|
-
for (const
|
|
399
|
+
for (const keyOrNodeOrEntryOrRaw of keysOrNodesOrEntriesOrRaws) {
|
|
417
400
|
let value = undefined;
|
|
418
401
|
if (valuesIterator) {
|
|
419
402
|
const valueResult = valuesIterator.next();
|
|
@@ -421,7 +404,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
421
404
|
value = valueResult.value;
|
|
422
405
|
}
|
|
423
406
|
}
|
|
424
|
-
inserted.push(this.add(
|
|
407
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRaw, value));
|
|
425
408
|
}
|
|
426
409
|
return inserted;
|
|
427
410
|
}
|
|
@@ -429,38 +412,38 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
429
412
|
* Time Complexity: O(k * n)
|
|
430
413
|
* Space Complexity: O(1)
|
|
431
414
|
*
|
|
432
|
-
* The `refill` function clears the
|
|
433
|
-
*
|
|
434
|
-
*
|
|
435
|
-
*
|
|
436
|
-
*
|
|
437
|
-
*
|
|
415
|
+
* The `refill` function clears the existing data structure and then adds new key-value pairs based
|
|
416
|
+
* on the provided input.
|
|
417
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter in the `refill`
|
|
418
|
+
* method can accept an iterable containing a mix of `BTNKeyOrNodeOrEntry<K, V, NODE>` objects or `R`
|
|
419
|
+
* objects.
|
|
420
|
+
* @param [values] - The `values` parameter in the `refill` method is an optional parameter that
|
|
421
|
+
* accepts an iterable of values of type `V` or `undefined`.
|
|
438
422
|
*/
|
|
439
|
-
refill(
|
|
423
|
+
refill(keysOrNodesOrEntriesOrRaws, values) {
|
|
440
424
|
this.clear();
|
|
441
|
-
this.addMany(
|
|
425
|
+
this.addMany(keysOrNodesOrEntriesOrRaws, values);
|
|
442
426
|
}
|
|
443
427
|
/**
|
|
444
428
|
* Time Complexity: O(n)
|
|
445
429
|
* Space Complexity: O(1)
|
|
446
430
|
*
|
|
447
|
-
* The
|
|
448
|
-
* the deleted node
|
|
449
|
-
* @param {
|
|
450
|
-
*
|
|
451
|
-
*
|
|
452
|
-
*
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
*
|
|
456
|
-
*
|
|
431
|
+
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
432
|
+
* the deleted node along with information for tree balancing.
|
|
433
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
434
|
+
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
435
|
+
* node, entry, raw data, or a custom predicate. The method returns an array of
|
|
436
|
+
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
437
|
+
* balancing is needed.
|
|
438
|
+
* @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
|
|
439
|
+
* the array contains information about the node that was deleted (`deleted`) and the node that may
|
|
440
|
+
* need to be balanced (`needBalanced`).
|
|
457
441
|
*/
|
|
458
|
-
delete(
|
|
442
|
+
delete(keyOrNodeOrEntryOrRawOrPredicate) {
|
|
459
443
|
const deletedResult = [];
|
|
460
|
-
if (!this.
|
|
444
|
+
if (!this._root)
|
|
461
445
|
return deletedResult;
|
|
462
|
-
|
|
463
|
-
const curr = this.getNode(identifier, callback);
|
|
446
|
+
const curr = this.getNode(keyOrNodeOrEntryOrRawOrPredicate);
|
|
464
447
|
if (!curr)
|
|
465
448
|
return deletedResult;
|
|
466
449
|
const parent = curr === null || curr === void 0 ? void 0 : curr.parent;
|
|
@@ -497,7 +480,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
497
480
|
this._setRoot(curr.right);
|
|
498
481
|
curr.right = undefined;
|
|
499
482
|
}
|
|
500
|
-
this._size = this.
|
|
483
|
+
this._size = this._size - 1;
|
|
501
484
|
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
502
485
|
return deletedResult;
|
|
503
486
|
}
|
|
@@ -505,47 +488,46 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
505
488
|
* Time Complexity: O(n)
|
|
506
489
|
* Space Complexity: O(k + log n)
|
|
507
490
|
*
|
|
508
|
-
* The function `getNodes`
|
|
509
|
-
* recursive or iterative
|
|
510
|
-
* @param {
|
|
511
|
-
*
|
|
512
|
-
*
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
518
|
-
*
|
|
519
|
-
*
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
525
|
-
* iteration to be performed on the nodes of a binary tree. It can have two possible values:
|
|
526
|
-
* @returns an array of NODE objects.
|
|
491
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
492
|
+
* or predicate, with options for recursive or iterative traversal.
|
|
493
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
494
|
+
* - The `getNodes` function you provided takes several parameters:
|
|
495
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
496
|
+
* determines whether to return only the first node that matches the criteria specified by the
|
|
497
|
+
* `keyOrNodeOrEntryOrRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
498
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
499
|
+
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
500
|
+
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
501
|
+
* not provided, the default value is set to `this._root
|
|
502
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
503
|
+
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
504
|
+
* can have two possible values:
|
|
505
|
+
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
506
|
+
* based on the input parameters and the iteration type specified.
|
|
527
507
|
*/
|
|
528
|
-
getNodes(
|
|
529
|
-
if (
|
|
508
|
+
getNodes(keyOrNodeOrEntryOrRawOrPredicate, onlyOne = false, beginRoot = this._root, iterationType = this.iterationType) {
|
|
509
|
+
if (keyOrNodeOrEntryOrRawOrPredicate === undefined)
|
|
530
510
|
return [];
|
|
531
|
-
if (
|
|
511
|
+
if (keyOrNodeOrEntryOrRawOrPredicate === null)
|
|
532
512
|
return [];
|
|
533
513
|
beginRoot = this.ensureNode(beginRoot);
|
|
534
514
|
if (!beginRoot)
|
|
535
515
|
return [];
|
|
536
|
-
callback = this.
|
|
516
|
+
const callback = this._ensurePredicate(keyOrNodeOrEntryOrRawOrPredicate);
|
|
537
517
|
const ans = [];
|
|
538
518
|
if (iterationType === 'RECURSIVE') {
|
|
539
519
|
const dfs = (cur) => {
|
|
540
|
-
if (callback(cur)
|
|
520
|
+
if (callback(cur)) {
|
|
541
521
|
ans.push(cur);
|
|
542
522
|
if (onlyOne)
|
|
543
523
|
return;
|
|
544
524
|
}
|
|
545
525
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
546
526
|
return;
|
|
547
|
-
this.isRealNode(cur.left)
|
|
548
|
-
|
|
527
|
+
if (this.isRealNode(cur.left))
|
|
528
|
+
dfs(cur.left);
|
|
529
|
+
if (this.isRealNode(cur.right))
|
|
530
|
+
dfs(cur.right);
|
|
549
531
|
};
|
|
550
532
|
dfs(beginRoot);
|
|
551
533
|
}
|
|
@@ -554,13 +536,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
554
536
|
while (stack.length > 0) {
|
|
555
537
|
const cur = stack.pop();
|
|
556
538
|
if (this.isRealNode(cur)) {
|
|
557
|
-
if (callback(cur)
|
|
539
|
+
if (callback(cur)) {
|
|
558
540
|
ans.push(cur);
|
|
559
541
|
if (onlyOne)
|
|
560
542
|
return ans;
|
|
561
543
|
}
|
|
562
|
-
this.isRealNode(cur.left)
|
|
563
|
-
|
|
544
|
+
if (this.isRealNode(cur.left))
|
|
545
|
+
stack.push(cur.left);
|
|
546
|
+
if (this.isRealNode(cur.right))
|
|
547
|
+
stack.push(cur.right);
|
|
564
548
|
}
|
|
565
549
|
}
|
|
566
550
|
}
|
|
@@ -570,93 +554,97 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
570
554
|
* Time Complexity: O(n)
|
|
571
555
|
* Space Complexity: O(log n).
|
|
572
556
|
*
|
|
573
|
-
* The
|
|
574
|
-
*
|
|
575
|
-
* @param {
|
|
576
|
-
*
|
|
577
|
-
*
|
|
578
|
-
* @param {
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
*
|
|
584
|
-
* of
|
|
585
|
-
*
|
|
557
|
+
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
558
|
+
* predicate.
|
|
559
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
560
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `getNode` function can accept a key,
|
|
561
|
+
* node, entry, raw data, or a predicate function.
|
|
562
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
563
|
+
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
564
|
+
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
565
|
+
* is typically the root node of the binary tree.
|
|
566
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is
|
|
567
|
+
* used to specify the type of iteration to be performed when searching for a node. It has a default
|
|
568
|
+
* value of `this.iterationType`, which means it will use the iteration type defined in the current
|
|
569
|
+
* context if no specific value is provided
|
|
570
|
+
* @returns The `getNode` function is returning the first node that matches the specified criteria,
|
|
571
|
+
* or `null` if no matching node is found.
|
|
586
572
|
*/
|
|
587
|
-
getNode(
|
|
573
|
+
getNode(keyOrNodeOrEntryOrRawOrPredicate, beginRoot = this._root, iterationType = this.iterationType) {
|
|
588
574
|
var _a;
|
|
589
|
-
return (_a = this.getNodes(
|
|
575
|
+
return (_a = this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
590
576
|
}
|
|
591
577
|
/**
|
|
592
578
|
* Time Complexity: O(n)
|
|
593
579
|
* Space Complexity: O(log n)
|
|
594
580
|
*
|
|
595
|
-
* The function `getNodeByKey`
|
|
596
|
-
* @param {K} key - The key parameter is the value
|
|
597
|
-
*
|
|
598
|
-
* @param {IterationType}
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
* @returns
|
|
581
|
+
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
582
|
+
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
583
|
+
* structure.
|
|
584
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
585
|
+
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
586
|
+
* is an optional parameter with a default value of `this.iterationType`.
|
|
587
|
+
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
588
|
+
* (`OptBTNOrNull<NODE>`).
|
|
602
589
|
*/
|
|
603
590
|
getNodeByKey(key, iterationType = this.iterationType) {
|
|
604
|
-
return this.getNode(key, this.
|
|
591
|
+
return this.getNode(key, this._root, iterationType);
|
|
605
592
|
}
|
|
606
593
|
/**
|
|
607
594
|
* Time Complexity: O(n)
|
|
608
595
|
* Space Complexity: O(log n)
|
|
609
596
|
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
* @param {
|
|
613
|
-
*
|
|
614
|
-
*
|
|
615
|
-
* @param {
|
|
616
|
-
*
|
|
617
|
-
*
|
|
618
|
-
*
|
|
619
|
-
*
|
|
620
|
-
*
|
|
621
|
-
*
|
|
622
|
-
*
|
|
623
|
-
*
|
|
624
|
-
*
|
|
625
|
-
*
|
|
597
|
+
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
598
|
+
* node, entry, raw data, or predicate in a data structure.
|
|
599
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
600
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `get` method can accept one of the
|
|
601
|
+
* following types:
|
|
602
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `get`
|
|
603
|
+
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
604
|
+
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
605
|
+
* tree (`this._root`).
|
|
606
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `get` method is used
|
|
607
|
+
* to specify the type of iteration to be performed when searching for a key in the binary tree. It
|
|
608
|
+
* is an optional parameter with a default value of `this.iterationType`, which means it will use the
|
|
609
|
+
* iteration type defined in the
|
|
610
|
+
* @returns The `get` method is returning the value associated with the specified key, node, entry,
|
|
611
|
+
* raw data, or predicate in the binary tree map. If the specified key or node is found in the tree,
|
|
612
|
+
* the method returns the corresponding value. If the key or node is not found, it returns
|
|
613
|
+
* `undefined`.
|
|
626
614
|
*/
|
|
627
|
-
get(
|
|
615
|
+
get(keyOrNodeOrEntryOrRawOrPredicate, beginRoot = this._root, iterationType = this.iterationType) {
|
|
628
616
|
var _a;
|
|
629
|
-
return (_a = this.getNode(
|
|
617
|
+
return (_a = this.getNode(keyOrNodeOrEntryOrRawOrPredicate, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
|
|
630
618
|
}
|
|
631
619
|
/**
|
|
632
620
|
* Time Complexity: O(n)
|
|
633
621
|
* Space Complexity: O(log n)
|
|
634
622
|
*
|
|
635
|
-
* The `has` function checks if a
|
|
636
|
-
*
|
|
637
|
-
* @param {
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
*
|
|
643
|
-
* `
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
*
|
|
647
|
-
* @
|
|
648
|
-
*
|
|
649
|
-
*
|
|
623
|
+
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
624
|
+
* exists in the data structure.
|
|
625
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
626
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `override has` method can accept one of
|
|
627
|
+
* the following types:
|
|
628
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
629
|
+
* `override` method is used to specify the starting point for the search operation within the data
|
|
630
|
+
* structure. It defaults to `this._root` if not provided explicitly.
|
|
631
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
632
|
+
* is used to specify the type of iteration to be performed. It has a default value of
|
|
633
|
+
* `this.iterationType`, which means it will use the iteration type defined in the current context if
|
|
634
|
+
* no value is provided when calling the method.
|
|
635
|
+
* @returns The `override has` method is returning a boolean value. It checks if there are any nodes
|
|
636
|
+
* that match the provided key, node, entry, raw data, or predicate in the tree structure. If there
|
|
637
|
+
* are matching nodes, it returns `true`, indicating that the tree contains the specified element.
|
|
638
|
+
* Otherwise, it returns `false`.
|
|
650
639
|
*/
|
|
651
|
-
has(
|
|
652
|
-
|
|
653
|
-
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
640
|
+
has(keyOrNodeOrEntryOrRawOrPredicate, beginRoot = this._root, iterationType = this.iterationType) {
|
|
641
|
+
return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType).length > 0;
|
|
654
642
|
}
|
|
655
643
|
/**
|
|
656
644
|
* Time Complexity: O(1)
|
|
657
645
|
* Space Complexity: O(1)
|
|
658
646
|
*
|
|
659
|
-
*
|
|
647
|
+
* The `clear` function resets the root node and size of a data structure to empty.
|
|
660
648
|
*/
|
|
661
649
|
clear() {
|
|
662
650
|
this._setRoot(undefined);
|
|
@@ -666,42 +654,51 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
666
654
|
* Time Complexity: O(1)
|
|
667
655
|
* Space Complexity: O(1)
|
|
668
656
|
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
657
|
+
* The `isEmpty` function in TypeScript checks if a data structure has no elements and returns a
|
|
658
|
+
* boolean value.
|
|
659
|
+
* @returns The `isEmpty()` method is returning a boolean value, specifically `true` if the `_size`
|
|
660
|
+
* property is equal to 0, indicating that the data structure is empty, and `false` otherwise.
|
|
671
661
|
*/
|
|
672
662
|
isEmpty() {
|
|
673
|
-
return this.
|
|
663
|
+
return this._size === 0;
|
|
674
664
|
}
|
|
675
665
|
/**
|
|
676
666
|
* Time Complexity: O(n)
|
|
677
667
|
* Space Complexity: O(log n)
|
|
678
668
|
*
|
|
679
|
-
* The function checks if a binary tree is perfectly balanced by comparing
|
|
680
|
-
* height
|
|
681
|
-
* @param {
|
|
682
|
-
*
|
|
683
|
-
*
|
|
684
|
-
*
|
|
685
|
-
*
|
|
669
|
+
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
670
|
+
* its height.
|
|
671
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
672
|
+
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
673
|
+
* binary tree or a specific node from which the balance check should begin.
|
|
674
|
+
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
675
|
+
* the tree starting from the `beginRoot` node is perfectly balanced or not. The return value is
|
|
676
|
+
* determined by comparing the minimum height of the tree with the height of the tree. If the minimum
|
|
677
|
+
* height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
|
|
678
|
+
* balanced and
|
|
686
679
|
*/
|
|
687
|
-
isPerfectlyBalanced(beginRoot = this.
|
|
680
|
+
isPerfectlyBalanced(beginRoot = this._root) {
|
|
688
681
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
689
682
|
}
|
|
690
683
|
/**
|
|
691
684
|
* Time Complexity: O(n)
|
|
692
685
|
* Space Complexity: O(1)
|
|
693
686
|
*
|
|
694
|
-
* The function `isBST` checks if a binary search tree is valid
|
|
695
|
-
*
|
|
696
|
-
*
|
|
697
|
-
*
|
|
698
|
-
*
|
|
699
|
-
*
|
|
700
|
-
*
|
|
701
|
-
*
|
|
702
|
-
*
|
|
687
|
+
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
688
|
+
* or iterative methods.
|
|
689
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `isBST`
|
|
690
|
+
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
691
|
+
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
692
|
+
* provided, the function will default to
|
|
693
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `isBST` function
|
|
694
|
+
* determines whether the function should use a recursive approach or an iterative approach to check
|
|
695
|
+
* if the binary search tree (BST) is valid.
|
|
696
|
+
* @returns The `isBST` method is returning a boolean value, which indicates whether the binary
|
|
697
|
+
* search tree (BST) represented by the given root node is a valid BST or not. The method checks if
|
|
698
|
+
* the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
|
|
699
|
+
* less than the node's key, and all nodes in its right subtree have keys greater than the node's
|
|
703
700
|
*/
|
|
704
|
-
isBST(beginRoot = this.
|
|
701
|
+
isBST(beginRoot = this._root, iterationType = this.iterationType) {
|
|
705
702
|
// TODO there is a bug
|
|
706
703
|
beginRoot = this.ensureNode(beginRoot);
|
|
707
704
|
if (!beginRoot)
|
|
@@ -747,17 +744,19 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
747
744
|
* Time Complexity: O(n)
|
|
748
745
|
* Space Complexity: O(1)
|
|
749
746
|
*
|
|
750
|
-
* The function calculates the depth
|
|
751
|
-
* @param {
|
|
752
|
-
*
|
|
753
|
-
*
|
|
754
|
-
* @param {
|
|
755
|
-
* represents the starting point from which to calculate the depth
|
|
756
|
-
* node
|
|
757
|
-
* `
|
|
758
|
-
* @returns the depth of a node
|
|
747
|
+
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
748
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} dist - The `dist` parameter in the `getDepth`
|
|
749
|
+
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
750
|
+
* It is the target node for which you want to calculate the depth from the `beginRoot` node.
|
|
751
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
752
|
+
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
753
|
+
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
754
|
+
* for `beginRoot` is set to the root of the binary
|
|
755
|
+
* @returns The `getDepth` method returns the depth of a given node `dist` relative to the
|
|
756
|
+
* `beginRoot` node in a binary tree. If the `dist` node is not found in the path to the `beginRoot`
|
|
757
|
+
* node, it returns the depth of the `dist` node from the root of the tree.
|
|
759
758
|
*/
|
|
760
|
-
getDepth(dist, beginRoot = this.
|
|
759
|
+
getDepth(dist, beginRoot = this._root) {
|
|
761
760
|
let distEnsured = this.ensureNode(dist);
|
|
762
761
|
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
763
762
|
let depth = 0;
|
|
@@ -775,15 +774,19 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
775
774
|
* Space Complexity: O(1)
|
|
776
775
|
*
|
|
777
776
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
778
|
-
* or iterative approach.
|
|
779
|
-
* @param {
|
|
780
|
-
*
|
|
781
|
-
*
|
|
782
|
-
*
|
|
783
|
-
*
|
|
784
|
-
*
|
|
777
|
+
* or iterative approach in TypeScript.
|
|
778
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
779
|
+
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
780
|
+
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
781
|
+
* binary tree data structure.
|
|
782
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
783
|
+
* of iteration to be performed while calculating the height of the binary tree. It can have two
|
|
784
|
+
* possible values:
|
|
785
|
+
* @returns The `getHeight` method returns the height of the binary tree starting from the specified
|
|
786
|
+
* root node. The height is calculated based on the maximum depth of the tree, considering either a
|
|
787
|
+
* recursive approach or an iterative approach depending on the `iterationType` parameter.
|
|
785
788
|
*/
|
|
786
|
-
getHeight(beginRoot = this.
|
|
789
|
+
getHeight(beginRoot = this._root, iterationType = this.iterationType) {
|
|
787
790
|
beginRoot = this.ensureNode(beginRoot);
|
|
788
791
|
if (!this.isRealNode(beginRoot))
|
|
789
792
|
return -1;
|
|
@@ -816,19 +819,20 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
816
819
|
* Space Complexity: O(log n)
|
|
817
820
|
*
|
|
818
821
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
819
|
-
* recursive or iterative approach.
|
|
820
|
-
* @param {
|
|
821
|
-
* starting
|
|
822
|
-
*
|
|
823
|
-
* tree.
|
|
824
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
825
|
-
* iteration to
|
|
826
|
-
* values:
|
|
827
|
-
* @returns The
|
|
828
|
-
*
|
|
822
|
+
* recursive or iterative approach in TypeScript.
|
|
823
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
824
|
+
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
825
|
+
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
826
|
+
* tree. If not provided, the default value is the root
|
|
827
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getMinHeight` method
|
|
828
|
+
* specifies the type of iteration to use when calculating the minimum height of a binary tree. It
|
|
829
|
+
* can have two possible values:
|
|
830
|
+
* @returns The `getMinHeight` method returns the minimum height of the binary tree starting from the
|
|
831
|
+
* specified root node. The height is calculated based on the shortest path from the root node to a
|
|
832
|
+
* leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
|
|
833
|
+
* a stack) based on the `iterationType` parameter.
|
|
829
834
|
*/
|
|
830
|
-
getMinHeight(beginRoot = this.
|
|
831
|
-
var _a, _b, _c;
|
|
835
|
+
getMinHeight(beginRoot = this._root, iterationType = this.iterationType) {
|
|
832
836
|
beginRoot = this.ensureNode(beginRoot);
|
|
833
837
|
if (!beginRoot)
|
|
834
838
|
return -1;
|
|
@@ -858,8 +862,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
858
862
|
if (!this.isRealNode(node.right) || last === node.right) {
|
|
859
863
|
node = stack.pop();
|
|
860
864
|
if (this.isRealNode(node)) {
|
|
861
|
-
const leftMinHeight = this.isRealNode(node.left) ?
|
|
862
|
-
const rightMinHeight = this.isRealNode(node.right) ?
|
|
865
|
+
const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) : -1;
|
|
866
|
+
const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) : -1;
|
|
863
867
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
864
868
|
last = node;
|
|
865
869
|
node = null;
|
|
@@ -869,33 +873,40 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
869
873
|
node = node.right;
|
|
870
874
|
}
|
|
871
875
|
}
|
|
872
|
-
return
|
|
876
|
+
return depths.get(beginRoot);
|
|
873
877
|
}
|
|
874
878
|
}
|
|
875
879
|
/**
|
|
876
880
|
* Time Complexity: O(log n)
|
|
877
881
|
* Space Complexity: O(log n)
|
|
878
882
|
*
|
|
879
|
-
* The function `getPathToRoot`
|
|
880
|
-
*
|
|
881
|
-
* @param {
|
|
882
|
-
*
|
|
883
|
-
*
|
|
884
|
-
*
|
|
885
|
-
*
|
|
886
|
-
*
|
|
883
|
+
* The function `getPathToRoot` in TypeScript retrieves the path from a given node to the root of a
|
|
884
|
+
* tree structure, applying a specified callback function along the way.
|
|
885
|
+
* @param {C} callback - The `callback` parameter is a function that is used to process each node in
|
|
886
|
+
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
887
|
+
* a value based on that node. The return type of the callback function is determined by the generic
|
|
888
|
+
* type `C
|
|
889
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginNode - The `beginNode` parameter in the
|
|
890
|
+
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
891
|
+
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
892
|
+
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
893
|
+
* not. If `isReverse` is set to `true`, the path will be reversed before being returned. If `is
|
|
894
|
+
* @returns The function `getPathToRoot` returns an array of the return values of the callback
|
|
895
|
+
* function `callback` applied to each node in the path from the `beginNode` to the root node. The
|
|
896
|
+
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
897
|
+
* parameter.
|
|
887
898
|
*/
|
|
888
|
-
getPathToRoot(beginNode, isReverse = true) {
|
|
899
|
+
getPathToRoot(callback = this._DEFAULT_BTN_CALLBACK, beginNode, isReverse = true) {
|
|
889
900
|
const result = [];
|
|
890
901
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
891
902
|
if (!beginNodeEnsured)
|
|
892
903
|
return result;
|
|
893
904
|
while (beginNodeEnsured.parent) {
|
|
894
905
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
895
|
-
result.push(beginNodeEnsured);
|
|
906
|
+
result.push(callback(beginNodeEnsured));
|
|
896
907
|
beginNodeEnsured = beginNodeEnsured.parent;
|
|
897
908
|
}
|
|
898
|
-
result.push(beginNodeEnsured);
|
|
909
|
+
result.push(callback(beginNodeEnsured));
|
|
899
910
|
return isReverse ? result.reverse() : result;
|
|
900
911
|
}
|
|
901
912
|
/**
|
|
@@ -905,21 +916,21 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
905
916
|
* The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
|
|
906
917
|
* tail-recursive iteration.
|
|
907
918
|
* @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
|
|
908
|
-
* node of a binary tree or
|
|
909
|
-
* if not
|
|
910
|
-
* @param {
|
|
919
|
+
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
920
|
+
* value of `_DEFAULT_BTN_CALLBACK` if not specified.
|
|
921
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
911
922
|
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
912
|
-
* tree. It can be either a
|
|
913
|
-
* the
|
|
923
|
+
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
924
|
+
* starting point is provided, the function will default
|
|
914
925
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getLeftMost` function
|
|
915
926
|
* specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
|
|
916
927
|
* possible values:
|
|
917
928
|
* @returns The `getLeftMost` function returns the result of the callback function `C` applied to the
|
|
918
|
-
* leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` is
|
|
919
|
-
* it returns the result of the callback function applied to `undefined`. If the `beginRoot`
|
|
920
|
-
* real node, it returns the result of the callback
|
|
929
|
+
* leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` node is
|
|
930
|
+
* `NIL`, it returns the result of the callback function applied to `undefined`. If the `beginRoot`
|
|
931
|
+
* node is not a real node, it returns the result of the callback
|
|
921
932
|
*/
|
|
922
|
-
getLeftMost(callback = this.
|
|
933
|
+
getLeftMost(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
|
|
923
934
|
if (this.isNIL(beginRoot))
|
|
924
935
|
return callback(undefined);
|
|
925
936
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -950,22 +961,22 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
950
961
|
* The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
|
|
951
962
|
* or iterative traversal methods.
|
|
952
963
|
* @param {C} callback - The `callback` parameter is a function that will be called with the result
|
|
953
|
-
* of the
|
|
954
|
-
*
|
|
955
|
-
*
|
|
964
|
+
* of finding the rightmost node in a binary tree. It is of type `BTNCallback<OptBTNOrNull<NODE>>`,
|
|
965
|
+
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
966
|
+
* as
|
|
967
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
956
968
|
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
957
|
-
* tree. It can be either a
|
|
958
|
-
*
|
|
969
|
+
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
970
|
+
* starting point is provided, the function will default
|
|
959
971
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getRightMost`
|
|
960
|
-
* function specifies the type of iteration to be used when
|
|
961
|
-
*
|
|
962
|
-
* @returns The `getRightMost` function returns the result of the callback function `C
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
*
|
|
966
|
-
* rightmost node is returned
|
|
972
|
+
* function specifies the type of iteration to be used when traversing the binary tree nodes. It can
|
|
973
|
+
* have two possible values:
|
|
974
|
+
* @returns The `getRightMost` function returns the result of the callback function `C`, which is
|
|
975
|
+
* passed as a parameter to the function. The callback function is called with the rightmost node in
|
|
976
|
+
* the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
|
|
977
|
+
* other).
|
|
967
978
|
*/
|
|
968
|
-
getRightMost(callback = this.
|
|
979
|
+
getRightMost(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
|
|
969
980
|
if (this.isNIL(beginRoot))
|
|
970
981
|
return callback(undefined);
|
|
971
982
|
// TODO support get right most by passing key in
|
|
@@ -994,10 +1005,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
994
1005
|
* Time Complexity: O(log n)
|
|
995
1006
|
* Space Complexity: O(1)
|
|
996
1007
|
*
|
|
997
|
-
* The function returns the predecessor node of a given node in a
|
|
998
|
-
*
|
|
999
|
-
*
|
|
1000
|
-
*
|
|
1008
|
+
* The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
|
|
1009
|
+
* binary tree.
|
|
1010
|
+
* @param {NODE} node - The `getPredecessor` function you provided seems to be attempting to find the
|
|
1011
|
+
* predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
|
|
1012
|
+
* while loop condition that might cause an infinite loop.
|
|
1013
|
+
* @returns The `getPredecessor` function returns the predecessor node of the input `NODE` parameter.
|
|
1014
|
+
* If the left child of the input node exists, it traverses to the rightmost node of the left subtree
|
|
1015
|
+
* to find the predecessor. If the left child does not exist, it returns the input node itself.
|
|
1001
1016
|
*/
|
|
1002
1017
|
getPredecessor(node) {
|
|
1003
1018
|
if (this.isRealNode(node.left)) {
|
|
@@ -1017,10 +1032,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1017
1032
|
* Time Complexity: O(log n)
|
|
1018
1033
|
* Space Complexity: O(1)
|
|
1019
1034
|
*
|
|
1020
|
-
* The function `getSuccessor` returns the next node in
|
|
1021
|
-
*
|
|
1022
|
-
* @
|
|
1023
|
-
*
|
|
1035
|
+
* The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
|
|
1036
|
+
* binary tree.
|
|
1037
|
+
* @param {K | NODE | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
|
|
1038
|
+
* type `K`, `NODE`, or `null`.
|
|
1039
|
+
* @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
|
|
1040
|
+
* a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
|
|
1041
|
+
* have a right child, the function traverses up the parent nodes until it finds a node that is not
|
|
1042
|
+
* the right child of its parent, and returns that node
|
|
1024
1043
|
*/
|
|
1025
1044
|
getSuccessor(x) {
|
|
1026
1045
|
x = this.ensureNode(x);
|
|
@@ -1040,26 +1059,29 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1040
1059
|
* Time complexity: O(n)
|
|
1041
1060
|
* Space complexity: O(n)
|
|
1042
1061
|
*
|
|
1043
|
-
* The `dfs`
|
|
1044
|
-
*
|
|
1045
|
-
* @param {C} callback - The `callback` parameter is a
|
|
1046
|
-
*
|
|
1047
|
-
*
|
|
1048
|
-
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter
|
|
1049
|
-
*
|
|
1050
|
-
*
|
|
1051
|
-
*
|
|
1052
|
-
* is
|
|
1053
|
-
*
|
|
1054
|
-
*
|
|
1055
|
-
*
|
|
1056
|
-
*
|
|
1057
|
-
*
|
|
1058
|
-
*
|
|
1059
|
-
* values
|
|
1060
|
-
*
|
|
1062
|
+
* The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
|
|
1063
|
+
* specified parameters.
|
|
1064
|
+
* @param {C} callback - The `callback` parameter is a generic type `C` that extends the
|
|
1065
|
+
* `BTNCallback` interface with a type parameter of `OptBTNOrNull<NODE>`. It has a default value of
|
|
1066
|
+
* `this._DEFAULT_BTN_CALLBACK as C`.
|
|
1067
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
|
|
1068
|
+
* order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
|
|
1069
|
+
* possible values for the `pattern` parameter are:
|
|
1070
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `dfs`
|
|
1071
|
+
* method is used to specify the starting point for the Depth-First Search traversal. It can be
|
|
1072
|
+
* either a `BTNKeyOrNodeOrEntry` object representing a key, node, or entry in the binary tree map,
|
|
1073
|
+
* or it can be a
|
|
1074
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` method specifies
|
|
1075
|
+
* the type of iteration to be performed during the depth-first search traversal. It is used to
|
|
1076
|
+
* determine the order in which nodes are visited during the traversal.
|
|
1077
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `dfs` method is a boolean flag
|
|
1078
|
+
* that determines whether null values should be included in the traversal or not. If `includeNull`
|
|
1079
|
+
* is set to `true`, then null values will be included in the traversal process. If it is set to
|
|
1080
|
+
* `false`,
|
|
1081
|
+
* @returns The `dfs` method is returning an array of the return type specified by the generic type
|
|
1082
|
+
* parameter `C`. The return type is determined by the callback function provided to the method.
|
|
1061
1083
|
*/
|
|
1062
|
-
dfs(callback = this.
|
|
1084
|
+
dfs(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1063
1085
|
beginRoot = this.ensureNode(beginRoot);
|
|
1064
1086
|
if (!beginRoot)
|
|
1065
1087
|
return [];
|
|
@@ -1069,25 +1091,26 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1069
1091
|
* Time complexity: O(n)
|
|
1070
1092
|
* Space complexity: O(n)
|
|
1071
1093
|
*
|
|
1072
|
-
* The `bfs` function performs a breadth-first search on a binary tree
|
|
1073
|
-
*
|
|
1074
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1075
|
-
* the breadth-first search traversal. It
|
|
1076
|
-
*
|
|
1077
|
-
* @param {
|
|
1078
|
-
* starting point
|
|
1079
|
-
*
|
|
1080
|
-
*
|
|
1081
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
1082
|
-
* iteration to be performed. It can have two
|
|
1083
|
-
*
|
|
1084
|
-
*
|
|
1085
|
-
* `
|
|
1086
|
-
* set to `
|
|
1087
|
-
*
|
|
1088
|
-
* `
|
|
1094
|
+
* The `bfs` function performs a breadth-first search traversal on a binary tree or binary search
|
|
1095
|
+
* tree, executing a specified callback function on each node visited.
|
|
1096
|
+
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
1097
|
+
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
1098
|
+
* that extends the `BTNCallback` type, which takes a parameter of type `NODE` or `null`.
|
|
1099
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `bfs`
|
|
1100
|
+
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
1101
|
+
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
1102
|
+
* default value is the root node of the binary
|
|
1103
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `bfs` function
|
|
1104
|
+
* determines the type of iteration to be performed on the binary tree nodes. It can have two
|
|
1105
|
+
* possible values:
|
|
1106
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `bfs` function determines whether
|
|
1107
|
+
* to include `null` values in the breadth-first search traversal of a binary tree. If `includeNull`
|
|
1108
|
+
* is set to `true`, the traversal will include `null` values for nodes that do not have children
|
|
1109
|
+
* (left
|
|
1110
|
+
* @returns The `bfs` function returns an array of values that are the result of applying the
|
|
1111
|
+
* provided callback function to each node in the binary tree in a breadth-first search manner.
|
|
1089
1112
|
*/
|
|
1090
|
-
bfs(callback = this.
|
|
1113
|
+
bfs(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1091
1114
|
beginRoot = this.ensureNode(beginRoot);
|
|
1092
1115
|
if (!beginRoot)
|
|
1093
1116
|
return [];
|
|
@@ -1143,27 +1166,25 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1143
1166
|
* Time complexity: O(n)
|
|
1144
1167
|
* Space complexity: O(n)
|
|
1145
1168
|
*
|
|
1146
|
-
* The `leaves` function in TypeScript
|
|
1147
|
-
*
|
|
1169
|
+
* The `leaves` function in TypeScript returns an array of values from leaf nodes in a binary tree
|
|
1170
|
+
* structure based on a specified callback and iteration type.
|
|
1148
1171
|
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
|
|
1149
|
-
* in the binary tree. It is
|
|
1150
|
-
*
|
|
1151
|
-
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the `leaves`
|
|
1172
|
+
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
1173
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `leaves`
|
|
1152
1174
|
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
1153
|
-
* tree. It
|
|
1154
|
-
*
|
|
1175
|
+
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
1176
|
+
* explicitly provided, the default value
|
|
1155
1177
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
|
|
1156
1178
|
* specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
|
|
1157
1179
|
* can have two possible values:
|
|
1158
1180
|
* @returns The `leaves` method returns an array of values that are the result of applying the
|
|
1159
|
-
* provided callback function to
|
|
1181
|
+
* provided callback function to each leaf node in the binary tree.
|
|
1160
1182
|
*/
|
|
1161
|
-
leaves(callback = this.
|
|
1183
|
+
leaves(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
|
|
1162
1184
|
beginRoot = this.ensureNode(beginRoot);
|
|
1163
1185
|
const leaves = [];
|
|
1164
|
-
if (!this.isRealNode(beginRoot))
|
|
1186
|
+
if (!this.isRealNode(beginRoot))
|
|
1165
1187
|
return [];
|
|
1166
|
-
}
|
|
1167
1188
|
if (iterationType === 'RECURSIVE') {
|
|
1168
1189
|
const dfs = (cur) => {
|
|
1169
1190
|
if (this.isLeaf(cur)) {
|
|
@@ -1171,8 +1192,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1171
1192
|
}
|
|
1172
1193
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
1173
1194
|
return;
|
|
1174
|
-
this.isRealNode(cur.left)
|
|
1175
|
-
|
|
1195
|
+
if (this.isRealNode(cur.left))
|
|
1196
|
+
dfs(cur.left);
|
|
1197
|
+
if (this.isRealNode(cur.right))
|
|
1198
|
+
dfs(cur.right);
|
|
1176
1199
|
};
|
|
1177
1200
|
dfs(beginRoot);
|
|
1178
1201
|
}
|
|
@@ -1184,8 +1207,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1184
1207
|
if (this.isLeaf(cur)) {
|
|
1185
1208
|
leaves.push(callback(cur));
|
|
1186
1209
|
}
|
|
1187
|
-
this.isRealNode(cur.left)
|
|
1188
|
-
|
|
1210
|
+
if (this.isRealNode(cur.left))
|
|
1211
|
+
queue.push(cur.left);
|
|
1212
|
+
if (this.isRealNode(cur.right))
|
|
1213
|
+
queue.push(cur.right);
|
|
1189
1214
|
}
|
|
1190
1215
|
}
|
|
1191
1216
|
}
|
|
@@ -1195,24 +1220,27 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1195
1220
|
* Time complexity: O(n)
|
|
1196
1221
|
* Space complexity: O(n)
|
|
1197
1222
|
*
|
|
1198
|
-
* The `listLevels` function
|
|
1199
|
-
*
|
|
1200
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1201
|
-
* the tree. It
|
|
1202
|
-
*
|
|
1203
|
-
* @param {
|
|
1204
|
-
* starting point for traversing the tree. It can be
|
|
1205
|
-
*
|
|
1206
|
-
*
|
|
1207
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
1208
|
-
* iteration to be performed on the binary tree. It can have two
|
|
1209
|
-
*
|
|
1210
|
-
*
|
|
1211
|
-
*
|
|
1212
|
-
*
|
|
1213
|
-
*
|
|
1223
|
+
* The `listLevels` function in TypeScript generates a list of nodes at each level of a binary tree,
|
|
1224
|
+
* using either recursive or iterative traversal based on the specified iteration type.
|
|
1225
|
+
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
1226
|
+
* the binary tree during the traversal. It is used to process each node and determine what
|
|
1227
|
+
* information to include in the output for each level of the tree.
|
|
1228
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1229
|
+
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
1230
|
+
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
1231
|
+
* root of the binary tree.
|
|
1232
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `listLevels` function
|
|
1233
|
+
* determines the type of iteration to be performed on the binary tree nodes. It can have two
|
|
1234
|
+
* possible values:
|
|
1235
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `listLevels` method determines
|
|
1236
|
+
* whether or not to include null nodes in the traversal of the binary tree. If `includeNull` is set
|
|
1237
|
+
* to `true`, the traversal will include null nodes in the levels of the tree. If set to `false`,
|
|
1238
|
+
* null
|
|
1239
|
+
* @returns The `listLevels` method returns an array of arrays, where each inner array represents a
|
|
1240
|
+
* level in a binary tree. Each inner array contains the return value of the provided callback
|
|
1241
|
+
* function applied to the nodes at that level.
|
|
1214
1242
|
*/
|
|
1215
|
-
listLevels(callback = this.
|
|
1243
|
+
listLevels(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1216
1244
|
beginRoot = this.ensureNode(beginRoot);
|
|
1217
1245
|
const levelsNodes = [];
|
|
1218
1246
|
if (!beginRoot)
|
|
@@ -1265,24 +1293,25 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1265
1293
|
* Time complexity: O(n)
|
|
1266
1294
|
* Space complexity: O(n)
|
|
1267
1295
|
*
|
|
1268
|
-
* The `morris` function performs a
|
|
1269
|
-
* algorithm.
|
|
1270
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1271
|
-
*
|
|
1272
|
-
*
|
|
1273
|
-
* the
|
|
1274
|
-
*
|
|
1275
|
-
*
|
|
1276
|
-
*
|
|
1277
|
-
*
|
|
1278
|
-
*
|
|
1279
|
-
*
|
|
1280
|
-
* @returns The
|
|
1281
|
-
* callback function
|
|
1296
|
+
* The `morris` function in TypeScript performs a Depth-First Search traversal on a binary tree using
|
|
1297
|
+
* Morris Traversal algorithm with different order patterns.
|
|
1298
|
+
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
1299
|
+
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
1300
|
+
* `BTNCallback<NODE>` type. The default value for `callback` is `this._DEFAULT
|
|
1301
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
1302
|
+
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
1303
|
+
* values for the `pattern` parameter are:
|
|
1304
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `morris`
|
|
1305
|
+
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
1306
|
+
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
1307
|
+
* key, a node, an entry, or a reference
|
|
1308
|
+
* @returns The `morris` function is returning an array of values that are the result of applying the
|
|
1309
|
+
* provided callback function to each node in the binary tree in the specified order pattern (IN,
|
|
1310
|
+
* PRE, or POST).
|
|
1282
1311
|
*/
|
|
1283
|
-
morris(callback = this.
|
|
1312
|
+
morris(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root) {
|
|
1284
1313
|
beginRoot = this.ensureNode(beginRoot);
|
|
1285
|
-
if (beginRoot
|
|
1314
|
+
if (!beginRoot)
|
|
1286
1315
|
return [];
|
|
1287
1316
|
const ans = [];
|
|
1288
1317
|
let cur = beginRoot;
|
|
@@ -1369,8 +1398,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1369
1398
|
* Time complexity: O(n)
|
|
1370
1399
|
* Space complexity: O(n)
|
|
1371
1400
|
*
|
|
1372
|
-
* The `clone` function creates a deep copy of a tree
|
|
1373
|
-
*
|
|
1401
|
+
* The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first
|
|
1402
|
+
* search.
|
|
1403
|
+
* @returns The `clone()` method is returning a cloned copy of the tree with the same structure and
|
|
1404
|
+
* values as the original tree. The method creates a new tree, iterates over the nodes of the
|
|
1405
|
+
* original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
|
|
1406
|
+
* the original tree is null, a null node is added to the cloned tree. If a node
|
|
1374
1407
|
*/
|
|
1375
1408
|
clone() {
|
|
1376
1409
|
const cloned = this.createTree();
|
|
@@ -1379,23 +1412,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1379
1412
|
cloned.add(null);
|
|
1380
1413
|
else
|
|
1381
1414
|
cloned.add([node.key, node.value]);
|
|
1382
|
-
}, this.
|
|
1415
|
+
}, this._root, this.iterationType, true);
|
|
1383
1416
|
return cloned;
|
|
1384
1417
|
}
|
|
1385
1418
|
/**
|
|
1386
1419
|
* Time Complexity: O(n)
|
|
1387
1420
|
* Space Complexity: O(n)
|
|
1388
1421
|
*
|
|
1389
|
-
* The `filter` function
|
|
1390
|
-
*
|
|
1391
|
-
*
|
|
1392
|
-
*
|
|
1393
|
-
* of the
|
|
1394
|
-
* @param {any} [thisArg] - The `thisArg` parameter
|
|
1395
|
-
*
|
|
1396
|
-
*
|
|
1397
|
-
*
|
|
1398
|
-
*
|
|
1422
|
+
* The `filter` function iterates over key-value pairs in a tree data structure and creates a new
|
|
1423
|
+
* tree with elements that satisfy a given predicate.
|
|
1424
|
+
* @param predicate - The `predicate` parameter in the `filter` method is a function that will be
|
|
1425
|
+
* called with four arguments: the `value` of the current entry, the `key` of the current entry, the
|
|
1426
|
+
* `index` of the current entry in the iteration, and the reference to the tree itself (`
|
|
1427
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `filter` method allows you to specify the
|
|
1428
|
+
* value of `this` that should be used when executing the `predicate` function. This is useful when
|
|
1429
|
+
* the `predicate` function relies on the context of a specific object or value. By providing a
|
|
1430
|
+
* `thisArg
|
|
1431
|
+
* @returns The `filter` method is returning a new tree that contains entries that pass the provided
|
|
1432
|
+
* predicate function.
|
|
1399
1433
|
*/
|
|
1400
1434
|
filter(predicate, thisArg) {
|
|
1401
1435
|
const newTree = this.createTree();
|
|
@@ -1411,16 +1445,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1411
1445
|
* Time Complexity: O(n)
|
|
1412
1446
|
* Space Complexity: O(n)
|
|
1413
1447
|
*
|
|
1414
|
-
* The `map` function
|
|
1415
|
-
* tree.
|
|
1416
|
-
* @param callback - The callback parameter is a function that will be called
|
|
1417
|
-
* tree. It takes
|
|
1418
|
-
*
|
|
1419
|
-
*
|
|
1420
|
-
*
|
|
1421
|
-
*
|
|
1422
|
-
*
|
|
1423
|
-
*
|
|
1448
|
+
* The `map` function iterates over key-value pairs in a tree data structure, applies a callback
|
|
1449
|
+
* function to each value, and returns a new tree with the updated values.
|
|
1450
|
+
* @param callback - The `callback` parameter in the `map` method is a function that will be called
|
|
1451
|
+
* on each entry in the tree. It takes four arguments:
|
|
1452
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is an optional parameter
|
|
1453
|
+
* that specifies the value to be passed as `this` when executing the callback function. If provided,
|
|
1454
|
+
* the `thisArg` value will be used as the `this` value within the callback function. If `thisArg
|
|
1455
|
+
* @returns The `map` method is returning a new tree with the entries modified by the provided
|
|
1456
|
+
* callback function. Each entry in the original tree is passed to the callback function, and the
|
|
1457
|
+
* result of the callback function is added to the new tree.
|
|
1424
1458
|
*/
|
|
1425
1459
|
map(callback, thisArg) {
|
|
1426
1460
|
const newTree = this.createTree();
|
|
@@ -1443,17 +1477,21 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1443
1477
|
* Time Complexity: O(n)
|
|
1444
1478
|
* Space Complexity: O(n)
|
|
1445
1479
|
*
|
|
1446
|
-
* The `
|
|
1447
|
-
*
|
|
1448
|
-
*
|
|
1449
|
-
*
|
|
1450
|
-
*
|
|
1451
|
-
*
|
|
1452
|
-
*
|
|
1453
|
-
*
|
|
1454
|
-
*
|
|
1480
|
+
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
1481
|
+
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
1482
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1483
|
+
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
1484
|
+
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
1485
|
+
* the default is set to the root
|
|
1486
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter in the `toVisual` method is an
|
|
1487
|
+
* object that contains the following properties:
|
|
1488
|
+
* @returns The `override toVisual` method returns a string that represents the visual display of the
|
|
1489
|
+
* binary tree based on the provided options for showing undefined, null, and Red-Black NIL nodes.
|
|
1490
|
+
* The method constructs the visual representation by calling the `_displayAux` method and appending
|
|
1491
|
+
* the lines to the output string. The final output string contains the visual representation of the
|
|
1492
|
+
* binary tree with the specified options.
|
|
1455
1493
|
*/
|
|
1456
|
-
|
|
1494
|
+
toVisual(beginRoot = this._root, options) {
|
|
1457
1495
|
const opts = Object.assign({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
|
|
1458
1496
|
beginRoot = this.ensureNode(beginRoot);
|
|
1459
1497
|
let output = '';
|
|
@@ -1483,50 +1521,49 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1483
1521
|
* Time complexity: O(n)
|
|
1484
1522
|
* Space complexity: O(n)
|
|
1485
1523
|
*
|
|
1486
|
-
* The
|
|
1524
|
+
* The `_dfs` function performs a depth-first search traversal on a binary tree structure based on
|
|
1487
1525
|
* the specified order pattern and callback function.
|
|
1488
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1489
|
-
* visited during the depth-first search. It is of type `C`, which
|
|
1490
|
-
* `BTNCallback<OptBTNOrNull<NODE>>`. The default value
|
|
1491
|
-
* provided.
|
|
1526
|
+
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
1527
|
+
* called on each node visited during the depth-first search traversal. It is of type `C`, which
|
|
1528
|
+
* extends `BTNCallback<OptBTNOrNull<NODE>>`. The default value for this parameter is `this._DEFAULT
|
|
1492
1529
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
1493
|
-
* order in which the Depth-First Search
|
|
1494
|
-
*
|
|
1495
|
-
* @param {
|
|
1530
|
+
* order in which the nodes are visited during the Depth-First Search traversal. It can have one of
|
|
1531
|
+
* the following values:
|
|
1532
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `_dfs`
|
|
1496
1533
|
* method is used to specify the starting point for the depth-first search traversal in a binary
|
|
1497
|
-
* tree. It can be provided as either
|
|
1498
|
-
*
|
|
1534
|
+
* tree. It can be provided as either a `BTNKeyOrNodeOrEntry` object or a reference to the root node
|
|
1535
|
+
* of the tree. If no specific
|
|
1499
1536
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
|
|
1500
|
-
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal
|
|
1501
|
-
* can have two possible values:
|
|
1537
|
+
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
|
|
1538
|
+
* binary tree. It can have two possible values:
|
|
1502
1539
|
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag
|
|
1503
1540
|
* that determines whether null nodes should be included in the depth-first search traversal. If
|
|
1504
|
-
* `includeNull` is set to `true`,
|
|
1505
|
-
*
|
|
1541
|
+
* `includeNull` is set to `true`, null nodes will be considered during the traversal process. If it
|
|
1542
|
+
* is set to `false`,
|
|
1506
1543
|
* @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input
|
|
1507
1544
|
* and returns a boolean value. It is used to determine whether the left child of a node should be
|
|
1508
1545
|
* visited during the depth-first search traversal. By default, it checks if the node is truthy (not
|
|
1509
1546
|
* null or undefined
|
|
1510
|
-
* @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as
|
|
1511
|
-
*
|
|
1547
|
+
* @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as an
|
|
1548
|
+
* argument and returns a boolean value. It is used to determine whether the right child of a node
|
|
1512
1549
|
* should be visited during the depth-first search traversal. The default implementation checks if
|
|
1513
|
-
* the node is truthy before visiting the right child
|
|
1550
|
+
* the node is truthy before visiting the right child
|
|
1514
1551
|
* @param shouldVisitRoot - The `shouldVisitRoot` parameter is a function that takes a node as an
|
|
1515
|
-
* argument and returns a boolean value. It is used to determine whether
|
|
1552
|
+
* argument and returns a boolean value. It is used to determine whether the root node should be
|
|
1516
1553
|
* visited during the depth-first search traversal based on certain conditions. The default
|
|
1517
1554
|
* implementation checks if the node is a real node or null based
|
|
1518
|
-
* @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as
|
|
1519
|
-
*
|
|
1520
|
-
* depth-first search traversal. The default implementation
|
|
1521
|
-
*
|
|
1522
|
-
* @returns The `_dfs`
|
|
1555
|
+
* @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as an
|
|
1556
|
+
* argument and returns a boolean value indicating whether the node should be processed during the
|
|
1557
|
+
* depth-first search traversal. The default implementation checks if the node is a real node or null
|
|
1558
|
+
* based on the `includeNull` flag. If `
|
|
1559
|
+
* @returns The function `_dfs` returns an array of the return type of the callback function provided
|
|
1523
1560
|
* as input.
|
|
1524
1561
|
*/
|
|
1525
|
-
_dfs(callback = this.
|
|
1562
|
+
_dfs(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
|
|
1526
1563
|
if (includeNull)
|
|
1527
1564
|
return this.isRealNodeOrNull(node);
|
|
1528
1565
|
return this.isRealNode(node);
|
|
1529
|
-
}, shouldProcessRoot = node =>
|
|
1566
|
+
}, shouldProcessRoot = node => this.isRealNodeOrNull(node)) {
|
|
1530
1567
|
beginRoot = this.ensureNode(beginRoot);
|
|
1531
1568
|
if (!beginRoot)
|
|
1532
1569
|
return [];
|
|
@@ -1567,20 +1604,20 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1567
1604
|
dfs(beginRoot);
|
|
1568
1605
|
}
|
|
1569
1606
|
else {
|
|
1570
|
-
const stack = [{ opt:
|
|
1607
|
+
const stack = [{ opt: constants_1.DFSOperation.VISIT, node: beginRoot }];
|
|
1571
1608
|
const pushLeft = (cur) => {
|
|
1572
1609
|
var _a;
|
|
1573
1610
|
if (shouldVisitLeft(cur.node))
|
|
1574
|
-
stack.push({ opt:
|
|
1611
|
+
stack.push({ opt: constants_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.left });
|
|
1575
1612
|
};
|
|
1576
1613
|
const pushRight = (cur) => {
|
|
1577
1614
|
var _a;
|
|
1578
1615
|
if (shouldVisitRight(cur.node))
|
|
1579
|
-
stack.push({ opt:
|
|
1616
|
+
stack.push({ opt: constants_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.right });
|
|
1580
1617
|
};
|
|
1581
1618
|
const pushRoot = (cur) => {
|
|
1582
1619
|
if (shouldVisitRoot(cur.node))
|
|
1583
|
-
stack.push({ opt:
|
|
1620
|
+
stack.push({ opt: constants_1.DFSOperation.PROCESS, node: cur.node });
|
|
1584
1621
|
};
|
|
1585
1622
|
while (stack.length > 0) {
|
|
1586
1623
|
const cur = stack.pop();
|
|
@@ -1588,7 +1625,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1588
1625
|
continue;
|
|
1589
1626
|
if (!shouldVisitRoot(cur.node))
|
|
1590
1627
|
continue;
|
|
1591
|
-
if (cur.opt ===
|
|
1628
|
+
if (cur.opt === constants_1.DFSOperation.PROCESS) {
|
|
1592
1629
|
if (shouldProcessRoot(cur.node))
|
|
1593
1630
|
ans.push(callback(cur.node));
|
|
1594
1631
|
}
|
|
@@ -1619,13 +1656,18 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1619
1656
|
* Time Complexity: O(1)
|
|
1620
1657
|
* Space Complexity: O(1)
|
|
1621
1658
|
*
|
|
1622
|
-
* The function `_getIterator`
|
|
1623
|
-
*
|
|
1624
|
-
* @param node - The `node` parameter
|
|
1625
|
-
* initially set to the root node of the
|
|
1626
|
-
*
|
|
1659
|
+
* The function `_getIterator` returns an iterable iterator for a binary tree data structure, either
|
|
1660
|
+
* using an iterative approach or a recursive approach based on the specified iteration type.
|
|
1661
|
+
* @param node - The `node` parameter in the `_getIterator` method represents the current node being
|
|
1662
|
+
* processed during iteration. It is initially set to the root node of the data structure (or the
|
|
1663
|
+
* node passed as an argument), and then it is traversed through the data structure based on the
|
|
1664
|
+
* iteration type specified (`ITER
|
|
1665
|
+
* @returns The `_getIterator` method returns an IterableIterator containing key-value pairs of nodes
|
|
1666
|
+
* in a binary tree structure. The method uses an iterative approach to traverse the tree based on
|
|
1667
|
+
* the `iterationType` property. If the `iterationType` is set to 'ITERATIVE', the method uses a
|
|
1668
|
+
* stack to perform an in-order traversal of the tree. If the `iterationType` is not 'ITERATIVE
|
|
1627
1669
|
*/
|
|
1628
|
-
*_getIterator(node = this.
|
|
1670
|
+
*_getIterator(node = this._root) {
|
|
1629
1671
|
if (!node)
|
|
1630
1672
|
return;
|
|
1631
1673
|
if (this.iterationType === 'ITERATIVE') {
|
|
@@ -1657,18 +1699,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1657
1699
|
* Time Complexity: O(n)
|
|
1658
1700
|
* Space Complexity: O(n)
|
|
1659
1701
|
*
|
|
1660
|
-
* The `_displayAux`
|
|
1661
|
-
*
|
|
1662
|
-
* @param
|
|
1663
|
-
* It can be
|
|
1664
|
-
*
|
|
1665
|
-
*
|
|
1666
|
-
*
|
|
1667
|
-
*
|
|
1668
|
-
*
|
|
1669
|
-
*
|
|
1670
|
-
* 3. `totalHeight`: The total height of the node display.
|
|
1671
|
-
* 4. `middleIndex`: The index of the middle character
|
|
1702
|
+
* The function `_displayAux` in TypeScript is responsible for generating the display layout of nodes
|
|
1703
|
+
* in a binary tree based on specified options.
|
|
1704
|
+
* @param node - The `node` parameter in the `_displayAux` function represents a node in a binary
|
|
1705
|
+
* tree. It can be either a valid node containing a key or a special type of node like null,
|
|
1706
|
+
* undefined, or a Red-Black tree NIL node. The function checks the type of the node and its
|
|
1707
|
+
* @param {BinaryTreePrintOptions} options - The `options` parameter in the `_displayAux` function
|
|
1708
|
+
* contains the following properties:
|
|
1709
|
+
* @returns The `_displayAux` function returns a `NodeDisplayLayout`, which is an array containing
|
|
1710
|
+
* information about how to display a node in a binary tree. The `NodeDisplayLayout` consists of four
|
|
1711
|
+
* elements:
|
|
1672
1712
|
*/
|
|
1673
1713
|
_displayAux(node, options) {
|
|
1674
1714
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
@@ -1726,14 +1766,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1726
1766
|
* Time Complexity: O(1)
|
|
1727
1767
|
* Space Complexity: O(1)
|
|
1728
1768
|
*
|
|
1729
|
-
* The function
|
|
1730
|
-
* @param {
|
|
1731
|
-
*
|
|
1732
|
-
*
|
|
1733
|
-
* @param {
|
|
1734
|
-
* the properties will be swapped with
|
|
1735
|
-
*
|
|
1736
|
-
*
|
|
1769
|
+
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
1770
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} srcNode - The `srcNode` parameter in the
|
|
1771
|
+
* `_swapProperties` method can be either a BTNKeyOrNodeOrEntry object containing key and value
|
|
1772
|
+
* properties, or it can be of type R.
|
|
1773
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} destNode - The `destNode` parameter in the
|
|
1774
|
+
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
1775
|
+
* the `srcNode`. It can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. The method ensures that
|
|
1776
|
+
* both `srcNode
|
|
1777
|
+
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
1778
|
+
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
1737
1779
|
*/
|
|
1738
1780
|
_swapProperties(srcNode, destNode) {
|
|
1739
1781
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -1755,13 +1797,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1755
1797
|
* Time Complexity: O(1)
|
|
1756
1798
|
* Space Complexity: O(1)
|
|
1757
1799
|
*
|
|
1758
|
-
* The function replaces
|
|
1759
|
-
*
|
|
1760
|
-
*
|
|
1761
|
-
*
|
|
1762
|
-
*
|
|
1763
|
-
*
|
|
1764
|
-
*
|
|
1800
|
+
* The _replaceNode function replaces an old node with a new node in a binary tree structure.
|
|
1801
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that you want to replace in a
|
|
1802
|
+
* tree data structure.
|
|
1803
|
+
* @param {NODE} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
|
|
1804
|
+
* that will replace the `oldNode` in a tree data structure. This function is responsible for
|
|
1805
|
+
* updating the parent, left child, right child, and root (if necessary) references when replacing a
|
|
1806
|
+
* node in the tree.
|
|
1807
|
+
* @returns The method `_replaceNode` is returning the `newNode` that was passed as a parameter after
|
|
1808
|
+
* replacing the `oldNode` with it in the binary tree structure.
|
|
1765
1809
|
*/
|
|
1766
1810
|
_replaceNode(oldNode, newNode) {
|
|
1767
1811
|
if (oldNode.parent) {
|
|
@@ -1775,8 +1819,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1775
1819
|
newNode.left = oldNode.left;
|
|
1776
1820
|
newNode.right = oldNode.right;
|
|
1777
1821
|
newNode.parent = oldNode.parent;
|
|
1778
|
-
if (this.
|
|
1779
|
-
this.
|
|
1822
|
+
if (this._root === oldNode) {
|
|
1823
|
+
this._setRoot(newNode);
|
|
1780
1824
|
}
|
|
1781
1825
|
return newNode;
|
|
1782
1826
|
}
|
|
@@ -1784,10 +1828,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1784
1828
|
* Time Complexity: O(1)
|
|
1785
1829
|
* Space Complexity: O(1)
|
|
1786
1830
|
*
|
|
1787
|
-
* The function sets the root
|
|
1788
|
-
*
|
|
1789
|
-
* @param
|
|
1790
|
-
*
|
|
1831
|
+
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
1832
|
+
* of the previous root node.
|
|
1833
|
+
* @param v - The parameter `v` in the `_setRoot` method is of type `OptBTNOrNull<NODE>`, which means
|
|
1834
|
+
* it can either be an optional `NODE` type or `null`.
|
|
1791
1835
|
*/
|
|
1792
1836
|
_setRoot(v) {
|
|
1793
1837
|
if (v) {
|
|
@@ -1799,20 +1843,47 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1799
1843
|
* Time Complexity: O(1)
|
|
1800
1844
|
* Space Complexity: O(1)
|
|
1801
1845
|
*
|
|
1802
|
-
* The function `
|
|
1803
|
-
*
|
|
1804
|
-
*
|
|
1805
|
-
* the
|
|
1806
|
-
*
|
|
1807
|
-
*
|
|
1808
|
-
*
|
|
1809
|
-
* @returns the callback parameter.
|
|
1846
|
+
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
1847
|
+
* predicate function for a binary tree node.
|
|
1848
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrEntryOrRawOrPredicate - The
|
|
1849
|
+
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
1850
|
+
* parameter `keyOrEntryOrRawOrPredicate` is transformed into a valid predicate function that can be
|
|
1851
|
+
* used for filtering nodes in a binary tree.
|
|
1852
|
+
* @returns A BTNPredicate<NODE> function is being returned.
|
|
1810
1853
|
*/
|
|
1811
|
-
|
|
1812
|
-
if (
|
|
1813
|
-
|
|
1854
|
+
_ensurePredicate(keyOrEntryOrRawOrPredicate) {
|
|
1855
|
+
if (keyOrEntryOrRawOrPredicate === null || keyOrEntryOrRawOrPredicate === undefined)
|
|
1856
|
+
return (node) => (node ? false : false);
|
|
1857
|
+
if (this._isPredicated(keyOrEntryOrRawOrPredicate))
|
|
1858
|
+
return keyOrEntryOrRawOrPredicate;
|
|
1859
|
+
if (this.isRealNode(keyOrEntryOrRawOrPredicate))
|
|
1860
|
+
return (node) => node === keyOrEntryOrRawOrPredicate;
|
|
1861
|
+
if (this.isEntry(keyOrEntryOrRawOrPredicate)) {
|
|
1862
|
+
const [key] = keyOrEntryOrRawOrPredicate;
|
|
1863
|
+
return (node) => node.key === key;
|
|
1814
1864
|
}
|
|
1815
|
-
|
|
1865
|
+
if (this.isKey(keyOrEntryOrRawOrPredicate))
|
|
1866
|
+
return (node) => node.key === keyOrEntryOrRawOrPredicate;
|
|
1867
|
+
if (this._toEntryFn) {
|
|
1868
|
+
const [key] = this._toEntryFn(keyOrEntryOrRawOrPredicate);
|
|
1869
|
+
return (node) => node.key === key;
|
|
1870
|
+
}
|
|
1871
|
+
return (node) => node.key === keyOrEntryOrRawOrPredicate;
|
|
1872
|
+
}
|
|
1873
|
+
/**
|
|
1874
|
+
* Time Complexity: O(1)
|
|
1875
|
+
* Space Complexity: O(1)
|
|
1876
|
+
*
|
|
1877
|
+
* The function `_isPredicated` checks if a given parameter is a function.
|
|
1878
|
+
* @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
|
|
1879
|
+
* of value. In this context, the function `_isPredicated` is checking if `p` is a function that
|
|
1880
|
+
* satisfies the type `BTNPredicate<NODE>`.
|
|
1881
|
+
* @returns The function is checking if the input `p` is a function and returning a boolean value
|
|
1882
|
+
* based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
|
|
1883
|
+
* predicate function for a binary tree node. If `p` is not a function, it will return `false`.
|
|
1884
|
+
*/
|
|
1885
|
+
_isPredicated(p) {
|
|
1886
|
+
return typeof p === 'function';
|
|
1816
1887
|
}
|
|
1817
1888
|
}
|
|
1818
1889
|
exports.BinaryTree = BinaryTree;
|