data-structure-typed 1.20.0 → 1.21.0
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/LICENSE +21 -0
- package/README.md +6 -4
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +11 -18
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +52 -74
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +20 -28
- package/dist/data-structures/graph/abstract-graph.d.ts +3 -3
- package/dist/data-structures/graph/abstract-graph.js +6 -2
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +0 -2
- package/dist/data-structures/tree/tree.d.ts +1 -1
- package/dist/data-structures/types/abstract-binary-tree.d.ts +0 -1
- package/dist/data-structures/types/abstract-graph.d.ts +1 -0
- package/dist/data-structures/types/index.d.ts +1 -0
- package/dist/data-structures/types/index.js +1 -0
- package/dist/data-structures/types/map-graph.d.ts +1 -0
- package/dist/data-structures/types/map-graph.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +1 -3
- package/package.json +1 -1
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1479
- package/src/data-structures/binary-tree/avl-tree.ts +0 -301
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
- package/src/data-structures/binary-tree/binary-tree.ts +0 -42
- package/src/data-structures/binary-tree/bst.ts +0 -438
- package/src/data-structures/binary-tree/index.ts +0 -12
- package/src/data-structures/binary-tree/rb-tree.ts +0 -102
- package/src/data-structures/binary-tree/segment-tree.ts +0 -243
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -712
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -5
- package/src/data-structures/graph/abstract-graph.ts +0 -1033
- package/src/data-structures/graph/directed-graph.ts +0 -472
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -270
- package/src/data-structures/hash/coordinate-map.ts +0 -67
- package/src/data-structures/hash/coordinate-set.ts +0 -56
- package/src/data-structures/hash/hash-table.ts +0 -3
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/hash/pair.ts +0 -3
- package/src/data-structures/hash/tree-map.ts +0 -3
- package/src/data-structures/hash/tree-set.ts +0 -3
- package/src/data-structures/heap/heap.ts +0 -200
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -34
- package/src/data-structures/index.ts +0 -15
- package/src/data-structures/interfaces/abstract-binary-tree.ts +0 -194
- package/src/data-structures/interfaces/abstract-graph.ts +0 -40
- package/src/data-structures/interfaces/avl-tree.ts +0 -28
- package/src/data-structures/interfaces/binary-tree.ts +0 -8
- package/src/data-structures/interfaces/bst.ts +0 -32
- package/src/data-structures/interfaces/directed-graph.ts +0 -20
- package/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
- package/src/data-structures/interfaces/heap.ts +0 -1
- package/src/data-structures/interfaces/index.ts +0 -15
- package/src/data-structures/interfaces/navigator.ts +0 -1
- package/src/data-structures/interfaces/priority-queue.ts +0 -1
- package/src/data-structures/interfaces/rb-tree.ts +0 -11
- package/src/data-structures/interfaces/segment-tree.ts +0 -1
- package/src/data-structures/interfaces/singly-linked-list.ts +0 -1
- package/src/data-structures/interfaces/tree-multiset.ts +0 -12
- package/src/data-structures/interfaces/undirected-graph.ts +0 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
- package/src/data-structures/linked-list/index.ts +0 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -490
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -3
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -208
- package/src/data-structures/matrix/navigator.ts +0 -122
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
- package/src/data-structures/priority-queue/priority-queue.ts +0 -354
- package/src/data-structures/queue/deque.ts +0 -251
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -120
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/tree/index.ts +0 -1
- package/src/data-structures/tree/tree.ts +0 -69
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -227
- package/src/data-structures/types/abstract-binary-tree.ts +0 -41
- package/src/data-structures/types/abstract-graph.ts +0 -5
- package/src/data-structures/types/avl-tree.ts +0 -5
- package/src/data-structures/types/binary-tree.ts +0 -9
- package/src/data-structures/types/bst.ts +0 -12
- package/src/data-structures/types/directed-graph.ts +0 -8
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -5
- package/src/data-structures/types/helpers.ts +0 -1
- package/src/data-structures/types/index.ts +0 -15
- package/src/data-structures/types/navigator.ts +0 -13
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/rb-tree.ts +0 -8
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -1
- package/src/data-structures/types/tree-multiset.ts +0 -8
- package/src/index.ts +0 -2
- package/src/utils/index.ts +0 -3
- package/src/utils/types/index.ts +0 -2
- package/src/utils/types/utils.ts +0 -6
- package/src/utils/types/validate-type.ts +0 -25
- package/src/utils/utils.ts +0 -78
- package/src/utils/validate-type.ts +0 -69
|
@@ -1,1479 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import {trampoline} from '../../utils';
|
|
10
|
-
import type {
|
|
11
|
-
AbstractBinaryTreeNodeNested,
|
|
12
|
-
AbstractBinaryTreeNodeProperties,
|
|
13
|
-
AbstractBinaryTreeNodeProperty,
|
|
14
|
-
BinaryTreeDeletedResult,
|
|
15
|
-
BinaryTreeNodeId,
|
|
16
|
-
BinaryTreeNodePropertyName,
|
|
17
|
-
DFSOrderPattern,
|
|
18
|
-
NodeOrPropertyName
|
|
19
|
-
} from '../types';
|
|
20
|
-
import {AbstractBinaryTreeOptions, FamilyPosition, LoopType} from '../types';
|
|
21
|
-
import {IAbstractBinaryTree, IAbstractBinaryTreeNode} from '../interfaces';
|
|
22
|
-
|
|
23
|
-
export abstract class AbstractBinaryTreeNode<T = any, NEIGHBOR extends AbstractBinaryTreeNode<T, NEIGHBOR> = AbstractBinaryTreeNodeNested<T>> implements IAbstractBinaryTreeNode<T, NEIGHBOR> {
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The constructor function initializes a BinaryTreeNode object with an id and an optional value.
|
|
27
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
|
|
28
|
-
* of the binary tree node. It is used to distinguish one node from another in the binary tree.
|
|
29
|
-
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It represents the value that will be
|
|
30
|
-
* stored in the binary tree node. If no value is provided, it will be set to undefined.
|
|
31
|
-
*/
|
|
32
|
-
protected constructor(id: BinaryTreeNodeId, val?: T) {
|
|
33
|
-
this._id = id;
|
|
34
|
-
this._val = val;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private _id: BinaryTreeNodeId;
|
|
38
|
-
|
|
39
|
-
get id(): BinaryTreeNodeId {
|
|
40
|
-
return this._id;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
set id(v: BinaryTreeNodeId) {
|
|
44
|
-
this._id = v;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
private _val: T | undefined;
|
|
48
|
-
|
|
49
|
-
get val(): T | undefined {
|
|
50
|
-
return this._val;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
set val(value: T | undefined) {
|
|
54
|
-
this._val = value;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private _left: NEIGHBOR | null | undefined;
|
|
58
|
-
|
|
59
|
-
get left(): NEIGHBOR | null | undefined {
|
|
60
|
-
return this._left;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
set left(v: NEIGHBOR | null | undefined) {
|
|
64
|
-
if (v) {
|
|
65
|
-
v.parent = this as unknown as NEIGHBOR;
|
|
66
|
-
}
|
|
67
|
-
this._left = v;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private _right: NEIGHBOR | null | undefined;
|
|
71
|
-
|
|
72
|
-
get right(): NEIGHBOR | null | undefined {
|
|
73
|
-
return this._right;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
set right(v: NEIGHBOR | null | undefined) {
|
|
77
|
-
if (v) {
|
|
78
|
-
v.parent = this as unknown as NEIGHBOR;
|
|
79
|
-
}
|
|
80
|
-
this._right = v;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
private _parent: NEIGHBOR | null | undefined;
|
|
84
|
-
|
|
85
|
-
get parent(): NEIGHBOR | null | undefined {
|
|
86
|
-
return this._parent;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
set parent(v: NEIGHBOR | null | undefined) {
|
|
90
|
-
this._parent = v;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
private _height = 0;
|
|
94
|
-
|
|
95
|
-
get height(): number {
|
|
96
|
-
return this._height;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
set height(v: number) {
|
|
100
|
-
this._height = v;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* The function determines the position of a node in a family tree structure.
|
|
105
|
-
* @returns a value of type `FamilyPosition`.
|
|
106
|
-
*/
|
|
107
|
-
get familyPosition(): FamilyPosition {
|
|
108
|
-
const that = this as unknown as NEIGHBOR;
|
|
109
|
-
if (that.parent) {
|
|
110
|
-
if (that.parent.left === that) {
|
|
111
|
-
if (that.left || that.right) {
|
|
112
|
-
return FamilyPosition.ROOT_LEFT;
|
|
113
|
-
} else {
|
|
114
|
-
return FamilyPosition.LEFT;
|
|
115
|
-
}
|
|
116
|
-
} else if (that.parent.right === that) {
|
|
117
|
-
if (that.left || that.right) {
|
|
118
|
-
return FamilyPosition.ROOT_RIGHT;
|
|
119
|
-
} else {
|
|
120
|
-
return FamilyPosition.RIGHT;
|
|
121
|
-
}
|
|
122
|
-
} else {
|
|
123
|
-
return FamilyPosition.MAL_NODE;
|
|
124
|
-
}
|
|
125
|
-
} else {
|
|
126
|
-
if (that.left || that.right) {
|
|
127
|
-
return FamilyPosition.ROOT;
|
|
128
|
-
} else {
|
|
129
|
-
return FamilyPosition.ISOLATED;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'], N> = AbstractBinaryTreeNode> implements IAbstractBinaryTree<N> {
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* The protected constructor initializes the options for an abstract binary tree.
|
|
139
|
-
* @param {AbstractBinaryTreeOptions} [options] - An optional object that contains configuration options for the binary
|
|
140
|
-
* tree.
|
|
141
|
-
*/
|
|
142
|
-
protected constructor(options?: AbstractBinaryTreeOptions) {
|
|
143
|
-
if (options !== undefined) {
|
|
144
|
-
const {
|
|
145
|
-
loopType = LoopType.ITERATIVE,
|
|
146
|
-
isMergeDuplicatedNodeById = true
|
|
147
|
-
} = options;
|
|
148
|
-
this._isMergeDuplicatedNodeById = isMergeDuplicatedNodeById;
|
|
149
|
-
this._loopType = loopType;
|
|
150
|
-
} else {
|
|
151
|
-
this._isMergeDuplicatedNodeById = true;
|
|
152
|
-
this._loopType = LoopType.ITERATIVE;
|
|
153
|
-
}
|
|
154
|
-
this.clear();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
private _root: N | null = null;
|
|
158
|
-
|
|
159
|
-
get root(): N | null {
|
|
160
|
-
return this._root;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
private _size: number = 0;
|
|
164
|
-
|
|
165
|
-
get size(): number {
|
|
166
|
-
return this._size;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
private _loopType: LoopType = LoopType.ITERATIVE;
|
|
170
|
-
|
|
171
|
-
get loopType(): LoopType {
|
|
172
|
-
return this._loopType;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// TODO this variable may be moved to TreeMultiset
|
|
176
|
-
private _isMergeDuplicatedNodeById: boolean = true;
|
|
177
|
-
|
|
178
|
-
get isMergeDuplicatedNodeById(): boolean {
|
|
179
|
-
return this._isMergeDuplicatedNodeById;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
private _visitedId: BinaryTreeNodeId[] = [];
|
|
183
|
-
|
|
184
|
-
get visitedId(): BinaryTreeNodeId[] {
|
|
185
|
-
return this._visitedId;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
private _visitedVal: N['val'][] = [];
|
|
189
|
-
|
|
190
|
-
get visitedVal(): N['val'][] {
|
|
191
|
-
return this._visitedVal;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
private _visitedNode: N[] = [];
|
|
195
|
-
|
|
196
|
-
get visitedNode(): N[] {
|
|
197
|
-
return this._visitedNode;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
private _visitedLeftSum: number[] = [];
|
|
201
|
-
|
|
202
|
-
get visitedLeftSum(): number[] {
|
|
203
|
-
return this._visitedLeftSum;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
abstract createNode(id: BinaryTreeNodeId, val?: N['val']): N | null ;
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* The `swapLocation` function swaps the location of two nodes in a binary tree.
|
|
210
|
-
* @param {N} srcNode - The source node that you want to swap with the destination node.
|
|
211
|
-
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
212
|
-
* be swapped to.
|
|
213
|
-
* @returns The `destNode` is being returned.
|
|
214
|
-
*/
|
|
215
|
-
swapLocation(srcNode: N, destNode: N): N {
|
|
216
|
-
const {id, val, height} = destNode;
|
|
217
|
-
const tempNode = this.createNode(id, val);
|
|
218
|
-
|
|
219
|
-
if (tempNode) {
|
|
220
|
-
tempNode.height = height;
|
|
221
|
-
|
|
222
|
-
destNode.id = srcNode.id;
|
|
223
|
-
destNode.val = srcNode.val;
|
|
224
|
-
destNode.height = srcNode.height;
|
|
225
|
-
|
|
226
|
-
srcNode.id = tempNode.id;
|
|
227
|
-
srcNode.val = tempNode.val;
|
|
228
|
-
srcNode.height = tempNode.height;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return destNode;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* The clear() function resets the root, size, and maxId properties to their initial values.
|
|
236
|
-
*/
|
|
237
|
-
clear() {
|
|
238
|
-
this._root = null;
|
|
239
|
-
this._size = 0;
|
|
240
|
-
this._clearResults();
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
245
|
-
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
246
|
-
*/
|
|
247
|
-
isEmpty(): boolean {
|
|
248
|
-
return this.size === 0;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
|
|
253
|
-
* In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
|
|
254
|
-
*/
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
|
|
258
|
-
* @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId`, which
|
|
259
|
-
* is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
|
|
260
|
-
* node itself. It can also be `null` if no node is specified.
|
|
261
|
-
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
262
|
-
* being added to the binary tree.
|
|
263
|
-
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
264
|
-
*/
|
|
265
|
-
add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val']): N | null | undefined {
|
|
266
|
-
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
267
|
-
const queue: Array<N | null> = [root];
|
|
268
|
-
while (queue.length > 0) {
|
|
269
|
-
const cur = queue.shift();
|
|
270
|
-
if (cur) {
|
|
271
|
-
const inserted = this.addTo(newNode, cur);
|
|
272
|
-
if (inserted !== undefined) return inserted;
|
|
273
|
-
if (cur.left) queue.push(cur.left);
|
|
274
|
-
if (cur.right) queue.push(cur.right);
|
|
275
|
-
} else return;
|
|
276
|
-
}
|
|
277
|
-
return;
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
let inserted: N | null | undefined, needInsert: N | null;
|
|
281
|
-
|
|
282
|
-
if (idOrNode === null) {
|
|
283
|
-
needInsert = null;
|
|
284
|
-
} else if (typeof idOrNode === 'number') {
|
|
285
|
-
needInsert = this.createNode(idOrNode, val);
|
|
286
|
-
} else if (idOrNode instanceof AbstractBinaryTreeNode) {
|
|
287
|
-
needInsert = idOrNode;
|
|
288
|
-
} else {
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
const existNode = idOrNode ? this.get(idOrNode, 'id') : undefined;
|
|
293
|
-
|
|
294
|
-
if (this.root) {
|
|
295
|
-
if (existNode) {
|
|
296
|
-
existNode.val = val;
|
|
297
|
-
inserted = existNode;
|
|
298
|
-
} else {
|
|
299
|
-
inserted = _bfs(this.root, needInsert);
|
|
300
|
-
}
|
|
301
|
-
} else {
|
|
302
|
-
this._setRoot(needInsert);
|
|
303
|
-
if (needInsert !== null) {
|
|
304
|
-
this._setSize(1);
|
|
305
|
-
} else {
|
|
306
|
-
this._setSize(0);
|
|
307
|
-
}
|
|
308
|
-
inserted = this.root;
|
|
309
|
-
}
|
|
310
|
-
return inserted;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* The function adds a new node to the left or right child of a parent node, updating the size of the tree if
|
|
315
|
-
* necessary.
|
|
316
|
-
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
|
|
317
|
-
* either a node object (`N`) or `null`.
|
|
318
|
-
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
319
|
-
* child.
|
|
320
|
-
* @returns either the left child node, the right child node, or undefined.
|
|
321
|
-
*/
|
|
322
|
-
addTo(newNode: N | null, parent: N): N | null | undefined {
|
|
323
|
-
if (parent) {
|
|
324
|
-
// When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
|
|
325
|
-
// In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
|
|
326
|
-
if (parent.left === undefined) {
|
|
327
|
-
parent.left = newNode;
|
|
328
|
-
if (newNode) {
|
|
329
|
-
this._setSize(this.size + 1);
|
|
330
|
-
}
|
|
331
|
-
return parent.left;
|
|
332
|
-
} else if (parent.right === undefined) {
|
|
333
|
-
parent.right = newNode;
|
|
334
|
-
if (newNode) {
|
|
335
|
-
this._setSize(this.size + 1);
|
|
336
|
-
}
|
|
337
|
-
return parent.right;
|
|
338
|
-
} else {
|
|
339
|
-
return;
|
|
340
|
-
}
|
|
341
|
-
} else {
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* The `addMany` function adds multiple nodes to a tree data structure and returns an array of the inserted nodes or
|
|
348
|
-
* null/undefined values.
|
|
349
|
-
* @param {(BinaryTreeNodeId|N)[]} idsOrNodes - An array of BinaryTreeNodeId or N objects. These can be either the ID
|
|
350
|
-
* of a binary tree node or the actual node object itself.
|
|
351
|
-
* @param {N['val'][]} [data] - Optional array of values to be added to the nodes. If provided, the length of this
|
|
352
|
-
* array should be the same as the length of the `idsOrNodes` array.
|
|
353
|
-
* @returns The function `addMany` returns an array of values `(N | null | undefined)[]`.
|
|
354
|
-
*/
|
|
355
|
-
addMany(idsOrNodes: (BinaryTreeNodeId | N | null)[], data?: N['val'][]): (N | null | undefined)[] {
|
|
356
|
-
// TODO not sure addMany not be run multi times
|
|
357
|
-
const inserted: (N | null | undefined)[] = [];
|
|
358
|
-
const map: Map<N | BinaryTreeNodeId | null, number> = new Map();
|
|
359
|
-
|
|
360
|
-
if (this.isMergeDuplicatedNodeById) {
|
|
361
|
-
for (const idOrNode of idsOrNodes) map.set(idOrNode, (map.get(idOrNode) ?? 0) + 1);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
for (let i = 0; i < idsOrNodes.length; i++) {
|
|
365
|
-
const idOrNode = idsOrNodes[i];
|
|
366
|
-
if (idOrNode instanceof AbstractBinaryTreeNode) {
|
|
367
|
-
inserted.push(this.add(idOrNode.id, idOrNode.val));
|
|
368
|
-
continue;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
if (idOrNode === null) {
|
|
372
|
-
inserted.push(this.add(null));
|
|
373
|
-
continue;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
const val = data?.[i];
|
|
377
|
-
if (this.isMergeDuplicatedNodeById) {
|
|
378
|
-
if (map.has(idOrNode)) {
|
|
379
|
-
inserted.push(this.add(idOrNode, val));
|
|
380
|
-
map.delete(idOrNode);
|
|
381
|
-
}
|
|
382
|
-
} else {
|
|
383
|
-
inserted.push(this.add(idOrNode, val));
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
return inserted;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* The `fill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
391
|
-
* @param {(BinaryTreeNodeId | N)[]} idsOrNodes - The `idsOrNodes` parameter is an array that can contain either
|
|
392
|
-
* `BinaryTreeNodeId` or `N` values.
|
|
393
|
-
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
394
|
-
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `idsOrNodes`
|
|
395
|
-
* array. Each value in the `data` array will be assigned to the
|
|
396
|
-
* @returns The method is returning a boolean value.
|
|
397
|
-
*/
|
|
398
|
-
fill(idsOrNodes: (BinaryTreeNodeId | N | null)[], data?: N[] | Array<N['val']>): boolean {
|
|
399
|
-
this.clear();
|
|
400
|
-
return idsOrNodes.length === this.addMany(idsOrNodes, data).length;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
405
|
-
* node that needs to be balanced.
|
|
406
|
-
* @param {N | BinaryTreeNodeId} nodeOrId - The `nodeOrId` parameter can be either a node object (`N`) or a binary tree
|
|
407
|
-
* node ID (`BinaryTreeNodeId`).
|
|
408
|
-
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
409
|
-
* whether to ignore the count of the nodes in the binary tree. If `ignoreCount` is set to `true`, the count of the
|
|
410
|
-
* nodes in the binary tree will not be updated after removing a node. If `ignoreCount`
|
|
411
|
-
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
412
|
-
*/
|
|
413
|
-
remove(nodeOrId: N | BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[] {
|
|
414
|
-
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
415
|
-
if (!this.root) return bstDeletedResult;
|
|
416
|
-
|
|
417
|
-
const curr: N | null = (typeof nodeOrId === 'number') ? this.get(nodeOrId) : nodeOrId;
|
|
418
|
-
if (!curr) return bstDeletedResult;
|
|
419
|
-
|
|
420
|
-
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
421
|
-
let needBalanced: N | null = null, orgCurrent = curr;
|
|
422
|
-
|
|
423
|
-
if (!curr.left) {
|
|
424
|
-
if (!parent) {
|
|
425
|
-
if (curr.right !== undefined) this._setRoot(curr.right);
|
|
426
|
-
} else {
|
|
427
|
-
const {familyPosition: fp} = curr;
|
|
428
|
-
if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
|
|
429
|
-
parent.left = curr.right;
|
|
430
|
-
} else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
|
|
431
|
-
parent.right = curr.right;
|
|
432
|
-
}
|
|
433
|
-
needBalanced = parent;
|
|
434
|
-
}
|
|
435
|
-
} else {
|
|
436
|
-
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
437
|
-
if (leftSubTreeRightMost) {
|
|
438
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
439
|
-
orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
|
|
440
|
-
if (parentOfLeftSubTreeMax) {
|
|
441
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
442
|
-
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
443
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
this._setSize(this.size - 1);
|
|
448
|
-
|
|
449
|
-
bstDeletedResult.push({deleted: orgCurrent, needBalanced});
|
|
450
|
-
return bstDeletedResult;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
* The function calculates the depth of a node in a binary tree.
|
|
455
|
-
* @param {N | BinaryTreeNodeId | null} beginRoot - The `beginRoot` parameter can be one of the following:
|
|
456
|
-
* @returns the depth of the given node or binary tree.
|
|
457
|
-
*/
|
|
458
|
-
getDepth(beginRoot: N | BinaryTreeNodeId | null): number {
|
|
459
|
-
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
|
|
460
|
-
|
|
461
|
-
let depth = 0;
|
|
462
|
-
while (beginRoot?.parent) {
|
|
463
|
-
depth++;
|
|
464
|
-
beginRoot = beginRoot.parent;
|
|
465
|
-
}
|
|
466
|
-
return depth;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
/**
|
|
470
|
-
* The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
|
|
471
|
-
* @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
472
|
-
* generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
|
|
473
|
-
* node), or `null`.
|
|
474
|
-
* @returns the height of the binary tree.
|
|
475
|
-
*/
|
|
476
|
-
getHeight(beginRoot?: N | BinaryTreeNodeId | null): number {
|
|
477
|
-
beginRoot = beginRoot ?? this.root;
|
|
478
|
-
|
|
479
|
-
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
|
|
480
|
-
if (!beginRoot) return -1;
|
|
481
|
-
|
|
482
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
483
|
-
const _getMaxHeight = (cur: N | null | undefined): number => {
|
|
484
|
-
if (!cur) return -1;
|
|
485
|
-
const leftHeight = _getMaxHeight(cur.left);
|
|
486
|
-
const rightHeight = _getMaxHeight(cur.right);
|
|
487
|
-
return Math.max(leftHeight, rightHeight) + 1;
|
|
488
|
-
};
|
|
489
|
-
|
|
490
|
-
return _getMaxHeight(beginRoot);
|
|
491
|
-
} else {
|
|
492
|
-
if (!beginRoot) {
|
|
493
|
-
return -1;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
|
|
497
|
-
let maxHeight = 0;
|
|
498
|
-
|
|
499
|
-
while (stack.length > 0) {
|
|
500
|
-
const {node, depth} = stack.pop()!;
|
|
501
|
-
|
|
502
|
-
if (node.left) {
|
|
503
|
-
stack.push({node: node.left, depth: depth + 1});
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
if (node.right) {
|
|
507
|
-
stack.push({node: node.right, depth: depth + 1});
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
maxHeight = Math.max(maxHeight, depth);
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
return maxHeight;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
|
|
519
|
-
* approach.
|
|
520
|
-
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
|
|
521
|
-
* represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
|
|
522
|
-
* for `beginRoot`, the `this.root` property is used as the default value.
|
|
523
|
-
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
524
|
-
*/
|
|
525
|
-
getMinHeight(beginRoot?: N | null): number {
|
|
526
|
-
beginRoot = beginRoot || this.root;
|
|
527
|
-
if (!beginRoot) return -1;
|
|
528
|
-
|
|
529
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
530
|
-
const _getMinHeight = (cur: N | null | undefined): number => {
|
|
531
|
-
if (!cur) return 0;
|
|
532
|
-
if (!cur.left && !cur.right) return 0;
|
|
533
|
-
const leftMinHeight = _getMinHeight(cur.left);
|
|
534
|
-
const rightMinHeight = _getMinHeight(cur.right);
|
|
535
|
-
return Math.min(leftMinHeight, rightMinHeight) + 1;
|
|
536
|
-
};
|
|
537
|
-
|
|
538
|
-
return _getMinHeight(beginRoot);
|
|
539
|
-
} else {
|
|
540
|
-
const stack: N[] = [];
|
|
541
|
-
let node: N | null | undefined = beginRoot, last: N | null = null;
|
|
542
|
-
const depths: Map<N, number> = new Map();
|
|
543
|
-
|
|
544
|
-
while (stack.length > 0 || node) {
|
|
545
|
-
if (node) {
|
|
546
|
-
stack.push(node);
|
|
547
|
-
node = node.left;
|
|
548
|
-
} else {
|
|
549
|
-
node = stack[stack.length - 1]
|
|
550
|
-
if (!node.right || last === node.right) {
|
|
551
|
-
node = stack.pop();
|
|
552
|
-
if (node) {
|
|
553
|
-
const leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
|
|
554
|
-
const rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
|
|
555
|
-
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
556
|
-
last = node;
|
|
557
|
-
node = null;
|
|
558
|
-
}
|
|
559
|
-
} else node = node.right
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
return depths.get(beginRoot) ?? -1;
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
/**
|
|
568
|
-
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
|
|
569
|
-
* tree.
|
|
570
|
-
* @param {N | null} [beginRoot] - The parameter `beginRoot` is of type `N` or `null`. It represents the root node of a
|
|
571
|
-
* tree or null if the tree is empty.
|
|
572
|
-
* @returns The method is returning a boolean value.
|
|
573
|
-
*/
|
|
574
|
-
isPerfectlyBalanced(beginRoot?: N | null): boolean {
|
|
575
|
-
return (this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot));
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
/**
|
|
579
|
-
* The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
|
|
580
|
-
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
581
|
-
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
582
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
583
|
-
* specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
|
|
584
|
-
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
585
|
-
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
|
|
586
|
-
* function will stop traversing the tree and return the first matching node. If `only
|
|
587
|
-
* @returns an array of nodes (type N).
|
|
588
|
-
*/
|
|
589
|
-
getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean): N[] {
|
|
590
|
-
if (!this.root) return [];
|
|
591
|
-
propertyName = propertyName ?? 'id';
|
|
592
|
-
|
|
593
|
-
const result: N[] = [];
|
|
594
|
-
|
|
595
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
596
|
-
const _traverse = (cur: N) => {
|
|
597
|
-
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
|
|
598
|
-
if (!cur.left && !cur.right) return;
|
|
599
|
-
cur.left && _traverse(cur.left);
|
|
600
|
-
cur.right && _traverse(cur.right);
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
_traverse(this.root);
|
|
604
|
-
} else {
|
|
605
|
-
const queue: N[] = [this.root];
|
|
606
|
-
while (queue.length > 0) {
|
|
607
|
-
const cur = queue.shift();
|
|
608
|
-
if (cur) {
|
|
609
|
-
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result;
|
|
610
|
-
cur.left && queue.push(cur.left);
|
|
611
|
-
cur.right && queue.push(cur.right);
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
return result;
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
/**
|
|
620
|
-
* The function checks if a binary tree node has a specific property.
|
|
621
|
-
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
|
|
622
|
-
* It represents the property of the binary tree node that you want to check.
|
|
623
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
624
|
-
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'id'.
|
|
625
|
-
* @returns a boolean value.
|
|
626
|
-
*/
|
|
627
|
-
has(nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName): boolean {
|
|
628
|
-
propertyName = propertyName ?? 'id';
|
|
629
|
-
// TODO may support finding node by value equal
|
|
630
|
-
return this.getNodes(nodeProperty, propertyName).length > 0;
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
/**
|
|
634
|
-
* The function returns the first node that matches the given property name and value, or null if no matching node is
|
|
635
|
-
* found.
|
|
636
|
-
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
|
|
637
|
-
* It represents the property of the binary tree node that you want to search for.
|
|
638
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
639
|
-
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
|
|
640
|
-
* default value is set to `'id'`.
|
|
641
|
-
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
642
|
-
* If no matching node is found, it returns null.
|
|
643
|
-
*/
|
|
644
|
-
get(nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName): N | null {
|
|
645
|
-
propertyName = propertyName ?? 'id';
|
|
646
|
-
// TODO may support finding node by value equal
|
|
647
|
-
return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
/**
|
|
651
|
-
* The function getPathToRoot takes a node and returns an array of nodes representing the path from the given node to
|
|
652
|
-
* the root node.
|
|
653
|
-
* @param {N} node - The parameter `node` represents a node in a tree data structure.
|
|
654
|
-
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
655
|
-
*/
|
|
656
|
-
getPathToRoot(node: N): N[] {
|
|
657
|
-
// TODO to support get path through passing id
|
|
658
|
-
const result: N[] = [];
|
|
659
|
-
while (node.parent) {
|
|
660
|
-
result.unshift(node);
|
|
661
|
-
node = node.parent;
|
|
662
|
-
}
|
|
663
|
-
result.unshift(node);
|
|
664
|
-
return result;
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
getLeftMost(): N | null;
|
|
668
|
-
|
|
669
|
-
getLeftMost(node: N): N;
|
|
670
|
-
|
|
671
|
-
/**
|
|
672
|
-
* The `getLeftMost` function returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
673
|
-
* no node is specified.
|
|
674
|
-
* @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
675
|
-
* generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
|
|
676
|
-
* node), or `null`.
|
|
677
|
-
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
678
|
-
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the
|
|
679
|
-
* traversal from the root of the binary tree. If there are no nodes in the binary tree, it returns `null`.
|
|
680
|
-
*/
|
|
681
|
-
getLeftMost(beginRoot?: N | BinaryTreeNodeId | null): N | null {
|
|
682
|
-
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
|
|
683
|
-
|
|
684
|
-
beginRoot = beginRoot ?? this.root;
|
|
685
|
-
if (!beginRoot) return beginRoot;
|
|
686
|
-
|
|
687
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
688
|
-
|
|
689
|
-
const _traverse = (cur: N): N => {
|
|
690
|
-
if (!cur.left) return cur;
|
|
691
|
-
return _traverse(cur.left);
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
return _traverse(beginRoot);
|
|
695
|
-
} else {
|
|
696
|
-
// Indirect implementation of iteration using tail recursion optimization
|
|
697
|
-
const _traverse = trampoline((cur: N) => {
|
|
698
|
-
if (!cur.left) return cur;
|
|
699
|
-
return _traverse.cont(cur.left);
|
|
700
|
-
});
|
|
701
|
-
|
|
702
|
-
return _traverse(beginRoot);
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
getRightMost(): N | null;
|
|
707
|
-
|
|
708
|
-
getRightMost(node: N): N;
|
|
709
|
-
|
|
710
|
-
/**
|
|
711
|
-
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using
|
|
712
|
-
* tail recursion optimization.
|
|
713
|
-
* @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
714
|
-
* starting node from which we want to find the rightmost node. If no node is provided, the `node` parameter defaults
|
|
715
|
-
* to `this.root`, which is the root node of the data structure
|
|
716
|
-
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the `node` parameter is
|
|
717
|
-
* not provided, it defaults to the root node of the tree. If the tree is empty or the `node` parameter is `null`, the
|
|
718
|
-
* function returns `null`.
|
|
719
|
-
*/
|
|
720
|
-
getRightMost(node?: N | null): N | null {
|
|
721
|
-
// TODO support get right most by passing id in
|
|
722
|
-
node = node ?? this.root;
|
|
723
|
-
if (!node) return node;
|
|
724
|
-
|
|
725
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
726
|
-
const _traverse = (cur: N): N => {
|
|
727
|
-
if (!cur.right) return cur;
|
|
728
|
-
return _traverse(cur.right);
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
return _traverse(node);
|
|
732
|
-
} else {
|
|
733
|
-
// Indirect implementation of iteration using tail recursion optimization
|
|
734
|
-
const _traverse = trampoline((cur: N) => {
|
|
735
|
-
if (!cur.right) return cur;
|
|
736
|
-
return _traverse.cont(cur.right);
|
|
737
|
-
});
|
|
738
|
-
|
|
739
|
-
return _traverse(node);
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
/**
|
|
744
|
-
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
745
|
-
* @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
|
|
746
|
-
* @returns a boolean value.
|
|
747
|
-
*/
|
|
748
|
-
isSubtreeBST(node: N | null): boolean {
|
|
749
|
-
// TODO there is a bug
|
|
750
|
-
if (!node) return true;
|
|
751
|
-
|
|
752
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
753
|
-
const dfs = (cur: N | null | undefined, min: BinaryTreeNodeId, max: BinaryTreeNodeId): boolean => {
|
|
754
|
-
if (!cur) return true;
|
|
755
|
-
if (cur.id <= min || cur.id >= max) return false;
|
|
756
|
-
return dfs(cur.left, min, cur.id) && dfs(cur.right, cur.id, max);
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
760
|
-
} else {
|
|
761
|
-
const stack = [];
|
|
762
|
-
let prev = Number.MIN_SAFE_INTEGER, curr: N | null | undefined = node;
|
|
763
|
-
while (curr || stack.length > 0) {
|
|
764
|
-
while (curr) {
|
|
765
|
-
stack.push(curr);
|
|
766
|
-
curr = curr.left;
|
|
767
|
-
}
|
|
768
|
-
curr = stack.pop()!;
|
|
769
|
-
if (!(curr) || prev >= curr.id) return false;
|
|
770
|
-
prev = curr.id;
|
|
771
|
-
curr = curr.right;
|
|
772
|
-
}
|
|
773
|
-
return true;
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
/**
|
|
778
|
-
* The function isBST checks if the binary search tree is valid.
|
|
779
|
-
* @returns The `isBST()` function is returning a boolean value.
|
|
780
|
-
*/
|
|
781
|
-
isBST(): boolean {
|
|
782
|
-
return this.isSubtreeBST(this.root);
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
/**
|
|
786
|
-
* The function calculates the size of a subtree by traversing it either recursively or iteratively.
|
|
787
|
-
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
788
|
-
* binary tree.
|
|
789
|
-
* @returns the size of the subtree rooted at `subTreeRoot`.
|
|
790
|
-
*/
|
|
791
|
-
getSubTreeSize(subTreeRoot: N | null | undefined) {
|
|
792
|
-
// TODO support id passed in
|
|
793
|
-
let size = 0;
|
|
794
|
-
if (!subTreeRoot) return size;
|
|
795
|
-
|
|
796
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
797
|
-
const _traverse = (cur: N) => {
|
|
798
|
-
size++;
|
|
799
|
-
cur.left && _traverse(cur.left);
|
|
800
|
-
cur.right && _traverse(cur.right);
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
_traverse(subTreeRoot);
|
|
804
|
-
return size;
|
|
805
|
-
} else {
|
|
806
|
-
const stack: N[] = [subTreeRoot];
|
|
807
|
-
|
|
808
|
-
while (stack.length > 0) {
|
|
809
|
-
const cur = stack.pop()!;
|
|
810
|
-
size++;
|
|
811
|
-
cur.right && stack.push(cur.right);
|
|
812
|
-
cur.left && stack.push(cur.left);
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
return size;
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
/**
|
|
820
|
-
* The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
|
|
821
|
-
* @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
822
|
-
* tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
|
|
823
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
|
|
824
|
-
* property of the binary tree node to use for calculating the sum. It can be either 'id' or 'val'. If propertyName is
|
|
825
|
-
* not provided, it defaults to 'id'.
|
|
826
|
-
* @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
|
|
827
|
-
*/
|
|
828
|
-
subTreeSum(subTreeRoot: N | BinaryTreeNodeId | null, propertyName ?: BinaryTreeNodePropertyName): number {
|
|
829
|
-
propertyName = propertyName ?? 'id';
|
|
830
|
-
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
|
|
831
|
-
|
|
832
|
-
if (!subTreeRoot) return 0;
|
|
833
|
-
|
|
834
|
-
let sum = 0;
|
|
835
|
-
|
|
836
|
-
const _sumByProperty = (cur: N) => {
|
|
837
|
-
let needSum: number;
|
|
838
|
-
switch (propertyName) {
|
|
839
|
-
case 'id':
|
|
840
|
-
needSum = cur.id;
|
|
841
|
-
break;
|
|
842
|
-
case 'val':
|
|
843
|
-
needSum = typeof cur.val === 'number' ? cur.val : 0;
|
|
844
|
-
break;
|
|
845
|
-
default:
|
|
846
|
-
needSum = cur.id;
|
|
847
|
-
break;
|
|
848
|
-
}
|
|
849
|
-
return needSum;
|
|
850
|
-
}
|
|
851
|
-
|
|
852
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
853
|
-
const _traverse = (cur: N): void => {
|
|
854
|
-
sum += _sumByProperty(cur);
|
|
855
|
-
cur.left && _traverse(cur.left);
|
|
856
|
-
cur.right && _traverse(cur.right);
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
_traverse(subTreeRoot);
|
|
860
|
-
} else {
|
|
861
|
-
const stack: N[] = [subTreeRoot];
|
|
862
|
-
|
|
863
|
-
while (stack.length > 0) {
|
|
864
|
-
const cur = stack.pop()!;
|
|
865
|
-
sum += _sumByProperty(cur);
|
|
866
|
-
cur.right && stack.push(cur.right);
|
|
867
|
-
cur.left && stack.push(cur.left);
|
|
868
|
-
}
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
return sum;
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
/**
|
|
875
|
-
* The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
|
|
876
|
-
* @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
877
|
-
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
878
|
-
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
879
|
-
* each node in the subtree should be incremented.
|
|
880
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
881
|
-
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'id'.
|
|
882
|
-
* @returns a boolean value.
|
|
883
|
-
*/
|
|
884
|
-
subTreeAdd(subTreeRoot: N | BinaryTreeNodeId | null, delta: number, propertyName ?: BinaryTreeNodePropertyName): boolean {
|
|
885
|
-
propertyName = propertyName ?? 'id';
|
|
886
|
-
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
|
|
887
|
-
|
|
888
|
-
if (!subTreeRoot) return false;
|
|
889
|
-
|
|
890
|
-
const _addByProperty = (cur: N) => {
|
|
891
|
-
switch (propertyName) {
|
|
892
|
-
case 'id':
|
|
893
|
-
cur.id += delta;
|
|
894
|
-
break;
|
|
895
|
-
default:
|
|
896
|
-
cur.id += delta;
|
|
897
|
-
break;
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
|
|
901
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
902
|
-
const _traverse = (cur: N) => {
|
|
903
|
-
_addByProperty(cur);
|
|
904
|
-
cur.left && _traverse(cur.left);
|
|
905
|
-
cur.right && _traverse(cur.right);
|
|
906
|
-
};
|
|
907
|
-
|
|
908
|
-
_traverse(subTreeRoot);
|
|
909
|
-
} else {
|
|
910
|
-
const stack: N[] = [subTreeRoot];
|
|
911
|
-
|
|
912
|
-
while (stack.length > 0) {
|
|
913
|
-
const cur = stack.pop()!;
|
|
914
|
-
|
|
915
|
-
_addByProperty(cur);
|
|
916
|
-
cur.right && stack.push(cur.right);
|
|
917
|
-
cur.left && stack.push(cur.left);
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
return true;
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
BFS(): BinaryTreeNodeId[];
|
|
924
|
-
|
|
925
|
-
BFS(nodeOrPropertyName: 'id'): BinaryTreeNodeId[];
|
|
926
|
-
|
|
927
|
-
BFS(nodeOrPropertyName: 'val'): N['val'][];
|
|
928
|
-
|
|
929
|
-
BFS(nodeOrPropertyName: 'node'): N[];
|
|
930
|
-
|
|
931
|
-
/**
|
|
932
|
-
* The BFS function performs a breadth-first search on a binary tree, accumulating properties of each node based on a
|
|
933
|
-
* specified property name.
|
|
934
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
935
|
-
* represents either a node or a property name. If a node is provided, the breadth-first search (BFS) algorithm will be
|
|
936
|
-
* performed starting from that node. If a property name is provided, the BFS algorithm will be performed starting from
|
|
937
|
-
* the
|
|
938
|
-
* @returns an instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
|
|
939
|
-
*/
|
|
940
|
-
BFS(nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
|
941
|
-
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
|
|
942
|
-
this._clearResults();
|
|
943
|
-
const queue: Array<N | null | undefined> = [this.root];
|
|
944
|
-
|
|
945
|
-
while (queue.length !== 0) {
|
|
946
|
-
const cur = queue.shift();
|
|
947
|
-
if (cur) {
|
|
948
|
-
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
949
|
-
if (cur?.left !== null) queue.push(cur.left);
|
|
950
|
-
if (cur?.right !== null) queue.push(cur.right);
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
|
|
954
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
DFS(): BinaryTreeNodeId[];
|
|
958
|
-
|
|
959
|
-
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
|
|
960
|
-
|
|
961
|
-
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
|
|
962
|
-
|
|
963
|
-
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
|
|
964
|
-
|
|
965
|
-
/**
|
|
966
|
-
* The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
967
|
-
* each node based on the specified pattern and property name.
|
|
968
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is used to specify the traversal order of the
|
|
969
|
-
* binary tree. It can have three possible values:
|
|
970
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is a string that represents
|
|
971
|
-
* the name of a property of the nodes in the binary tree. This property will be used to accumulate values during the
|
|
972
|
-
* depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'id'`.
|
|
973
|
-
* @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the
|
|
974
|
-
* binary tree nodes based on the specified pattern and node or property name.
|
|
975
|
-
*/
|
|
976
|
-
DFS(pattern ?: 'in' | 'pre' | 'post', nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
|
977
|
-
pattern = pattern ?? 'in';
|
|
978
|
-
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
|
|
979
|
-
this._clearResults();
|
|
980
|
-
const _traverse = (node: N) => {
|
|
981
|
-
switch (pattern) {
|
|
982
|
-
case 'in':
|
|
983
|
-
if (node.left) _traverse(node.left);
|
|
984
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
985
|
-
if (node.right) _traverse(node.right);
|
|
986
|
-
break;
|
|
987
|
-
case 'pre':
|
|
988
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
989
|
-
if (node.left) _traverse(node.left);
|
|
990
|
-
if (node.right) _traverse(node.right);
|
|
991
|
-
break;
|
|
992
|
-
case 'post':
|
|
993
|
-
if (node.left) _traverse(node.left);
|
|
994
|
-
if (node.right) _traverse(node.right);
|
|
995
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
996
|
-
break;
|
|
997
|
-
}
|
|
998
|
-
};
|
|
999
|
-
|
|
1000
|
-
this.root && _traverse(this.root);
|
|
1001
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
// --- start additional methods ---
|
|
1006
|
-
|
|
1007
|
-
DFSIterative(): BinaryTreeNodeId[];
|
|
1008
|
-
|
|
1009
|
-
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
|
|
1010
|
-
|
|
1011
|
-
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
|
|
1012
|
-
|
|
1013
|
-
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
|
|
1014
|
-
|
|
1015
|
-
/**
|
|
1016
|
-
* The DFSIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
|
|
1017
|
-
* specify the traversal pattern and the property name to accumulate results by.
|
|
1018
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter determines the order in which the nodes of the
|
|
1019
|
-
* binary tree are visited during the depth-first search. It can have one of the following values:
|
|
1020
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
|
|
1021
|
-
* property of the nodes that you want to retrieve or perform operations on during the depth-first search traversal. By
|
|
1022
|
-
* default, it is set to `'id'`, which means that the traversal will accumulate results based on the `id` property of
|
|
1023
|
-
* the
|
|
1024
|
-
* @returns an object of type AbstractBinaryTreeNodeProperties<N>.
|
|
1025
|
-
*/
|
|
1026
|
-
DFSIterative(pattern ?: 'in' | 'pre' | 'post', nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
|
1027
|
-
pattern = pattern || 'in';
|
|
1028
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
1029
|
-
this._clearResults();
|
|
1030
|
-
if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1031
|
-
// 0: visit, 1: print
|
|
1032
|
-
const stack: { opt: 0 | 1, node: N | null | undefined }[] = [{opt: 0, node: this.root}];
|
|
1033
|
-
|
|
1034
|
-
while (stack.length > 0) {
|
|
1035
|
-
const cur = stack.pop();
|
|
1036
|
-
if (!cur || !cur.node) continue;
|
|
1037
|
-
if (cur.opt === 1) {
|
|
1038
|
-
this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
|
|
1039
|
-
} else {
|
|
1040
|
-
switch (pattern) {
|
|
1041
|
-
case 'in':
|
|
1042
|
-
stack.push({opt: 0, node: cur.node.right});
|
|
1043
|
-
stack.push({opt: 1, node: cur.node});
|
|
1044
|
-
stack.push({opt: 0, node: cur.node.left});
|
|
1045
|
-
break;
|
|
1046
|
-
case 'pre':
|
|
1047
|
-
stack.push({opt: 0, node: cur.node.right});
|
|
1048
|
-
stack.push({opt: 0, node: cur.node.left});
|
|
1049
|
-
stack.push({opt: 1, node: cur.node});
|
|
1050
|
-
break;
|
|
1051
|
-
case 'post':
|
|
1052
|
-
stack.push({opt: 1, node: cur.node});
|
|
1053
|
-
stack.push({opt: 0, node: cur.node.right});
|
|
1054
|
-
stack.push({opt: 0, node: cur.node.left});
|
|
1055
|
-
break;
|
|
1056
|
-
default:
|
|
1057
|
-
stack.push({opt: 0, node: cur.node.right});
|
|
1058
|
-
stack.push({opt: 1, node: cur.node});
|
|
1059
|
-
stack.push({opt: 0, node: cur.node.left});
|
|
1060
|
-
break;
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1066
|
-
}
|
|
1067
|
-
|
|
1068
|
-
levelIterative(node: N | null): BinaryTreeNodeId[];
|
|
1069
|
-
|
|
1070
|
-
levelIterative(node: N | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
|
|
1071
|
-
|
|
1072
|
-
levelIterative(node: N | null, nodeOrPropertyName?: 'val'): N['val'][];
|
|
1073
|
-
|
|
1074
|
-
levelIterative(node: N | null, nodeOrPropertyName?: 'node'): N[];
|
|
1075
|
-
|
|
1076
|
-
/**
|
|
1077
|
-
* The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
|
|
1078
|
-
* in an array, based on a specified property name.
|
|
1079
|
-
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
|
|
1080
|
-
* node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
|
|
1081
|
-
* the tree is used as the starting node.
|
|
1082
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1083
|
-
* can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
|
|
1084
|
-
* will accumulate results based on that property. If no property name is provided, the function will default to
|
|
1085
|
-
* accumulating results
|
|
1086
|
-
* @returns The function `levelIterative` returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
1087
|
-
*/
|
|
1088
|
-
levelIterative(node: N | null, nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
|
1089
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
1090
|
-
node = node || this.root;
|
|
1091
|
-
if (!node) return [];
|
|
1092
|
-
|
|
1093
|
-
this._clearResults();
|
|
1094
|
-
const queue: N[] = [node];
|
|
1095
|
-
|
|
1096
|
-
while (queue.length > 0) {
|
|
1097
|
-
const cur = queue.shift();
|
|
1098
|
-
if (cur) {
|
|
1099
|
-
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1100
|
-
if (cur.left) {
|
|
1101
|
-
queue.push(cur.left);
|
|
1102
|
-
}
|
|
1103
|
-
if (cur.right) {
|
|
1104
|
-
queue.push(cur.right);
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1110
|
-
}
|
|
1111
|
-
|
|
1112
|
-
listLevels(node: N | null): BinaryTreeNodeId[][];
|
|
1113
|
-
|
|
1114
|
-
listLevels(node: N | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[][];
|
|
1115
|
-
|
|
1116
|
-
listLevels(node: N | null, nodeOrPropertyName?: 'val'): N['val'][][];
|
|
1117
|
-
|
|
1118
|
-
listLevels(node: N | null, nodeOrPropertyName?: 'node'): N[][];
|
|
1119
|
-
|
|
1120
|
-
/**
|
|
1121
|
-
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1122
|
-
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the
|
|
1123
|
-
* root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
|
|
1124
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1125
|
-
* specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following
|
|
1126
|
-
* values:
|
|
1127
|
-
* @returns The function `listLevels` returns a 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
|
|
1128
|
-
*/
|
|
1129
|
-
listLevels(node: N | null, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperty<N>[][] {
|
|
1130
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
1131
|
-
node = node || this.root;
|
|
1132
|
-
if (!node) return [];
|
|
1133
|
-
|
|
1134
|
-
const levelsNodes: AbstractBinaryTreeNodeProperty<N>[][] = [];
|
|
1135
|
-
|
|
1136
|
-
const collectByProperty = (node: N, level: number) => {
|
|
1137
|
-
switch (nodeOrPropertyName) {
|
|
1138
|
-
case 'id':
|
|
1139
|
-
levelsNodes[level].push(node.id);
|
|
1140
|
-
break;
|
|
1141
|
-
case 'val':
|
|
1142
|
-
levelsNodes[level].push(node.val);
|
|
1143
|
-
break;
|
|
1144
|
-
case 'node':
|
|
1145
|
-
levelsNodes[level].push(node);
|
|
1146
|
-
break;
|
|
1147
|
-
default:
|
|
1148
|
-
levelsNodes[level].push(node.id);
|
|
1149
|
-
break;
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
|
|
1153
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
1154
|
-
const _recursive = (node: N, level: number) => {
|
|
1155
|
-
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
1156
|
-
collectByProperty(node, level);
|
|
1157
|
-
if (node.left) _recursive(node.left, level + 1);
|
|
1158
|
-
if (node.right) _recursive(node.right, level + 1);
|
|
1159
|
-
};
|
|
1160
|
-
|
|
1161
|
-
_recursive(node, 0);
|
|
1162
|
-
} else {
|
|
1163
|
-
const stack: [N, number][] = [[node, 0]];
|
|
1164
|
-
|
|
1165
|
-
while (stack.length > 0) {
|
|
1166
|
-
const head = stack.pop()!;
|
|
1167
|
-
const [node, level] = head;
|
|
1168
|
-
|
|
1169
|
-
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
1170
|
-
collectByProperty(node, level);
|
|
1171
|
-
if (node.right) stack.push([node.right, level + 1]);
|
|
1172
|
-
if (node.left) stack.push([node.left, level + 1]);
|
|
1173
|
-
}
|
|
1174
|
-
}
|
|
1175
|
-
|
|
1176
|
-
return levelsNodes;
|
|
1177
|
-
}
|
|
1178
|
-
|
|
1179
|
-
/**
|
|
1180
|
-
* The function returns the predecessor of a given node in a binary tree.
|
|
1181
|
-
* @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
|
|
1182
|
-
* @returns the predecessor of the given node in a binary tree.
|
|
1183
|
-
*/
|
|
1184
|
-
getPredecessor(node: N): N {
|
|
1185
|
-
if (node.left) {
|
|
1186
|
-
let predecessor: N | null | undefined = node.left;
|
|
1187
|
-
while (!(predecessor) || predecessor.right && predecessor.right !== node) {
|
|
1188
|
-
if (predecessor) {
|
|
1189
|
-
predecessor = predecessor.right;
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
return predecessor;
|
|
1193
|
-
} else {
|
|
1194
|
-
return node;
|
|
1195
|
-
}
|
|
1196
|
-
}
|
|
1197
|
-
|
|
1198
|
-
morris(): BinaryTreeNodeId[];
|
|
1199
|
-
|
|
1200
|
-
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
|
|
1201
|
-
|
|
1202
|
-
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
|
|
1203
|
-
|
|
1204
|
-
/**
|
|
1205
|
-
* Time complexity is O(n)
|
|
1206
|
-
* Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
|
|
1207
|
-
*/
|
|
1208
|
-
|
|
1209
|
-
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
|
|
1210
|
-
|
|
1211
|
-
/**
|
|
1212
|
-
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris
|
|
1213
|
-
* traversal algorithm.
|
|
1214
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter determines the traversal pattern for the binary
|
|
1215
|
-
* tree. It can have one of three values:
|
|
1216
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
|
|
1217
|
-
* property name of the nodes that you want to retrieve. It can be any valid property name of the nodes in the binary
|
|
1218
|
-
* tree.
|
|
1219
|
-
* @returns an array of AbstractBinaryTreeNodeProperties<N> objects.
|
|
1220
|
-
*/
|
|
1221
|
-
morris(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
|
1222
|
-
if (this.root === null) return [];
|
|
1223
|
-
|
|
1224
|
-
pattern = pattern || 'in';
|
|
1225
|
-
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
1226
|
-
|
|
1227
|
-
this._clearResults();
|
|
1228
|
-
|
|
1229
|
-
let cur: N | null | undefined = this.root;
|
|
1230
|
-
const _reverseEdge = (node: N | null | undefined) => {
|
|
1231
|
-
let pre: N | null | undefined = null;
|
|
1232
|
-
let next: N | null | undefined = null;
|
|
1233
|
-
while (node) {
|
|
1234
|
-
next = node.right;
|
|
1235
|
-
node.right = pre;
|
|
1236
|
-
pre = node;
|
|
1237
|
-
node = next;
|
|
1238
|
-
}
|
|
1239
|
-
return pre;
|
|
1240
|
-
};
|
|
1241
|
-
const _printEdge = (node: N | null) => {
|
|
1242
|
-
const tail: N | null | undefined = _reverseEdge(node);
|
|
1243
|
-
let cur: N | null | undefined = tail;
|
|
1244
|
-
while (cur) {
|
|
1245
|
-
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1246
|
-
cur = cur.right;
|
|
1247
|
-
}
|
|
1248
|
-
_reverseEdge(tail);
|
|
1249
|
-
};
|
|
1250
|
-
switch (pattern) {
|
|
1251
|
-
case 'in':
|
|
1252
|
-
while (cur) {
|
|
1253
|
-
if (cur.left) {
|
|
1254
|
-
const predecessor = this.getPredecessor(cur);
|
|
1255
|
-
if (!predecessor.right) {
|
|
1256
|
-
predecessor.right = cur;
|
|
1257
|
-
cur = cur.left;
|
|
1258
|
-
continue;
|
|
1259
|
-
} else {
|
|
1260
|
-
predecessor.right = null;
|
|
1261
|
-
}
|
|
1262
|
-
}
|
|
1263
|
-
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1264
|
-
cur = cur.right;
|
|
1265
|
-
}
|
|
1266
|
-
break;
|
|
1267
|
-
case 'pre':
|
|
1268
|
-
while (cur) {
|
|
1269
|
-
if (cur.left) {
|
|
1270
|
-
const predecessor = this.getPredecessor(cur);
|
|
1271
|
-
if (!predecessor.right) {
|
|
1272
|
-
predecessor.right = cur;
|
|
1273
|
-
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1274
|
-
cur = cur.left;
|
|
1275
|
-
continue;
|
|
1276
|
-
} else {
|
|
1277
|
-
predecessor.right = null;
|
|
1278
|
-
}
|
|
1279
|
-
} else {
|
|
1280
|
-
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
1281
|
-
}
|
|
1282
|
-
cur = cur.right;
|
|
1283
|
-
}
|
|
1284
|
-
break;
|
|
1285
|
-
case 'post':
|
|
1286
|
-
while (cur) {
|
|
1287
|
-
if (cur.left) {
|
|
1288
|
-
const predecessor = this.getPredecessor(cur);
|
|
1289
|
-
if (predecessor.right === null) {
|
|
1290
|
-
predecessor.right = cur;
|
|
1291
|
-
cur = cur.left;
|
|
1292
|
-
continue;
|
|
1293
|
-
} else {
|
|
1294
|
-
predecessor.right = null;
|
|
1295
|
-
_printEdge(cur.left);
|
|
1296
|
-
}
|
|
1297
|
-
}
|
|
1298
|
-
cur = cur.right;
|
|
1299
|
-
}
|
|
1300
|
-
_printEdge(this.root);
|
|
1301
|
-
break;
|
|
1302
|
-
}
|
|
1303
|
-
|
|
1304
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1305
|
-
}
|
|
1306
|
-
|
|
1307
|
-
/**
|
|
1308
|
-
* The function sets the loop type for a protected variable.
|
|
1309
|
-
* @param {LoopType} value - The value parameter is of type LoopType.
|
|
1310
|
-
*/
|
|
1311
|
-
protected _setLoopType(value: LoopType) {
|
|
1312
|
-
this._loopType = value;
|
|
1313
|
-
}
|
|
1314
|
-
|
|
1315
|
-
/**
|
|
1316
|
-
* The function sets the value of the `_visitedId` property in a protected manner.
|
|
1317
|
-
* @param {BinaryTreeNodeId[]} value - value is an array of BinaryTreeNodeId values.
|
|
1318
|
-
*/
|
|
1319
|
-
protected _setVisitedId(value: BinaryTreeNodeId[]) {
|
|
1320
|
-
this._visitedId = value;
|
|
1321
|
-
}
|
|
1322
|
-
|
|
1323
|
-
/**
|
|
1324
|
-
* The function sets the value of the "_visitedVal" property to the given array.
|
|
1325
|
-
* @param value - An array of type N.
|
|
1326
|
-
*/
|
|
1327
|
-
protected _setVisitedVal(value: Array<N>) {
|
|
1328
|
-
this._visitedVal = value;
|
|
1329
|
-
}
|
|
1330
|
-
|
|
1331
|
-
/**
|
|
1332
|
-
* The function sets the value of the _visitedNode property.
|
|
1333
|
-
* @param {N[]} value - N[] is an array of elements of type N.
|
|
1334
|
-
*/
|
|
1335
|
-
protected _setVisitedNode(value: N[]) {
|
|
1336
|
-
this._visitedNode = value;
|
|
1337
|
-
}
|
|
1338
|
-
|
|
1339
|
-
/**
|
|
1340
|
-
* The function sets the value of the `_visitedLeftSum` property to the provided array.
|
|
1341
|
-
* @param {number[]} value - An array of numbers that represents the visited left sum.
|
|
1342
|
-
*/
|
|
1343
|
-
protected _setVisitedLeftSum(value: number[]) {
|
|
1344
|
-
this._visitedLeftSum = value;
|
|
1345
|
-
}
|
|
1346
|
-
|
|
1347
|
-
/**
|
|
1348
|
-
* The function sets the value of a protected property called "_isMergeDuplicatedNodeById".
|
|
1349
|
-
* @param {boolean} value - The value parameter is a boolean value that determines whether the isMergeDuplicatedNodeById
|
|
1350
|
-
* property should be set to true or false.
|
|
1351
|
-
*/
|
|
1352
|
-
protected _setIsDuplicatedVal(value: boolean) {
|
|
1353
|
-
this._isMergeDuplicatedNodeById = value;
|
|
1354
|
-
}
|
|
1355
|
-
|
|
1356
|
-
/**
|
|
1357
|
-
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
1358
|
-
* parent property of the value to undefined.
|
|
1359
|
-
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of type `N` or `null`.
|
|
1360
|
-
*/
|
|
1361
|
-
protected _setRoot(v: N | null) {
|
|
1362
|
-
if (v) {
|
|
1363
|
-
v.parent = undefined;
|
|
1364
|
-
}
|
|
1365
|
-
this._root = v;
|
|
1366
|
-
}
|
|
1367
|
-
|
|
1368
|
-
/**
|
|
1369
|
-
* The function sets the size of a protected variable.
|
|
1370
|
-
* @param {number} v - number
|
|
1371
|
-
*/
|
|
1372
|
-
protected _setSize(v: number) {
|
|
1373
|
-
this._size = v;
|
|
1374
|
-
}
|
|
1375
|
-
|
|
1376
|
-
/**
|
|
1377
|
-
* The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
|
|
1378
|
-
* properties.
|
|
1379
|
-
*/
|
|
1380
|
-
protected _clearResults() {
|
|
1381
|
-
this._visitedId = [];
|
|
1382
|
-
this._visitedVal = [];
|
|
1383
|
-
this._visitedNode = [];
|
|
1384
|
-
this._visitedLeftSum = [];
|
|
1385
|
-
}
|
|
1386
|
-
|
|
1387
|
-
/**
|
|
1388
|
-
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
|
|
1389
|
-
* a result array.
|
|
1390
|
-
* @param {N} cur - The current node being processed.
|
|
1391
|
-
* @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
|
|
1392
|
-
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeId` or a `N`
|
|
1393
|
-
* type. It represents the property value that we are comparing against in the switch statement.
|
|
1394
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
1395
|
-
* specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'id'`
|
|
1396
|
-
* or `'val'`. If it is not provided or is not equal to `'id'` or `'val'`, the
|
|
1397
|
-
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
1398
|
-
* stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
|
|
1399
|
-
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
|
|
1400
|
-
* @returns a boolean value indicating whether only one matching node should be pushed into the result array.
|
|
1401
|
-
*/
|
|
1402
|
-
protected _pushByPropertyNameStopOrNot(cur: N, result: (N | null | undefined)[], nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean) {
|
|
1403
|
-
switch (propertyName) {
|
|
1404
|
-
case 'id':
|
|
1405
|
-
if (cur.id === nodeProperty) {
|
|
1406
|
-
result.push(cur);
|
|
1407
|
-
return !!onlyOne;
|
|
1408
|
-
}
|
|
1409
|
-
break;
|
|
1410
|
-
case 'val':
|
|
1411
|
-
if (cur.val === nodeProperty) {
|
|
1412
|
-
result.push(cur);
|
|
1413
|
-
return !!onlyOne;
|
|
1414
|
-
}
|
|
1415
|
-
break;
|
|
1416
|
-
default:
|
|
1417
|
-
if (cur.id === nodeProperty) {
|
|
1418
|
-
result.push(cur);
|
|
1419
|
-
return !!onlyOne;
|
|
1420
|
-
}
|
|
1421
|
-
break;
|
|
1422
|
-
}
|
|
1423
|
-
}
|
|
1424
|
-
|
|
1425
|
-
/**
|
|
1426
|
-
* The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
|
|
1427
|
-
* @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
|
|
1428
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1429
|
-
* can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
|
|
1430
|
-
* specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
|
|
1431
|
-
*/
|
|
1432
|
-
protected _accumulatedByPropertyName(node: N, nodeOrPropertyName ?: NodeOrPropertyName) {
|
|
1433
|
-
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
|
|
1434
|
-
|
|
1435
|
-
switch (nodeOrPropertyName) {
|
|
1436
|
-
case 'id':
|
|
1437
|
-
this._visitedId.push(node.id);
|
|
1438
|
-
break;
|
|
1439
|
-
case 'val':
|
|
1440
|
-
this._visitedVal.push(node.val);
|
|
1441
|
-
break;
|
|
1442
|
-
case 'node':
|
|
1443
|
-
this._visitedNode.push(node);
|
|
1444
|
-
break;
|
|
1445
|
-
default:
|
|
1446
|
-
this._visitedId.push(node.id);
|
|
1447
|
-
break;
|
|
1448
|
-
}
|
|
1449
|
-
}
|
|
1450
|
-
|
|
1451
|
-
/**
|
|
1452
|
-
* The time complexity of Morris traversal is O(n), it's may slower than others
|
|
1453
|
-
* The space complexity Morris traversal is O(1) because no using stack
|
|
1454
|
-
*/
|
|
1455
|
-
|
|
1456
|
-
/**
|
|
1457
|
-
* The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
|
|
1458
|
-
* name.
|
|
1459
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1460
|
-
* can accept either a `NodeOrPropertyName` type or be undefined.
|
|
1461
|
-
* @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
|
|
1462
|
-
*/
|
|
1463
|
-
protected _getResultByPropertyName(nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
|
|
1464
|
-
nodeOrPropertyName = nodeOrPropertyName ?? 'id';
|
|
1465
|
-
|
|
1466
|
-
switch (nodeOrPropertyName) {
|
|
1467
|
-
case 'id':
|
|
1468
|
-
return this._visitedId;
|
|
1469
|
-
case 'val':
|
|
1470
|
-
return this._visitedVal;
|
|
1471
|
-
case 'node':
|
|
1472
|
-
return this._visitedNode;
|
|
1473
|
-
default:
|
|
1474
|
-
return this._visitedId;
|
|
1475
|
-
}
|
|
1476
|
-
}
|
|
1477
|
-
|
|
1478
|
-
// --- end additional methods ---
|
|
1479
|
-
}
|