@@ -139,7 +139,7 @@ export default {
139
139
if ( tt4 ) {
140
140
vis . graph . updateTID ( tt4 , 't4' ) ;
141
141
} else {
142
- // if t4 is null, show "t4 is null " on the graph
142
+ // if t4 is null, show "t4 is Empty " on the graph
143
143
vis . graph . setTagInfo ( 't4 ' ) ;
144
144
}
145
145
@@ -180,15 +180,15 @@ export default {
180
180
) ;
181
181
182
182
// if t4 is not null, let t6's left child point to t4
183
- if ( D ) {
183
+ // if (D) { // we now animate this step even if t4 is null
184
184
chunker . add ( 't6.left = t4' ,
185
185
( vis , r , d ) => {
186
186
if ( d !== null ) vis . graph . addEdge ( r , d ) ;
187
187
} ,
188
- [ R . key , D . key ] ,
188
+ [ R . key , D ? D . key : null ] ,
189
189
depth
190
190
)
191
- }
191
+ // }
192
192
193
193
// perform the rotation in our objects
194
194
const temp = root . left ;
@@ -199,6 +199,7 @@ export default {
199
199
200
200
// update height in the graph, it will overwrite the tid as well
201
201
chunker . add ( 'recompute heights of t6 and t2' , ( vis , r , h1 , t , h2 , d , h3 ) => {
202
+ vis . graph . setTagInfo ( '' ) ;
202
203
vis . graph . updateHeight ( r , h1 ) ;
203
204
vis . graph . updateHeight ( t , h2 ) ;
204
205
if ( d !== null ) vis . graph . updateHeight ( d , h3 ) ;
@@ -220,8 +221,8 @@ export default {
220
221
// finalise the rotation
221
222
chunker . add ( 'return t2' ,
222
223
( vis , p , t2 , t6 ) => {
223
- vis . graph . clearTID ( ) ;
224
- vis . graph . setTagInfo ( ' ') ;
224
+ // vis.graph.clearTID();
225
+ vis . graph . updateTID ( t2 , 't2 ') ;
225
226
if ( p !== null ) {
226
227
vis . graph . removeEdge ( p , t6 ) ;
227
228
vis . graph . addEdge ( p , t2 ) ;
@@ -298,7 +299,7 @@ export default {
298
299
if ( tt4 ) {
299
300
vis . graph . updateTID ( tt4 , 't4' ) ;
300
301
} else {
301
- // if t4 is null, show "t4 is null " on the graph
302
+ // if t4 is null, show "t4 is Empty " on the graph
302
303
vis . graph . setTagInfo ( 't4 ' ) ;
303
304
}
304
305
@@ -340,16 +341,16 @@ export default {
340
341
) ;
341
342
342
343
// if t4 is not null, let t2's right child point to t4
343
- if ( D ) {
344
+ // if (D) { // we now animate this step even if t4 is null
344
345
chunker . add ( 't2.right = t4' ,
345
346
// reconnect the edge between t2 and t4
346
347
( vis , r , d ) => {
347
348
if ( d !== null ) vis . graph . addEdge ( r , d ) ;
348
349
} ,
349
- [ R . key , D . key ] ,
350
+ [ R . key , D ? D . key : null ] ,
350
351
depth
351
352
)
352
- }
353
+ // }
353
354
354
355
// perform the rotation in our objects
355
356
const temp = root . right ;
@@ -360,6 +361,7 @@ export default {
360
361
361
362
// update height in the graph
362
363
chunker . add ( 'recompute heights of t2 and t6' , ( vis , r , h1 , t , h2 , d , h3 ) => {
364
+ vis . graph . setTagInfo ( '' ) ;
363
365
vis . graph . updateHeight ( r , h1 ) ;
364
366
vis . graph . updateHeight ( t , h2 ) ;
365
367
if ( d !== null ) vis . graph . updateHeight ( d , h3 ) ;
@@ -381,8 +383,8 @@ export default {
381
383
// finalise the rotation
382
384
chunker . add ( 'return t6' ,
383
385
( vis , p , t6 , t2 ) => {
384
- vis . graph . clearTID ( ) ;
385
- vis . graph . setTagInfo ( ' ') ;
386
+ // vis.graph.clearTID();
387
+ vis . graph . updateTID ( t6 , 't6 ') ;
386
388
if ( p !== null ) {
387
389
vis . graph . removeEdge ( p , t2 ) ;
388
390
vis . graph . addEdge ( p , t6 ) ;
@@ -411,12 +413,13 @@ export default {
411
413
412
414
// highlight the rotation in the visualisation
413
415
chunker . add ( 'left(t) <- leftRotate(left(t));' ,
414
- ( vis , g ) => {
416
+ ( vis , g , r ) => {
417
+ vis . graph . updateHeight ( r . key , r . height ) ;
415
418
vis . graph . setPauseLayout ( false ) ;
416
419
vis . graph . layoutAVL ( g , true , false ) ;
417
- } , [ ( parentNode !== null ) ? globalRoot . key : root . key ] , depth ) ;
420
+ } , [ ( parentNode !== null ) ? globalRoot . key : root . key , root . left ] , depth ) ;
418
421
419
- chunker . add ( 'return right rotation on t' , ( vis ) => { } , [ ] , depth ) ;
422
+ // chunker.add('return right rotation on t', (vis) => { }, [], depth);
420
423
421
424
// perform right rotation on the root node
422
425
return LLCR ( root , parentNode , depth , true ) ;
@@ -439,12 +442,13 @@ export default {
439
442
440
443
// highlight the rotation in the visualisation
441
444
chunker . add ( 'right(t) <- rightRotate(right(t));' ,
442
- ( vis , g ) => {
445
+ ( vis , g , r ) => {
446
+ vis . graph . updateHeight ( r . key , r . height ) ;
443
447
vis . graph . setPauseLayout ( false ) ;
444
448
vis . graph . layoutAVL ( g , true , false ) ;
445
- } , [ ( parentNode !== null ) ? globalRoot . key : root . key ] , depth ) ;
449
+ } , [ ( parentNode !== null ) ? globalRoot . key : root . key , root . right ] , depth ) ;
446
450
447
- chunker . add ( 'return left rotation on t' , ( vis ) => { } , [ ] , depth ) ;
451
+ // chunker.add('return left rotation on t', (vis) => { }, [], depth);
448
452
449
453
// perform left rotation on the root node
450
454
return RRCR ( root , parentNode , depth , true ) ;
@@ -488,31 +492,36 @@ export default {
488
492
// if the tree is empty, create a new node as the root
489
493
if ( root === null ) {
490
494
chunker . add ( 'if t = Empty' , ( vis ) => null , [ ] , depth ) ;
491
- chunker . add ( 'create new node' ,
495
+
496
+ // Initialize the AVL tree with the first key
497
+ let root = new AVLNode ( key ) ;
498
+
499
+ // chunker.add('create new node',
500
+ chunker . add ( 'return n' ,
492
501
( vis , r , p ) => {
493
502
vis . graph . addNode ( r , r ) ;
494
503
vis . graph . updateHeight ( r , 1 ) ;
495
504
496
505
if ( p !== null ) {
497
506
vis . graph . addEdge ( p , r ) ;
498
507
}
499
- vis . graph . select ( r , p ) ;
508
+ // vis.graph.select(r, p);
509
+ //// vis.graph.resetVisitAndSelect(r, p);
500
510
} ,
501
511
[ key , parentNode ? parentNode . key : null ] ,
502
512
depth
503
513
) ;
504
514
505
- // Initialize the AVL tree with the first key
506
- let root = new AVLNode ( key ) ;
507
-
508
515
// clear all highlighting before actual returning
516
+ /*
509
517
chunker.add('return n',
510
518
(vis, r, p) => {
511
519
vis.graph.resetVisitAndSelect(r, p);
512
520
},
513
521
[key, parentNode ? parentNode.key : null],
514
522
depth
515
523
);
524
+ */
516
525
517
526
// update the chilld of the parent node
518
527
if ( parentNode !== null ) {
@@ -590,7 +599,7 @@ export default {
590
599
const balance = leftHeight - rightHeight ;
591
600
592
601
// update the balance factor in the graph
593
- chunker . add ( 'balance = left(t).height - right(t).height ' , ( vis , r ) => {
602
+ chunker . add ( 'switch balanceCase of ' , ( vis , r ) => {
594
603
vis . graph . setFunctionNode ( `${ r } ` ) ;
595
604
vis . graph . clearSelect_Circle_Count ( ) ;
596
605
vis . graph . setSelect_Circle_Count ( r ) ;
@@ -603,13 +612,13 @@ export default {
603
612
let rotateDepth = depth + 1 ;
604
613
605
614
// check the balance factor and perform rotations if necessary
606
- chunker . add ( 'if balance > 1 && k < left(t).key' , ( vis ) => null , [ ] , depth ) ;
615
+ // chunker.add('if balance > 1 && k < left(t).key', (vis) => null, [], depth);
607
616
if ( balance > 1 && key < root . left . key ) {
608
617
// detect LL case
609
618
chunker . add ( 'perform right rotation to re-balance t' ,
610
619
( vis , r , b , rl , rll ) => {
611
620
// show the rotation type and the node to be rotated
612
- vis . graph . setFunctionName ( `Rotaiton : ` ) ;
621
+ vis . graph . setFunctionName ( `balanceCase : ` ) ;
613
622
vis . graph . setFunctionInsertText ( `LL` ) ;
614
623
vis . graph . clearSelect_Circle_Count ( ) ;
615
624
vis . graph . setSelect_Circle_Count ( r ) ;
@@ -629,20 +638,21 @@ export default {
629
638
630
639
// clear the function information after the rotation
631
640
// and tidy up the nodes' position
632
- chunker . add ( 'return rightRotate(t)' , ( vis , g ) => {
641
+ chunker . add ( 'return rightRotate(t)' , ( vis , g , r ) => {
642
+ vis . graph . updateHeight ( r . key , r . height ) ;
633
643
vis . graph . setFunctionNode ( null ) ;
634
644
vis . graph . clearSelect_Circle_Count ( ) ;
635
645
vis . graph . setFunctionBalance ( null ) ;
636
646
vis . graph . setPauseLayout ( false ) ;
637
647
vis . graph . layoutAVL ( g , true , false ) ;
638
- } , [ ( parentNode !== null ) ? globalRoot . key : root . key ] , depth ) ;
648
+ } , [ ( parentNode !== null ) ? globalRoot . key : root . key , root ] , depth ) ;
639
649
} else if ( balance < - 1 && key > root . right . key ) {
640
- chunker . add ( 'if balance < -1 && k > right(t).key' , ( vis ) => null , [ ] , depth ) ;
650
+ // chunker.add('if balance < -1 && k > right(t).key', (vis) => null, [], depth);
641
651
// detect RR case
642
652
chunker . add ( 'perform left rotation to re-balance t' ,
643
653
( vis , r , b , rr , rrr ) => {
644
654
// show the rotation type and the node to be rotated
645
- vis . graph . setFunctionName ( `Rotaiton : ` ) ;
655
+ vis . graph . setFunctionName ( `balanceCase : ` ) ;
646
656
vis . graph . setFunctionInsertText ( `RR` ) ;
647
657
vis . graph . setFunctionNode ( `${ r } ` ) ;
648
658
vis . graph . clearSelect_Circle_Count ( ) ;
@@ -663,20 +673,22 @@ export default {
663
673
664
674
// clear the function information after the rotation
665
675
// and tidy up the nodes' position
666
- chunker . add ( 'return leftRotate(t)' , ( vis , g ) => {
676
+ chunker . add ( 'return leftRotate(t)' , ( vis , g , r ) => {
677
+ vis . graph . updateHeight ( r . key , r . height ) ;
678
+ // vis.graph.clearTID();
667
679
vis . graph . setFunctionNode ( null ) ;
668
680
vis . graph . setFunctionBalance ( null ) ;
669
681
vis . graph . clearSelect_Circle_Count ( ) ;
670
682
vis . graph . setPauseLayout ( false ) ;
671
683
vis . graph . layoutAVL ( g , true , false ) ;
672
- } , [ ( parentNode !== null ) ? globalRoot . key : root . key ] , depth ) ;
684
+ } , [ ( parentNode !== null ) ? globalRoot . key : root . key , root ] , depth ) ;
673
685
} else if ( balance > 1 && key > root . left . key ) {
674
- chunker . add ( 'if balance > 1 && k > left(t).key' , ( vis ) => null , [ ] , depth ) ;
686
+ // chunker.add('if balance > 1 && k > left(t).key', (vis) => null, [], depth);
675
687
// detect LR case
676
688
chunker . add ( 'perform left rotation on the left subtree' ,
677
689
( vis , r , b , rl , rlr ) => {
678
690
// show the rotation type and the node to be rotated
679
- vis . graph . setFunctionName ( `Rotaiton : ` ) ;
691
+ vis . graph . setFunctionName ( `balanceCase : ` ) ;
680
692
vis . graph . setFunctionInsertText ( `LR` ) ;
681
693
vis . graph . clearSelect_Circle_Count ( ) ;
682
694
vis . graph . setSelect_Circle_Count ( r ) ;
@@ -696,22 +708,24 @@ export default {
696
708
root = LRCR ( root , parentNode , rotateDepth ) ;
697
709
// clear the function information after the rotation
698
710
chunker . add ( 'return rightRotate(t) after leftRotate' ,
699
- ( vis , g ) => {
711
+ ( vis , g , r ) => {
712
+ vis . graph . updateHeight ( r . key , r . height ) ;
713
+ // vis.graph.clearTID();
700
714
vis . graph . setFunctionNode ( null ) ;
701
715
vis . graph . setFunctionBalance ( null ) ;
702
716
vis . graph . clearSelect_Circle_Count ( ) ;
703
717
vis . graph . setPauseLayout ( false ) ;
704
718
vis . graph . layoutAVL ( g , true , false ) ;
705
719
} ,
706
- [ ( parentNode !== null ) ? globalRoot . key : root . key ] ,
720
+ [ ( parentNode !== null ) ? globalRoot . key : root . key , root ] ,
707
721
depth ) ;
708
722
} else if ( balance < - 1 && key < root . right . key ) {
709
- chunker . add ( 'if balance < -1 && k < right(t).key' , ( vis ) => null , [ ] , depth ) ;
723
+ // chunker.add('if balance < -1 && k < right(t).key', (vis) => null, [], depth);
710
724
// detect RL case
711
725
chunker . add ( 'perform right rotation on the right subtree' ,
712
726
( vis , r , b , rr , rrl ) => {
713
727
// show the rotation type and the node to be rotated
714
- vis . graph . setFunctionName ( `Rotaiton : ` ) ;
728
+ vis . graph . setFunctionName ( `balanceCase : ` ) ;
715
729
vis . graph . setFunctionInsertText ( `RL` ) ;
716
730
vis . graph . setFunctionNode ( `${ r } ` ) ;
717
731
vis . graph . clearSelect_Circle_Count ( ) ;
@@ -732,22 +746,25 @@ export default {
732
746
733
747
// clear the function information after the rotation
734
748
chunker . add ( 'return leftRotate(t) after rightRotate' ,
735
- ( vis , g ) => {
749
+ ( vis , g , r ) => {
750
+ vis . graph . updateHeight ( r . key , r . height ) ;
751
+ // vis.graph.clearTID();
736
752
vis . graph . setFunctionNode ( null ) ;
737
753
vis . graph . setFunctionBalance ( null ) ;
738
754
vis . graph . clearSelect_Circle_Count ( ) ;
739
755
vis . graph . setPauseLayout ( false ) ;
740
756
vis . graph . layoutAVL ( g , true , false ) ;
741
- } , [ ( parentNode !== null ) ? globalRoot . key : root . key ] , depth ) ;
757
+ } , [ ( parentNode !== null ) ? globalRoot . key : root . key , root ] , depth ) ;
758
+ } else {
759
+ chunker . add ( 'case Balanced' , ( vis ) => null , [ ] , depth ) ;
760
+ chunker . add ( 'return t' ,
761
+ ( vis , r , p ) => {
762
+ vis . graph . resetVisitAndSelect ( r , p ) ; // clear all highlighting
763
+ } ,
764
+ [ root . key , parentNode ? parentNode . key : null ] ,
765
+ depth
766
+ ) ;
742
767
}
743
-
744
- chunker . add ( 'return t' ,
745
- ( vis , r , p ) => {
746
- vis . graph . resetVisitAndSelect ( r , p ) ; // clear all highlighting
747
- } ,
748
- [ root . key , parentNode ? parentNode . key : null ] ,
749
- depth
750
- ) ;
751
768
return root ;
752
769
}
753
770
@@ -777,7 +794,8 @@ export default {
777
794
) ;
778
795
779
796
// initialise the tree with the first key
780
- chunker . add ( 'create new node' ,
797
+ // chunker.add('create new node',
798
+ chunker . add ( 'return n' ,
781
799
( vis , k ) => {
782
800
vis . graph . addNode ( k , k ) ;
783
801
vis . graph . updateHeight ( k , 1 ) ;
@@ -787,7 +805,7 @@ export default {
787
805
1
788
806
) ;
789
807
790
- chunker . add ( 'return n' , ( vis ) => { } , [ ] , 1 ) ;
808
+ // chunker.add('return n', (vis) => { }, [], 1);
791
809
792
810
// store the global root node
793
811
let globalRoot = new AVLNode ( nodes [ 0 ] ) ;
@@ -797,8 +815,9 @@ export default {
797
815
globalRoot = insert ( globalRoot , nodes [ i ] , i , null , 1 ) ;
798
816
}
799
817
800
- // finalise the visualisation
801
- chunker . add ( 'done' ,
818
+ // finalise the visualisation; cursur back to top to save having a
819
+ // "Done" extra line of code
820
+ chunker . add ( 'AVLT_Insert(t, k)' ,
802
821
vis => {
803
822
vis . graph . setFunctionInsertText ( ) ;
804
823
vis . graph . setFunctionName ( "Complete" ) ;
@@ -811,4 +830,4 @@ export default {
811
830
) ;
812
831
return globalRoot ;
813
832
}
814
- } ;
833
+ } ;
0 commit comments