min-heap-typed 1.42.2 → 1.42.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/data-structures/binary-tree/binary-tree.d.ts +57 -53
- package/dist/data-structures/binary-tree/binary-tree.js +116 -54
- package/dist/data-structures/binary-tree/bst.d.ts +42 -15
- package/dist/data-structures/binary-tree/bst.js +77 -21
- package/dist/data-structures/binary-tree/rb-tree.d.ts +28 -51
- package/dist/data-structures/binary-tree/rb-tree.js +148 -180
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +10 -10
- package/dist/data-structures/binary-tree/tree-multiset.js +20 -17
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +0 -5
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +5 -4
- package/src/data-structures/binary-tree/binary-tree.ts +227 -158
- package/src/data-structures/binary-tree/bst.ts +100 -34
- package/src/data-structures/binary-tree/rb-tree.ts +227 -236
- package/src/data-structures/binary-tree/tree-multiset.ts +24 -23
- package/src/data-structures/graph/abstract-graph.ts +18 -14
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +5 -5
|
@@ -12,8 +12,53 @@ import {IBinaryTree} from '../../interfaces';
|
|
|
12
12
|
import {Queue} from '../queue';
|
|
13
13
|
|
|
14
14
|
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
15
|
+
override parent: N | undefined;
|
|
15
16
|
constructor(key: BTNKey, value?: V) {
|
|
16
17
|
super(key, value);
|
|
18
|
+
this.parent = undefined;
|
|
19
|
+
this._left = undefined;
|
|
20
|
+
this._right = undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected override _left: N | undefined;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Get the left child node.
|
|
27
|
+
*/
|
|
28
|
+
override get left(): N | undefined {
|
|
29
|
+
return this._left;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Set the left child node.
|
|
34
|
+
* @param {N | undefined} v - The left child node.
|
|
35
|
+
*/
|
|
36
|
+
override set left(v: N | undefined) {
|
|
37
|
+
if (v) {
|
|
38
|
+
v.parent = this as unknown as N;
|
|
39
|
+
}
|
|
40
|
+
this._left = v;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
protected override _right: N | undefined;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Get the right child node.
|
|
48
|
+
*/
|
|
49
|
+
override get right(): N | undefined {
|
|
50
|
+
return this._right;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Set the right child node.
|
|
55
|
+
* @param {N | undefined} v - The right child node.
|
|
56
|
+
*/
|
|
57
|
+
override set right(v: N | undefined) {
|
|
58
|
+
if (v) {
|
|
59
|
+
v.parent = this as unknown as N;
|
|
60
|
+
}
|
|
61
|
+
this._right = v;
|
|
17
62
|
}
|
|
18
63
|
}
|
|
19
64
|
|
|
@@ -29,6 +74,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
29
74
|
*/
|
|
30
75
|
constructor(options?: BSTOptions) {
|
|
31
76
|
super(options);
|
|
77
|
+
this._root = undefined;
|
|
32
78
|
if (options !== undefined) {
|
|
33
79
|
const {comparator} = options;
|
|
34
80
|
if (comparator !== undefined) {
|
|
@@ -36,6 +82,14 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
36
82
|
}
|
|
37
83
|
}
|
|
38
84
|
}
|
|
85
|
+
protected override _root: N | undefined = undefined;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get the root node of the binary tree.
|
|
89
|
+
*/
|
|
90
|
+
override get root(): N | undefined {
|
|
91
|
+
return this._root;
|
|
92
|
+
}
|
|
39
93
|
|
|
40
94
|
/**
|
|
41
95
|
* The function creates a new binary search tree node with the given key and value.
|
|
@@ -52,25 +106,29 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
52
106
|
/**
|
|
53
107
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
54
108
|
* into the tree.
|
|
55
|
-
* @param {BTNKey | N |
|
|
56
|
-
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `
|
|
109
|
+
* @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
|
|
110
|
+
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `undefined`.
|
|
57
111
|
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
|
58
112
|
* binary search tree.
|
|
59
113
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
60
|
-
* was not added or if the parameters were invalid, it returns
|
|
114
|
+
* was not added or if the parameters were invalid, it returns undefined or undefined.
|
|
61
115
|
*/
|
|
62
|
-
override add(keyOrNode: BTNKey | N | null, value?: V): N |
|
|
116
|
+
override add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined {
|
|
117
|
+
if (keyOrNode === 8) {
|
|
118
|
+
debugger
|
|
119
|
+
}
|
|
120
|
+
if (keyOrNode === null) return undefined;
|
|
63
121
|
// TODO support node as a parameter
|
|
64
|
-
let inserted:
|
|
65
|
-
let newNode:
|
|
122
|
+
let inserted:N | undefined;
|
|
123
|
+
let newNode:N | undefined;
|
|
66
124
|
if (keyOrNode instanceof BSTNode) {
|
|
67
125
|
newNode = keyOrNode;
|
|
68
126
|
} else if (typeof keyOrNode === 'number') {
|
|
69
127
|
newNode = this.createNode(keyOrNode, value);
|
|
70
|
-
} else
|
|
71
|
-
newNode =
|
|
128
|
+
} else {
|
|
129
|
+
newNode = undefined;
|
|
72
130
|
}
|
|
73
|
-
if (this.root ===
|
|
131
|
+
if (this.root === undefined) {
|
|
74
132
|
this._setRoot(newNode);
|
|
75
133
|
this._size = this.size + 1;
|
|
76
134
|
inserted = this.root;
|
|
@@ -78,7 +136,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
78
136
|
let cur = this.root;
|
|
79
137
|
let traversing = true;
|
|
80
138
|
while (traversing) {
|
|
81
|
-
if (cur !==
|
|
139
|
+
if (cur !== undefined && newNode !== undefined) {
|
|
82
140
|
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
83
141
|
if (newNode) {
|
|
84
142
|
cur.value = newNode.value;
|
|
@@ -131,29 +189,29 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
131
189
|
* @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
132
190
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
133
191
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
134
|
-
* `
|
|
192
|
+
* `undefined
|
|
135
193
|
* @param {V[]} data - The values of tree nodes
|
|
136
194
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
137
195
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
138
196
|
* It can have two possible values:
|
|
139
|
-
* @returns The `addMany` function returns an array of `N`, `
|
|
197
|
+
* @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
|
|
140
198
|
*/
|
|
141
199
|
|
|
142
200
|
override addMany(
|
|
143
|
-
keysOrNodes: (BTNKey |
|
|
201
|
+
keysOrNodes: (BTNKey | undefined)[] | (N | undefined)[],
|
|
144
202
|
data?: V[],
|
|
145
203
|
isBalanceAdd = true,
|
|
146
204
|
iterationType = this.iterationType
|
|
147
|
-
): (N |
|
|
205
|
+
): (N | undefined)[] {
|
|
148
206
|
// TODO this addMany function is inefficient, it should be optimized
|
|
149
|
-
function hasNoNull(arr: (BTNKey |
|
|
150
|
-
return arr.indexOf(
|
|
207
|
+
function hasNoNull(arr: (BTNKey | undefined)[] | (N | undefined)[]): arr is BTNKey[] | N[] {
|
|
208
|
+
return arr.indexOf(undefined) === -1;
|
|
151
209
|
}
|
|
152
210
|
|
|
153
211
|
if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
|
|
154
|
-
return super.addMany(keysOrNodes, data);
|
|
212
|
+
return super.addMany(keysOrNodes, data).map(n => n ?? undefined);
|
|
155
213
|
}
|
|
156
|
-
const inserted: (N |
|
|
214
|
+
const inserted: (N | undefined)[] = [];
|
|
157
215
|
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
158
216
|
(value: BTNKey | N, index) => [value, data?.[index]] as [BTNKey | N, V]
|
|
159
217
|
);
|
|
@@ -169,7 +227,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
169
227
|
return false;
|
|
170
228
|
}
|
|
171
229
|
|
|
172
|
-
let sortedKeysOrNodes: (number | N |
|
|
230
|
+
let sortedKeysOrNodes: (number | N | undefined)[] = [],
|
|
173
231
|
sortedData: (V | undefined)[] | undefined = [];
|
|
174
232
|
|
|
175
233
|
if (isNodeOrNullTuple(combinedArr)) {
|
|
@@ -181,7 +239,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
181
239
|
}
|
|
182
240
|
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
183
241
|
sortedData = sorted.map(([, value]) => value);
|
|
184
|
-
const recursive = (arr: (BTNKey |
|
|
242
|
+
const recursive = (arr: (BTNKey | undefined | N)[], data?: (V | undefined)[]) => {
|
|
185
243
|
if (arr.length === 0) return;
|
|
186
244
|
|
|
187
245
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
@@ -220,7 +278,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
220
278
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
221
279
|
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
222
280
|
* rightmost node otherwise.
|
|
223
|
-
* @param {N |
|
|
281
|
+
* @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting point for finding the last
|
|
224
282
|
* key in a binary tree. It represents the root node of the subtree from which the search for the
|
|
225
283
|
* last key should begin. If no specific `beginRoot` is provided, the search will start from the root
|
|
226
284
|
* of the entire binary
|
|
@@ -231,7 +289,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
231
289
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
232
290
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
233
291
|
*/
|
|
234
|
-
lastKey(beginRoot: N |
|
|
292
|
+
lastKey(beginRoot: N | undefined = this.root, iterationType = this.iterationType): BTNKey {
|
|
235
293
|
if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
236
294
|
else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
|
|
237
295
|
else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
@@ -251,18 +309,18 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
251
309
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
252
310
|
* containing only that node. If set to false (default), the function will continue the traversal and
|
|
253
311
|
* return an array containing all nodes that match the node
|
|
254
|
-
* @param {N |
|
|
312
|
+
* @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
|
|
255
313
|
* specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
|
|
256
|
-
* is `
|
|
314
|
+
* is `undefined`, an empty array will be returned.
|
|
257
315
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
258
316
|
* traverse the binary tree. It can have one of the following values:
|
|
259
317
|
* @returns an array of nodes (N[]).
|
|
260
318
|
*/
|
|
261
319
|
override getNodes<C extends BTNCallback<N>>(
|
|
262
|
-
identifier: ReturnType<C> |
|
|
320
|
+
identifier: ReturnType<C> | undefined,
|
|
263
321
|
callback: C = this.defaultOneParamCallback as C,
|
|
264
322
|
onlyOne = false,
|
|
265
|
-
beginRoot: N |
|
|
323
|
+
beginRoot: N | undefined = this.root,
|
|
266
324
|
iterationType = this.iterationType
|
|
267
325
|
): N[] {
|
|
268
326
|
if (!beginRoot) return [];
|
|
@@ -324,10 +382,10 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
324
382
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
325
383
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
326
384
|
* of the following values:
|
|
327
|
-
* @param {BTNKey | N |
|
|
385
|
+
* @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter in the
|
|
328
386
|
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
329
387
|
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
330
|
-
* (`BTNKey`), or `
|
|
388
|
+
* (`BTNKey`), or `undefined` to
|
|
331
389
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
332
390
|
* done recursively or iteratively. It can have two possible values:
|
|
333
391
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
@@ -335,10 +393,10 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
335
393
|
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
|
|
336
394
|
callback: C = this.defaultOneParamCallback as C,
|
|
337
395
|
lesserOrGreater: CP = CP.lt,
|
|
338
|
-
targetNode: BTNKey | N |
|
|
396
|
+
targetNode: BTNKey | N | undefined = this.root,
|
|
339
397
|
iterationType = this.iterationType
|
|
340
398
|
): ReturnType<C>[] {
|
|
341
|
-
if (typeof targetNode === 'number') targetNode = this.getNode(targetNode);
|
|
399
|
+
if (typeof targetNode === 'number') targetNode = this.getNode(targetNode) ?? undefined;
|
|
342
400
|
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
343
401
|
if (!targetNode) return ans;
|
|
344
402
|
const targetKey = targetNode.key;
|
|
@@ -417,6 +475,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
417
475
|
if (l <= r) {
|
|
418
476
|
const m = l + Math.floor((r - l) / 2);
|
|
419
477
|
const midNode = sorted[m];
|
|
478
|
+
debugger
|
|
420
479
|
this.add(midNode.key, midNode.value);
|
|
421
480
|
stack.push([m + 1, r]);
|
|
422
481
|
stack.push([l, m - 1]);
|
|
@@ -439,7 +498,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
439
498
|
let balanced = true;
|
|
440
499
|
|
|
441
500
|
if (iterationType === IterationType.RECURSIVE) {
|
|
442
|
-
const _height = (cur: N |
|
|
501
|
+
const _height = (cur: N | undefined): number => {
|
|
443
502
|
if (!cur) return 0;
|
|
444
503
|
const leftHeight = _height(cur.left),
|
|
445
504
|
rightHeight = _height(cur.right);
|
|
@@ -449,8 +508,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
449
508
|
_height(this.root);
|
|
450
509
|
} else {
|
|
451
510
|
const stack: N[] = [];
|
|
452
|
-
let node: N |
|
|
453
|
-
last:
|
|
511
|
+
let node: N | undefined = this.root,
|
|
512
|
+
last:N | undefined = undefined;
|
|
454
513
|
const depths: Map<N, number> = new Map();
|
|
455
514
|
|
|
456
515
|
while (stack.length > 0 || node) {
|
|
@@ -467,7 +526,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
467
526
|
if (Math.abs(left - right) > 1) return false;
|
|
468
527
|
depths.set(node, 1 + Math.max(left, right));
|
|
469
528
|
last = node;
|
|
470
|
-
node =
|
|
529
|
+
node = undefined;
|
|
471
530
|
}
|
|
472
531
|
} else node = node.right;
|
|
473
532
|
}
|
|
@@ -479,6 +538,13 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
479
538
|
|
|
480
539
|
protected _comparator: BSTComparator = (a, b) => a - b;
|
|
481
540
|
|
|
541
|
+
protected _setRoot(v: N | undefined) {
|
|
542
|
+
if (v) {
|
|
543
|
+
v.parent = undefined;
|
|
544
|
+
}
|
|
545
|
+
this._root = v;
|
|
546
|
+
}
|
|
547
|
+
|
|
482
548
|
/**
|
|
483
549
|
* The function compares two values using a comparator function and returns whether the first value
|
|
484
550
|
* is greater than, less than, or equal to the second value.
|