data-structure-typed 1.34.7 → 1.34.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +1 -0
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +0 -0
- package/README.md +8 -8
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +314 -457
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +64 -90
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +12 -31
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +157 -244
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +20 -44
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.js +80 -122
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +168 -239
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +351 -585
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +152 -282
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +47 -88
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +91 -180
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.js +85 -247
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +87 -128
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/pair.js.map +1 -1
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-map.js.map +1 -1
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/hash/tree-set.js.map +1 -1
- package/dist/data-structures/heap/heap.js +56 -80
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +57 -70
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.js +63 -84
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/queue.js +57 -157
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/tree.js +33 -46
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/utils/utils.js +22 -107
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +124 -123
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
- package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
- package/lib/data-structures/binary-tree/avl-tree.js +35 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +86 -89
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +40 -42
- package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
- package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
- package/lib/data-structures/graph/abstract-graph.js +86 -86
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.js +3 -3
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +37 -42
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +6 -4
- package/lib/interfaces/bst.d.ts +10 -9
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-binary-tree.js +0 -1
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +6 -6
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
- package/src/data-structures/binary-tree/avl-tree.ts +43 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +101 -96
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
- package/src/data-structures/graph/abstract-graph.ts +114 -109
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +37 -50
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +6 -4
- package/src/interfaces/bst.ts +9 -9
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -4
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/integration/avl-tree.test.ts +24 -24
- package/test/integration/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +28 -28
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -5
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -9,28 +9,15 @@ import { trampoline } from '../../utils';
|
|
|
9
9
|
import { FamilyPosition, LoopType } from '../../types';
|
|
10
10
|
export class AbstractBinaryTreeNode {
|
|
11
11
|
/**
|
|
12
|
-
* The constructor function initializes a BinaryTreeNode object with
|
|
13
|
-
* @param {
|
|
12
|
+
* The constructor function initializes a BinaryTreeNode object with a key and an optional value.
|
|
13
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
14
14
|
* of the binary tree node. It is used to distinguish one node from another in the binary tree.
|
|
15
15
|
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
|
|
16
16
|
* stored in the binary tree node. If no value is provided, it will be set to undefined.
|
|
17
17
|
*/
|
|
18
|
-
constructor(
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this._val = val;
|
|
22
|
-
}
|
|
23
|
-
get id() {
|
|
24
|
-
return this._id;
|
|
25
|
-
}
|
|
26
|
-
set id(v) {
|
|
27
|
-
this._id = v;
|
|
28
|
-
}
|
|
29
|
-
get val() {
|
|
30
|
-
return this._val;
|
|
31
|
-
}
|
|
32
|
-
set val(value) {
|
|
33
|
-
this._val = value;
|
|
18
|
+
constructor(key, val) {
|
|
19
|
+
this.key = key;
|
|
20
|
+
this.val = val;
|
|
34
21
|
}
|
|
35
22
|
get left() {
|
|
36
23
|
return this._left;
|
|
@@ -50,18 +37,6 @@ export class AbstractBinaryTreeNode {
|
|
|
50
37
|
}
|
|
51
38
|
this._right = v;
|
|
52
39
|
}
|
|
53
|
-
get parent() {
|
|
54
|
-
return this._parent;
|
|
55
|
-
}
|
|
56
|
-
set parent(v) {
|
|
57
|
-
this._parent = v;
|
|
58
|
-
}
|
|
59
|
-
get height() {
|
|
60
|
-
return this._height;
|
|
61
|
-
}
|
|
62
|
-
set height(v) {
|
|
63
|
-
this._height = v;
|
|
64
|
-
}
|
|
65
40
|
/**
|
|
66
41
|
* The function determines the position of a node in a family tree structure.
|
|
67
42
|
* @returns a value of type `FamilyPosition`.
|
|
@@ -110,9 +85,9 @@ export class AbstractBinaryTree {
|
|
|
110
85
|
this._root = null;
|
|
111
86
|
this._size = 0;
|
|
112
87
|
this._loopType = LoopType.ITERATIVE;
|
|
113
|
-
this.
|
|
114
|
-
this.
|
|
115
|
-
this.
|
|
88
|
+
this.visitedKey = [];
|
|
89
|
+
this.visitedVal = [];
|
|
90
|
+
this.visitedNode = [];
|
|
116
91
|
if (options !== undefined) {
|
|
117
92
|
const { loopType = LoopType.ITERATIVE } = options;
|
|
118
93
|
this._loopType = loopType;
|
|
@@ -128,15 +103,6 @@ export class AbstractBinaryTree {
|
|
|
128
103
|
get loopType() {
|
|
129
104
|
return this._loopType;
|
|
130
105
|
}
|
|
131
|
-
get visitedId() {
|
|
132
|
-
return this._visitedId;
|
|
133
|
-
}
|
|
134
|
-
get visitedVal() {
|
|
135
|
-
return this._visitedVal;
|
|
136
|
-
}
|
|
137
|
-
get visitedNode() {
|
|
138
|
-
return this._visitedNode;
|
|
139
|
-
}
|
|
140
106
|
/**
|
|
141
107
|
* The `swapLocation` function swaps the location of two nodes in a binary tree.
|
|
142
108
|
* @param {N} srcNode - The source node that you want to swap with the destination node.
|
|
@@ -145,21 +111,18 @@ export class AbstractBinaryTree {
|
|
|
145
111
|
* @returns The `destNode` is being returned.
|
|
146
112
|
*/
|
|
147
113
|
swapLocation(srcNode, destNode) {
|
|
148
|
-
const {
|
|
149
|
-
const tempNode = this.createNode(
|
|
114
|
+
const { key, val } = destNode;
|
|
115
|
+
const tempNode = this.createNode(key, val);
|
|
150
116
|
if (tempNode) {
|
|
151
|
-
|
|
152
|
-
destNode.id = srcNode.id;
|
|
117
|
+
destNode.key = srcNode.key;
|
|
153
118
|
destNode.val = srcNode.val;
|
|
154
|
-
|
|
155
|
-
srcNode.id = tempNode.id;
|
|
119
|
+
srcNode.key = tempNode.key;
|
|
156
120
|
srcNode.val = tempNode.val;
|
|
157
|
-
srcNode.height = tempNode.height;
|
|
158
121
|
}
|
|
159
122
|
return destNode;
|
|
160
123
|
}
|
|
161
124
|
/**
|
|
162
|
-
* The clear() function resets the root, size, and
|
|
125
|
+
* The clear() function resets the root, size, and maxKey properties to their initial values.
|
|
163
126
|
*/
|
|
164
127
|
clear() {
|
|
165
128
|
this._root = null;
|
|
@@ -179,20 +142,20 @@ export class AbstractBinaryTree {
|
|
|
179
142
|
*/
|
|
180
143
|
/**
|
|
181
144
|
* The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
|
|
182
|
-
* @param {
|
|
145
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey`, which
|
|
183
146
|
* is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
|
|
184
147
|
* node itself. It can also be `null` if no node is specified.
|
|
185
148
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
186
149
|
* being added to the binary tree.
|
|
187
150
|
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
188
151
|
*/
|
|
189
|
-
add(
|
|
152
|
+
add(keyOrNode, val) {
|
|
190
153
|
const _bfs = (root, newNode) => {
|
|
191
154
|
const queue = [root];
|
|
192
155
|
while (queue.length > 0) {
|
|
193
156
|
const cur = queue.shift();
|
|
194
157
|
if (cur) {
|
|
195
|
-
if (newNode && cur.
|
|
158
|
+
if (newNode && cur.key === newNode.key)
|
|
196
159
|
return;
|
|
197
160
|
const inserted = this._addTo(newNode, cur);
|
|
198
161
|
if (inserted !== undefined)
|
|
@@ -208,19 +171,19 @@ export class AbstractBinaryTree {
|
|
|
208
171
|
return;
|
|
209
172
|
};
|
|
210
173
|
let inserted, needInsert;
|
|
211
|
-
if (
|
|
174
|
+
if (keyOrNode === null) {
|
|
212
175
|
needInsert = null;
|
|
213
176
|
}
|
|
214
|
-
else if (typeof
|
|
215
|
-
needInsert = this.createNode(
|
|
177
|
+
else if (typeof keyOrNode === 'number') {
|
|
178
|
+
needInsert = this.createNode(keyOrNode, val);
|
|
216
179
|
}
|
|
217
|
-
else if (
|
|
218
|
-
needInsert =
|
|
180
|
+
else if (keyOrNode instanceof AbstractBinaryTreeNode) {
|
|
181
|
+
needInsert = keyOrNode;
|
|
219
182
|
}
|
|
220
183
|
else {
|
|
221
184
|
return;
|
|
222
185
|
}
|
|
223
|
-
const existNode =
|
|
186
|
+
const existNode = keyOrNode ? this.get(keyOrNode, 'key') : undefined;
|
|
224
187
|
if (this.root) {
|
|
225
188
|
if (existNode) {
|
|
226
189
|
existNode.val = val;
|
|
@@ -245,56 +208,56 @@ export class AbstractBinaryTree {
|
|
|
245
208
|
/**
|
|
246
209
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
247
210
|
* values, and adds them to the binary tree.
|
|
248
|
-
* @param {(
|
|
211
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
249
212
|
* objects, or null values.
|
|
250
213
|
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
251
214
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
|
|
252
215
|
* the value of the nodes will be `undefined`.
|
|
253
216
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
254
217
|
*/
|
|
255
|
-
addMany(
|
|
218
|
+
addMany(keysOrNodes, data) {
|
|
256
219
|
// TODO not sure addMany not be run multi times
|
|
257
220
|
const inserted = [];
|
|
258
|
-
for (let i = 0; i <
|
|
259
|
-
const
|
|
260
|
-
if (
|
|
261
|
-
inserted.push(this.add(
|
|
221
|
+
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
222
|
+
const keyOrNode = keysOrNodes[i];
|
|
223
|
+
if (keyOrNode instanceof AbstractBinaryTreeNode) {
|
|
224
|
+
inserted.push(this.add(keyOrNode.key, keyOrNode.val));
|
|
262
225
|
continue;
|
|
263
226
|
}
|
|
264
|
-
if (
|
|
227
|
+
if (keyOrNode === null) {
|
|
265
228
|
inserted.push(this.add(null));
|
|
266
229
|
continue;
|
|
267
230
|
}
|
|
268
231
|
const val = data === null || data === void 0 ? void 0 : data[i];
|
|
269
|
-
inserted.push(this.add(
|
|
232
|
+
inserted.push(this.add(keyOrNode, val));
|
|
270
233
|
}
|
|
271
234
|
return inserted;
|
|
272
235
|
}
|
|
273
236
|
/**
|
|
274
|
-
* The `
|
|
275
|
-
* @param {(
|
|
276
|
-
* `
|
|
237
|
+
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
238
|
+
* @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
239
|
+
* `BinaryTreeNodeKey` or `N` values.
|
|
277
240
|
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
278
|
-
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `
|
|
241
|
+
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
279
242
|
* array. Each value in the `data` array will be assigned to the
|
|
280
243
|
* @returns The method is returning a boolean value.
|
|
281
244
|
*/
|
|
282
|
-
|
|
245
|
+
refill(keysOrNodes, data) {
|
|
283
246
|
this.clear();
|
|
284
|
-
return
|
|
247
|
+
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
285
248
|
}
|
|
286
249
|
/**
|
|
287
250
|
* The `remove` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
|
|
288
251
|
* containing the deleted node and the node that needs to be balanced.
|
|
289
|
-
* @param {N |
|
|
290
|
-
* node ID (`
|
|
252
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
|
|
253
|
+
* node ID (`BinaryTreeNodeKey`).
|
|
291
254
|
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
292
255
|
*/
|
|
293
|
-
remove(
|
|
256
|
+
remove(nodeOrKey) {
|
|
294
257
|
const bstDeletedResult = [];
|
|
295
258
|
if (!this.root)
|
|
296
259
|
return bstDeletedResult;
|
|
297
|
-
const curr = typeof
|
|
260
|
+
const curr = typeof nodeOrKey === 'number' ? this.get(nodeOrKey) : nodeOrKey;
|
|
298
261
|
if (!curr)
|
|
299
262
|
return bstDeletedResult;
|
|
300
263
|
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
@@ -335,12 +298,12 @@ export class AbstractBinaryTree {
|
|
|
335
298
|
}
|
|
336
299
|
/**
|
|
337
300
|
* The function calculates the depth of a node in a binary tree.
|
|
338
|
-
* @param {N |
|
|
301
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be one of the following:
|
|
339
302
|
* @returns the depth of the given node or binary tree.
|
|
340
303
|
*/
|
|
341
|
-
getDepth(beginRoot) {
|
|
304
|
+
getDepth(beginRoot = this.root) {
|
|
342
305
|
if (typeof beginRoot === 'number')
|
|
343
|
-
beginRoot = this.get(beginRoot, '
|
|
306
|
+
beginRoot = this.get(beginRoot, 'key');
|
|
344
307
|
let depth = 0;
|
|
345
308
|
while (beginRoot === null || beginRoot === void 0 ? void 0 : beginRoot.parent) {
|
|
346
309
|
depth++;
|
|
@@ -350,15 +313,14 @@ export class AbstractBinaryTree {
|
|
|
350
313
|
}
|
|
351
314
|
/**
|
|
352
315
|
* The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
|
|
353
|
-
* @param {N |
|
|
354
|
-
* generic type representing a node in a binary tree), `
|
|
316
|
+
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
317
|
+
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
355
318
|
* node), or `null`.
|
|
356
319
|
* @returns the height of the binary tree.
|
|
357
320
|
*/
|
|
358
|
-
getHeight(beginRoot) {
|
|
359
|
-
beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
|
|
321
|
+
getHeight(beginRoot = this.root) {
|
|
360
322
|
if (typeof beginRoot === 'number')
|
|
361
|
-
beginRoot = this.get(beginRoot, '
|
|
323
|
+
beginRoot = this.get(beginRoot, 'key');
|
|
362
324
|
if (!beginRoot)
|
|
363
325
|
return -1;
|
|
364
326
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
@@ -398,9 +360,8 @@ export class AbstractBinaryTree {
|
|
|
398
360
|
* for `beginRoot`, the `this.root` property is used as the default value.
|
|
399
361
|
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
400
362
|
*/
|
|
401
|
-
getMinHeight(beginRoot) {
|
|
363
|
+
getMinHeight(beginRoot = this.root) {
|
|
402
364
|
var _a, _b, _c;
|
|
403
|
-
beginRoot = beginRoot || this.root;
|
|
404
365
|
if (!beginRoot)
|
|
405
366
|
return -1;
|
|
406
367
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
@@ -450,24 +411,23 @@ export class AbstractBinaryTree {
|
|
|
450
411
|
* tree or null if the tree is empty.
|
|
451
412
|
* @returns The method is returning a boolean value.
|
|
452
413
|
*/
|
|
453
|
-
isPerfectlyBalanced(beginRoot) {
|
|
414
|
+
isPerfectlyBalanced(beginRoot = this.root) {
|
|
454
415
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
455
416
|
}
|
|
456
417
|
/**
|
|
457
418
|
* The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
|
|
458
|
-
* @param {
|
|
419
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
459
420
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
460
421
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
461
|
-
* specifies the property name to use when searching for nodes. If not provided, it defaults to '
|
|
422
|
+
* specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
|
|
462
423
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
463
424
|
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
|
|
464
425
|
* function will stop traversing the tree and return the first matching node. If `only
|
|
465
426
|
* @returns an array of nodes (type N).
|
|
466
427
|
*/
|
|
467
|
-
getNodes(nodeProperty, propertyName, onlyOne) {
|
|
428
|
+
getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
|
|
468
429
|
if (!this.root)
|
|
469
430
|
return [];
|
|
470
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
471
431
|
const result = [];
|
|
472
432
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
473
433
|
const _traverse = (cur) => {
|
|
@@ -496,31 +456,29 @@ export class AbstractBinaryTree {
|
|
|
496
456
|
}
|
|
497
457
|
/**
|
|
498
458
|
* The function checks if a binary tree node has a specific property.
|
|
499
|
-
* @param {
|
|
459
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
500
460
|
* It represents the property of the binary tree node that you want to check.
|
|
501
461
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
502
|
-
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to '
|
|
462
|
+
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
|
|
503
463
|
* @returns a boolean value.
|
|
504
464
|
*/
|
|
505
|
-
has(nodeProperty, propertyName) {
|
|
506
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
465
|
+
has(nodeProperty, propertyName = 'key') {
|
|
507
466
|
// TODO may support finding node by value equal
|
|
508
467
|
return this.getNodes(nodeProperty, propertyName).length > 0;
|
|
509
468
|
}
|
|
510
469
|
/**
|
|
511
470
|
* The function returns the first node that matches the given property name and value, or null if no matching node is
|
|
512
471
|
* found.
|
|
513
|
-
* @param {
|
|
472
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
514
473
|
* It represents the property of the binary tree node that you want to search for.
|
|
515
474
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
516
475
|
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
|
|
517
|
-
* default value is set to `'
|
|
476
|
+
* default value is set to `'key'`.
|
|
518
477
|
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
519
478
|
* If no matching node is found, it returns null.
|
|
520
479
|
*/
|
|
521
|
-
get(nodeProperty, propertyName) {
|
|
480
|
+
get(nodeProperty, propertyName = 'key') {
|
|
522
481
|
var _a;
|
|
523
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
524
482
|
// TODO may support finding node by value equal
|
|
525
483
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
526
484
|
}
|
|
@@ -535,7 +493,7 @@ export class AbstractBinaryTree {
|
|
|
535
493
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
536
494
|
*/
|
|
537
495
|
getPathToRoot(node, isReverse = true) {
|
|
538
|
-
// TODO to support get path through passing
|
|
496
|
+
// TODO to support get path through passing key
|
|
539
497
|
const result = [];
|
|
540
498
|
while (node.parent) {
|
|
541
499
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
@@ -549,18 +507,17 @@ export class AbstractBinaryTree {
|
|
|
549
507
|
/**
|
|
550
508
|
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
551
509
|
* no node is specified.
|
|
552
|
-
* @param {N |
|
|
553
|
-
* generic type representing a node in a binary tree), `
|
|
510
|
+
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
511
|
+
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
554
512
|
* node), or `null`.
|
|
555
513
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
556
514
|
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
557
515
|
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
558
516
|
* node is found (
|
|
559
517
|
*/
|
|
560
|
-
getLeftMost(beginRoot) {
|
|
518
|
+
getLeftMost(beginRoot = this.root) {
|
|
561
519
|
if (typeof beginRoot === 'number')
|
|
562
|
-
beginRoot = this.get(beginRoot, '
|
|
563
|
-
beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
|
|
520
|
+
beginRoot = this.get(beginRoot, 'key');
|
|
564
521
|
if (!beginRoot)
|
|
565
522
|
return beginRoot;
|
|
566
523
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
@@ -584,25 +541,24 @@ export class AbstractBinaryTree {
|
|
|
584
541
|
/**
|
|
585
542
|
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
|
|
586
543
|
* recursion optimization.
|
|
587
|
-
* @param {N | null} [
|
|
544
|
+
* @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
588
545
|
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
|
|
589
546
|
* using the root node of the data structure.
|
|
590
547
|
* @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
|
|
591
548
|
* it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
|
|
592
549
|
* rightmost node starting from the root of the binary tree.
|
|
593
550
|
*/
|
|
594
|
-
getRightMost(
|
|
595
|
-
// TODO support get right most by passing
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
return node;
|
|
551
|
+
getRightMost(beginRoot = this.root) {
|
|
552
|
+
// TODO support get right most by passing key in
|
|
553
|
+
if (!beginRoot)
|
|
554
|
+
return beginRoot;
|
|
599
555
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
600
556
|
const _traverse = (cur) => {
|
|
601
557
|
if (!cur.right)
|
|
602
558
|
return cur;
|
|
603
559
|
return _traverse(cur.right);
|
|
604
560
|
};
|
|
605
|
-
return _traverse(
|
|
561
|
+
return _traverse(beginRoot);
|
|
606
562
|
}
|
|
607
563
|
else {
|
|
608
564
|
// Indirect implementation of iteration using tail recursion optimization
|
|
@@ -611,7 +567,7 @@ export class AbstractBinaryTree {
|
|
|
611
567
|
return cur;
|
|
612
568
|
return _traverse.cont(cur.right);
|
|
613
569
|
});
|
|
614
|
-
return _traverse(
|
|
570
|
+
return _traverse(beginRoot);
|
|
615
571
|
}
|
|
616
572
|
}
|
|
617
573
|
/**
|
|
@@ -627,9 +583,9 @@ export class AbstractBinaryTree {
|
|
|
627
583
|
const dfs = (cur, min, max) => {
|
|
628
584
|
if (!cur)
|
|
629
585
|
return true;
|
|
630
|
-
if (cur.
|
|
586
|
+
if (cur.key <= min || cur.key >= max)
|
|
631
587
|
return false;
|
|
632
|
-
return dfs(cur.left, min, cur.
|
|
588
|
+
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
633
589
|
};
|
|
634
590
|
return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
635
591
|
}
|
|
@@ -642,9 +598,9 @@ export class AbstractBinaryTree {
|
|
|
642
598
|
curr = curr.left;
|
|
643
599
|
}
|
|
644
600
|
curr = stack.pop();
|
|
645
|
-
if (!curr || prev >= curr.
|
|
601
|
+
if (!curr || prev >= curr.key)
|
|
646
602
|
return false;
|
|
647
|
-
prev = curr.
|
|
603
|
+
prev = curr.key;
|
|
648
604
|
curr = curr.right;
|
|
649
605
|
}
|
|
650
606
|
return true;
|
|
@@ -664,7 +620,7 @@ export class AbstractBinaryTree {
|
|
|
664
620
|
* @returns the size of the subtree rooted at `subTreeRoot`.
|
|
665
621
|
*/
|
|
666
622
|
getSubTreeSize(subTreeRoot) {
|
|
667
|
-
// TODO support
|
|
623
|
+
// TODO support key passed in
|
|
668
624
|
let size = 0;
|
|
669
625
|
if (!subTreeRoot)
|
|
670
626
|
return size;
|
|
@@ -690,31 +646,30 @@ export class AbstractBinaryTree {
|
|
|
690
646
|
}
|
|
691
647
|
/**
|
|
692
648
|
* The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
|
|
693
|
-
* @param {N |
|
|
649
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
694
650
|
* tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
|
|
695
651
|
* @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
|
|
696
|
-
* property of the binary tree node to use for calculating the sum. It can be either '
|
|
697
|
-
* not provided, it defaults to '
|
|
652
|
+
* property of the binary tree node to use for calculating the sum. It can be either 'key' or 'val'. If propertyName is
|
|
653
|
+
* not provided, it defaults to 'key'.
|
|
698
654
|
* @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
|
|
699
655
|
*/
|
|
700
|
-
subTreeSum(subTreeRoot, propertyName) {
|
|
701
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
656
|
+
subTreeSum(subTreeRoot, propertyName = 'key') {
|
|
702
657
|
if (typeof subTreeRoot === 'number')
|
|
703
|
-
subTreeRoot = this.get(subTreeRoot, '
|
|
658
|
+
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
704
659
|
if (!subTreeRoot)
|
|
705
660
|
return 0;
|
|
706
661
|
let sum = 0;
|
|
707
662
|
const _sumByProperty = (cur) => {
|
|
708
663
|
let needSum;
|
|
709
664
|
switch (propertyName) {
|
|
710
|
-
case '
|
|
711
|
-
needSum = cur.
|
|
665
|
+
case 'key':
|
|
666
|
+
needSum = cur.key;
|
|
712
667
|
break;
|
|
713
668
|
case 'val':
|
|
714
669
|
needSum = typeof cur.val === 'number' ? cur.val : 0;
|
|
715
670
|
break;
|
|
716
671
|
default:
|
|
717
|
-
needSum = cur.
|
|
672
|
+
needSum = cur.key;
|
|
718
673
|
break;
|
|
719
674
|
}
|
|
720
675
|
return needSum;
|
|
@@ -740,27 +695,26 @@ export class AbstractBinaryTree {
|
|
|
740
695
|
}
|
|
741
696
|
/**
|
|
742
697
|
* The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
|
|
743
|
-
* @param {N |
|
|
698
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
744
699
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
745
700
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
746
701
|
* each node in the subtree should be incremented.
|
|
747
702
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
748
|
-
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to '
|
|
703
|
+
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
749
704
|
* @returns a boolean value.
|
|
750
705
|
*/
|
|
751
|
-
subTreeAdd(subTreeRoot, delta, propertyName) {
|
|
752
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
706
|
+
subTreeAdd(subTreeRoot, delta, propertyName = 'key') {
|
|
753
707
|
if (typeof subTreeRoot === 'number')
|
|
754
|
-
subTreeRoot = this.get(subTreeRoot, '
|
|
708
|
+
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
755
709
|
if (!subTreeRoot)
|
|
756
710
|
return false;
|
|
757
711
|
const _addByProperty = (cur) => {
|
|
758
712
|
switch (propertyName) {
|
|
759
|
-
case '
|
|
760
|
-
cur.
|
|
713
|
+
case 'key':
|
|
714
|
+
cur.key += delta;
|
|
761
715
|
break;
|
|
762
716
|
default:
|
|
763
|
-
cur.
|
|
717
|
+
cur.key += delta;
|
|
764
718
|
break;
|
|
765
719
|
}
|
|
766
720
|
};
|
|
@@ -784,14 +738,13 @@ export class AbstractBinaryTree {
|
|
|
784
738
|
return true;
|
|
785
739
|
}
|
|
786
740
|
/**
|
|
787
|
-
* The
|
|
741
|
+
* The bfs function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
|
|
788
742
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
|
|
789
|
-
* If a node is provided, the
|
|
790
|
-
* If a property name is provided, the
|
|
743
|
+
* If a node is provided, the bfs algorithm will be performed starting from that node.
|
|
744
|
+
* If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property.
|
|
791
745
|
* @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
|
|
792
746
|
*/
|
|
793
|
-
|
|
794
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
747
|
+
bfs(nodeOrPropertyName = 'key') {
|
|
795
748
|
this._clearResults();
|
|
796
749
|
const queue = [this.root];
|
|
797
750
|
while (queue.length !== 0) {
|
|
@@ -807,15 +760,13 @@ export class AbstractBinaryTree {
|
|
|
807
760
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
808
761
|
}
|
|
809
762
|
/**
|
|
810
|
-
* The
|
|
763
|
+
* The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
811
764
|
* each node based on the specified pattern and property name.
|
|
812
765
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
813
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'
|
|
766
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'key'`.
|
|
814
767
|
* @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
|
|
815
768
|
*/
|
|
816
|
-
|
|
817
|
-
pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
|
|
818
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
769
|
+
dfs(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
819
770
|
this._clearResults();
|
|
820
771
|
const _traverse = (node) => {
|
|
821
772
|
switch (pattern) {
|
|
@@ -846,15 +797,13 @@ export class AbstractBinaryTree {
|
|
|
846
797
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
847
798
|
}
|
|
848
799
|
/**
|
|
849
|
-
* The
|
|
800
|
+
* The dfsIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
|
|
850
801
|
* specify the traversal pattern and the property name to accumulate results by.
|
|
851
802
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
852
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'
|
|
803
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'key'`.
|
|
853
804
|
* @returns An object of type AbstractBinaryTreeNodeProperties<N>.
|
|
854
805
|
*/
|
|
855
|
-
|
|
856
|
-
pattern = pattern || 'in';
|
|
857
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
806
|
+
dfsIterative(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
858
807
|
this._clearResults();
|
|
859
808
|
if (!this.root)
|
|
860
809
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
@@ -901,14 +850,12 @@ export class AbstractBinaryTree {
|
|
|
901
850
|
* node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
|
|
902
851
|
* the tree is used as the starting node.
|
|
903
852
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
904
|
-
* can be either a `BinaryTreeNode` property name or the string `'
|
|
853
|
+
* can be either a `BinaryTreeNode` property name or the string `'key'`. If a property name is provided, the function
|
|
905
854
|
* will accumulate results based on that property. If no property name is provided, the function will default to
|
|
906
|
-
* accumulating results based on the '
|
|
855
|
+
* accumulating results based on the 'key' property.
|
|
907
856
|
* @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
908
857
|
*/
|
|
909
|
-
levelIterative(node, nodeOrPropertyName) {
|
|
910
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
911
|
-
node = node || this.root;
|
|
858
|
+
levelIterative(node = this.root, nodeOrPropertyName = 'key') {
|
|
912
859
|
if (!node)
|
|
913
860
|
return [];
|
|
914
861
|
this._clearResults();
|
|
@@ -930,19 +877,17 @@ export class AbstractBinaryTree {
|
|
|
930
877
|
/**
|
|
931
878
|
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
932
879
|
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
|
|
933
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: '
|
|
880
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: 'key', 'val', or 'node'. If not provided, it defaults to 'key'.
|
|
934
881
|
* @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
|
|
935
882
|
*/
|
|
936
|
-
listLevels(node, nodeOrPropertyName) {
|
|
937
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
938
|
-
node = node || this.root;
|
|
883
|
+
listLevels(node = this.root, nodeOrPropertyName = 'key') {
|
|
939
884
|
if (!node)
|
|
940
885
|
return [];
|
|
941
886
|
const levelsNodes = [];
|
|
942
887
|
const collectByProperty = (node, level) => {
|
|
943
888
|
switch (nodeOrPropertyName) {
|
|
944
|
-
case '
|
|
945
|
-
levelsNodes[level].push(node.
|
|
889
|
+
case 'key':
|
|
890
|
+
levelsNodes[level].push(node.key);
|
|
946
891
|
break;
|
|
947
892
|
case 'val':
|
|
948
893
|
levelsNodes[level].push(node.val);
|
|
@@ -951,7 +896,7 @@ export class AbstractBinaryTree {
|
|
|
951
896
|
levelsNodes[level].push(node);
|
|
952
897
|
break;
|
|
953
898
|
default:
|
|
954
|
-
levelsNodes[level].push(node.
|
|
899
|
+
levelsNodes[level].push(node.key);
|
|
955
900
|
break;
|
|
956
901
|
}
|
|
957
902
|
};
|
|
@@ -1005,14 +950,12 @@ export class AbstractBinaryTree {
|
|
|
1005
950
|
/**
|
|
1006
951
|
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
1007
952
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1008
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to '
|
|
953
|
+
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'key'.
|
|
1009
954
|
* @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
|
|
1010
955
|
*/
|
|
1011
|
-
morris(pattern, nodeOrPropertyName) {
|
|
956
|
+
morris(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
1012
957
|
if (this.root === null)
|
|
1013
958
|
return [];
|
|
1014
|
-
pattern = pattern || 'in';
|
|
1015
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
1016
959
|
this._clearResults();
|
|
1017
960
|
let cur = this.root;
|
|
1018
961
|
const _reverseEdge = (node) => {
|
|
@@ -1137,27 +1080,6 @@ export class AbstractBinaryTree {
|
|
|
1137
1080
|
_setLoopType(value) {
|
|
1138
1081
|
this._loopType = value;
|
|
1139
1082
|
}
|
|
1140
|
-
/**
|
|
1141
|
-
* The function sets the value of the `_visitedId` property in a protected manner.
|
|
1142
|
-
* @param {BinaryTreeNodeId[]} value - value is an array of BinaryTreeNodeId values.
|
|
1143
|
-
*/
|
|
1144
|
-
_setVisitedId(value) {
|
|
1145
|
-
this._visitedId = value;
|
|
1146
|
-
}
|
|
1147
|
-
/**
|
|
1148
|
-
* The function sets the value of the "_visitedVal" property to the given array.
|
|
1149
|
-
* @param value - An array of type N.
|
|
1150
|
-
*/
|
|
1151
|
-
_setVisitedVal(value) {
|
|
1152
|
-
this._visitedVal = value;
|
|
1153
|
-
}
|
|
1154
|
-
/**
|
|
1155
|
-
* The function sets the value of the _visitedNode property.
|
|
1156
|
-
* @param {N[]} value - N[] is an array of elements of type N.
|
|
1157
|
-
*/
|
|
1158
|
-
_setVisitedNode(value) {
|
|
1159
|
-
this._visitedNode = value;
|
|
1160
|
-
}
|
|
1161
1083
|
/**
|
|
1162
1084
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
1163
1085
|
* parent property of the value to undefined.
|
|
@@ -1181,43 +1103,43 @@ export class AbstractBinaryTree {
|
|
|
1181
1103
|
* properties.
|
|
1182
1104
|
*/
|
|
1183
1105
|
_clearResults() {
|
|
1184
|
-
this.
|
|
1185
|
-
this.
|
|
1186
|
-
this.
|
|
1106
|
+
this.visitedKey = [];
|
|
1107
|
+
this.visitedVal = [];
|
|
1108
|
+
this.visitedNode = [];
|
|
1187
1109
|
}
|
|
1188
1110
|
/**
|
|
1189
1111
|
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
|
|
1190
1112
|
* a result array.
|
|
1191
1113
|
* @param {N} cur - The current node being processed.
|
|
1192
1114
|
* @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
|
|
1193
|
-
* @param {
|
|
1115
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeKey` or a `N`
|
|
1194
1116
|
* type. It represents the property value that we are comparing against in the switch statement.
|
|
1195
1117
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
1196
|
-
* specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'
|
|
1197
|
-
* or `'val'`. If it is not provided or is not equal to `'
|
|
1118
|
+
* specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'key'`
|
|
1119
|
+
* or `'val'`. If it is not provided or is not equal to `'key'` or `'val'`, the
|
|
1198
1120
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
1199
1121
|
* stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
|
|
1200
1122
|
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
|
|
1201
1123
|
* @returns a boolean value indicating whether only one matching node should be pushed into the result array.
|
|
1202
1124
|
*/
|
|
1203
|
-
_pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne) {
|
|
1125
|
+
_pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName = 'key', onlyOne = false) {
|
|
1204
1126
|
switch (propertyName) {
|
|
1205
|
-
case '
|
|
1206
|
-
if (cur.
|
|
1127
|
+
case 'key':
|
|
1128
|
+
if (cur.key === nodeProperty) {
|
|
1207
1129
|
result.push(cur);
|
|
1208
|
-
return
|
|
1130
|
+
return onlyOne;
|
|
1209
1131
|
}
|
|
1210
1132
|
break;
|
|
1211
1133
|
case 'val':
|
|
1212
1134
|
if (cur.val === nodeProperty) {
|
|
1213
1135
|
result.push(cur);
|
|
1214
|
-
return
|
|
1136
|
+
return onlyOne;
|
|
1215
1137
|
}
|
|
1216
1138
|
break;
|
|
1217
1139
|
default:
|
|
1218
|
-
if (cur.
|
|
1140
|
+
if (cur.key === nodeProperty) {
|
|
1219
1141
|
result.push(cur);
|
|
1220
|
-
return
|
|
1142
|
+
return onlyOne;
|
|
1221
1143
|
}
|
|
1222
1144
|
break;
|
|
1223
1145
|
}
|
|
@@ -1229,25 +1151,24 @@ export class AbstractBinaryTree {
|
|
|
1229
1151
|
* can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
|
|
1230
1152
|
* specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
|
|
1231
1153
|
*/
|
|
1232
|
-
_accumulatedByPropertyName(node, nodeOrPropertyName) {
|
|
1233
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
1154
|
+
_accumulatedByPropertyName(node, nodeOrPropertyName = 'key') {
|
|
1234
1155
|
switch (nodeOrPropertyName) {
|
|
1235
|
-
case '
|
|
1236
|
-
this.
|
|
1156
|
+
case 'key':
|
|
1157
|
+
this.visitedKey.push(node.key);
|
|
1237
1158
|
break;
|
|
1238
1159
|
case 'val':
|
|
1239
|
-
this.
|
|
1160
|
+
this.visitedVal.push(node.val);
|
|
1240
1161
|
break;
|
|
1241
1162
|
case 'node':
|
|
1242
|
-
this.
|
|
1163
|
+
this.visitedNode.push(node);
|
|
1243
1164
|
break;
|
|
1244
1165
|
default:
|
|
1245
|
-
this.
|
|
1166
|
+
this.visitedKey.push(node.key);
|
|
1246
1167
|
break;
|
|
1247
1168
|
}
|
|
1248
1169
|
}
|
|
1249
1170
|
/**
|
|
1250
|
-
* The time complexity of Morris traversal is O(n), it
|
|
1171
|
+
* The time complexity of Morris traversal is O(n), it may slower than others
|
|
1251
1172
|
* The space complexity Morris traversal is O(1) because no using stack
|
|
1252
1173
|
*/
|
|
1253
1174
|
/**
|
|
@@ -1257,17 +1178,16 @@ export class AbstractBinaryTree {
|
|
|
1257
1178
|
* can accept either a `NodeOrPropertyName` type or be undefined.
|
|
1258
1179
|
* @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
|
|
1259
1180
|
*/
|
|
1260
|
-
_getResultByPropertyName(nodeOrPropertyName) {
|
|
1261
|
-
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
1181
|
+
_getResultByPropertyName(nodeOrPropertyName = 'key') {
|
|
1262
1182
|
switch (nodeOrPropertyName) {
|
|
1263
|
-
case '
|
|
1264
|
-
return this.
|
|
1183
|
+
case 'key':
|
|
1184
|
+
return this.visitedKey;
|
|
1265
1185
|
case 'val':
|
|
1266
|
-
return this.
|
|
1186
|
+
return this.visitedVal;
|
|
1267
1187
|
case 'node':
|
|
1268
|
-
return this.
|
|
1188
|
+
return this.visitedNode;
|
|
1269
1189
|
default:
|
|
1270
|
-
return this.
|
|
1190
|
+
return this.visitedKey;
|
|
1271
1191
|
}
|
|
1272
1192
|
}
|
|
1273
1193
|
}
|