Skip to content

Commit 15985a2

Browse files
committed
AVLT overhaul (still some to do)
1 parent 1f1abfa commit 15985a2

File tree

3 files changed

+187
-228
lines changed

3 files changed

+187
-228
lines changed

src/algorithms/controllers/AVLTreeInsertion.js

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export default {
139139
if (tt4) {
140140
vis.graph.updateTID(tt4, 't4');
141141
} 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
143143
vis.graph.setTagInfo('t4 ');
144144
}
145145

@@ -180,15 +180,15 @@ export default {
180180
);
181181

182182
// 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
184184
chunker.add('t6.left = t4',
185185
(vis, r, d) => {
186186
if (d !== null) vis.graph.addEdge(r, d);
187187
},
188-
[R.key, D.key],
188+
[R.key, D ? D.key : null],
189189
depth
190190
)
191-
}
191+
// }
192192

193193
// perform the rotation in our objects
194194
const temp = root.left;
@@ -199,6 +199,7 @@ export default {
199199

200200
// update height in the graph, it will overwrite the tid as well
201201
chunker.add('recompute heights of t6 and t2', (vis, r, h1, t, h2, d, h3) => {
202+
vis.graph.setTagInfo('');
202203
vis.graph.updateHeight(r, h1);
203204
vis.graph.updateHeight(t, h2);
204205
if (d !== null) vis.graph.updateHeight(d, h3);
@@ -220,8 +221,8 @@ export default {
220221
// finalise the rotation
221222
chunker.add('return t2',
222223
(vis, p, t2, t6) => {
223-
vis.graph.clearTID();
224-
vis.graph.setTagInfo('');
224+
// vis.graph.clearTID();
225+
vis.graph.updateTID(t2, 't2');
225226
if (p !== null) {
226227
vis.graph.removeEdge(p, t6);
227228
vis.graph.addEdge(p, t2);
@@ -298,7 +299,7 @@ export default {
298299
if (tt4) {
299300
vis.graph.updateTID(tt4, 't4');
300301
} 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
302303
vis.graph.setTagInfo('t4 ');
303304
}
304305

@@ -340,16 +341,16 @@ export default {
340341
);
341342

342343
// 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
344345
chunker.add('t2.right = t4',
345346
// reconnect the edge between t2 and t4
346347
(vis, r, d) => {
347348
if (d !== null) vis.graph.addEdge(r, d);
348349
},
349-
[R.key, D.key],
350+
[R.key, D ? D.key : null],
350351
depth
351352
)
352-
}
353+
// }
353354

354355
// perform the rotation in our objects
355356
const temp = root.right;
@@ -360,6 +361,7 @@ export default {
360361

361362
// update height in the graph
362363
chunker.add('recompute heights of t2 and t6', (vis, r, h1, t, h2, d, h3) => {
364+
vis.graph.setTagInfo('');
363365
vis.graph.updateHeight(r, h1);
364366
vis.graph.updateHeight(t, h2);
365367
if (d !== null) vis.graph.updateHeight(d, h3);
@@ -381,8 +383,8 @@ export default {
381383
// finalise the rotation
382384
chunker.add('return t6',
383385
(vis, p, t6, t2) => {
384-
vis.graph.clearTID();
385-
vis.graph.setTagInfo('');
386+
// vis.graph.clearTID();
387+
vis.graph.updateTID(t6, 't6');
386388
if (p !== null) {
387389
vis.graph.removeEdge(p, t2);
388390
vis.graph.addEdge(p, t6);
@@ -411,12 +413,13 @@ export default {
411413

412414
// highlight the rotation in the visualisation
413415
chunker.add('left(t) <- leftRotate(left(t));',
414-
(vis, g) => {
416+
(vis, g, r) => {
417+
vis.graph.updateHeight(r.key, r.height);
415418
vis.graph.setPauseLayout(false);
416419
vis.graph.layoutAVL(g, true, false);
417-
}, [(parentNode !== null) ? globalRoot.key : root.key], depth);
420+
}, [(parentNode !== null) ? globalRoot.key : root.key, root.left], depth);
418421

419-
chunker.add('return right rotation on t', (vis) => { }, [], depth);
422+
// chunker.add('return right rotation on t', (vis) => { }, [], depth);
420423

421424
// perform right rotation on the root node
422425
return LLCR(root, parentNode, depth, true);
@@ -439,12 +442,13 @@ export default {
439442

440443
// highlight the rotation in the visualisation
441444
chunker.add('right(t) <- rightRotate(right(t));',
442-
(vis, g) => {
445+
(vis, g, r) => {
446+
vis.graph.updateHeight(r.key, r.height);
443447
vis.graph.setPauseLayout(false);
444448
vis.graph.layoutAVL(g, true, false);
445-
}, [(parentNode !== null) ? globalRoot.key : root.key], depth);
449+
}, [(parentNode !== null) ? globalRoot.key : root.key, root.right], depth);
446450

447-
chunker.add('return left rotation on t', (vis) => { }, [], depth);
451+
// chunker.add('return left rotation on t', (vis) => { }, [], depth);
448452

449453
// perform left rotation on the root node
450454
return RRCR(root, parentNode, depth, true);
@@ -488,31 +492,36 @@ export default {
488492
// if the tree is empty, create a new node as the root
489493
if (root === null) {
490494
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',
492501
(vis, r, p) => {
493502
vis.graph.addNode(r, r);
494503
vis.graph.updateHeight(r, 1);
495504

496505
if (p !== null) {
497506
vis.graph.addEdge(p, r);
498507
}
499-
vis.graph.select(r, p);
508+
// vis.graph.select(r, p);
509+
//// vis.graph.resetVisitAndSelect(r, p);
500510
},
501511
[key, parentNode ? parentNode.key : null],
502512
depth
503513
);
504514

505-
// Initialize the AVL tree with the first key
506-
let root = new AVLNode(key);
507-
508515
// clear all highlighting before actual returning
516+
/*
509517
chunker.add('return n',
510518
(vis, r, p) => {
511519
vis.graph.resetVisitAndSelect(r, p);
512520
},
513521
[key, parentNode ? parentNode.key : null],
514522
depth
515523
);
524+
*/
516525

517526
// update the chilld of the parent node
518527
if (parentNode !== null) {
@@ -590,7 +599,7 @@ export default {
590599
const balance = leftHeight - rightHeight;
591600

592601
// 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) => {
594603
vis.graph.setFunctionNode(`${r}`);
595604
vis.graph.clearSelect_Circle_Count();
596605
vis.graph.setSelect_Circle_Count(r);
@@ -603,13 +612,13 @@ export default {
603612
let rotateDepth = depth + 1;
604613

605614
// 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);
607616
if (balance > 1 && key < root.left.key) {
608617
// detect LL case
609618
chunker.add('perform right rotation to re-balance t',
610619
(vis, r, b, rl, rll) => {
611620
// show the rotation type and the node to be rotated
612-
vis.graph.setFunctionName(`Rotaiton: `);
621+
vis.graph.setFunctionName(`balanceCase: `);
613622
vis.graph.setFunctionInsertText(`LL`);
614623
vis.graph.clearSelect_Circle_Count();
615624
vis.graph.setSelect_Circle_Count(r);
@@ -629,20 +638,21 @@ export default {
629638

630639
// clear the function information after the rotation
631640
// 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);
633643
vis.graph.setFunctionNode(null);
634644
vis.graph.clearSelect_Circle_Count();
635645
vis.graph.setFunctionBalance(null);
636646
vis.graph.setPauseLayout(false);
637647
vis.graph.layoutAVL(g, true, false);
638-
}, [(parentNode !== null) ? globalRoot.key : root.key], depth);
648+
}, [(parentNode !== null) ? globalRoot.key : root.key, root], depth);
639649
} 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);
641651
// detect RR case
642652
chunker.add('perform left rotation to re-balance t',
643653
(vis, r, b, rr, rrr) => {
644654
// show the rotation type and the node to be rotated
645-
vis.graph.setFunctionName(`Rotaiton: `);
655+
vis.graph.setFunctionName(`balanceCase: `);
646656
vis.graph.setFunctionInsertText(`RR`);
647657
vis.graph.setFunctionNode(`${r}`);
648658
vis.graph.clearSelect_Circle_Count();
@@ -663,20 +673,22 @@ export default {
663673

664674
// clear the function information after the rotation
665675
// 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();
667679
vis.graph.setFunctionNode(null);
668680
vis.graph.setFunctionBalance(null);
669681
vis.graph.clearSelect_Circle_Count();
670682
vis.graph.setPauseLayout(false);
671683
vis.graph.layoutAVL(g, true, false);
672-
}, [(parentNode !== null) ? globalRoot.key : root.key], depth);
684+
}, [(parentNode !== null) ? globalRoot.key : root.key, root], depth);
673685
} 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);
675687
// detect LR case
676688
chunker.add('perform left rotation on the left subtree',
677689
(vis, r, b, rl, rlr) => {
678690
// show the rotation type and the node to be rotated
679-
vis.graph.setFunctionName(`Rotaiton: `);
691+
vis.graph.setFunctionName(`balanceCase: `);
680692
vis.graph.setFunctionInsertText(`LR`);
681693
vis.graph.clearSelect_Circle_Count();
682694
vis.graph.setSelect_Circle_Count(r);
@@ -696,22 +708,24 @@ export default {
696708
root = LRCR(root, parentNode, rotateDepth);
697709
// clear the function information after the rotation
698710
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();
700714
vis.graph.setFunctionNode(null);
701715
vis.graph.setFunctionBalance(null);
702716
vis.graph.clearSelect_Circle_Count();
703717
vis.graph.setPauseLayout(false);
704718
vis.graph.layoutAVL(g, true, false);
705719
},
706-
[(parentNode !== null) ? globalRoot.key : root.key],
720+
[(parentNode !== null) ? globalRoot.key : root.key, root],
707721
depth);
708722
} 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);
710724
// detect RL case
711725
chunker.add('perform right rotation on the right subtree',
712726
(vis, r, b, rr, rrl) => {
713727
// show the rotation type and the node to be rotated
714-
vis.graph.setFunctionName(`Rotaiton: `);
728+
vis.graph.setFunctionName(`balanceCase: `);
715729
vis.graph.setFunctionInsertText(`RL`);
716730
vis.graph.setFunctionNode(`${r}`);
717731
vis.graph.clearSelect_Circle_Count();
@@ -732,22 +746,25 @@ export default {
732746

733747
// clear the function information after the rotation
734748
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();
736752
vis.graph.setFunctionNode(null);
737753
vis.graph.setFunctionBalance(null);
738754
vis.graph.clearSelect_Circle_Count();
739755
vis.graph.setPauseLayout(false);
740756
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+
);
742767
}
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-
);
751768
return root;
752769
}
753770

@@ -777,7 +794,8 @@ export default {
777794
);
778795

779796
// initialise the tree with the first key
780-
chunker.add('create new node',
797+
// chunker.add('create new node',
798+
chunker.add('return n',
781799
(vis, k) => {
782800
vis.graph.addNode(k, k);
783801
vis.graph.updateHeight(k, 1);
@@ -787,7 +805,7 @@ export default {
787805
1
788806
);
789807

790-
chunker.add('return n', (vis) => { }, [], 1);
808+
// chunker.add('return n', (vis) => { }, [], 1);
791809

792810
// store the global root node
793811
let globalRoot = new AVLNode(nodes[0]);
@@ -797,8 +815,9 @@ export default {
797815
globalRoot = insert(globalRoot, nodes[i], i, null, 1);
798816
}
799817

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)',
802821
vis => {
803822
vis.graph.setFunctionInsertText();
804823
vis.graph.setFunctionName("Complete");
@@ -811,4 +830,4 @@ export default {
811830
);
812831
return globalRoot;
813832
}
814-
};
833+
};

0 commit comments

Comments
 (0)