data-structure-typed 1.19.7 → 1.19.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +41 -58
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +107 -145
- package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -0
- package/dist/data-structures/binary-tree/avl-tree.js +4 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +2 -1
- package/dist/data-structures/binary-tree/binary-tree.js +3 -0
- package/dist/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/data-structures/binary-tree/bst.js +4 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +2 -3
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +22 -16
- package/dist/data-structures/binary-tree/tree-multiset.js +138 -122
- package/dist/data-structures/heap/heap.d.ts +4 -3
- package/dist/data-structures/heap/heap.js +12 -33
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +6 -9
- package/dist/data-structures/types/abstract-binary-tree.d.ts +1 -2
- package/dist/data-structures/types/tree-multiset.d.ts +2 -2
- package/package.json +22 -5
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +112 -161
- package/src/data-structures/binary-tree/avl-tree.ts +4 -0
- package/src/data-structures/binary-tree/binary-tree.ts +4 -2
- package/src/data-structures/binary-tree/bst.ts +4 -1
- package/src/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +136 -118
- package/src/data-structures/graph/abstract-graph.ts +1 -0
- package/src/data-structures/heap/heap.ts +12 -38
- package/src/data-structures/interfaces/abstract-binary-tree.ts +6 -43
- package/src/data-structures/types/abstract-binary-tree.ts +1 -2
- package/src/data-structures/types/tree-multiset.ts +2 -2
- package/tsconfig.json +1 -2
|
@@ -16,7 +16,7 @@ export declare abstract class AbstractBinaryTreeNode<T = any, NEIGHBOR extends A
|
|
|
16
16
|
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It represents the value that will be
|
|
17
17
|
* stored in the binary tree node. If no value is provided, it will be set to undefined.
|
|
18
18
|
*/
|
|
19
|
-
constructor(id: BinaryTreeNodeId, val?: T);
|
|
19
|
+
protected constructor(id: BinaryTreeNodeId, val?: T);
|
|
20
20
|
private _id;
|
|
21
21
|
get id(): BinaryTreeNodeId;
|
|
22
22
|
set id(v: BinaryTreeNodeId);
|
|
@@ -54,20 +54,14 @@ export declare abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNod
|
|
|
54
54
|
get size(): number;
|
|
55
55
|
private _loopType;
|
|
56
56
|
get loopType(): LoopType;
|
|
57
|
-
private
|
|
58
|
-
get
|
|
59
|
-
private _maxId;
|
|
60
|
-
get maxId(): number;
|
|
61
|
-
private _isMergeDuplicatedVal;
|
|
62
|
-
get isMergeDuplicatedVal(): boolean;
|
|
57
|
+
private _isMergeDuplicatedNodeById;
|
|
58
|
+
get isMergeDuplicatedNodeById(): boolean;
|
|
63
59
|
private _visitedId;
|
|
64
60
|
get visitedId(): BinaryTreeNodeId[];
|
|
65
61
|
private _visitedVal;
|
|
66
|
-
get visitedVal():
|
|
62
|
+
get visitedVal(): N['val'][];
|
|
67
63
|
private _visitedNode;
|
|
68
64
|
get visitedNode(): N[];
|
|
69
|
-
private _visitedCount;
|
|
70
|
-
get visitedCount(): number[];
|
|
71
65
|
private _visitedLeftSum;
|
|
72
66
|
get visitedLeftSum(): number[];
|
|
73
67
|
abstract createNode(id: BinaryTreeNodeId, val?: N['val']): N | null;
|
|
@@ -89,15 +83,19 @@ export declare abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNod
|
|
|
89
83
|
*/
|
|
90
84
|
isEmpty(): boolean;
|
|
91
85
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. If no value is
|
|
95
|
-
* provided, the default value will be the same as the `id` parameter.
|
|
96
|
-
* @param {number} [count] - The `count` parameter is an optional number that represents the number of times the value
|
|
97
|
-
* should be added to the binary tree. If not provided, the default value is `undefined`.
|
|
98
|
-
* @returns The function `add` returns either a `BinaryTreeNode` object (`N`), `null`, or `undefined`.
|
|
86
|
+
* When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
|
|
87
|
+
* In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
|
|
99
88
|
*/
|
|
100
|
-
|
|
89
|
+
/**
|
|
90
|
+
* The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
|
|
91
|
+
* @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId`, which
|
|
92
|
+
* is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
|
|
93
|
+
* node itself. It can also be `null` if no node is specified.
|
|
94
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
95
|
+
* being added to the binary tree.
|
|
96
|
+
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
97
|
+
*/
|
|
98
|
+
add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val']): N | null | undefined;
|
|
101
99
|
/**
|
|
102
100
|
* The function adds a new node to the left or right child of a parent node, updating the size of the tree if
|
|
103
101
|
* necessary.
|
|
@@ -109,21 +107,25 @@ export declare abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNod
|
|
|
109
107
|
*/
|
|
110
108
|
addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
111
109
|
/**
|
|
112
|
-
* The `addMany` function adds multiple nodes to a
|
|
110
|
+
* The `addMany` function adds multiple nodes to a tree data structure and returns an array of the inserted nodes or
|
|
113
111
|
* null/undefined values.
|
|
114
|
-
* @param {
|
|
115
|
-
*
|
|
116
|
-
* @
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
112
|
+
* @param {(BinaryTreeNodeId|N)[]} idsOrNodes - An array of BinaryTreeNodeId or N objects. These can be either the ID
|
|
113
|
+
* of a binary tree node or the actual node object itself.
|
|
114
|
+
* @param {N['val'][]} [data] - Optional array of values to be added to the nodes. If provided, the length of this
|
|
115
|
+
* array should be the same as the length of the `idsOrNodes` array.
|
|
116
|
+
* @returns The function `addMany` returns an array of values `(N | null | undefined)[]`.
|
|
117
|
+
*/
|
|
118
|
+
addMany(idsOrNodes: (BinaryTreeNodeId | N | null)[], data?: N['val'][]): (N | null | undefined)[];
|
|
119
|
+
/**
|
|
120
|
+
* The `fill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
121
|
+
* @param {(BinaryTreeNodeId | N)[]} idsOrNodes - The `idsOrNodes` parameter is an array that can contain either
|
|
122
|
+
* `BinaryTreeNodeId` or `N` values.
|
|
123
|
+
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
124
|
+
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `idsOrNodes`
|
|
125
|
+
* array. Each value in the `data` array will be assigned to the
|
|
126
|
+
* @returns The method is returning a boolean value.
|
|
125
127
|
*/
|
|
126
|
-
fill(
|
|
128
|
+
fill(idsOrNodes: (BinaryTreeNodeId | N | null)[], data?: N[] | Array<N['val']>): boolean;
|
|
127
129
|
/**
|
|
128
130
|
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
129
131
|
* node that needs to be balanced.
|
|
@@ -215,14 +217,12 @@ export declare abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNod
|
|
|
215
217
|
* @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
|
|
216
218
|
* @returns a boolean value.
|
|
217
219
|
*/
|
|
218
|
-
|
|
220
|
+
isSubtreeBST(node: N | null): boolean;
|
|
219
221
|
/**
|
|
220
|
-
* The function checks if
|
|
221
|
-
* @
|
|
222
|
-
* search tree (BST).
|
|
223
|
-
* @returns a boolean value.
|
|
222
|
+
* The function isBST checks if the binary search tree is valid.
|
|
223
|
+
* @returns The `isBST()` function is returning a boolean value.
|
|
224
224
|
*/
|
|
225
|
-
isBST(
|
|
225
|
+
isBST(): boolean;
|
|
226
226
|
/**
|
|
227
227
|
* The function calculates the size of a subtree by traversing it either recursively or iteratively.
|
|
228
228
|
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
@@ -305,31 +305,14 @@ export declare abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNod
|
|
|
305
305
|
* @param {N[]} value - N[] is an array of elements of type N.
|
|
306
306
|
*/
|
|
307
307
|
protected _setVisitedNode(value: N[]): void;
|
|
308
|
-
/**
|
|
309
|
-
* The function sets the value of the visitedCount property.
|
|
310
|
-
* @param {number[]} value - The value parameter is an array of numbers.
|
|
311
|
-
*/
|
|
312
|
-
protected setVisitedCount(value: number[]): void;
|
|
313
308
|
/**
|
|
314
309
|
* The function sets the value of the `_visitedLeftSum` property to the provided array.
|
|
315
310
|
* @param {number[]} value - An array of numbers that represents the visited left sum.
|
|
316
311
|
*/
|
|
317
312
|
protected _setVisitedLeftSum(value: number[]): void;
|
|
318
313
|
/**
|
|
319
|
-
* The function sets the value of
|
|
320
|
-
* @param {boolean} value - The value parameter is a boolean that determines whether the
|
|
321
|
-
* incremented or not. If value is true, the id will be automatically incremented. If value is false, the id will not
|
|
322
|
-
* be automatically incremented.
|
|
323
|
-
*/
|
|
324
|
-
protected _setAutoIncrementId(value: boolean): void;
|
|
325
|
-
/**
|
|
326
|
-
* The function sets the maximum ID value.
|
|
327
|
-
* @param {number} value - The value parameter is a number that represents the new maximum ID value.
|
|
328
|
-
*/
|
|
329
|
-
protected _setMaxId(value: number): void;
|
|
330
|
-
/**
|
|
331
|
-
* The function sets the value of a protected property called "_isMergeDuplicatedVal".
|
|
332
|
-
* @param {boolean} value - The value parameter is a boolean value that determines whether the isMergeDuplicatedVal
|
|
314
|
+
* The function sets the value of a protected property called "_isMergeDuplicatedNodeById".
|
|
315
|
+
* @param {boolean} value - The value parameter is a boolean value that determines whether the isMergeDuplicatedNodeById
|
|
333
316
|
* property should be set to true or false.
|
|
334
317
|
*/
|
|
335
318
|
protected _setIsDuplicatedVal(value: boolean): void;
|
|
@@ -345,10 +328,10 @@ export declare abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNod
|
|
|
345
328
|
*/
|
|
346
329
|
protected _setSize(v: number): void;
|
|
347
330
|
/**
|
|
348
|
-
* The function `
|
|
331
|
+
* The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
|
|
349
332
|
* properties.
|
|
350
333
|
*/
|
|
351
|
-
protected
|
|
334
|
+
protected _clearResults(): void;
|
|
352
335
|
/**
|
|
353
336
|
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
|
|
354
337
|
* a result array.
|
|
@@ -113,21 +113,22 @@ class AbstractBinaryTree {
|
|
|
113
113
|
this._root = null;
|
|
114
114
|
this._size = 0;
|
|
115
115
|
this._loopType = types_1.LoopType.ITERATIVE;
|
|
116
|
-
this._autoIncrementId = false;
|
|
117
|
-
this._maxId = -1;
|
|
118
116
|
// TODO this variable may be moved to TreeMultiset
|
|
119
|
-
this.
|
|
117
|
+
this._isMergeDuplicatedNodeById = true;
|
|
120
118
|
this._visitedId = [];
|
|
121
119
|
this._visitedVal = [];
|
|
122
120
|
this._visitedNode = [];
|
|
123
|
-
this._visitedCount = [];
|
|
124
121
|
this._visitedLeftSum = [];
|
|
125
122
|
if (options !== undefined) {
|
|
126
|
-
const { loopType = types_1.LoopType.ITERATIVE,
|
|
127
|
-
this.
|
|
128
|
-
this._autoIncrementId = autoIncrementId;
|
|
123
|
+
const { loopType = types_1.LoopType.ITERATIVE, isMergeDuplicatedNodeById = true } = options;
|
|
124
|
+
this._isMergeDuplicatedNodeById = isMergeDuplicatedNodeById;
|
|
129
125
|
this._loopType = loopType;
|
|
130
126
|
}
|
|
127
|
+
else {
|
|
128
|
+
this._isMergeDuplicatedNodeById = true;
|
|
129
|
+
this._loopType = types_1.LoopType.ITERATIVE;
|
|
130
|
+
}
|
|
131
|
+
this.clear();
|
|
131
132
|
}
|
|
132
133
|
get root() {
|
|
133
134
|
return this._root;
|
|
@@ -138,14 +139,8 @@ class AbstractBinaryTree {
|
|
|
138
139
|
get loopType() {
|
|
139
140
|
return this._loopType;
|
|
140
141
|
}
|
|
141
|
-
get
|
|
142
|
-
return this.
|
|
143
|
-
}
|
|
144
|
-
get maxId() {
|
|
145
|
-
return this._maxId;
|
|
146
|
-
}
|
|
147
|
-
get isMergeDuplicatedVal() {
|
|
148
|
-
return this._isMergeDuplicatedVal;
|
|
142
|
+
get isMergeDuplicatedNodeById() {
|
|
143
|
+
return this._isMergeDuplicatedNodeById;
|
|
149
144
|
}
|
|
150
145
|
get visitedId() {
|
|
151
146
|
return this._visitedId;
|
|
@@ -156,9 +151,6 @@ class AbstractBinaryTree {
|
|
|
156
151
|
get visitedNode() {
|
|
157
152
|
return this._visitedNode;
|
|
158
153
|
}
|
|
159
|
-
get visitedCount() {
|
|
160
|
-
return this._visitedCount;
|
|
161
|
-
}
|
|
162
154
|
get visitedLeftSum() {
|
|
163
155
|
return this._visitedLeftSum;
|
|
164
156
|
}
|
|
@@ -170,19 +162,16 @@ class AbstractBinaryTree {
|
|
|
170
162
|
* @returns The `destNode` is being returned.
|
|
171
163
|
*/
|
|
172
164
|
swapLocation(srcNode, destNode) {
|
|
173
|
-
const { val, height
|
|
165
|
+
const { id, val, height } = destNode;
|
|
174
166
|
const tempNode = this.createNode(id, val);
|
|
175
167
|
if (tempNode) {
|
|
176
168
|
tempNode.height = height;
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
srcNode.val = tempNode.val;
|
|
184
|
-
srcNode.height = tempNode.height;
|
|
185
|
-
}
|
|
169
|
+
destNode.id = srcNode.id;
|
|
170
|
+
destNode.val = srcNode.val;
|
|
171
|
+
destNode.height = srcNode.height;
|
|
172
|
+
srcNode.id = tempNode.id;
|
|
173
|
+
srcNode.val = tempNode.val;
|
|
174
|
+
srcNode.height = tempNode.height;
|
|
186
175
|
}
|
|
187
176
|
return destNode;
|
|
188
177
|
}
|
|
@@ -190,9 +179,9 @@ class AbstractBinaryTree {
|
|
|
190
179
|
* The clear() function resets the root, size, and maxId properties to their initial values.
|
|
191
180
|
*/
|
|
192
181
|
clear() {
|
|
193
|
-
this.
|
|
194
|
-
this.
|
|
195
|
-
this.
|
|
182
|
+
this._root = null;
|
|
183
|
+
this._size = 0;
|
|
184
|
+
this._clearResults();
|
|
196
185
|
}
|
|
197
186
|
/**
|
|
198
187
|
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
@@ -202,15 +191,19 @@ class AbstractBinaryTree {
|
|
|
202
191
|
return this.size === 0;
|
|
203
192
|
}
|
|
204
193
|
/**
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
194
|
+
* When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
|
|
195
|
+
* In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
|
|
196
|
+
*/
|
|
197
|
+
/**
|
|
198
|
+
* The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
|
|
199
|
+
* @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId`, which
|
|
200
|
+
* is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
|
|
201
|
+
* node itself. It can also be `null` if no node is specified.
|
|
202
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
203
|
+
* being added to the binary tree.
|
|
204
|
+
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
212
205
|
*/
|
|
213
|
-
add(
|
|
206
|
+
add(idOrNode, val) {
|
|
214
207
|
const _bfs = (root, newNode) => {
|
|
215
208
|
const queue = [root];
|
|
216
209
|
while (queue.length > 0) {
|
|
@@ -229,25 +222,37 @@ class AbstractBinaryTree {
|
|
|
229
222
|
}
|
|
230
223
|
return;
|
|
231
224
|
};
|
|
232
|
-
let inserted;
|
|
233
|
-
|
|
234
|
-
|
|
225
|
+
let inserted, needInsert;
|
|
226
|
+
if (idOrNode === null) {
|
|
227
|
+
needInsert = null;
|
|
228
|
+
}
|
|
229
|
+
else if (typeof idOrNode === 'number') {
|
|
230
|
+
needInsert = this.createNode(idOrNode, val);
|
|
231
|
+
}
|
|
232
|
+
else if (idOrNode instanceof AbstractBinaryTreeNode) {
|
|
233
|
+
needInsert = idOrNode;
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const existNode = idOrNode ? this.get(idOrNode, 'id') : undefined;
|
|
235
239
|
if (this.root) {
|
|
236
240
|
if (existNode) {
|
|
237
|
-
existNode.val = val
|
|
238
|
-
|
|
239
|
-
inserted = existNode;
|
|
240
|
-
}
|
|
241
|
+
existNode.val = val;
|
|
242
|
+
inserted = existNode;
|
|
241
243
|
}
|
|
242
244
|
else {
|
|
243
245
|
inserted = _bfs(this.root, needInsert);
|
|
244
246
|
}
|
|
245
247
|
}
|
|
246
248
|
else {
|
|
247
|
-
this._setRoot(
|
|
249
|
+
this._setRoot(needInsert);
|
|
248
250
|
if (needInsert !== null) {
|
|
249
251
|
this._setSize(1);
|
|
250
252
|
}
|
|
253
|
+
else {
|
|
254
|
+
this._setSize(0);
|
|
255
|
+
}
|
|
251
256
|
inserted = this.root;
|
|
252
257
|
}
|
|
253
258
|
return inserted;
|
|
@@ -263,22 +268,18 @@ class AbstractBinaryTree {
|
|
|
263
268
|
*/
|
|
264
269
|
addTo(newNode, parent) {
|
|
265
270
|
if (parent) {
|
|
271
|
+
// When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
|
|
272
|
+
// In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
|
|
266
273
|
if (parent.left === undefined) {
|
|
267
|
-
if (newNode) {
|
|
268
|
-
newNode.parent = parent;
|
|
269
|
-
}
|
|
270
274
|
parent.left = newNode;
|
|
271
|
-
if (newNode
|
|
275
|
+
if (newNode) {
|
|
272
276
|
this._setSize(this.size + 1);
|
|
273
277
|
}
|
|
274
278
|
return parent.left;
|
|
275
279
|
}
|
|
276
280
|
else if (parent.right === undefined) {
|
|
277
|
-
if (newNode) {
|
|
278
|
-
newNode.parent = parent;
|
|
279
|
-
}
|
|
280
281
|
parent.right = newNode;
|
|
281
|
-
if (newNode
|
|
282
|
+
if (newNode) {
|
|
282
283
|
this._setSize(this.size + 1);
|
|
283
284
|
}
|
|
284
285
|
return parent.right;
|
|
@@ -292,77 +293,58 @@ class AbstractBinaryTree {
|
|
|
292
293
|
}
|
|
293
294
|
}
|
|
294
295
|
/**
|
|
295
|
-
* The `addMany` function adds multiple nodes to a
|
|
296
|
+
* The `addMany` function adds multiple nodes to a tree data structure and returns an array of the inserted nodes or
|
|
296
297
|
* null/undefined values.
|
|
297
|
-
* @param {
|
|
298
|
-
*
|
|
299
|
-
* @
|
|
298
|
+
* @param {(BinaryTreeNodeId|N)[]} idsOrNodes - An array of BinaryTreeNodeId or N objects. These can be either the ID
|
|
299
|
+
* of a binary tree node or the actual node object itself.
|
|
300
|
+
* @param {N['val'][]} [data] - Optional array of values to be added to the nodes. If provided, the length of this
|
|
301
|
+
* array should be the same as the length of the `idsOrNodes` array.
|
|
302
|
+
* @returns The function `addMany` returns an array of values `(N | null | undefined)[]`.
|
|
300
303
|
*/
|
|
301
|
-
addMany(data) {
|
|
304
|
+
addMany(idsOrNodes, data) {
|
|
302
305
|
var _a;
|
|
303
306
|
// TODO not sure addMany not be run multi times
|
|
304
307
|
const inserted = [];
|
|
305
308
|
const map = new Map();
|
|
306
|
-
if (this.
|
|
307
|
-
for (const
|
|
308
|
-
map.set(
|
|
309
|
+
if (this.isMergeDuplicatedNodeById) {
|
|
310
|
+
for (const idOrNode of idsOrNodes)
|
|
311
|
+
map.set(idOrNode, ((_a = map.get(idOrNode)) !== null && _a !== void 0 ? _a : 0) + 1);
|
|
309
312
|
}
|
|
310
|
-
for (
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
}
|
|
315
|
-
if (nodeOrId === null) {
|
|
316
|
-
inserted.push(this.add(NaN, null, 0));
|
|
313
|
+
for (let i = 0; i < idsOrNodes.length; i++) {
|
|
314
|
+
const idOrNode = idsOrNodes[i];
|
|
315
|
+
if (idOrNode instanceof AbstractBinaryTreeNode) {
|
|
316
|
+
inserted.push(this.add(idOrNode.id, idOrNode.val));
|
|
317
317
|
continue;
|
|
318
318
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
let newId;
|
|
322
|
-
if (typeof nodeOrId === 'number') {
|
|
323
|
-
newId = this.autoIncrementId ? this.maxId + 1 : nodeOrId;
|
|
324
|
-
}
|
|
325
|
-
else if (nodeOrId instanceof Object) {
|
|
326
|
-
if (this.autoIncrementId) {
|
|
327
|
-
newId = this.maxId + 1;
|
|
328
|
-
}
|
|
329
|
-
else {
|
|
330
|
-
if (Object.keys(nodeOrId).includes('id')) {
|
|
331
|
-
newId = nodeOrId.id;
|
|
332
|
-
}
|
|
333
|
-
else {
|
|
334
|
-
console.warn(nodeOrId, 'Object value must has an id property when the autoIncrementId is false');
|
|
335
|
-
continue;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
else {
|
|
340
|
-
console.warn(nodeOrId, ` is not added`);
|
|
319
|
+
if (idOrNode === null) {
|
|
320
|
+
inserted.push(this.add(null));
|
|
341
321
|
continue;
|
|
342
322
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
323
|
+
const val = data === null || data === void 0 ? void 0 : data[i];
|
|
324
|
+
if (this.isMergeDuplicatedNodeById) {
|
|
325
|
+
if (map.has(idOrNode)) {
|
|
326
|
+
inserted.push(this.add(idOrNode, val));
|
|
327
|
+
map.delete(idOrNode);
|
|
347
328
|
}
|
|
348
329
|
}
|
|
349
330
|
else {
|
|
350
|
-
inserted.push(this.add(
|
|
331
|
+
inserted.push(this.add(idOrNode, val));
|
|
351
332
|
}
|
|
352
|
-
this._setMaxId(newId);
|
|
353
333
|
}
|
|
354
334
|
return inserted;
|
|
355
335
|
}
|
|
356
336
|
/**
|
|
357
|
-
* The `fill` function clears the
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
337
|
+
* The `fill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
338
|
+
* @param {(BinaryTreeNodeId | N)[]} idsOrNodes - The `idsOrNodes` parameter is an array that can contain either
|
|
339
|
+
* `BinaryTreeNodeId` or `N` values.
|
|
340
|
+
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
341
|
+
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `idsOrNodes`
|
|
342
|
+
* array. Each value in the `data` array will be assigned to the
|
|
343
|
+
* @returns The method is returning a boolean value.
|
|
362
344
|
*/
|
|
363
|
-
fill(data) {
|
|
345
|
+
fill(idsOrNodes, data) {
|
|
364
346
|
this.clear();
|
|
365
|
-
return
|
|
347
|
+
return idsOrNodes.length === this.addMany(idsOrNodes, data).length;
|
|
366
348
|
}
|
|
367
349
|
/**
|
|
368
350
|
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
@@ -588,6 +570,7 @@ class AbstractBinaryTree {
|
|
|
588
570
|
*/
|
|
589
571
|
has(nodeProperty, propertyName) {
|
|
590
572
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
573
|
+
// TODO may support finding node by value equal
|
|
591
574
|
return this.getNodes(nodeProperty, propertyName).length > 0;
|
|
592
575
|
}
|
|
593
576
|
/**
|
|
@@ -604,6 +587,7 @@ class AbstractBinaryTree {
|
|
|
604
587
|
get(nodeProperty, propertyName) {
|
|
605
588
|
var _a;
|
|
606
589
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
590
|
+
// TODO may support finding node by value equal
|
|
607
591
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
608
592
|
}
|
|
609
593
|
/**
|
|
@@ -613,6 +597,7 @@ class AbstractBinaryTree {
|
|
|
613
597
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
614
598
|
*/
|
|
615
599
|
getPathToRoot(node) {
|
|
600
|
+
// TODO to support get path through passing id
|
|
616
601
|
const result = [];
|
|
617
602
|
while (node.parent) {
|
|
618
603
|
result.unshift(node);
|
|
@@ -666,6 +651,7 @@ class AbstractBinaryTree {
|
|
|
666
651
|
* function returns `null`.
|
|
667
652
|
*/
|
|
668
653
|
getRightMost(node) {
|
|
654
|
+
// TODO support get right most by passing id in
|
|
669
655
|
node = node !== null && node !== void 0 ? node : this.root;
|
|
670
656
|
if (!node)
|
|
671
657
|
return node;
|
|
@@ -692,7 +678,7 @@ class AbstractBinaryTree {
|
|
|
692
678
|
* @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
|
|
693
679
|
* @returns a boolean value.
|
|
694
680
|
*/
|
|
695
|
-
|
|
681
|
+
isSubtreeBST(node) {
|
|
696
682
|
// TODO there is a bug
|
|
697
683
|
if (!node)
|
|
698
684
|
return true;
|
|
@@ -724,13 +710,11 @@ class AbstractBinaryTree {
|
|
|
724
710
|
}
|
|
725
711
|
}
|
|
726
712
|
/**
|
|
727
|
-
* The function checks if
|
|
728
|
-
* @
|
|
729
|
-
* search tree (BST).
|
|
730
|
-
* @returns a boolean value.
|
|
713
|
+
* The function isBST checks if the binary search tree is valid.
|
|
714
|
+
* @returns The `isBST()` function is returning a boolean value.
|
|
731
715
|
*/
|
|
732
|
-
isBST(
|
|
733
|
-
return this.
|
|
716
|
+
isBST() {
|
|
717
|
+
return this.isSubtreeBST(this.root);
|
|
734
718
|
}
|
|
735
719
|
/**
|
|
736
720
|
* The function calculates the size of a subtree by traversing it either recursively or iteratively.
|
|
@@ -739,6 +723,7 @@ class AbstractBinaryTree {
|
|
|
739
723
|
* @returns the size of the subtree rooted at `subTreeRoot`.
|
|
740
724
|
*/
|
|
741
725
|
getSubTreeSize(subTreeRoot) {
|
|
726
|
+
// TODO support id passed in
|
|
742
727
|
let size = 0;
|
|
743
728
|
if (!subTreeRoot)
|
|
744
729
|
return size;
|
|
@@ -868,7 +853,7 @@ class AbstractBinaryTree {
|
|
|
868
853
|
*/
|
|
869
854
|
BFS(nodeOrPropertyName) {
|
|
870
855
|
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
871
|
-
this.
|
|
856
|
+
this._clearResults();
|
|
872
857
|
const queue = [this.root];
|
|
873
858
|
while (queue.length !== 0) {
|
|
874
859
|
const cur = queue.shift();
|
|
@@ -896,7 +881,7 @@ class AbstractBinaryTree {
|
|
|
896
881
|
DFS(pattern, nodeOrPropertyName) {
|
|
897
882
|
pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
|
|
898
883
|
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
899
|
-
this.
|
|
884
|
+
this._clearResults();
|
|
900
885
|
const _traverse = (node) => {
|
|
901
886
|
switch (pattern) {
|
|
902
887
|
case 'in':
|
|
@@ -939,7 +924,7 @@ class AbstractBinaryTree {
|
|
|
939
924
|
DFSIterative(pattern, nodeOrPropertyName) {
|
|
940
925
|
pattern = pattern || 'in';
|
|
941
926
|
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
942
|
-
this.
|
|
927
|
+
this._clearResults();
|
|
943
928
|
if (!this.root)
|
|
944
929
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
945
930
|
// 0: visit, 1: print
|
|
@@ -995,7 +980,7 @@ class AbstractBinaryTree {
|
|
|
995
980
|
node = node || this.root;
|
|
996
981
|
if (!node)
|
|
997
982
|
return [];
|
|
998
|
-
this.
|
|
983
|
+
this._clearResults();
|
|
999
984
|
const queue = [node];
|
|
1000
985
|
while (queue.length > 0) {
|
|
1001
986
|
const cur = queue.shift();
|
|
@@ -1104,7 +1089,7 @@ class AbstractBinaryTree {
|
|
|
1104
1089
|
return [];
|
|
1105
1090
|
pattern = pattern || 'in';
|
|
1106
1091
|
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
1107
|
-
this.
|
|
1092
|
+
this._clearResults();
|
|
1108
1093
|
let cur = this.root;
|
|
1109
1094
|
const _reverseEdge = (node) => {
|
|
1110
1095
|
let pre = null;
|
|
@@ -1213,13 +1198,6 @@ class AbstractBinaryTree {
|
|
|
1213
1198
|
_setVisitedNode(value) {
|
|
1214
1199
|
this._visitedNode = value;
|
|
1215
1200
|
}
|
|
1216
|
-
/**
|
|
1217
|
-
* The function sets the value of the visitedCount property.
|
|
1218
|
-
* @param {number[]} value - The value parameter is an array of numbers.
|
|
1219
|
-
*/
|
|
1220
|
-
setVisitedCount(value) {
|
|
1221
|
-
this._visitedCount = value;
|
|
1222
|
-
}
|
|
1223
1201
|
/**
|
|
1224
1202
|
* The function sets the value of the `_visitedLeftSum` property to the provided array.
|
|
1225
1203
|
* @param {number[]} value - An array of numbers that represents the visited left sum.
|
|
@@ -1228,28 +1206,12 @@ class AbstractBinaryTree {
|
|
|
1228
1206
|
this._visitedLeftSum = value;
|
|
1229
1207
|
}
|
|
1230
1208
|
/**
|
|
1231
|
-
* The function sets the value of
|
|
1232
|
-
* @param {boolean} value - The value parameter is a boolean that determines whether the
|
|
1233
|
-
* incremented or not. If value is true, the id will be automatically incremented. If value is false, the id will not
|
|
1234
|
-
* be automatically incremented.
|
|
1235
|
-
*/
|
|
1236
|
-
_setAutoIncrementId(value) {
|
|
1237
|
-
this._autoIncrementId = value;
|
|
1238
|
-
}
|
|
1239
|
-
/**
|
|
1240
|
-
* The function sets the maximum ID value.
|
|
1241
|
-
* @param {number} value - The value parameter is a number that represents the new maximum ID value.
|
|
1242
|
-
*/
|
|
1243
|
-
_setMaxId(value) {
|
|
1244
|
-
this._maxId = value;
|
|
1245
|
-
}
|
|
1246
|
-
/**
|
|
1247
|
-
* The function sets the value of a protected property called "_isMergeDuplicatedVal".
|
|
1248
|
-
* @param {boolean} value - The value parameter is a boolean value that determines whether the isMergeDuplicatedVal
|
|
1209
|
+
* The function sets the value of a protected property called "_isMergeDuplicatedNodeById".
|
|
1210
|
+
* @param {boolean} value - The value parameter is a boolean value that determines whether the isMergeDuplicatedNodeById
|
|
1249
1211
|
* property should be set to true or false.
|
|
1250
1212
|
*/
|
|
1251
1213
|
_setIsDuplicatedVal(value) {
|
|
1252
|
-
this.
|
|
1214
|
+
this._isMergeDuplicatedNodeById = value;
|
|
1253
1215
|
}
|
|
1254
1216
|
/**
|
|
1255
1217
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
@@ -1270,10 +1232,10 @@ class AbstractBinaryTree {
|
|
|
1270
1232
|
this._size = v;
|
|
1271
1233
|
}
|
|
1272
1234
|
/**
|
|
1273
|
-
* The function `
|
|
1235
|
+
* The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
|
|
1274
1236
|
* properties.
|
|
1275
1237
|
*/
|
|
1276
|
-
|
|
1238
|
+
_clearResults() {
|
|
1277
1239
|
this._visitedId = [];
|
|
1278
1240
|
this._visitedVal = [];
|
|
1279
1241
|
this._visitedNode = [];
|
|
@@ -9,6 +9,7 @@ import { BST, BSTNode } from './bst';
|
|
|
9
9
|
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId } from '../types';
|
|
10
10
|
import { IAVLTree, IAVLTreeNode } from '../interfaces';
|
|
11
11
|
export declare class AVLTreeNode<T = any, NEIGHBOR extends AVLTreeNode<T, NEIGHBOR> = AVLTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IAVLTreeNode<T, NEIGHBOR> {
|
|
12
|
+
constructor(id: BinaryTreeNodeId, val?: T);
|
|
12
13
|
}
|
|
13
14
|
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
|
|
14
15
|
/**
|