data-structure-typed 2.4.4 → 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/CHANGELOG.md +22 -1
- package/README.md +34 -1
- package/dist/cjs/index.cjs +10639 -2151
- package/dist/cjs-legacy/index.cjs +10694 -2195
- package/dist/esm/index.mjs +10639 -2150
- package/dist/esm-legacy/index.mjs +10694 -2194
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- 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 +439 -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 +217 -31
- 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/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -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 +313 -66
- 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/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/data-structure-typed.js +10725 -2221
- package/dist/umd/data-structure-typed.min.js +4 -2
- package/package.json +5 -4
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +146 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
- package/src/data-structures/binary-tree/binary-tree.ts +567 -121
- package/src/data-structures/binary-tree/bst.ts +370 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
- package/src/data-structures/binary-tree/segment-tree.ts +378 -248
- package/src/data-structures/binary-tree/tree-map.ts +1411 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
- package/src/data-structures/binary-tree/tree-set.ts +1257 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +233 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +308 -59
- package/src/data-structures/hash/hash-map.ts +254 -79
- package/src/data-structures/heap/heap.ts +305 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
- package/src/data-structures/matrix/matrix.ts +433 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- 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 +358 -68
- package/src/data-structures/queue/queue.ts +223 -42
- package/src/data-structures/stack/stack.ts +184 -32
- package/src/data-structures/trie/trie.ts +227 -44
- 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/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -126,34 +126,6 @@ export declare class BSTNode<K = any, V = any> {
|
|
|
126
126
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
127
127
|
*
|
|
128
128
|
* @example
|
|
129
|
-
* // basic BST creation and add operation
|
|
130
|
-
* // Create a simple BST with numeric keys
|
|
131
|
-
* const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
132
|
-
*
|
|
133
|
-
* // Keep the example output in source comments but avoid noisy test logs.
|
|
134
|
-
* await withMutedConsole(() => bst.print());
|
|
135
|
-
* // _______8__________
|
|
136
|
-
* // / \
|
|
137
|
-
* // ___4___ ____12_____
|
|
138
|
-
* // / \ / \
|
|
139
|
-
* // _2_ _6_ _10__ _14__
|
|
140
|
-
* // / \ / \ / \ / \
|
|
141
|
-
* // 1 3 5 7 9 11 13 15__
|
|
142
|
-
* // \
|
|
143
|
-
* // 16
|
|
144
|
-
*
|
|
145
|
-
* // Verify size
|
|
146
|
-
* console.log(bst.size); // 16;
|
|
147
|
-
*
|
|
148
|
-
* // Add new elements
|
|
149
|
-
* bst.set(17);
|
|
150
|
-
* bst.set(0);
|
|
151
|
-
* console.log(bst.size); // 18;
|
|
152
|
-
*
|
|
153
|
-
* // Verify keys are searchable
|
|
154
|
-
* console.log(bst.has(11)); // true;
|
|
155
|
-
* console.log(bst.has(100)); // false;
|
|
156
|
-
* @example
|
|
157
129
|
* // BST delete and search after deletion
|
|
158
130
|
* const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
159
131
|
*
|
|
@@ -336,10 +308,90 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
336
308
|
* @returns True if the key is valid, false otherwise.
|
|
337
309
|
*/
|
|
338
310
|
isValidKey(key: any): key is K;
|
|
311
|
+
/**
|
|
312
|
+
* Depth-first search traversal
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
* @example
|
|
336
|
+
* // Depth-first traversal
|
|
337
|
+
* const bst = new BST<number>([5, 3, 7, 1, 4]);
|
|
338
|
+
* const inOrder = bst.dfs(node => node.key, 'IN');
|
|
339
|
+
* console.log(inOrder); // [1, 3, 4, 5, 7];
|
|
340
|
+
*/
|
|
339
341
|
dfs(): (K | undefined)[];
|
|
340
342
|
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
343
|
+
/**
|
|
344
|
+
* BinaryTree level-order traversal
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
* @example
|
|
360
|
+
* // Breadth-first traversal
|
|
361
|
+
* const bst = new BST<number>([5, 3, 7]);
|
|
362
|
+
* const result = bst.bfs(node => node.key);
|
|
363
|
+
* console.log(result.length); // 3;
|
|
364
|
+
*/
|
|
341
365
|
bfs(): (K | undefined)[];
|
|
342
366
|
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
367
|
+
/**
|
|
368
|
+
* Level-order grouping
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
* @example
|
|
387
|
+
* // Level-order grouping
|
|
388
|
+
* const bst = new BST<number>([5, 3, 7, 1, 4]);
|
|
389
|
+
* const levels = bst.listLevels(node => node.key);
|
|
390
|
+
* console.log(levels.length); // > 0;
|
|
391
|
+
* console.log(levels[0].length); // 1; // root level has 1 node
|
|
392
|
+
* const allKeys = levels.flat().sort((a, b) => a - b);
|
|
393
|
+
* console.log(allKeys); // [1, 3, 4, 5, 7];
|
|
394
|
+
*/
|
|
343
395
|
listLevels(): (K | undefined)[][];
|
|
344
396
|
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[][];
|
|
345
397
|
/**
|
|
@@ -350,10 +402,73 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
350
402
|
* @param [startNode=this._root] - The node to start the search from.
|
|
351
403
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
352
404
|
* @returns The first matching node, or undefined if not found.
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
* @example
|
|
425
|
+
* // Get node object by key
|
|
426
|
+
* const bst = new BST<number, string>([[5, 'root'], [3, 'left'], [7, 'right']]);
|
|
427
|
+
* const node = bst.getNode(3);
|
|
428
|
+
* console.log(node?.key); // 3;
|
|
429
|
+
* console.log(node?.value); // 'left';
|
|
353
430
|
*/
|
|
354
431
|
getNode(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, startNode?: BSTNOptKeyOrNode<K, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>;
|
|
432
|
+
/**
|
|
433
|
+
* Search nodes by predicate
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
* @example
|
|
447
|
+
* // Search nodes by predicate
|
|
448
|
+
* const bst = new BST<number, string>([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']]);
|
|
449
|
+
* const found = bst.search(node => node.key > 2, true);
|
|
450
|
+
* console.log(found.length); // >= 1;
|
|
451
|
+
*/
|
|
355
452
|
search(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean): (K | undefined)[];
|
|
356
453
|
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne: boolean, callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
454
|
+
/**
|
|
455
|
+
* Find all keys in a range
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
* @example
|
|
468
|
+
* // Find all keys in a range
|
|
469
|
+
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
470
|
+
* console.log(bst.rangeSearch([15, 35])); // [20, 30];
|
|
471
|
+
*/
|
|
357
472
|
rangeSearch(range: Range<K> | [K, K]): (K | undefined)[];
|
|
358
473
|
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
359
474
|
/**
|
|
@@ -363,6 +478,37 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
363
478
|
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
364
479
|
* @param [value] - The value, if providing just a key.
|
|
365
480
|
* @returns True if the addition was successful, false otherwise.
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
* @example
|
|
507
|
+
* // Set a key-value pair
|
|
508
|
+
* const bst = new BST<number, string>();
|
|
509
|
+
* bst.set(1, 'one');
|
|
510
|
+
* bst.set(2, 'two');
|
|
511
|
+
* console.log(bst.get(1)); // 'one';
|
|
366
512
|
*/
|
|
367
513
|
set(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
368
514
|
/**
|
|
@@ -376,6 +522,23 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
376
522
|
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
377
523
|
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
378
524
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
* @example
|
|
537
|
+
* // Set multiple key-value pairs
|
|
538
|
+
* const bst = new BST<number, string>();
|
|
539
|
+
* bst.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
540
|
+
* console.log(bst.size); // 3;
|
|
541
|
+
* console.log(bst.get(2)); // 'b';
|
|
379
542
|
*/
|
|
380
543
|
setMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
381
544
|
/**
|
|
@@ -383,6 +546,23 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
383
546
|
* Equivalent to Java TreeMap.ceiling.
|
|
384
547
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
385
548
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
* @example
|
|
561
|
+
* // Find the least key ≥ target
|
|
562
|
+
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
563
|
+
* console.log(bst.ceiling(25)); // 30;
|
|
564
|
+
* console.log(bst.ceiling(30)); // 30;
|
|
565
|
+
* console.log(bst.ceiling(55)); // undefined;
|
|
386
566
|
*/
|
|
387
567
|
ceiling(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
388
568
|
/**
|
|
@@ -396,6 +576,22 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
396
576
|
* Equivalent to Java TreeMap.higher.
|
|
397
577
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
398
578
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
* @example
|
|
591
|
+
* // Find the least key strictly > target
|
|
592
|
+
* const bst = new BST<number>([10, 20, 30, 40]);
|
|
593
|
+
* console.log(bst.higher(20)); // 30;
|
|
594
|
+
* console.log(bst.higher(40)); // undefined;
|
|
399
595
|
*/
|
|
400
596
|
higher(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
401
597
|
/**
|
|
@@ -409,6 +605,23 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
409
605
|
* Equivalent to Java TreeMap.floor.
|
|
410
606
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
411
607
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
* @example
|
|
620
|
+
* // Find the greatest key ≤ target
|
|
621
|
+
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
622
|
+
* console.log(bst.floor(25)); // 20;
|
|
623
|
+
* console.log(bst.floor(10)); // 10;
|
|
624
|
+
* console.log(bst.floor(5)); // undefined;
|
|
412
625
|
*/
|
|
413
626
|
floor(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
414
627
|
/**
|
|
@@ -422,6 +635,22 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
422
635
|
* Equivalent to Java TreeMap.lower.
|
|
423
636
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
424
637
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
* @example
|
|
650
|
+
* // Find the greatest key strictly < target
|
|
651
|
+
* const bst = new BST<number>([10, 20, 30, 40]);
|
|
652
|
+
* console.log(bst.lower(30)); // 20;
|
|
653
|
+
* console.log(bst.lower(10)); // undefined;
|
|
425
654
|
*/
|
|
426
655
|
lower(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
427
656
|
/**
|
|
@@ -438,6 +667,23 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
438
667
|
*
|
|
439
668
|
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
440
669
|
* @returns True if successful, false if the tree was empty.
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
* @example
|
|
680
|
+
* // Rebalance the tree
|
|
681
|
+
* const bst = new BST<number>();
|
|
682
|
+
* // Insert in sorted order (worst case for BST)
|
|
683
|
+
* for (let i = 1; i <= 7; i++) bst.add(i);
|
|
684
|
+
* console.log(bst.isAVLBalanced()); // false;
|
|
685
|
+
* bst.perfectlyBalance();
|
|
686
|
+
* console.log(bst.isAVLBalanced()); // true;
|
|
441
687
|
*/
|
|
442
688
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
443
689
|
/**
|
|
@@ -446,6 +692,19 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
446
692
|
*
|
|
447
693
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
448
694
|
* @returns True if the tree is AVL balanced, false otherwise.
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
* @example
|
|
705
|
+
* // Check if tree is height-balanced
|
|
706
|
+
* const bst = new BST<number>([3, 1, 5, 2, 4]);
|
|
707
|
+
* console.log(bst.isAVLBalanced()); // true;
|
|
449
708
|
*/
|
|
450
709
|
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
451
710
|
/**
|
|
@@ -460,6 +719,30 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
460
719
|
* @param [options] - Options for the new BST.
|
|
461
720
|
* @param [thisArg] - `this` context for the callback.
|
|
462
721
|
* @returns A new, mapped BST.
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
* @example
|
|
742
|
+
* // Transform to new tree
|
|
743
|
+
* const bst = new BST<number, number>([[1, 10], [2, 20], [3, 30]]);
|
|
744
|
+
* const doubled = bst.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
|
|
745
|
+
* console.log([...doubled.values()]); // [20, 40, 60];
|
|
463
746
|
*/
|
|
464
747
|
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BSTOptions<MK, MV, MR>>, thisArg?: unknown): BST<MK, MV, MR>;
|
|
465
748
|
/**
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, FamilyPosition, NodePredicate, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, FamilyPosition, NodePredicate, RBTNColor, IterationType, RedBlackTreeOptions } from '../../types';
|
|
9
9
|
import { BST } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any> {
|
|
@@ -56,12 +56,6 @@ export declare class RedBlackTreeNode<K = any, V = any> {
|
|
|
56
56
|
* @returns The height.
|
|
57
57
|
*/
|
|
58
58
|
get height(): number;
|
|
59
|
-
/**
|
|
60
|
-
* Sets the height of the node.
|
|
61
|
-
* @remarks Time O(1), Space O(1)
|
|
62
|
-
*
|
|
63
|
-
* @param value - The new height.
|
|
64
|
-
*/
|
|
65
59
|
set height(value: number);
|
|
66
60
|
_color: RBTNColor;
|
|
67
61
|
/**
|
|
@@ -86,13 +80,6 @@ export declare class RedBlackTreeNode<K = any, V = any> {
|
|
|
86
80
|
* @returns The subtree node count.
|
|
87
81
|
*/
|
|
88
82
|
get count(): number;
|
|
89
|
-
/**
|
|
90
|
-
* Sets the count of nodes in the subtree.
|
|
91
|
-
* @remarks Time O(1), Space O(1)
|
|
92
|
-
*
|
|
93
|
-
* @param value - The new count.
|
|
94
|
-
*/
|
|
95
|
-
set count(value: number);
|
|
96
83
|
/**
|
|
97
84
|
* Gets the position of the node relative to its parent.
|
|
98
85
|
* @remarks Time O(1), Space O(1)
|
|
@@ -111,23 +98,6 @@ export declare class RedBlackTreeNode<K = any, V = any> {
|
|
|
111
98
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
112
99
|
*
|
|
113
100
|
* @example
|
|
114
|
-
* // basic Red-Black Tree with simple number keys
|
|
115
|
-
* // Create a simple Red-Black Tree with numeric keys
|
|
116
|
-
* const tree = new RedBlackTree([5, 2, 8, 1, 9]);
|
|
117
|
-
*
|
|
118
|
-
* tree.print();
|
|
119
|
-
* // _2___
|
|
120
|
-
* // / \
|
|
121
|
-
* // 1 _8_
|
|
122
|
-
* // / \
|
|
123
|
-
* // 5 9
|
|
124
|
-
*
|
|
125
|
-
* // Verify the tree maintains sorted order
|
|
126
|
-
* console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
|
|
127
|
-
*
|
|
128
|
-
* // Check size
|
|
129
|
-
* console.log(tree.size); // 5;
|
|
130
|
-
* @example
|
|
131
101
|
* // Red-Black Tree with key-value pairs for lookups
|
|
132
102
|
* interface Employee {
|
|
133
103
|
* id: number;
|
|
@@ -254,6 +224,46 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
254
224
|
/**
|
|
255
225
|
* Remove all nodes and clear internal caches.
|
|
256
226
|
* @remarks Time O(n) average, Space O(1)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
* @example
|
|
263
|
+
* // Remove all entries
|
|
264
|
+
* const rbt = new RedBlackTree<number>([1, 2, 3]);
|
|
265
|
+
* rbt.clear();
|
|
266
|
+
* console.log(rbt.isEmpty()); // true;
|
|
257
267
|
*/
|
|
258
268
|
clear(): void;
|
|
259
269
|
/**
|
|
@@ -355,6 +365,59 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
355
365
|
* - updates via a single-pass search (no double walk)
|
|
356
366
|
*
|
|
357
367
|
* @remarks Time O(log n) average, Space O(1)
|
|
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
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
* @example
|
|
405
|
+
* // basic Red-Black Tree with simple number keys
|
|
406
|
+
* // Create a simple Red-Black Tree with numeric keys
|
|
407
|
+
* const tree = new RedBlackTree([5, 2, 8, 1, 9]);
|
|
408
|
+
*
|
|
409
|
+
* tree.print();
|
|
410
|
+
* // _2___
|
|
411
|
+
* // / \
|
|
412
|
+
* // 1 _8_
|
|
413
|
+
* // / \
|
|
414
|
+
* // 5 9
|
|
415
|
+
*
|
|
416
|
+
* // Verify the tree maintains sorted order
|
|
417
|
+
* console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
|
|
418
|
+
*
|
|
419
|
+
* // Check size
|
|
420
|
+
* console.log(tree.size); // 5;
|
|
358
421
|
*/
|
|
359
422
|
set(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
360
423
|
/**
|
|
@@ -362,6 +425,50 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
362
425
|
* @remarks Time O(log n) average, Space O(1)
|
|
363
426
|
* @param keyNodeEntryRawOrPredicate - Key, node, or [key, value] entry identifying the node to delete.
|
|
364
427
|
* @returns Array with deletion metadata (removed node, rebalancing hint if any).
|
|
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
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
* @example
|
|
467
|
+
* // Remove and rebalance
|
|
468
|
+
* const rbt = new RedBlackTree<number>([10, 5, 15, 3, 7]);
|
|
469
|
+
* rbt.delete(5);
|
|
470
|
+
* console.log(rbt.has(5)); // false;
|
|
471
|
+
* console.log(rbt.size); // 4;
|
|
365
472
|
*/
|
|
366
473
|
delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, RedBlackTreeNode<K, V>> | NodePredicate<RedBlackTreeNode<K, V> | null>): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[];
|
|
367
474
|
/**
|
|
@@ -375,6 +482,85 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
375
482
|
* @param [thisArg] - See parameter type for details.
|
|
376
483
|
* @returns A new RedBlackTree with mapped entries.
|
|
377
484
|
*/
|
|
485
|
+
/**
|
|
486
|
+
* Red-Black trees are self-balancing — `perfectlyBalance` rebuilds via
|
|
487
|
+
* sorted bulk insert, which naturally produces a balanced RBT.
|
|
488
|
+
* @remarks Time O(N), Space O(N)
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
* @example
|
|
515
|
+
* // Rebalance tree
|
|
516
|
+
* const rbt = new RedBlackTree<number>([1, 2, 3, 4, 5]);
|
|
517
|
+
* rbt.perfectlyBalance();
|
|
518
|
+
* console.log(rbt.isAVLBalanced()); // true;
|
|
519
|
+
*/
|
|
520
|
+
perfectlyBalance(_iterationType?: IterationType): boolean;
|
|
521
|
+
/**
|
|
522
|
+
* Transform to new tree
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
* @example
|
|
559
|
+
* // Transform to new tree
|
|
560
|
+
* const rbt = new RedBlackTree<number, number>([[1, 10], [2, 20]]);
|
|
561
|
+
* const doubled = rbt.map((v, k) => [k, (v ?? 0) * 2] as [number, number]);
|
|
562
|
+
* console.log([...doubled.values()]); // [20, 40];
|
|
563
|
+
*/
|
|
378
564
|
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<RedBlackTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
379
565
|
/**
|
|
380
566
|
* (Internal) Create an empty instance of the same concrete tree type.
|