binary-tree-typed 2.4.5 → 2.5.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/README.md +0 -84
- package/dist/cjs/index.cjs +867 -404
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +864 -401
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +867 -404
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +864 -401
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/binary-tree-typed.js +860 -397
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +2 -2
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "binary-tree-typed",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "Binary Tree. Javascript & Typescript Data Structure.",
|
|
5
5
|
"browser": "dist/umd/binary-tree-typed.min.js",
|
|
6
6
|
"umd:main": "dist/umd/binary-tree-typed.min.js",
|
|
@@ -178,6 +178,6 @@
|
|
|
178
178
|
"typescript": "^4.9.5"
|
|
179
179
|
},
|
|
180
180
|
"dependencies": {
|
|
181
|
-
"data-structure-typed": "^2.
|
|
181
|
+
"data-structure-typed": "^2.5.0"
|
|
182
182
|
}
|
|
183
183
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
|
|
2
|
-
import { ERR } from '../../common';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Base class that makes a data structure iterable and provides common
|
|
@@ -26,7 +25,7 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
|
|
|
26
25
|
if (options) {
|
|
27
26
|
const { toElementFn } = options;
|
|
28
27
|
if (typeof toElementFn === 'function') this._toElementFn = toElementFn;
|
|
29
|
-
else if (toElementFn) throw new TypeError(
|
|
28
|
+
else if (toElementFn) throw new TypeError('toElementFn must be a function type');
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
31
|
|
|
@@ -36,7 +35,7 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
|
|
|
36
35
|
* @remarks
|
|
37
36
|
* Time O(1), Space O(1).
|
|
38
37
|
*/
|
|
39
|
-
protected
|
|
38
|
+
protected _toElementFn?: (rawElement: R) => E;
|
|
40
39
|
|
|
41
40
|
/**
|
|
42
41
|
* Exposes the current `toElementFn`, if configured.
|
|
@@ -225,12 +224,12 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
|
|
|
225
224
|
acc = initialValue as U;
|
|
226
225
|
} else {
|
|
227
226
|
const first = iter.next();
|
|
228
|
-
if (first.done) throw new TypeError(
|
|
227
|
+
if (first.done) throw new TypeError('Reduce of empty structure with no initial value');
|
|
229
228
|
acc = first.value as unknown as U;
|
|
230
229
|
index = 1;
|
|
231
230
|
}
|
|
232
231
|
|
|
233
|
-
for (const value of iter) {
|
|
232
|
+
for (const value of iter as unknown as Iterable<E>) {
|
|
234
233
|
acc = callbackfn(acc, value, index++, this);
|
|
235
234
|
}
|
|
236
235
|
return acc;
|
|
@@ -123,16 +123,12 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
123
123
|
*
|
|
124
124
|
* @returns The node's color.
|
|
125
125
|
*/
|
|
126
|
+
/* istanbul ignore next -- inherited field, used by RedBlackTree subclass */ /* istanbul ignore next -- inherited field, not used by AVLTree */
|
|
126
127
|
get color(): RBTNColor {
|
|
127
128
|
return this._color;
|
|
128
129
|
}
|
|
129
130
|
|
|
130
|
-
|
|
131
|
-
* Sets the color of the node.
|
|
132
|
-
* @remarks Time O(1), Space O(1)
|
|
133
|
-
*
|
|
134
|
-
* @param value - The new color.
|
|
135
|
-
*/
|
|
131
|
+
/* istanbul ignore next -- inherited field, not used by AVLTree */
|
|
136
132
|
set color(value: RBTNColor) {
|
|
137
133
|
this._color = value;
|
|
138
134
|
}
|
|
@@ -145,16 +141,12 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
145
141
|
*
|
|
146
142
|
* @returns The subtree node count.
|
|
147
143
|
*/
|
|
144
|
+
/* istanbul ignore next -- inherited field, not used by AVLTree */
|
|
148
145
|
get count(): number {
|
|
149
146
|
return this._count;
|
|
150
147
|
}
|
|
151
148
|
|
|
152
|
-
|
|
153
|
-
* Sets the count of nodes in the subtree.
|
|
154
|
-
* @remarks Time O(1), Space O(1)
|
|
155
|
-
*
|
|
156
|
-
* @param value - The new count.
|
|
157
|
-
*/
|
|
149
|
+
/* istanbul ignore next -- inherited field, not used by AVLTree */
|
|
158
150
|
set count(value: number) {
|
|
159
151
|
this._count = value;
|
|
160
152
|
}
|
|
@@ -176,6 +168,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
176
168
|
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
177
169
|
}
|
|
178
170
|
|
|
171
|
+
/* istanbul ignore next -- defensive: unreachable if tree structure is correct */
|
|
179
172
|
return 'MAL_NODE';
|
|
180
173
|
}
|
|
181
174
|
}
|
|
@@ -197,28 +190,6 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
197
190
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
198
191
|
*
|
|
199
192
|
* @example
|
|
200
|
-
* // basic AVLTree creation and add operation
|
|
201
|
-
* // Create a simple AVLTree with initial values
|
|
202
|
-
* const tree = new AVLTree([5, 2, 8, 1, 9]);
|
|
203
|
-
*
|
|
204
|
-
* tree.print();
|
|
205
|
-
* // _2___
|
|
206
|
-
* // / \
|
|
207
|
-
* // 1 _8_
|
|
208
|
-
* // / \
|
|
209
|
-
* // 5 9
|
|
210
|
-
*
|
|
211
|
-
* // Verify the tree maintains sorted order
|
|
212
|
-
* console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
|
|
213
|
-
*
|
|
214
|
-
* // Check size
|
|
215
|
-
* console.log(tree.size); // 5;
|
|
216
|
-
*
|
|
217
|
-
* // Add a new element
|
|
218
|
-
* tree.set(3);
|
|
219
|
-
* console.log(tree.size); // 6;
|
|
220
|
-
* console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
|
|
221
|
-
* @example
|
|
222
193
|
* // AVLTree has and get operations
|
|
223
194
|
* const tree = new AVLTree<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
224
195
|
*
|
|
@@ -233,23 +204,6 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
233
204
|
* // Verify tree is balanced
|
|
234
205
|
* console.log(tree.isAVLBalanced()); // true;
|
|
235
206
|
* @example
|
|
236
|
-
* // AVLTree delete and balance verification
|
|
237
|
-
* const tree = new AVLTree([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
238
|
-
*
|
|
239
|
-
* // Delete an element
|
|
240
|
-
* tree.delete(10);
|
|
241
|
-
* console.log(tree.has(10)); // false;
|
|
242
|
-
*
|
|
243
|
-
* // Tree should remain balanced after deletion
|
|
244
|
-
* console.log(tree.isAVLBalanced()); // true;
|
|
245
|
-
*
|
|
246
|
-
* // Size decreased
|
|
247
|
-
* console.log(tree.size); // 15;
|
|
248
|
-
*
|
|
249
|
-
* // Remaining elements are still sorted
|
|
250
|
-
* const keys = [...tree.keys()];
|
|
251
|
-
* console.log(keys); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16];
|
|
252
|
-
* @example
|
|
253
207
|
* // AVLTree for university ranking system with strict balance
|
|
254
208
|
* interface University {
|
|
255
209
|
* name: string;
|
|
@@ -406,6 +360,48 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
406
360
|
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
407
361
|
* @param [value] - The value, if providing just a key.
|
|
408
362
|
* @returns True if the addition was successful, false otherwise.
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
* @example
|
|
400
|
+
* // Set a key-value pair
|
|
401
|
+
* const avl = new AVLTree<number, string>();
|
|
402
|
+
* avl.set(1, 'one');
|
|
403
|
+
* avl.set(2, 'two');
|
|
404
|
+
* console.log(avl.get(1)); // 'one';
|
|
409
405
|
*/
|
|
410
406
|
override set(
|
|
411
407
|
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -424,6 +420,45 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
424
420
|
*
|
|
425
421
|
* @param keyNodeOrEntry - The node to delete.
|
|
426
422
|
* @returns An array containing deletion results.
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
* @example
|
|
457
|
+
* // Remove nodes and verify structure
|
|
458
|
+
* const avl = new AVLTree<number>([5, 3, 7, 1, 4, 6, 8]);
|
|
459
|
+
* avl.delete(3);
|
|
460
|
+
* console.log(avl.has(3)); // false;
|
|
461
|
+
* console.log(avl.size); // 6;
|
|
427
462
|
*/
|
|
428
463
|
override delete(
|
|
429
464
|
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
@@ -445,6 +480,26 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
445
480
|
*
|
|
446
481
|
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
447
482
|
* @returns True if successful, false if the tree was empty.
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
* @example
|
|
496
|
+
* // Rebalance the tree
|
|
497
|
+
* const avl = new AVLTree<number>();
|
|
498
|
+
* // Insert in sorted order (worst case for BST)
|
|
499
|
+
* for (let i = 1; i <= 7; i++) avl.add(i);
|
|
500
|
+
* console.log(avl.isAVLBalanced()); // false;
|
|
501
|
+
* avl.perfectlyBalance();
|
|
502
|
+
* console.log(avl.isAVLBalanced()); // true;
|
|
448
503
|
*/
|
|
449
504
|
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
450
505
|
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
@@ -486,6 +541,33 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
486
541
|
* @param [options] - Options for the new AVLTree.
|
|
487
542
|
* @param [thisArg] - `this` context for the callback.
|
|
488
543
|
* @returns A new, mapped AVLTree.
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
* @example
|
|
567
|
+
* // Transform to new tree
|
|
568
|
+
* const avl = new AVLTree<number, number>([[1, 10], [2, 20], [3, 30]]);
|
|
569
|
+
* const doubled = avl.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
|
|
570
|
+
* console.log([...doubled.values()]); // [20, 40, 60];
|
|
489
571
|
*/
|
|
490
572
|
override map<MK = K, MV = V, MR = any>(
|
|
491
573
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
@@ -574,6 +656,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
574
656
|
|
|
575
657
|
return destNodeEnsured;
|
|
576
658
|
}
|
|
659
|
+
/* istanbul ignore next -- defensive: srcNode/destNode are always valid when called internally */
|
|
577
660
|
return undefined;
|
|
578
661
|
}
|
|
579
662
|
|