data-structure-typed 1.48.1 → 1.48.3
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/CHANGELOG.md +1 -1
- package/README.md +24 -18
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +172 -172
- package/dist/cjs/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/data-structures/base/index.js +18 -0
- package/dist/cjs/data-structures/base/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/cjs/data-structures/base/iterable-base.js +313 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +16 -16
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +121 -152
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +140 -182
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +28 -47
- package/dist/cjs/data-structures/binary-tree/bst.js +54 -57
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +15 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +22 -22
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +11 -11
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/cjs/data-structures/hash/hash-map.js +69 -173
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/cjs/data-structures/heap/heap.js +60 -30
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/index.d.ts +1 -0
- package/dist/cjs/data-structures/index.js +1 -0
- package/dist/cjs/data-structures/index.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/cjs/data-structures/queue/deque.js +36 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/cjs/data-structures/queue/queue.js +69 -82
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/cjs/data-structures/stack/stack.js +50 -31
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/cjs/data-structures/trie/trie.js +53 -32
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +6 -6
- package/dist/cjs/types/common.d.ts +11 -8
- package/dist/cjs/types/common.js +6 -1
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/cjs/types/data-structures/base/base.js +3 -0
- package/dist/cjs/types/data-structures/base/base.js.map +1 -0
- package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/base/index.js +18 -0
- package/dist/cjs/types/data-structures/base/index.js.map +1 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/cjs/types/data-structures/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/index.js +1 -0
- package/dist/cjs/types/data-structures/index.js.map +1 -1
- package/dist/mjs/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/data-structures/base/index.js +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/mjs/data-structures/base/iterable-base.js +307 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +16 -16
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +7 -7
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +121 -152
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +140 -182
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +28 -47
- package/dist/mjs/data-structures/binary-tree/bst.js +55 -57
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +15 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +7 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +22 -22
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +11 -11
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
- package/dist/mjs/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/mjs/data-structures/hash/hash-map.js +69 -173
- package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/mjs/data-structures/heap/heap.js +60 -30
- package/dist/mjs/data-structures/index.d.ts +1 -0
- package/dist/mjs/data-structures/index.js +1 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/mjs/data-structures/queue/deque.js +36 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/mjs/data-structures/queue/queue.js +66 -79
- package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/mjs/data-structures/stack/stack.js +50 -31
- package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/mjs/data-structures/trie/trie.js +53 -32
- package/dist/mjs/interfaces/binary-tree.d.ts +6 -6
- package/dist/mjs/types/common.d.ts +11 -8
- package/dist/mjs/types/common.js +5 -0
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/mjs/types/data-structures/base/base.js +1 -0
- package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/base/index.js +1 -0
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/mjs/types/data-structures/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/index.js +1 -0
- package/dist/umd/data-structure-typed.js +991 -848
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/avl-tree.ts +20 -21
- package/src/data-structures/binary-tree/binary-tree.ts +222 -267
- package/src/data-structures/binary-tree/bst.ts +86 -82
- package/src/data-structures/binary-tree/rb-tree.ts +25 -26
- package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +76 -185
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
- package/src/types/data-structures/index.ts +1 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +13 -13
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +16 -16
- package/test/unit/data-structures/binary-tree/bst.test.ts +20 -19
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +9 -9
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +15 -15
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
- package/test/unit/data-structures/queue/deque.test.ts +25 -0
- package/test/unit/unrestricted-interconversion.test.ts +1 -1
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { FamilyPosition, IterationType } from '../../types';
|
|
9
9
|
import { trampoline } from '../../utils';
|
|
10
10
|
import { Queue } from '../queue';
|
|
11
|
+
import { IterablePairBase } from "../base";
|
|
11
12
|
/**
|
|
12
13
|
* Represents a node in a binary tree.
|
|
13
14
|
* @template V - The type of data stored in the node.
|
|
@@ -70,7 +71,7 @@ export class BinaryTreeNode {
|
|
|
70
71
|
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
71
72
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
72
73
|
*/
|
|
73
|
-
export class BinaryTree {
|
|
74
|
+
export class BinaryTree extends IterablePairBase {
|
|
74
75
|
iterationType = IterationType.ITERATIVE;
|
|
75
76
|
/**
|
|
76
77
|
* The constructor function initializes a binary tree object with optional elements and options.
|
|
@@ -82,16 +83,24 @@ export class BinaryTree {
|
|
|
82
83
|
* required.
|
|
83
84
|
*/
|
|
84
85
|
constructor(elements, options) {
|
|
86
|
+
super();
|
|
85
87
|
if (options) {
|
|
86
|
-
const { iterationType } = options;
|
|
88
|
+
const { iterationType, extractor } = options;
|
|
87
89
|
if (iterationType) {
|
|
88
90
|
this.iterationType = iterationType;
|
|
89
91
|
}
|
|
92
|
+
if (extractor) {
|
|
93
|
+
this._extractor = extractor;
|
|
94
|
+
}
|
|
90
95
|
}
|
|
91
96
|
this._size = 0;
|
|
92
97
|
if (elements)
|
|
93
98
|
this.addMany(elements);
|
|
94
99
|
}
|
|
100
|
+
_extractor = (key) => Number(key);
|
|
101
|
+
get extractor() {
|
|
102
|
+
return this._extractor;
|
|
103
|
+
}
|
|
95
104
|
_root;
|
|
96
105
|
get root() {
|
|
97
106
|
return this._root;
|
|
@@ -102,7 +111,7 @@ export class BinaryTree {
|
|
|
102
111
|
}
|
|
103
112
|
/**
|
|
104
113
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
105
|
-
* @param {
|
|
114
|
+
* @param {K} key - The key for the new node.
|
|
106
115
|
* @param {V} value - The value for the new node.
|
|
107
116
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
108
117
|
*/
|
|
@@ -121,7 +130,7 @@ export class BinaryTree {
|
|
|
121
130
|
}
|
|
122
131
|
/**
|
|
123
132
|
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
124
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V,
|
|
133
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V,N>`.
|
|
125
134
|
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
126
135
|
*/
|
|
127
136
|
isNode(exemplar) {
|
|
@@ -130,7 +139,7 @@ export class BinaryTree {
|
|
|
130
139
|
/**
|
|
131
140
|
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
|
|
132
141
|
* object.
|
|
133
|
-
* @param exemplar - BTNodeExemplar<V,
|
|
142
|
+
* @param exemplar - BTNodeExemplar<K, V,N> - A generic type representing the exemplar parameter of the
|
|
134
143
|
* function. It can be any type.
|
|
135
144
|
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
|
|
136
145
|
*/
|
|
@@ -156,7 +165,7 @@ export class BinaryTree {
|
|
|
156
165
|
else if (this.isNode(exemplar)) {
|
|
157
166
|
node = exemplar;
|
|
158
167
|
}
|
|
159
|
-
else if (this.
|
|
168
|
+
else if (this.isNotNodeInstance(exemplar)) {
|
|
160
169
|
node = this.createNode(exemplar);
|
|
161
170
|
}
|
|
162
171
|
else {
|
|
@@ -166,7 +175,7 @@ export class BinaryTree {
|
|
|
166
175
|
}
|
|
167
176
|
/**
|
|
168
177
|
* The function checks if a given value is an entry in a binary tree node.
|
|
169
|
-
* @param kne - BTNodeExemplar<V,
|
|
178
|
+
* @param kne - BTNodeExemplar<K, V,N> - A generic type representing a node in a binary tree. It has
|
|
170
179
|
* two type parameters V and N, representing the value and node type respectively.
|
|
171
180
|
* @returns a boolean value.
|
|
172
181
|
*/
|
|
@@ -234,7 +243,7 @@ export class BinaryTree {
|
|
|
234
243
|
* The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
|
|
235
244
|
* current instance, and returns an array of the inserted nodes.
|
|
236
245
|
* @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
|
|
237
|
-
* `BTNodeExemplar<V,
|
|
246
|
+
* `BTNodeExemplar<K, V,N>` objects.
|
|
238
247
|
* @returns The function `addMany` returns an array of values, where each value is either of type
|
|
239
248
|
* `N`, `null`, or `undefined`.
|
|
240
249
|
*/
|
|
@@ -334,11 +343,11 @@ export class BinaryTree {
|
|
|
334
343
|
* Space Complexity: O(1)
|
|
335
344
|
*
|
|
336
345
|
* The function calculates the depth of a given node in a binary tree.
|
|
337
|
-
* @param {
|
|
338
|
-
* the binary tree whose depth we want to find. It can be of type `
|
|
346
|
+
* @param {K | N | null | undefined} distNode - The `distNode` parameter represents the node in
|
|
347
|
+
* the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
|
|
339
348
|
* `undefined`.
|
|
340
|
-
* @param {
|
|
341
|
-
* from which we want to calculate the depth. It can be either a `
|
|
349
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
350
|
+
* from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
|
|
342
351
|
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
343
352
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
344
353
|
*/
|
|
@@ -365,9 +374,9 @@ export class BinaryTree {
|
|
|
365
374
|
*
|
|
366
375
|
* The function `getHeight` calculates the maximum height of a binary tree using either recursive or
|
|
367
376
|
* iterative traversal.
|
|
368
|
-
* @param {
|
|
377
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
369
378
|
* starting node of the binary tree from which we want to calculate the height. It can be of type
|
|
370
|
-
* `
|
|
379
|
+
* `K`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
|
|
371
380
|
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
372
381
|
* height of the tree using a recursive approach or an iterative approach. It can have two possible
|
|
373
382
|
* values:
|
|
@@ -412,9 +421,9 @@ export class BinaryTree {
|
|
|
412
421
|
*
|
|
413
422
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
414
423
|
* recursive or iterative approach.
|
|
415
|
-
* @param {
|
|
424
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
416
425
|
* starting node of the binary tree from which we want to calculate the minimum height. It can be of
|
|
417
|
-
* type `
|
|
426
|
+
* type `K`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
|
|
418
427
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
419
428
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
420
429
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
@@ -474,8 +483,8 @@ export class BinaryTree {
|
|
|
474
483
|
*
|
|
475
484
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
476
485
|
* height of the tree.
|
|
477
|
-
* @param {
|
|
478
|
-
* for calculating the height and minimum height of a binary tree. It can be either a `
|
|
486
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
487
|
+
* for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
|
|
479
488
|
* value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
|
|
480
489
|
* @returns a boolean value.
|
|
481
490
|
*/
|
|
@@ -500,7 +509,7 @@ export class BinaryTree {
|
|
|
500
509
|
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
501
510
|
* node and return that node. If set to false (default), the function will continue iterating and
|
|
502
511
|
* return all nodes that match the identifier.
|
|
503
|
-
* @param {
|
|
512
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
504
513
|
* starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
|
|
505
514
|
* it is `null` or `undefined`, an empty array will be returned.
|
|
506
515
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
@@ -557,8 +566,8 @@ export class BinaryTree {
|
|
|
557
566
|
* the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
|
|
558
567
|
* function should return a boolean value indicating whether the node should be included in the
|
|
559
568
|
* result or not.
|
|
560
|
-
* @param {
|
|
561
|
-
* for the search in the binary tree. It can be specified as a `
|
|
569
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
570
|
+
* for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
|
|
562
571
|
* node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
|
|
563
572
|
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
564
573
|
* iteration to be performed on the binary tree. It is used to specify whether the iteration should
|
|
@@ -583,7 +592,7 @@ export class BinaryTree {
|
|
|
583
592
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
584
593
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
585
594
|
* function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
|
|
586
|
-
* @param {
|
|
595
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
587
596
|
* for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
|
|
588
597
|
* If `null` or `undefined` is passed, the search will start from the root of the binary tree.
|
|
589
598
|
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
@@ -606,7 +615,7 @@ export class BinaryTree {
|
|
|
606
615
|
*
|
|
607
616
|
* The function `getNodeByKey` searches for a node in a binary tree by its key, using either
|
|
608
617
|
* recursive or iterative iteration.
|
|
609
|
-
* @param {
|
|
618
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
610
619
|
* It is used to find the node with the matching key value.
|
|
611
620
|
* @param iterationType - The `iterationType` parameter is used to determine whether the search for
|
|
612
621
|
* the node with the given key should be performed iteratively or recursively. It has two possible
|
|
@@ -650,7 +659,7 @@ export class BinaryTree {
|
|
|
650
659
|
/**
|
|
651
660
|
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
652
661
|
* key, otherwise it returns the key itself.
|
|
653
|
-
* @param {
|
|
662
|
+
* @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
|
|
654
663
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
655
664
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
656
665
|
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
@@ -659,7 +668,7 @@ export class BinaryTree {
|
|
|
659
668
|
* itself if it is not a valid node key.
|
|
660
669
|
*/
|
|
661
670
|
ensureNode(key, iterationType = IterationType.ITERATIVE) {
|
|
662
|
-
return this.
|
|
671
|
+
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
663
672
|
}
|
|
664
673
|
/**
|
|
665
674
|
* Time Complexity: O(n)
|
|
@@ -674,8 +683,8 @@ export class BinaryTree {
|
|
|
674
683
|
* the binary tree. It is used to determine whether a node matches the given identifier. The callback
|
|
675
684
|
* function should return a value that can be compared to the identifier to determine if it is a
|
|
676
685
|
* match.
|
|
677
|
-
* @param {
|
|
678
|
-
* for the search in the binary tree. It can be specified as a `
|
|
686
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
687
|
+
* for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
|
|
679
688
|
* node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
|
|
680
689
|
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
681
690
|
* be performed when searching for a node in the binary tree. It is an optional parameter with a
|
|
@@ -712,8 +721,8 @@ export class BinaryTree {
|
|
|
712
721
|
*
|
|
713
722
|
* The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
|
|
714
723
|
* structure, with the option to reverse the order of the nodes.
|
|
715
|
-
* @param {
|
|
716
|
-
* starting node from which you want to find the path to the root. It can be of type `
|
|
724
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
725
|
+
* starting node from which you want to find the path to the root. It can be of type `K`, `N`,
|
|
717
726
|
* `null`, or `undefined`.
|
|
718
727
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
719
728
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
@@ -745,8 +754,8 @@ export class BinaryTree {
|
|
|
745
754
|
*
|
|
746
755
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
|
|
747
756
|
* iteratively.
|
|
748
|
-
* @param {
|
|
749
|
-
* for finding the leftmost node in a binary tree. It can be either a `
|
|
757
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
758
|
+
* for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `N` (a
|
|
750
759
|
* node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
|
|
751
760
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
752
761
|
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
@@ -785,8 +794,8 @@ export class BinaryTree {
|
|
|
785
794
|
*
|
|
786
795
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
787
796
|
* iteratively.
|
|
788
|
-
* @param {
|
|
789
|
-
* starting node from which we want to find the rightmost node. It can be of type `
|
|
797
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
798
|
+
* starting node from which we want to find the rightmost node. It can be of type `K`, `N`,
|
|
790
799
|
* `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
|
|
791
800
|
* current object.
|
|
792
801
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
@@ -826,7 +835,7 @@ export class BinaryTree {
|
|
|
826
835
|
* Space Complexity: O(1)
|
|
827
836
|
*
|
|
828
837
|
* The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
|
|
829
|
-
* @param {
|
|
838
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
|
|
830
839
|
* node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
|
|
831
840
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
832
841
|
* type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
|
|
@@ -842,9 +851,10 @@ export class BinaryTree {
|
|
|
842
851
|
const dfs = (cur, min, max) => {
|
|
843
852
|
if (!cur)
|
|
844
853
|
return true;
|
|
845
|
-
|
|
854
|
+
const numKey = this.extractor(cur.key);
|
|
855
|
+
if (numKey <= min || numKey >= max)
|
|
846
856
|
return false;
|
|
847
|
-
return dfs(cur.left, min,
|
|
857
|
+
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
848
858
|
};
|
|
849
859
|
return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
850
860
|
}
|
|
@@ -857,9 +867,10 @@ export class BinaryTree {
|
|
|
857
867
|
curr = curr.left;
|
|
858
868
|
}
|
|
859
869
|
curr = stack.pop();
|
|
860
|
-
|
|
870
|
+
const numKey = this.extractor(curr.key);
|
|
871
|
+
if (!curr || prev >= numKey)
|
|
861
872
|
return false;
|
|
862
|
-
prev =
|
|
873
|
+
prev = numKey;
|
|
863
874
|
curr = curr.right;
|
|
864
875
|
}
|
|
865
876
|
return true;
|
|
@@ -894,8 +905,8 @@ export class BinaryTree {
|
|
|
894
905
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
895
906
|
* the subtree traversal. It takes a single parameter, which is the current node being traversed, and
|
|
896
907
|
* returns a value of any type.
|
|
897
|
-
* @param {
|
|
898
|
-
* starting node or key from which the subtree traversal should begin. It can be of type `
|
|
908
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
909
|
+
* starting node or key from which the subtree traversal should begin. It can be of type `K`,
|
|
899
910
|
* `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
|
|
900
911
|
* the default value.
|
|
901
912
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -977,13 +988,13 @@ export class BinaryTree {
|
|
|
977
988
|
return this.isRealNode(node) || node === null;
|
|
978
989
|
}
|
|
979
990
|
/**
|
|
980
|
-
* The function "
|
|
991
|
+
* The function "isNotNodeInstance" checks if a potential key is a number.
|
|
981
992
|
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
982
993
|
* data type.
|
|
983
994
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
984
995
|
*/
|
|
985
|
-
|
|
986
|
-
return
|
|
996
|
+
isNotNodeInstance(potentialKey) {
|
|
997
|
+
return !(potentialKey instanceof BinaryTreeNode);
|
|
987
998
|
}
|
|
988
999
|
/**
|
|
989
1000
|
* Time complexity: O(n)
|
|
@@ -997,7 +1008,7 @@ export class BinaryTree {
|
|
|
997
1008
|
* `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
|
|
998
1009
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
999
1010
|
* nodes are traversed during the depth-first search. It can have one of the following values:
|
|
1000
|
-
* @param {
|
|
1011
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
1001
1012
|
* for the depth-first search traversal. It can be specified as a key, a node object, or
|
|
1002
1013
|
* `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
|
|
1003
1014
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
@@ -1123,7 +1134,7 @@ export class BinaryTree {
|
|
|
1123
1134
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1124
1135
|
* the breadth-first search traversal. It takes a single parameter, which is the current node being
|
|
1125
1136
|
* visited, and returns a value of any type.
|
|
1126
|
-
* @param {
|
|
1137
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
1127
1138
|
* starting node for the breadth-first search traversal. It can be specified as a key, a node object,
|
|
1128
1139
|
* or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
|
|
1129
1140
|
* the class is used as
|
|
@@ -1197,9 +1208,9 @@ export class BinaryTree {
|
|
|
1197
1208
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1198
1209
|
* the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
|
|
1199
1210
|
* returns a value of any type.
|
|
1200
|
-
* @param {
|
|
1211
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
1201
1212
|
* starting node for traversing the tree. It can be either a node object (`N`), a key value
|
|
1202
|
-
* (`
|
|
1213
|
+
* (`K`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
|
|
1203
1214
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
1204
1215
|
* performed on the tree. It can have two possible values:
|
|
1205
1216
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
@@ -1259,7 +1270,7 @@ export class BinaryTree {
|
|
|
1259
1270
|
}
|
|
1260
1271
|
/**
|
|
1261
1272
|
* The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
|
|
1262
|
-
* @param {
|
|
1273
|
+
* @param {K | N | null | undefined} node - The `node` parameter can be of type `K`, `N`,
|
|
1263
1274
|
* `null`, or `undefined`.
|
|
1264
1275
|
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
1265
1276
|
*/
|
|
@@ -1282,7 +1293,7 @@ export class BinaryTree {
|
|
|
1282
1293
|
}
|
|
1283
1294
|
/**
|
|
1284
1295
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1285
|
-
* @param {
|
|
1296
|
+
* @param {K | N | null} [x] - The parameter `x` can be of type `K`, `N`, or `null`.
|
|
1286
1297
|
* @returns the successor of the given node or key. The successor is the node that comes immediately
|
|
1287
1298
|
* after the given node in the inorder traversal of the binary tree.
|
|
1288
1299
|
*/
|
|
@@ -1311,7 +1322,7 @@ export class BinaryTree {
|
|
|
1311
1322
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1312
1323
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
1313
1324
|
* following values:
|
|
1314
|
-
* @param {
|
|
1325
|
+
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
1315
1326
|
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
1316
1327
|
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
1317
1328
|
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
@@ -1403,43 +1414,6 @@ export class BinaryTree {
|
|
|
1403
1414
|
}
|
|
1404
1415
|
return ans;
|
|
1405
1416
|
}
|
|
1406
|
-
/**
|
|
1407
|
-
* Time complexity: O(n)
|
|
1408
|
-
* Space complexity: O(n)
|
|
1409
|
-
*/
|
|
1410
|
-
/**
|
|
1411
|
-
* Time complexity: O(n)
|
|
1412
|
-
* Space complexity: O(n)
|
|
1413
|
-
*
|
|
1414
|
-
* The function "keys" returns an array of keys from a given object.
|
|
1415
|
-
* @returns an array of BTNKey objects.
|
|
1416
|
-
*/
|
|
1417
|
-
keys() {
|
|
1418
|
-
const keys = [];
|
|
1419
|
-
for (const entry of this) {
|
|
1420
|
-
keys.push(entry[0]);
|
|
1421
|
-
}
|
|
1422
|
-
return keys;
|
|
1423
|
-
}
|
|
1424
|
-
/**
|
|
1425
|
-
* Time complexity: O(n)
|
|
1426
|
-
* Space complexity: O(n)
|
|
1427
|
-
*/
|
|
1428
|
-
/**
|
|
1429
|
-
* Time complexity: O(n)
|
|
1430
|
-
* Space complexity: O(n)
|
|
1431
|
-
*
|
|
1432
|
-
* The function "values" returns an array of values from a map-like object.
|
|
1433
|
-
* @returns The `values()` method is returning an array of values (`V`) from the entries in the
|
|
1434
|
-
* object.
|
|
1435
|
-
*/
|
|
1436
|
-
values() {
|
|
1437
|
-
const values = [];
|
|
1438
|
-
for (const entry of this) {
|
|
1439
|
-
values.push(entry[1]);
|
|
1440
|
-
}
|
|
1441
|
-
return values;
|
|
1442
|
-
}
|
|
1443
1417
|
/**
|
|
1444
1418
|
* Time complexity: O(n)
|
|
1445
1419
|
* Space complexity: O(n)
|
|
@@ -1458,117 +1432,73 @@ export class BinaryTree {
|
|
|
1458
1432
|
return cloned;
|
|
1459
1433
|
}
|
|
1460
1434
|
/**
|
|
1461
|
-
* Time
|
|
1462
|
-
* Space
|
|
1463
|
-
*/
|
|
1464
|
-
/**
|
|
1465
|
-
* The `forEach` function iterates over each entry in a tree and calls a callback function with the
|
|
1466
|
-
* entry and the tree as arguments.
|
|
1467
|
-
* @param callback - The callback parameter is a function that will be called for each entry in the
|
|
1468
|
-
* tree. It takes two parameters: entry and tree.
|
|
1435
|
+
* Time Complexity: O(n)
|
|
1436
|
+
* Space Complexity: O(n)
|
|
1469
1437
|
*/
|
|
1470
|
-
forEach(callback) {
|
|
1471
|
-
for (const entry of this) {
|
|
1472
|
-
callback(entry, this);
|
|
1473
|
-
}
|
|
1474
|
-
}
|
|
1475
1438
|
/**
|
|
1476
|
-
*
|
|
1477
|
-
*
|
|
1478
|
-
*
|
|
1479
|
-
* `tree
|
|
1480
|
-
*
|
|
1481
|
-
*
|
|
1482
|
-
|
|
1483
|
-
|
|
1439
|
+
* Time Complexity: O(n)
|
|
1440
|
+
* Space Complexity: O(n)
|
|
1441
|
+
*
|
|
1442
|
+
* The `filter` function creates a new tree by iterating over the elements of the current tree and
|
|
1443
|
+
* adding only the elements that satisfy the given predicate function.
|
|
1444
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
1445
|
+
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
1446
|
+
* included in the filtered tree or not.
|
|
1447
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1448
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
1449
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
1450
|
+
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that
|
|
1451
|
+
* pass the given predicate function.
|
|
1452
|
+
*/
|
|
1453
|
+
filter(predicate, thisArg) {
|
|
1484
1454
|
const newTree = this.createTree();
|
|
1455
|
+
let index = 0;
|
|
1485
1456
|
for (const [key, value] of this) {
|
|
1486
|
-
if (predicate(
|
|
1457
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
1487
1458
|
newTree.add([key, value]);
|
|
1488
1459
|
}
|
|
1489
1460
|
}
|
|
1490
1461
|
return newTree;
|
|
1491
1462
|
}
|
|
1492
1463
|
/**
|
|
1493
|
-
*
|
|
1494
|
-
*
|
|
1495
|
-
|
|
1464
|
+
* Time Complexity: O(n)
|
|
1465
|
+
* Space Complexity: O(n)
|
|
1466
|
+
*/
|
|
1467
|
+
/**
|
|
1468
|
+
* Time Complexity: O(n)
|
|
1469
|
+
* Space Complexity: O(n)
|
|
1470
|
+
*
|
|
1471
|
+
* The `map` function creates a new tree by applying a callback function to each key-value pair in
|
|
1472
|
+
* the original tree.
|
|
1473
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
1474
|
+
* in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
|
|
1475
|
+
* the index of the current pair, and a reference to the tree itself. The callback function should
|
|
1476
|
+
* return a new
|
|
1477
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1478
|
+
* specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
|
|
1479
|
+
* will be used as the `this` value when the callback function is called. If you don't pass a value
|
|
1496
1480
|
* @returns The `map` method is returning a new tree object.
|
|
1497
1481
|
*/
|
|
1498
|
-
map(callback) {
|
|
1482
|
+
map(callback, thisArg) {
|
|
1499
1483
|
const newTree = this.createTree();
|
|
1484
|
+
let index = 0;
|
|
1500
1485
|
for (const [key, value] of this) {
|
|
1501
|
-
newTree.add([key, callback(
|
|
1486
|
+
newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
|
|
1502
1487
|
}
|
|
1503
1488
|
return newTree;
|
|
1504
1489
|
}
|
|
1505
|
-
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1506
|
-
// map<NV>(callback: (entry: [
|
|
1507
|
-
// const newTree = this.createTree();
|
|
1508
|
-
// for (const [key, value] of this) {
|
|
1509
|
-
// newTree.add(key, callback([key, value], this));
|
|
1510
|
-
// }
|
|
1511
|
-
// return newTree;
|
|
1512
|
-
// }
|
|
1513
|
-
|
|
1514
|
-
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
1515
|
-
* entry, accumulating a single value.
|
|
1516
|
-
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
1517
|
-
* entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
|
|
1518
|
-
* based on the logic defined in the callback function.
|
|
1519
|
-
* @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
1520
|
-
* is the value that will be passed as the first argument to the callback function when reducing the
|
|
1521
|
-
* elements of the tree.
|
|
1522
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
1523
|
-
* all the entries in the tree and applying the callback function to each entry.
|
|
1524
|
-
*/
|
|
1525
|
-
reduce(callback, initialValue) {
|
|
1526
|
-
let accumulator = initialValue;
|
|
1527
|
-
for (const [key, value] of this) {
|
|
1528
|
-
accumulator = callback(accumulator, [key, value], this);
|
|
1529
|
-
}
|
|
1530
|
-
return accumulator;
|
|
1531
|
-
}
|
|
1532
|
-
/**
|
|
1533
|
-
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1534
|
-
* either an iterative or recursive manner.
|
|
1535
|
-
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
1536
|
-
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
1537
|
-
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
1538
|
-
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1539
|
-
* binary tree nodes in a specific order.
|
|
1540
|
-
*/
|
|
1541
|
-
*[Symbol.iterator](node = this.root) {
|
|
1542
|
-
if (!node)
|
|
1543
|
-
return;
|
|
1544
|
-
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1545
|
-
const stack = [];
|
|
1546
|
-
let current = node;
|
|
1547
|
-
while (current || stack.length > 0) {
|
|
1548
|
-
while (current && !isNaN(current.key)) {
|
|
1549
|
-
stack.push(current);
|
|
1550
|
-
current = current.left;
|
|
1551
|
-
}
|
|
1552
|
-
current = stack.pop();
|
|
1553
|
-
if (current && !isNaN(current.key)) {
|
|
1554
|
-
yield [current.key, current.value];
|
|
1555
|
-
current = current.right;
|
|
1556
|
-
}
|
|
1557
|
-
}
|
|
1558
|
-
}
|
|
1559
|
-
else {
|
|
1560
|
-
if (node.left && !isNaN(node.key)) {
|
|
1561
|
-
yield* this[Symbol.iterator](node.left);
|
|
1562
|
-
}
|
|
1563
|
-
yield [node.key, node.value];
|
|
1564
|
-
if (node.right && !isNaN(node.key)) {
|
|
1565
|
-
yield* this[Symbol.iterator](node.right);
|
|
1566
|
-
}
|
|
1567
|
-
}
|
|
1568
|
-
}
|
|
1490
|
+
// // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1491
|
+
// // map<NV>(callback: (entry: [K, V | undefined], tree: this) => NV) {
|
|
1492
|
+
// // const newTree = this.createTree();
|
|
1493
|
+
// // for (const [key, value] of this) {
|
|
1494
|
+
// // newTree.add(key, callback([key, value], this));
|
|
1495
|
+
// // }
|
|
1496
|
+
// // return newTree;
|
|
1497
|
+
// // }
|
|
1498
|
+
//
|
|
1569
1499
|
/**
|
|
1570
1500
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1571
|
-
* @param {
|
|
1501
|
+
* @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
|
|
1572
1502
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1573
1503
|
* following types:
|
|
1574
1504
|
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
@@ -1595,6 +1525,34 @@ export class BinaryTree {
|
|
|
1595
1525
|
};
|
|
1596
1526
|
display(beginRoot);
|
|
1597
1527
|
}
|
|
1528
|
+
*_getIterator(node = this.root) {
|
|
1529
|
+
if (!node)
|
|
1530
|
+
return;
|
|
1531
|
+
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1532
|
+
const stack = [];
|
|
1533
|
+
let current = node;
|
|
1534
|
+
while (current || stack.length > 0) {
|
|
1535
|
+
while (current && !isNaN(this.extractor(current.key))) {
|
|
1536
|
+
stack.push(current);
|
|
1537
|
+
current = current.left;
|
|
1538
|
+
}
|
|
1539
|
+
current = stack.pop();
|
|
1540
|
+
if (current && !isNaN(this.extractor(current.key))) {
|
|
1541
|
+
yield [current.key, current.value];
|
|
1542
|
+
current = current.right;
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
else {
|
|
1547
|
+
if (node.left && !isNaN(this.extractor(node.key))) {
|
|
1548
|
+
yield* this[Symbol.iterator](node.left);
|
|
1549
|
+
}
|
|
1550
|
+
yield [node.key, node.value];
|
|
1551
|
+
if (node.right && !isNaN(this.extractor(node.key))) {
|
|
1552
|
+
yield* this[Symbol.iterator](node.right);
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1598
1556
|
_displayAux(node, options) {
|
|
1599
1557
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1600
1558
|
const emptyDisplayLayout = [['─'], 1, 0, 0];
|
|
@@ -1605,12 +1563,12 @@ export class BinaryTree {
|
|
|
1605
1563
|
else if (node === undefined && !isShowUndefined) {
|
|
1606
1564
|
return emptyDisplayLayout;
|
|
1607
1565
|
}
|
|
1608
|
-
else if (node !== null && node !== undefined && isNaN(node.key) && !isShowRedBlackNIL) {
|
|
1566
|
+
else if (node !== null && node !== undefined && isNaN(this.extractor(node.key)) && !isShowRedBlackNIL) {
|
|
1609
1567
|
return emptyDisplayLayout;
|
|
1610
1568
|
}
|
|
1611
1569
|
else if (node !== null && node !== undefined) {
|
|
1612
1570
|
// Display logic of normal nodes
|
|
1613
|
-
const key = node.key, line = isNaN(key) ? 'S' : key.toString(), width = line.length;
|
|
1571
|
+
const key = node.key, line = isNaN(this.extractor(key)) ? 'S' : this.extractor(key).toString(), width = line.length;
|
|
1614
1572
|
return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
|
|
1615
1573
|
}
|
|
1616
1574
|
else {
|
|
@@ -1698,7 +1656,7 @@ export class BinaryTree {
|
|
|
1698
1656
|
* If the parent node is null, the function also returns undefined.
|
|
1699
1657
|
*/
|
|
1700
1658
|
_addTo(newNode, parent) {
|
|
1701
|
-
if (this.
|
|
1659
|
+
if (this.isNotNodeInstance(parent))
|
|
1702
1660
|
parent = this.getNode(parent);
|
|
1703
1661
|
if (parent) {
|
|
1704
1662
|
// When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
|