heap-typed 1.49.5 → 1.49.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +194 -153
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +114 -91
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/data-structures/hash/hash-map.js +35 -35
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -1
- package/dist/data-structures/heap/heap.d.ts +2 -1
- package/dist/data-structures/heap/heap.js +13 -13
- package/dist/data-structures/heap/max-heap.js +1 -1
- package/dist/data-structures/heap/min-heap.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/data-structures/matrix/matrix.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/data-structures/queue/deque.d.ts +2 -11
- package/dist/data-structures/queue/deque.js +9 -13
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +29 -25
- package/dist/data-structures/stack/stack.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +9 -5
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/index.js +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/types/data-structures/linked-list/index.js +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +1 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +255 -211
- package/src/data-structures/binary-tree/bst.ts +126 -107
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/data-structures/hash/hash-table.js +0 -281
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/hash-table.js +0 -2
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/matrix2d.js +0 -2
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/vector2d.js +0 -2
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
|
@@ -68,31 +68,29 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
68
68
|
*/
|
|
69
69
|
class BinaryTree extends base_1.IterableEntryBase {
|
|
70
70
|
/**
|
|
71
|
-
* The constructor function initializes a binary tree object with optional
|
|
72
|
-
* @param [
|
|
73
|
-
*
|
|
71
|
+
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
|
|
72
|
+
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
73
|
+
* nodes to be added to the binary tree.
|
|
74
74
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
75
75
|
* configuration options for the binary tree. In this case, it is of type
|
|
76
76
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
77
77
|
* required.
|
|
78
78
|
*/
|
|
79
|
-
constructor(
|
|
79
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
80
80
|
super();
|
|
81
81
|
this.iterationType = types_1.IterationType.ITERATIVE;
|
|
82
82
|
this._extractor = (key) => Number(key);
|
|
83
83
|
this._defaultOneParamCallback = (node) => (node ? node.key : undefined);
|
|
84
84
|
if (options) {
|
|
85
85
|
const { iterationType, extractor } = options;
|
|
86
|
-
if (iterationType)
|
|
86
|
+
if (iterationType)
|
|
87
87
|
this.iterationType = iterationType;
|
|
88
|
-
|
|
89
|
-
if (extractor) {
|
|
88
|
+
if (extractor)
|
|
90
89
|
this._extractor = extractor;
|
|
91
|
-
}
|
|
92
90
|
}
|
|
93
91
|
this._size = 0;
|
|
94
|
-
if (
|
|
95
|
-
this.addMany(
|
|
92
|
+
if (keysOrNodesOrEntries)
|
|
93
|
+
this.addMany(keysOrNodesOrEntries);
|
|
96
94
|
}
|
|
97
95
|
get extractor() {
|
|
98
96
|
return this._extractor;
|
|
@@ -123,30 +121,22 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
123
121
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
124
122
|
}
|
|
125
123
|
/**
|
|
126
|
-
* The function
|
|
127
|
-
* @param
|
|
128
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
129
|
-
*/
|
|
130
|
-
isNode(exemplar) {
|
|
131
|
-
return exemplar instanceof BinaryTreeNode;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
135
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
124
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
125
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
136
126
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
137
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
127
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
|
|
138
128
|
* is provided, it will be `undefined`.
|
|
139
129
|
* @returns a value of type N (node), or null, or undefined.
|
|
140
130
|
*/
|
|
141
|
-
exemplarToNode(
|
|
142
|
-
if (
|
|
131
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
132
|
+
if (keyOrNodeOrEntry === undefined)
|
|
143
133
|
return;
|
|
144
134
|
let node;
|
|
145
|
-
if (
|
|
135
|
+
if (keyOrNodeOrEntry === null) {
|
|
146
136
|
node = null;
|
|
147
137
|
}
|
|
148
|
-
else if (this.isEntry(
|
|
149
|
-
const [key, value] =
|
|
138
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
139
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
150
140
|
if (key === undefined) {
|
|
151
141
|
return;
|
|
152
142
|
}
|
|
@@ -157,25 +147,108 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
157
147
|
node = this.createNode(key, value);
|
|
158
148
|
}
|
|
159
149
|
}
|
|
160
|
-
else if (this.isNode(
|
|
161
|
-
node =
|
|
150
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
151
|
+
node = keyOrNodeOrEntry;
|
|
162
152
|
}
|
|
163
|
-
else if (this.isNotNodeInstance(
|
|
164
|
-
node = this.createNode(
|
|
153
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
154
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
165
155
|
}
|
|
166
156
|
else {
|
|
167
157
|
return;
|
|
168
158
|
}
|
|
169
159
|
return node;
|
|
170
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Time Complexity: O(n)
|
|
163
|
+
* Space Complexity: O(log n)
|
|
164
|
+
*/
|
|
165
|
+
/**
|
|
166
|
+
* Time Complexity: O(n)
|
|
167
|
+
* Space Complexity: O(log n)
|
|
168
|
+
*
|
|
169
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
170
|
+
* key, otherwise it returns the key itself.
|
|
171
|
+
* @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`,
|
|
172
|
+
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
173
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
174
|
+
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
175
|
+
* `IterationType.ITERATIVE`.
|
|
176
|
+
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
177
|
+
* itself if it is not a valid node key.
|
|
178
|
+
*/
|
|
179
|
+
ensureNode(keyOrNodeOrEntry, iterationType = types_1.IterationType.ITERATIVE) {
|
|
180
|
+
let res;
|
|
181
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
182
|
+
res = keyOrNodeOrEntry;
|
|
183
|
+
}
|
|
184
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
185
|
+
if (keyOrNodeOrEntry[0] === null)
|
|
186
|
+
res = null;
|
|
187
|
+
else if (keyOrNodeOrEntry[0] !== undefined)
|
|
188
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
if (keyOrNodeOrEntry === null)
|
|
192
|
+
res = null;
|
|
193
|
+
else if (keyOrNodeOrEntry !== undefined)
|
|
194
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
195
|
+
}
|
|
196
|
+
return res;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
200
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,N>`.
|
|
201
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N.
|
|
202
|
+
*/
|
|
203
|
+
isNode(keyOrNodeOrEntry) {
|
|
204
|
+
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
205
|
+
}
|
|
171
206
|
/**
|
|
172
207
|
* The function checks if a given value is an entry in a binary tree node.
|
|
173
|
-
* @param
|
|
208
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,N> - A generic type representing a node in a binary tree. It has
|
|
174
209
|
* two type parameters V and N, representing the value and node type respectively.
|
|
175
210
|
* @returns a boolean value.
|
|
176
211
|
*/
|
|
177
|
-
isEntry(
|
|
178
|
-
return Array.isArray(
|
|
212
|
+
isEntry(keyOrNodeOrEntry) {
|
|
213
|
+
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Time complexity: O(n)
|
|
217
|
+
* Space complexity: O(log n)
|
|
218
|
+
*/
|
|
219
|
+
/**
|
|
220
|
+
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
221
|
+
* BinaryTreeNode and its key is not NaN.
|
|
222
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
223
|
+
* @returns a boolean value.
|
|
224
|
+
*/
|
|
225
|
+
isRealNode(node) {
|
|
226
|
+
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
230
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
231
|
+
* @returns a boolean value.
|
|
232
|
+
*/
|
|
233
|
+
isNIL(node) {
|
|
234
|
+
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* The function checks if a given node is a real node or null.
|
|
238
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
239
|
+
* @returns a boolean value.
|
|
240
|
+
*/
|
|
241
|
+
isNodeOrNull(node) {
|
|
242
|
+
return this.isRealNode(node) || node === null;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
246
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
247
|
+
* data type.
|
|
248
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
249
|
+
*/
|
|
250
|
+
isNotNodeInstance(potentialKey) {
|
|
251
|
+
return !(potentialKey instanceof BinaryTreeNode);
|
|
179
252
|
}
|
|
180
253
|
/**
|
|
181
254
|
* Time Complexity O(log n) - O(n)
|
|
@@ -194,12 +267,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
194
267
|
add(keyOrNodeOrEntry, value) {
|
|
195
268
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
196
269
|
if (newNode === undefined)
|
|
197
|
-
return;
|
|
270
|
+
return false;
|
|
198
271
|
// If the tree is empty, directly set the new node as the root node
|
|
199
272
|
if (!this.root) {
|
|
200
273
|
this._root = newNode;
|
|
201
274
|
this._size = 1;
|
|
202
|
-
return
|
|
275
|
+
return true;
|
|
203
276
|
}
|
|
204
277
|
const queue = new queue_1.Queue([this.root]);
|
|
205
278
|
let potentialParent; // Record the parent node of the potential insertion location
|
|
@@ -210,7 +283,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
210
283
|
// Check for duplicate keys when newNode is not null
|
|
211
284
|
if (newNode !== null && cur.key === newNode.key) {
|
|
212
285
|
this._replaceNode(cur, newNode);
|
|
213
|
-
return
|
|
286
|
+
return true; // If duplicate keys are found, no insertion is performed
|
|
214
287
|
}
|
|
215
288
|
// Record the first possible insertion location found
|
|
216
289
|
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
@@ -233,9 +306,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
233
306
|
potentialParent.right = newNode;
|
|
234
307
|
}
|
|
235
308
|
this._size++;
|
|
236
|
-
return
|
|
309
|
+
return true;
|
|
237
310
|
}
|
|
238
|
-
return
|
|
311
|
+
return false; // If the insertion position cannot be found, return undefined
|
|
239
312
|
}
|
|
240
313
|
/**
|
|
241
314
|
* Time Complexity: O(k log n) - O(k * n)
|
|
@@ -246,20 +319,20 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
246
319
|
* Time Complexity: O(k log n) - O(k * n)
|
|
247
320
|
* Space Complexity: O(1)
|
|
248
321
|
*
|
|
249
|
-
* The `addMany` function takes in a collection of
|
|
322
|
+
* The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
|
|
250
323
|
* adds each node with its corresponding value to the data structure.
|
|
251
|
-
* @param
|
|
324
|
+
* @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
|
|
252
325
|
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
253
326
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
254
327
|
*/
|
|
255
|
-
addMany(
|
|
328
|
+
addMany(keysOrNodesOrEntries, values) {
|
|
256
329
|
// TODO not sure addMany not be run multi times
|
|
257
330
|
const inserted = [];
|
|
258
331
|
let valuesIterator;
|
|
259
332
|
if (values) {
|
|
260
333
|
valuesIterator = values[Symbol.iterator]();
|
|
261
334
|
}
|
|
262
|
-
for (const
|
|
335
|
+
for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
|
|
263
336
|
let value = undefined;
|
|
264
337
|
if (valuesIterator) {
|
|
265
338
|
const valueResult = valuesIterator.next();
|
|
@@ -267,17 +340,30 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
267
340
|
value = valueResult.value;
|
|
268
341
|
}
|
|
269
342
|
}
|
|
270
|
-
inserted.push(this.add(
|
|
343
|
+
inserted.push(this.add(keyOrNodeOrEntry, value));
|
|
271
344
|
}
|
|
272
345
|
return inserted;
|
|
273
346
|
}
|
|
274
347
|
/**
|
|
275
|
-
* Time Complexity: O(k * n)
|
|
348
|
+
* Time Complexity: O(k * n)
|
|
276
349
|
* Space Complexity: O(1)
|
|
350
|
+
* "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
277
351
|
*/
|
|
278
|
-
|
|
352
|
+
/**
|
|
353
|
+
* Time Complexity: O(k * n)
|
|
354
|
+
* Space Complexity: O(1)
|
|
355
|
+
*
|
|
356
|
+
* The `refill` function clears the current data and adds new key-value pairs to the data structure.
|
|
357
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
|
|
358
|
+
* KeyOrNodeOrEntry<K, V, N>.
|
|
359
|
+
* @param [values] - The `values` parameter is an optional iterable that contains the values to be
|
|
360
|
+
* associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
|
|
361
|
+
* the values will be associated with the corresponding keys or nodes or entries in the
|
|
362
|
+
* `keysOrNodesOrEntries` iterable
|
|
363
|
+
*/
|
|
364
|
+
refill(keysOrNodesOrEntries, values) {
|
|
279
365
|
this.clear();
|
|
280
|
-
this.addMany(
|
|
366
|
+
this.addMany(keysOrNodesOrEntries, values);
|
|
281
367
|
}
|
|
282
368
|
/**
|
|
283
369
|
* Time Complexity: O(n)
|
|
@@ -351,24 +437,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
351
437
|
* Space Complexity: O(1)
|
|
352
438
|
*
|
|
353
439
|
* The function calculates the depth of a given node in a binary tree.
|
|
354
|
-
* @param {K | N | null | undefined}
|
|
440
|
+
* @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
|
|
355
441
|
* the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
|
|
356
442
|
* `undefined`.
|
|
357
443
|
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
358
444
|
* from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
|
|
359
445
|
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
360
|
-
* @returns the depth of the `
|
|
446
|
+
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
361
447
|
*/
|
|
362
|
-
getDepth(
|
|
363
|
-
|
|
448
|
+
getDepth(dist, beginRoot = this.root) {
|
|
449
|
+
dist = this.ensureNode(dist);
|
|
364
450
|
beginRoot = this.ensureNode(beginRoot);
|
|
365
451
|
let depth = 0;
|
|
366
|
-
while (
|
|
367
|
-
if (
|
|
452
|
+
while (dist === null || dist === void 0 ? void 0 : dist.parent) {
|
|
453
|
+
if (dist === beginRoot) {
|
|
368
454
|
return depth;
|
|
369
455
|
}
|
|
370
456
|
depth++;
|
|
371
|
-
|
|
457
|
+
dist = dist.parent;
|
|
372
458
|
}
|
|
373
459
|
return depth;
|
|
374
460
|
}
|
|
@@ -565,6 +651,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
565
651
|
}
|
|
566
652
|
/**
|
|
567
653
|
* Time Complexity: O(n)
|
|
654
|
+
* Space Complexity: O(log n).
|
|
568
655
|
*
|
|
569
656
|
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
570
657
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
@@ -662,24 +749,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
662
749
|
}
|
|
663
750
|
}
|
|
664
751
|
}
|
|
665
|
-
/**
|
|
666
|
-
* Time Complexity: O(n)
|
|
667
|
-
* Space Complexity: O(log n)
|
|
668
|
-
*/
|
|
669
|
-
/**
|
|
670
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
671
|
-
* key, otherwise it returns the key itself.
|
|
672
|
-
* @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
|
|
673
|
-
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
674
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
675
|
-
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
676
|
-
* `IterationType.ITERATIVE`.
|
|
677
|
-
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
678
|
-
* itself if it is not a valid node key.
|
|
679
|
-
*/
|
|
680
|
-
ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
681
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
682
|
-
}
|
|
683
752
|
/**
|
|
684
753
|
* Time Complexity: O(n)
|
|
685
754
|
* Space Complexity: O(log n)
|
|
@@ -709,10 +778,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
709
778
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined;
|
|
710
779
|
}
|
|
711
780
|
/**
|
|
712
|
-
* Time Complexity: O(
|
|
713
|
-
* Space Complexity: O(
|
|
781
|
+
* Time Complexity: O(1)
|
|
782
|
+
* Space Complexity: O(1)
|
|
714
783
|
*/
|
|
715
784
|
/**
|
|
785
|
+
* Time Complexity: O(1)
|
|
786
|
+
* Space Complexity: O(1)
|
|
787
|
+
*
|
|
716
788
|
* Clear the binary tree, removing all nodes.
|
|
717
789
|
*/
|
|
718
790
|
clear() {
|
|
@@ -720,6 +792,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
720
792
|
this._size = 0;
|
|
721
793
|
}
|
|
722
794
|
/**
|
|
795
|
+
* Time Complexity: O(1)
|
|
796
|
+
* Space Complexity: O(1)
|
|
797
|
+
*/
|
|
798
|
+
/**
|
|
799
|
+
* Time Complexity: O(1)
|
|
800
|
+
* Space Complexity: O(1)
|
|
801
|
+
*
|
|
723
802
|
* Check if the binary tree is empty.
|
|
724
803
|
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
725
804
|
*/
|
|
@@ -757,7 +836,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
757
836
|
}
|
|
758
837
|
/**
|
|
759
838
|
* Time Complexity: O(log n)
|
|
760
|
-
* Space Complexity: O(
|
|
839
|
+
* Space Complexity: O(1)
|
|
761
840
|
*/
|
|
762
841
|
/**
|
|
763
842
|
* Time Complexity: O(log n)
|
|
@@ -853,7 +932,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
853
932
|
* possible values:
|
|
854
933
|
* @returns a boolean value.
|
|
855
934
|
*/
|
|
856
|
-
|
|
935
|
+
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
857
936
|
// TODO there is a bug
|
|
858
937
|
beginRoot = this.ensureNode(beginRoot);
|
|
859
938
|
if (!beginRoot)
|
|
@@ -867,46 +946,34 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
867
946
|
return false;
|
|
868
947
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
869
948
|
};
|
|
870
|
-
|
|
949
|
+
const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
950
|
+
const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
951
|
+
return isStandardBST || isInverseBST;
|
|
871
952
|
}
|
|
872
953
|
else {
|
|
873
|
-
const
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
954
|
+
const checkBST = (checkMax = false) => {
|
|
955
|
+
const stack = [];
|
|
956
|
+
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
957
|
+
// @ts-ignore
|
|
958
|
+
let curr = beginRoot;
|
|
959
|
+
while (curr || stack.length > 0) {
|
|
960
|
+
while (curr) {
|
|
961
|
+
stack.push(curr);
|
|
962
|
+
curr = curr.left;
|
|
963
|
+
}
|
|
964
|
+
curr = stack.pop();
|
|
965
|
+
const numKey = this.extractor(curr.key);
|
|
966
|
+
if (!curr || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
|
|
967
|
+
return false;
|
|
968
|
+
prev = numKey;
|
|
969
|
+
curr = curr.right;
|
|
879
970
|
}
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
prev = numKey;
|
|
885
|
-
curr = curr.right;
|
|
886
|
-
}
|
|
887
|
-
return true;
|
|
971
|
+
return true;
|
|
972
|
+
};
|
|
973
|
+
const isStandardBST = checkBST(false), isInverseBST = checkBST(true);
|
|
974
|
+
return isStandardBST || isInverseBST;
|
|
888
975
|
}
|
|
889
976
|
}
|
|
890
|
-
/**
|
|
891
|
-
* Time Complexity: O(n)
|
|
892
|
-
* Space Complexity: O(1)
|
|
893
|
-
*/
|
|
894
|
-
/**
|
|
895
|
-
* Time Complexity: O(n)
|
|
896
|
-
* Space Complexity: O(1)
|
|
897
|
-
*
|
|
898
|
-
* The function checks if a binary tree is a binary search tree.
|
|
899
|
-
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
900
|
-
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
901
|
-
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
902
|
-
* expected to be
|
|
903
|
-
* @returns a boolean value.
|
|
904
|
-
*/
|
|
905
|
-
isBST(iterationType = this.iterationType) {
|
|
906
|
-
if (this.root === null)
|
|
907
|
-
return true;
|
|
908
|
-
return this.isSubtreeBST(this.root, iterationType);
|
|
909
|
-
}
|
|
910
977
|
/**
|
|
911
978
|
* Time complexity: O(n)
|
|
912
979
|
* Space complexity: O(log n)
|
|
@@ -926,7 +993,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
926
993
|
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
927
994
|
* traversal will include null values, otherwise it will skip them.
|
|
928
995
|
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
929
|
-
* the `callback` function on each node in the subtree. The type of the array
|
|
996
|
+
* the `callback` function on each node in the subtree. The type of the array nodes is determined
|
|
930
997
|
* by the return type of the `callback` function.
|
|
931
998
|
*/
|
|
932
999
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
@@ -969,44 +1036,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
969
1036
|
}
|
|
970
1037
|
return ans;
|
|
971
1038
|
}
|
|
972
|
-
/**
|
|
973
|
-
* Time complexity: O(n)
|
|
974
|
-
* Space complexity: O(log n)
|
|
975
|
-
*/
|
|
976
|
-
/**
|
|
977
|
-
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
978
|
-
* BinaryTreeNode and its key is not NaN.
|
|
979
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
980
|
-
* @returns a boolean value.
|
|
981
|
-
*/
|
|
982
|
-
isRealNode(node) {
|
|
983
|
-
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
984
|
-
}
|
|
985
|
-
/**
|
|
986
|
-
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
987
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
988
|
-
* @returns a boolean value.
|
|
989
|
-
*/
|
|
990
|
-
isNIL(node) {
|
|
991
|
-
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
992
|
-
}
|
|
993
|
-
/**
|
|
994
|
-
* The function checks if a given node is a real node or null.
|
|
995
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
996
|
-
* @returns a boolean value.
|
|
997
|
-
*/
|
|
998
|
-
isNodeOrNull(node) {
|
|
999
|
-
return this.isRealNode(node) || node === null;
|
|
1000
|
-
}
|
|
1001
|
-
/**
|
|
1002
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
1003
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
1004
|
-
* data type.
|
|
1005
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
1006
|
-
*/
|
|
1007
|
-
isNotNodeInstance(potentialKey) {
|
|
1008
|
-
return !(potentialKey instanceof BinaryTreeNode);
|
|
1009
|
-
}
|
|
1010
1039
|
/**
|
|
1011
1040
|
* Time complexity: O(n)
|
|
1012
1041
|
* Space complexity: O(n)
|
|
@@ -1328,7 +1357,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1328
1357
|
}
|
|
1329
1358
|
/**
|
|
1330
1359
|
* Time complexity: O(n)
|
|
1331
|
-
* Space complexity: O(
|
|
1360
|
+
* Space complexity: O(n)
|
|
1361
|
+
*/
|
|
1362
|
+
/**
|
|
1363
|
+
* Time complexity: O(n)
|
|
1364
|
+
* Space complexity: O(n)
|
|
1365
|
+
*
|
|
1332
1366
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1333
1367
|
* algorithm.
|
|
1334
1368
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
@@ -1341,7 +1375,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1341
1375
|
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
1342
1376
|
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
1343
1377
|
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
1344
|
-
* `callback` function on each node in the binary tree. The type of the array
|
|
1378
|
+
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
1345
1379
|
* by the return type of the `callback` function.
|
|
1346
1380
|
*/
|
|
1347
1381
|
morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
|
|
@@ -1454,8 +1488,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1454
1488
|
* Time Complexity: O(n)
|
|
1455
1489
|
* Space Complexity: O(n)
|
|
1456
1490
|
*
|
|
1457
|
-
* The `filter` function creates a new tree by iterating over the
|
|
1458
|
-
* adding only the
|
|
1491
|
+
* The `filter` function creates a new tree by iterating over the nodes of the current tree and
|
|
1492
|
+
* adding only the nodes that satisfy the given predicate function.
|
|
1459
1493
|
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
1460
1494
|
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
1461
1495
|
* included in the filtered tree or not.
|
|
@@ -1512,6 +1546,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1512
1546
|
// // }
|
|
1513
1547
|
//
|
|
1514
1548
|
/**
|
|
1549
|
+
* Time Complexity: O(n)
|
|
1550
|
+
* Space Complexity: O(n)
|
|
1551
|
+
*/
|
|
1552
|
+
/**
|
|
1553
|
+
* Time Complexity: O(n)
|
|
1554
|
+
* Space Complexity: O(n)
|
|
1555
|
+
*
|
|
1515
1556
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1516
1557
|
* @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
|
|
1517
1558
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|