data-structure-typed 2.5.3 → 2.6.1
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/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- package/.husky/pre-commit +3 -0
- package/CHANGELOG.md +1 -1
- package/MIGRATION.md +48 -0
- package/README.md +20 -2
- package/README_CN.md +20 -2
- package/SPECIFICATION.md +24 -0
- package/SPECIFICATION.zh-CN.md +24 -0
- package/dist/cjs/binary-tree.cjs +1897 -19
- package/dist/cjs/graph.cjs +174 -0
- package/dist/cjs/hash.cjs +33 -0
- package/dist/cjs/heap.cjs +71 -0
- package/dist/cjs/index.cjs +2383 -3
- package/dist/cjs/linked-list.cjs +224 -2
- package/dist/cjs/matrix.cjs +24 -0
- package/dist/cjs/priority-queue.cjs +71 -0
- package/dist/cjs/queue.cjs +221 -1
- package/dist/cjs/stack.cjs +59 -0
- package/dist/cjs/trie.cjs +62 -0
- package/dist/cjs-legacy/binary-tree.cjs +1897 -19
- package/dist/cjs-legacy/graph.cjs +174 -0
- package/dist/cjs-legacy/hash.cjs +33 -0
- package/dist/cjs-legacy/heap.cjs +71 -0
- package/dist/cjs-legacy/index.cjs +2383 -3
- package/dist/cjs-legacy/linked-list.cjs +224 -2
- package/dist/cjs-legacy/matrix.cjs +24 -0
- package/dist/cjs-legacy/priority-queue.cjs +71 -0
- package/dist/cjs-legacy/queue.cjs +221 -1
- package/dist/cjs-legacy/stack.cjs +59 -0
- package/dist/cjs-legacy/trie.cjs +62 -0
- package/dist/esm/binary-tree.mjs +1897 -19
- package/dist/esm/graph.mjs +174 -0
- package/dist/esm/hash.mjs +33 -0
- package/dist/esm/heap.mjs +71 -0
- package/dist/esm/index.mjs +2383 -3
- package/dist/esm/linked-list.mjs +224 -2
- package/dist/esm/matrix.mjs +24 -0
- package/dist/esm/priority-queue.mjs +71 -0
- package/dist/esm/queue.mjs +221 -1
- package/dist/esm/stack.mjs +59 -0
- package/dist/esm/trie.mjs +62 -0
- package/dist/esm-legacy/binary-tree.mjs +1897 -19
- package/dist/esm-legacy/graph.mjs +174 -0
- package/dist/esm-legacy/hash.mjs +33 -0
- package/dist/esm-legacy/heap.mjs +71 -0
- package/dist/esm-legacy/index.mjs +2383 -3
- package/dist/esm-legacy/linked-list.mjs +224 -2
- package/dist/esm-legacy/matrix.mjs +24 -0
- package/dist/esm-legacy/priority-queue.mjs +71 -0
- package/dist/esm-legacy/queue.mjs +221 -1
- package/dist/esm-legacy/stack.mjs +59 -0
- package/dist/esm-legacy/trie.mjs +62 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
- package/dist/types/data-structures/heap/heap.d.ts +42 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
- package/dist/types/data-structures/queue/deque.d.ts +90 -1
- package/dist/types/data-structures/queue/queue.d.ts +36 -0
- package/dist/types/data-structures/stack/stack.d.ts +30 -0
- package/dist/types/data-structures/trie/trie.d.ts +36 -0
- package/dist/umd/data-structure-typed.js +2383 -3
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
- package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
- package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
- package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +30 -26
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
- package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
- package/jest.integration.config.js +1 -2
- package/package.json +51 -50
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +32 -3
- package/src/data-structures/base/linear-base.ts +13 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -493
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
- package/src/data-structures/binary-tree/bst.ts +158 -1010
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
- package/src/data-structures/binary-tree/segment-tree.ts +73 -333
- package/src/data-structures/binary-tree/tree-map.ts +462 -4749
- package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
- package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
- package/src/data-structures/binary-tree/tree-set.ts +437 -4443
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -532
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -484
- package/src/data-structures/hash/hash-map.ts +154 -549
- package/src/data-structures/heap/heap.ts +200 -753
- package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
- package/src/data-structures/matrix/matrix.ts +179 -494
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +241 -807
- package/src/data-structures/queue/queue.ts +102 -589
- package/src/data-structures/stack/stack.ts +76 -475
- package/src/data-structures/trie/trie.ts +98 -592
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
import { BST } from './bst';
|
|
10
9
|
import type {
|
|
11
10
|
AVLTreeOptions,
|
|
@@ -122,7 +121,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
122
121
|
*
|
|
123
122
|
* @returns The node's color.
|
|
124
123
|
*/
|
|
125
|
-
/* istanbul ignore next -- inherited field, used by RedBlackTree subclass */
|
|
124
|
+
/* istanbul ignore next -- inherited field, used by RedBlackTree subclass */ /* istanbul ignore next -- inherited field, not used by AVLTree */
|
|
126
125
|
get color(): RBTNColor {
|
|
127
126
|
return this._color;
|
|
128
127
|
}
|
|
@@ -140,6 +139,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
140
139
|
*
|
|
141
140
|
* @returns The subtree node count.
|
|
142
141
|
*/
|
|
142
|
+
|
|
143
143
|
/* istanbul ignore next -- inherited field, not used by AVLTree */
|
|
144
144
|
get count(): number {
|
|
145
145
|
return this._count;
|
|
@@ -160,13 +160,11 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
160
160
|
if (!this.parent) {
|
|
161
161
|
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
162
162
|
}
|
|
163
|
-
|
|
164
163
|
if (this.parent.left === this) {
|
|
165
164
|
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
166
165
|
} else if (this.parent.right === this) {
|
|
167
166
|
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
168
167
|
}
|
|
169
|
-
|
|
170
168
|
/* istanbul ignore next -- defensive: unreachable if tree structure is correct */
|
|
171
169
|
return 'MAL_NODE';
|
|
172
170
|
}
|
|
@@ -359,160 +357,12 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
359
357
|
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
360
358
|
* @param [value] - The value, if providing just a key.
|
|
361
359
|
* @returns True if the addition was successful, false otherwise.
|
|
362
|
-
|
|
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
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
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
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
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
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
* @example
|
|
511
|
-
* // Set a key-value pair
|
|
512
|
-
* const avl = new AVLTree<number, string>();
|
|
513
|
-
* avl.set(1, 'one');
|
|
514
|
-
* avl.set(2, 'two');
|
|
515
|
-
* console.log(avl.get(1)); // 'one';
|
|
360
|
+
* @example
|
|
361
|
+
* // Set a key-value pair
|
|
362
|
+
* const avl = new AVLTree<number, string>();
|
|
363
|
+
* avl.set(1, 'one');
|
|
364
|
+
* avl.set(2, 'two');
|
|
365
|
+
* console.log(avl.get(1)); // 'one';
|
|
516
366
|
*/
|
|
517
367
|
override set(
|
|
518
368
|
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -531,129 +381,12 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
531
381
|
*
|
|
532
382
|
* @param keyNodeOrEntry - The node to delete.
|
|
533
383
|
* @returns An array containing deletion results.
|
|
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
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
* @example
|
|
652
|
-
* // Remove nodes and verify structure
|
|
653
|
-
* const avl = new AVLTree<number>([5, 3, 7, 1, 4, 6, 8]);
|
|
654
|
-
* avl.delete(3);
|
|
655
|
-
* console.log(avl.has(3)); // false;
|
|
656
|
-
* console.log(avl.size); // 6;
|
|
384
|
+
* @example
|
|
385
|
+
* // Remove nodes and verify structure
|
|
386
|
+
* const avl = new AVLTree<number>([5, 3, 7, 1, 4, 6, 8]);
|
|
387
|
+
* avl.delete(3);
|
|
388
|
+
* console.log(avl.has(3)); // false;
|
|
389
|
+
* console.log(avl.size); // 6;
|
|
657
390
|
*/
|
|
658
391
|
override delete(
|
|
659
392
|
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
@@ -675,90 +408,20 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
675
408
|
*
|
|
676
409
|
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
677
410
|
* @returns True if successful, false if the tree was empty.
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
* @example
|
|
747
|
-
* // Rebalance the tree
|
|
748
|
-
* const avl = new AVLTree<number>();
|
|
749
|
-
* // Insert in sorted order (worst case for BST)
|
|
750
|
-
* for (let i = 1; i <= 7; i++) avl.add(i);
|
|
751
|
-
* console.log(avl.isAVLBalanced()); // false;
|
|
752
|
-
* avl.perfectlyBalance();
|
|
753
|
-
* console.log(avl.isAVLBalanced()); // true;
|
|
411
|
+
* @example
|
|
412
|
+
* // Rebalance the tree
|
|
413
|
+
* const avl = new AVLTree<number>();
|
|
414
|
+
* // Insert in sorted order (worst case for BST)
|
|
415
|
+
* for (let i = 1; i <= 7; i++) avl.add(i);
|
|
416
|
+
* console.log(avl.isAVLBalanced()); // false;
|
|
417
|
+
* avl.perfectlyBalance();
|
|
418
|
+
* console.log(avl.isAVLBalanced()); // true;
|
|
754
419
|
*/
|
|
755
420
|
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
756
421
|
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
757
422
|
const n = nodes.length;
|
|
758
423
|
if (n === 0) return false;
|
|
759
|
-
|
|
760
424
|
this._clearNodes();
|
|
761
|
-
|
|
762
425
|
// Build balanced tree from sorted array
|
|
763
426
|
const build = (l: number, r: number, parent?: AVLTreeNode<K, V>): AVLTreeNode<K, V> | undefined => {
|
|
764
427
|
if (l > r) return undefined;
|
|
@@ -767,14 +430,12 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
767
430
|
root.left = build(l, m - 1, root);
|
|
768
431
|
root.right = build(m + 1, r, root);
|
|
769
432
|
root.parent = parent;
|
|
770
|
-
|
|
771
433
|
// Update height during the build
|
|
772
434
|
const lh = root.left ? (root.left as AVLTreeNode<K, V>).height : -1;
|
|
773
435
|
const rh = root.right ? (root.right as AVLTreeNode<K, V>).height : -1;
|
|
774
436
|
root.height = Math.max(lh, rh) + 1;
|
|
775
437
|
return root;
|
|
776
438
|
};
|
|
777
|
-
|
|
778
439
|
const newRoot = build(0, n - 1, undefined);
|
|
779
440
|
this._setRoot(newRoot);
|
|
780
441
|
this._size = n;
|
|
@@ -792,117 +453,15 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
792
453
|
* @param [options] - Options for the new AVLTree.
|
|
793
454
|
* @param [thisArg] - `this` context for the callback.
|
|
794
455
|
* @returns A new, mapped AVLTree.
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
* @example
|
|
902
|
-
* // Transform to new tree
|
|
903
|
-
* const avl = new AVLTree<number, number>([[1, 10], [2, 20], [3, 30]]);
|
|
904
|
-
* const doubled = avl.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
|
|
905
|
-
* console.log([...doubled.values()]); // [20, 40, 60];
|
|
456
|
+
* @example
|
|
457
|
+
* // Transform to new tree
|
|
458
|
+
* const avl = new AVLTree<number, number>([
|
|
459
|
+
* [1, 10],
|
|
460
|
+
* [2, 20],
|
|
461
|
+
* [3, 30]
|
|
462
|
+
* ]);
|
|
463
|
+
* const doubled = avl.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
|
|
464
|
+
* console.log([...doubled.values()]); // [20, 40, 60];
|
|
906
465
|
*/
|
|
907
466
|
override map<MK = K, MV = V, MR = any>(
|
|
908
467
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
@@ -910,7 +469,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
910
469
|
thisArg?: unknown
|
|
911
470
|
): AVLTree<MK, MV, MR> {
|
|
912
471
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
913
|
-
|
|
914
472
|
let index = 0;
|
|
915
473
|
// Iterates in-order
|
|
916
474
|
for (const [key, value] of this) {
|
|
@@ -970,25 +528,20 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
970
528
|
): AVLTreeNode<K, V> | undefined {
|
|
971
529
|
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
972
530
|
const destNodeEnsured = this.ensureNode(destNode);
|
|
973
|
-
|
|
974
531
|
if (srcNodeEnsured && destNodeEnsured) {
|
|
975
532
|
const { key, value, height } = destNodeEnsured;
|
|
976
533
|
const tempNode = this.createNode(key, value);
|
|
977
|
-
|
|
978
534
|
if (tempNode) {
|
|
979
535
|
tempNode.height = height;
|
|
980
|
-
|
|
981
536
|
// Copy src to dest
|
|
982
537
|
destNodeEnsured.key = srcNodeEnsured.key;
|
|
983
538
|
if (!this._isMapMode) destNodeEnsured.value = srcNodeEnsured.value;
|
|
984
539
|
destNodeEnsured.height = srcNodeEnsured.height;
|
|
985
|
-
|
|
986
540
|
// Copy temp (original dest) to src
|
|
987
541
|
srcNodeEnsured.key = tempNode.key;
|
|
988
542
|
if (!this._isMapMode) srcNodeEnsured.value = tempNode.value;
|
|
989
543
|
srcNodeEnsured.height = tempNode.height;
|
|
990
544
|
}
|
|
991
|
-
|
|
992
545
|
return destNodeEnsured;
|
|
993
546
|
}
|
|
994
547
|
/* istanbul ignore next -- defensive: srcNode/destNode are always valid when called internally */
|
|
@@ -1034,7 +587,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1034
587
|
B.right.parent = A;
|
|
1035
588
|
}
|
|
1036
589
|
if (B) B.parent = parentOfA;
|
|
1037
|
-
|
|
1038
590
|
// Update parent's child pointer
|
|
1039
591
|
if (A === this.root) {
|
|
1040
592
|
if (B) this._setRoot(B);
|
|
@@ -1045,7 +597,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1045
597
|
if (parentOfA) parentOfA.right = B;
|
|
1046
598
|
}
|
|
1047
599
|
}
|
|
1048
|
-
|
|
1049
600
|
// Perform rotation
|
|
1050
601
|
if (B) {
|
|
1051
602
|
A.left = B.right;
|
|
@@ -1072,7 +623,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1072
623
|
}
|
|
1073
624
|
if (A && C !== null) A.parent = C;
|
|
1074
625
|
if (B && C !== null) B.parent = C;
|
|
1075
|
-
|
|
1076
626
|
if (C) {
|
|
1077
627
|
if (C.left) {
|
|
1078
628
|
if (B !== null) C.left.parent = B;
|
|
@@ -1082,7 +632,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1082
632
|
}
|
|
1083
633
|
C.parent = parentOfA;
|
|
1084
634
|
}
|
|
1085
|
-
|
|
1086
635
|
// Update parent's child pointer
|
|
1087
636
|
if (A === this.root) {
|
|
1088
637
|
if (C) this._setRoot(C);
|
|
@@ -1095,7 +644,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1095
644
|
}
|
|
1096
645
|
}
|
|
1097
646
|
}
|
|
1098
|
-
|
|
1099
647
|
// Perform rotation
|
|
1100
648
|
if (C) {
|
|
1101
649
|
A.left = C.right;
|
|
@@ -1103,7 +651,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1103
651
|
C.left = B;
|
|
1104
652
|
C.right = A;
|
|
1105
653
|
}
|
|
1106
|
-
|
|
1107
654
|
this._updateHeight(A);
|
|
1108
655
|
if (B) this._updateHeight(B);
|
|
1109
656
|
if (C) this._updateHeight(C);
|
|
@@ -1128,7 +675,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1128
675
|
}
|
|
1129
676
|
B.parent = parentOfA;
|
|
1130
677
|
}
|
|
1131
|
-
|
|
1132
678
|
// Update parent's child pointer
|
|
1133
679
|
if (A === this.root) {
|
|
1134
680
|
if (B) this._setRoot(B);
|
|
@@ -1141,7 +687,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1141
687
|
}
|
|
1142
688
|
}
|
|
1143
689
|
}
|
|
1144
|
-
|
|
1145
690
|
// Perform rotation
|
|
1146
691
|
if (B) {
|
|
1147
692
|
A.right = B.left;
|
|
@@ -1166,10 +711,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1166
711
|
if (B) {
|
|
1167
712
|
C = B.left; // The "middle" node
|
|
1168
713
|
}
|
|
1169
|
-
|
|
1170
714
|
if (C !== null) A.parent = C;
|
|
1171
715
|
if (B && C !== null) B.parent = C;
|
|
1172
|
-
|
|
1173
716
|
if (C) {
|
|
1174
717
|
if (C.left) {
|
|
1175
718
|
C.left.parent = A;
|
|
@@ -1179,7 +722,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1179
722
|
}
|
|
1180
723
|
C.parent = parentOfA;
|
|
1181
724
|
}
|
|
1182
|
-
|
|
1183
725
|
// Update parent's child pointer
|
|
1184
726
|
if (A === this.root) {
|
|
1185
727
|
if (C) this._setRoot(C);
|
|
@@ -1192,13 +734,11 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1192
734
|
}
|
|
1193
735
|
}
|
|
1194
736
|
}
|
|
1195
|
-
|
|
1196
737
|
// Perform rotation
|
|
1197
738
|
if (C) A.right = C.left;
|
|
1198
739
|
if (B && C) B.left = C.right;
|
|
1199
740
|
if (C) C.left = A;
|
|
1200
741
|
if (C) C.right = B;
|
|
1201
|
-
|
|
1202
742
|
this._updateHeight(A);
|
|
1203
743
|
if (B) this._updateHeight(B);
|
|
1204
744
|
if (C) this._updateHeight(C);
|
|
@@ -1217,14 +757,12 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1217
757
|
// Get the path from the node to the root.
|
|
1218
758
|
node = this.ensureNode(node);
|
|
1219
759
|
const path = this.getPathToRoot(node, node => node, false);
|
|
1220
|
-
|
|
1221
760
|
// Iterate up the path (from node to root)
|
|
1222
761
|
for (let i = 0; i < path.length; i++) {
|
|
1223
762
|
const A = path[i];
|
|
1224
763
|
if (A) {
|
|
1225
764
|
this._updateHeight(A);
|
|
1226
765
|
this._updateCount(A);
|
|
1227
|
-
|
|
1228
766
|
// Check the balance factor
|
|
1229
767
|
switch (this._balanceFactor(A)) {
|
|
1230
768
|
case -2: // Left-heavy
|