linked-list-typed 1.47.6 → 1.47.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/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/data-structures/binary-tree/bst.js +137 -154
- package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/data-structures/graph/abstract-graph.js +30 -30
- package/dist/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/data-structures/graph/directed-graph.js +28 -28
- package/dist/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/data-structures/graph/undirected-graph.js +18 -18
- package/dist/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/data-structures/hash/hash-map.js +5 -8
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +13 -13
- package/dist/data-structures/stack/stack.d.ts +6 -6
- package/dist/data-structures/stack/stack.js +7 -7
- package/dist/data-structures/trie/trie.d.ts +3 -0
- package/dist/data-structures/trie/trie.js +19 -4
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +6 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/graph/abstract-graph.ts +46 -46
- package/src/data-structures/graph/directed-graph.ts +40 -40
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +1 -2
|
@@ -17,41 +17,22 @@ const queue_1 = require("../queue");
|
|
|
17
17
|
* @template N - The type of the family relationship in the binary tree.
|
|
18
18
|
*/
|
|
19
19
|
class BinaryTreeNode {
|
|
20
|
-
/**
|
|
21
|
-
* Creates a new instance of BinaryTreeNode.
|
|
22
|
-
* @param {BTNKey} key - The key associated with the node.
|
|
23
|
-
* @param {V} value - The value stored in the node.
|
|
24
|
-
*/
|
|
25
20
|
constructor(key, value) {
|
|
26
21
|
this.key = key;
|
|
27
22
|
this.value = value;
|
|
28
23
|
}
|
|
29
|
-
/**
|
|
30
|
-
* Get the left child node.
|
|
31
|
-
*/
|
|
32
24
|
get left() {
|
|
33
25
|
return this._left;
|
|
34
26
|
}
|
|
35
|
-
/**
|
|
36
|
-
* Set the left child node.
|
|
37
|
-
* @param {N | null | undefined} v - The left child node.
|
|
38
|
-
*/
|
|
39
27
|
set left(v) {
|
|
40
28
|
if (v) {
|
|
41
29
|
v.parent = this;
|
|
42
30
|
}
|
|
43
31
|
this._left = v;
|
|
44
32
|
}
|
|
45
|
-
/**
|
|
46
|
-
* Get the right child node.
|
|
47
|
-
*/
|
|
48
33
|
get right() {
|
|
49
34
|
return this._right;
|
|
50
35
|
}
|
|
51
|
-
/**
|
|
52
|
-
* Set the right child node.
|
|
53
|
-
* @param {N | null | undefined} v - The right child node.
|
|
54
|
-
*/
|
|
55
36
|
set right(v) {
|
|
56
37
|
if (v) {
|
|
57
38
|
v.parent = this;
|
|
@@ -78,13 +59,25 @@ class BinaryTreeNode {
|
|
|
78
59
|
}
|
|
79
60
|
exports.BinaryTreeNode = BinaryTreeNode;
|
|
80
61
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
62
|
+
* 1. Two Children Maximum: Each node has at most two children.
|
|
63
|
+
* 2. Left and Right Children: Nodes have distinct left and right children.
|
|
64
|
+
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
65
|
+
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
66
|
+
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
67
|
+
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
68
|
+
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
69
|
+
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
70
|
+
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
83
71
|
*/
|
|
84
72
|
class BinaryTree {
|
|
85
73
|
/**
|
|
86
|
-
*
|
|
87
|
-
* @param
|
|
74
|
+
* The constructor function initializes a binary tree object with optional elements and options.
|
|
75
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
|
|
76
|
+
* elements to be added to the binary tree.
|
|
77
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
78
|
+
* configuration options for the binary tree. In this case, it is of type
|
|
79
|
+
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
80
|
+
* required.
|
|
88
81
|
*/
|
|
89
82
|
constructor(elements, options) {
|
|
90
83
|
this.iterationType = types_1.IterationType.ITERATIVE;
|
|
@@ -97,17 +90,11 @@ class BinaryTree {
|
|
|
97
90
|
}
|
|
98
91
|
this._size = 0;
|
|
99
92
|
if (elements)
|
|
100
|
-
this.
|
|
93
|
+
this.addMany(elements);
|
|
101
94
|
}
|
|
102
|
-
/**
|
|
103
|
-
* Get the root node of the binary tree.
|
|
104
|
-
*/
|
|
105
95
|
get root() {
|
|
106
96
|
return this._root;
|
|
107
97
|
}
|
|
108
|
-
/**
|
|
109
|
-
* Get the number of nodes in the binary tree.
|
|
110
|
-
*/
|
|
111
98
|
get size() {
|
|
112
99
|
return this._size;
|
|
113
100
|
}
|
|
@@ -120,30 +107,46 @@ class BinaryTree {
|
|
|
120
107
|
createNode(key, value) {
|
|
121
108
|
return new BinaryTreeNode(key, value);
|
|
122
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* The function creates a binary tree with the given options.
|
|
112
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
113
|
+
* behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
|
|
114
|
+
* you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
|
|
115
|
+
* @returns a new instance of a binary tree.
|
|
116
|
+
*/
|
|
123
117
|
createTree(options) {
|
|
124
118
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
125
119
|
}
|
|
126
120
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
121
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
122
|
+
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
123
|
+
* two type parameters V and N, representing the value and node type respectively.
|
|
124
|
+
* @returns a boolean value.
|
|
125
|
+
*/
|
|
126
|
+
isEntry(kne) {
|
|
127
|
+
return Array.isArray(kne) && kne.length === 2;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Time Complexity O(log n) - O(n)
|
|
131
|
+
* Space Complexity O(1)
|
|
132
|
+
*/
|
|
133
|
+
/**
|
|
134
|
+
* Time Complexity O(log n) - O(n)
|
|
135
|
+
* Space Complexity O(1)
|
|
129
136
|
*
|
|
130
|
-
* The `add` function adds a new node
|
|
131
|
-
*
|
|
132
|
-
* @
|
|
133
|
-
* following types:
|
|
134
|
-
* @param {V} [value] - The value to be associated with the key or node being added to the binary
|
|
135
|
-
* tree.
|
|
136
|
-
* @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
|
|
137
|
-
* tree, or `null` or `undefined` if the insertion was not successful.
|
|
137
|
+
* The `add` function adds a new node to a binary tree, either by key or by providing a node object.
|
|
138
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be one of the following:
|
|
139
|
+
* @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
|
|
138
140
|
*/
|
|
139
|
-
add(
|
|
141
|
+
add(keyOrNodeOrEntry) {
|
|
142
|
+
let inserted, needInsert;
|
|
140
143
|
const _bfs = (root, newNode) => {
|
|
141
144
|
const queue = new queue_1.Queue([root]);
|
|
142
145
|
while (queue.size > 0) {
|
|
143
146
|
const cur = queue.shift();
|
|
144
147
|
if (newNode && cur.key === newNode.key) {
|
|
145
|
-
cur
|
|
146
|
-
return;
|
|
148
|
+
this._replaceNode(cur, newNode);
|
|
149
|
+
return newNode;
|
|
147
150
|
}
|
|
148
151
|
const inserted = this._addTo(newNode, cur);
|
|
149
152
|
if (inserted !== undefined)
|
|
@@ -154,15 +157,26 @@ class BinaryTree {
|
|
|
154
157
|
queue.push(cur.right);
|
|
155
158
|
}
|
|
156
159
|
};
|
|
157
|
-
|
|
158
|
-
if (keyOrNode === null) {
|
|
160
|
+
if (keyOrNodeOrEntry === null) {
|
|
159
161
|
needInsert = null;
|
|
160
162
|
}
|
|
161
|
-
else if (this.isNodeKey(
|
|
162
|
-
needInsert = this.createNode(
|
|
163
|
+
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
164
|
+
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
163
165
|
}
|
|
164
|
-
else if (
|
|
165
|
-
needInsert =
|
|
166
|
+
else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
167
|
+
needInsert = keyOrNodeOrEntry;
|
|
168
|
+
}
|
|
169
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
170
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
171
|
+
if (key === undefined) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
else if (key === null) {
|
|
175
|
+
needInsert = null;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
needInsert = this.createNode(key, value);
|
|
179
|
+
}
|
|
166
180
|
}
|
|
167
181
|
else {
|
|
168
182
|
return;
|
|
@@ -183,36 +197,28 @@ class BinaryTree {
|
|
|
183
197
|
return inserted;
|
|
184
198
|
}
|
|
185
199
|
/**
|
|
186
|
-
* Time Complexity: O(n)
|
|
200
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
187
201
|
* Space Complexity: O(1)
|
|
188
202
|
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
189
203
|
*/
|
|
190
204
|
/**
|
|
191
|
-
* Time Complexity: O(k
|
|
205
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
192
206
|
* Space Complexity: O(1)
|
|
193
207
|
*
|
|
194
|
-
* The `addMany`
|
|
195
|
-
*
|
|
196
|
-
* @param
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
203
|
-
*/
|
|
204
|
-
addMany(keysOrNodes, values) {
|
|
208
|
+
* The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
|
|
209
|
+
* current instance, and returns an array of the inserted nodes.
|
|
210
|
+
* @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
|
|
211
|
+
* `BTNodeExemplar<V, N>` objects.
|
|
212
|
+
* @returns The function `addMany` returns an array of values, where each value is either of type
|
|
213
|
+
* `N`, `null`, or `undefined`.
|
|
214
|
+
*/
|
|
215
|
+
addMany(nodes) {
|
|
205
216
|
// TODO not sure addMany not be run multi times
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return this.add(null);
|
|
212
|
-
}
|
|
213
|
-
const value = values === null || values === void 0 ? void 0 : values[i];
|
|
214
|
-
return this.add(keyOrNode, value);
|
|
215
|
-
});
|
|
217
|
+
const inserted = [];
|
|
218
|
+
for (const kne of nodes) {
|
|
219
|
+
inserted.push(this.add(kne));
|
|
220
|
+
}
|
|
221
|
+
return inserted;
|
|
216
222
|
}
|
|
217
223
|
/**
|
|
218
224
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
@@ -222,17 +228,13 @@ class BinaryTree {
|
|
|
222
228
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
223
229
|
* Space Complexity: O(1)
|
|
224
230
|
*
|
|
225
|
-
* The `refill` function clears the
|
|
226
|
-
* @param
|
|
227
|
-
* `
|
|
228
|
-
* @param {N[] | Array<V>} [values] - The `data` parameter is an optional array of values that will be assigned to
|
|
229
|
-
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
230
|
-
* array. Each value in the `data` array will be assigned to the
|
|
231
|
-
* @returns The method is returning a boolean value.
|
|
231
|
+
* The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
|
|
232
|
+
* @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
|
|
233
|
+
* contain either `BTNodeExemplar` objects, keys, or entries.
|
|
232
234
|
*/
|
|
233
|
-
refill(
|
|
235
|
+
refill(nodesOrKeysOrEntries) {
|
|
234
236
|
this.clear();
|
|
235
|
-
|
|
237
|
+
this.addMany(nodesOrKeysOrEntries);
|
|
236
238
|
}
|
|
237
239
|
/**
|
|
238
240
|
* Time Complexity: O(n)
|
|
@@ -282,7 +284,7 @@ class BinaryTree {
|
|
|
282
284
|
const leftSubTreeRightMost = this.getRightMost(curr.left);
|
|
283
285
|
if (leftSubTreeRightMost) {
|
|
284
286
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
285
|
-
orgCurrent = this.
|
|
287
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
286
288
|
if (parentOfLeftSubTreeMax) {
|
|
287
289
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
288
290
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -315,8 +317,8 @@ class BinaryTree {
|
|
|
315
317
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
316
318
|
*/
|
|
317
319
|
getDepth(distNode, beginRoot = this.root) {
|
|
318
|
-
distNode = this.
|
|
319
|
-
beginRoot = this.
|
|
320
|
+
distNode = this.ensureNode(distNode);
|
|
321
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
320
322
|
let depth = 0;
|
|
321
323
|
while (distNode === null || distNode === void 0 ? void 0 : distNode.parent) {
|
|
322
324
|
if (distNode === beginRoot) {
|
|
@@ -346,7 +348,7 @@ class BinaryTree {
|
|
|
346
348
|
* @returns the height of the binary tree.
|
|
347
349
|
*/
|
|
348
350
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
349
|
-
beginRoot = this.
|
|
351
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
350
352
|
if (!beginRoot)
|
|
351
353
|
return -1;
|
|
352
354
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
@@ -393,7 +395,7 @@ class BinaryTree {
|
|
|
393
395
|
*/
|
|
394
396
|
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
395
397
|
var _a, _b, _c;
|
|
396
|
-
beginRoot = this.
|
|
398
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
397
399
|
if (!beginRoot)
|
|
398
400
|
return -1;
|
|
399
401
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
@@ -483,7 +485,7 @@ class BinaryTree {
|
|
|
483
485
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
484
486
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
485
487
|
callback = (node => node);
|
|
486
|
-
beginRoot = this.
|
|
488
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
487
489
|
if (!beginRoot)
|
|
488
490
|
return [];
|
|
489
491
|
const ans = [];
|
|
@@ -622,7 +624,7 @@ class BinaryTree {
|
|
|
622
624
|
* Space Complexity: O(log n)
|
|
623
625
|
*/
|
|
624
626
|
/**
|
|
625
|
-
* The function `
|
|
627
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
626
628
|
* key, otherwise it returns the key itself.
|
|
627
629
|
* @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
|
|
628
630
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
@@ -632,7 +634,7 @@ class BinaryTree {
|
|
|
632
634
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
633
635
|
* itself if it is not a valid node key.
|
|
634
636
|
*/
|
|
635
|
-
|
|
637
|
+
ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
636
638
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
637
639
|
}
|
|
638
640
|
/**
|
|
@@ -698,7 +700,7 @@ class BinaryTree {
|
|
|
698
700
|
getPathToRoot(beginRoot, isReverse = true) {
|
|
699
701
|
// TODO to support get path through passing key
|
|
700
702
|
const result = [];
|
|
701
|
-
beginRoot = this.
|
|
703
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
702
704
|
if (!beginRoot)
|
|
703
705
|
return result;
|
|
704
706
|
while (beginRoot.parent) {
|
|
@@ -729,7 +731,7 @@ class BinaryTree {
|
|
|
729
731
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
730
732
|
*/
|
|
731
733
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
732
|
-
beginRoot = this.
|
|
734
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
733
735
|
if (!beginRoot)
|
|
734
736
|
return beginRoot;
|
|
735
737
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
@@ -771,7 +773,7 @@ class BinaryTree {
|
|
|
771
773
|
*/
|
|
772
774
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
773
775
|
// TODO support get right most by passing key in
|
|
774
|
-
beginRoot = this.
|
|
776
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
775
777
|
if (!beginRoot)
|
|
776
778
|
return beginRoot;
|
|
777
779
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
@@ -810,7 +812,7 @@ class BinaryTree {
|
|
|
810
812
|
*/
|
|
811
813
|
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
812
814
|
// TODO there is a bug
|
|
813
|
-
beginRoot = this.
|
|
815
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
814
816
|
if (!beginRoot)
|
|
815
817
|
return true;
|
|
816
818
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
@@ -883,7 +885,7 @@ class BinaryTree {
|
|
|
883
885
|
* by the return type of the `callback` function.
|
|
884
886
|
*/
|
|
885
887
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
886
|
-
beginRoot = this.
|
|
888
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
887
889
|
const ans = [];
|
|
888
890
|
if (!beginRoot)
|
|
889
891
|
return ans;
|
|
@@ -984,7 +986,7 @@ class BinaryTree {
|
|
|
984
986
|
* @returns an array of values that are the return values of the callback function.
|
|
985
987
|
*/
|
|
986
988
|
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE, includeNull = false) {
|
|
987
|
-
beginRoot = this.
|
|
989
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
988
990
|
if (!beginRoot)
|
|
989
991
|
return [];
|
|
990
992
|
const ans = [];
|
|
@@ -1111,7 +1113,7 @@ class BinaryTree {
|
|
|
1111
1113
|
* the breadth-first traversal of a binary tree.
|
|
1112
1114
|
*/
|
|
1113
1115
|
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1114
|
-
beginRoot = this.
|
|
1116
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1115
1117
|
if (!beginRoot)
|
|
1116
1118
|
return [];
|
|
1117
1119
|
const ans = [];
|
|
@@ -1184,7 +1186,7 @@ class BinaryTree {
|
|
|
1184
1186
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1185
1187
|
*/
|
|
1186
1188
|
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1187
|
-
beginRoot = this.
|
|
1189
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1188
1190
|
const levelsNodes = [];
|
|
1189
1191
|
if (!beginRoot)
|
|
1190
1192
|
return levelsNodes;
|
|
@@ -1239,7 +1241,7 @@ class BinaryTree {
|
|
|
1239
1241
|
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
1240
1242
|
*/
|
|
1241
1243
|
getPredecessor(node) {
|
|
1242
|
-
node = this.
|
|
1244
|
+
node = this.ensureNode(node);
|
|
1243
1245
|
if (!this.isRealNode(node))
|
|
1244
1246
|
return undefined;
|
|
1245
1247
|
if (node.left) {
|
|
@@ -1262,7 +1264,7 @@ class BinaryTree {
|
|
|
1262
1264
|
* after the given node in the inorder traversal of the binary tree.
|
|
1263
1265
|
*/
|
|
1264
1266
|
getSuccessor(x) {
|
|
1265
|
-
x = this.
|
|
1267
|
+
x = this.ensureNode(x);
|
|
1266
1268
|
if (!x)
|
|
1267
1269
|
return undefined;
|
|
1268
1270
|
if (x.right) {
|
|
@@ -1294,7 +1296,7 @@ class BinaryTree {
|
|
|
1294
1296
|
* by the return type of the `callback` function.
|
|
1295
1297
|
*/
|
|
1296
1298
|
morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
|
|
1297
|
-
beginRoot = this.
|
|
1299
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1298
1300
|
if (beginRoot === null)
|
|
1299
1301
|
return [];
|
|
1300
1302
|
const ans = [];
|
|
@@ -1405,7 +1407,7 @@ class BinaryTree {
|
|
|
1405
1407
|
const newTree = this.createTree();
|
|
1406
1408
|
for (const [key, value] of this) {
|
|
1407
1409
|
if (predicate([key, value], this)) {
|
|
1408
|
-
newTree.add(key, value);
|
|
1410
|
+
newTree.add([key, value]);
|
|
1409
1411
|
}
|
|
1410
1412
|
}
|
|
1411
1413
|
return newTree;
|
|
@@ -1419,7 +1421,7 @@ class BinaryTree {
|
|
|
1419
1421
|
map(callback) {
|
|
1420
1422
|
const newTree = this.createTree();
|
|
1421
1423
|
for (const [key, value] of this) {
|
|
1422
|
-
newTree.add(key, callback([key, value], this));
|
|
1424
|
+
newTree.add([key, callback([key, value], this)]);
|
|
1423
1425
|
}
|
|
1424
1426
|
return newTree;
|
|
1425
1427
|
}
|
|
@@ -1496,7 +1498,7 @@ class BinaryTree {
|
|
|
1496
1498
|
*/
|
|
1497
1499
|
print(beginRoot = this.root, options) {
|
|
1498
1500
|
const opts = Object.assign({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
|
|
1499
|
-
beginRoot = this.
|
|
1501
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1500
1502
|
if (!beginRoot)
|
|
1501
1503
|
return;
|
|
1502
1504
|
if (opts.isShowUndefined)
|
|
@@ -1516,19 +1518,6 @@ class BinaryTree {
|
|
|
1516
1518
|
};
|
|
1517
1519
|
display(beginRoot);
|
|
1518
1520
|
}
|
|
1519
|
-
init(elements) {
|
|
1520
|
-
if (elements) {
|
|
1521
|
-
for (const entryOrKey of elements) {
|
|
1522
|
-
if (Array.isArray(entryOrKey)) {
|
|
1523
|
-
const [key, value] = entryOrKey;
|
|
1524
|
-
this.add(key, value);
|
|
1525
|
-
}
|
|
1526
|
-
else {
|
|
1527
|
-
this.add(entryOrKey);
|
|
1528
|
-
}
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
}
|
|
1532
1521
|
_displayAux(node, options) {
|
|
1533
1522
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1534
1523
|
const emptyDisplayLayout = [['─'], 1, 0, 0];
|
|
@@ -1578,9 +1567,9 @@ class BinaryTree {
|
|
|
1578
1567
|
* @param {N} destNode - The destination node to swap.
|
|
1579
1568
|
* @returns {N} - The destination node after the swap.
|
|
1580
1569
|
*/
|
|
1581
|
-
|
|
1582
|
-
srcNode = this.
|
|
1583
|
-
destNode = this.
|
|
1570
|
+
_swapProperties(srcNode, destNode) {
|
|
1571
|
+
srcNode = this.ensureNode(srcNode);
|
|
1572
|
+
destNode = this.ensureNode(destNode);
|
|
1584
1573
|
if (srcNode && destNode) {
|
|
1585
1574
|
const { key, value } = destNode;
|
|
1586
1575
|
const tempNode = this.createNode(key, value);
|
|
@@ -1594,6 +1583,31 @@ class BinaryTree {
|
|
|
1594
1583
|
}
|
|
1595
1584
|
return undefined;
|
|
1596
1585
|
}
|
|
1586
|
+
/**
|
|
1587
|
+
* The function replaces an old node with a new node in a binary tree.
|
|
1588
|
+
* @param {N} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
1589
|
+
* tree.
|
|
1590
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
1591
|
+
* tree.
|
|
1592
|
+
* @returns The method is returning the newNode.
|
|
1593
|
+
*/
|
|
1594
|
+
_replaceNode(oldNode, newNode) {
|
|
1595
|
+
if (oldNode.parent) {
|
|
1596
|
+
if (oldNode.parent.left === oldNode) {
|
|
1597
|
+
oldNode.parent.left = newNode;
|
|
1598
|
+
}
|
|
1599
|
+
else if (oldNode.parent.right === oldNode) {
|
|
1600
|
+
oldNode.parent.right = newNode;
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
newNode.left = oldNode.left;
|
|
1604
|
+
newNode.right = oldNode.right;
|
|
1605
|
+
newNode.parent = oldNode.parent;
|
|
1606
|
+
if (this.root === oldNode) {
|
|
1607
|
+
this._root = newNode;
|
|
1608
|
+
}
|
|
1609
|
+
return newNode;
|
|
1610
|
+
}
|
|
1597
1611
|
/**
|
|
1598
1612
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1599
1613
|
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|