Skip to content

Commit 0450579

Browse files
authored
Merge pull request #49 from csvistool/bugsFix
Fixed order property bug in BST and AVL
2 parents 65fba93 + ff0ca2e commit 0450579

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

src/algo/AVL.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,22 @@ export default class AVL extends Algorithm {
383383
}
384384
}
385385

386+
compare(a, b) {
387+
const numA = parseInt(a);
388+
const numB = parseInt(b);
389+
390+
const isNumA = !isNaN(numA);
391+
const isNumB = !isNaN(numB);
392+
393+
if (isNumA && isNumB) {
394+
return numA - numB;
395+
} else if (!isNumA && !isNumB) {
396+
return a.localeCompare(b);
397+
} else {
398+
return isNumA ? -1 : 1;
399+
}
400+
}
401+
386402
add(data) {
387403
this.commands = [];
388404
this.cmd(act.setText, 0, ' Inserting ' + data);
@@ -411,15 +427,17 @@ export default class AVL extends Algorithm {
411427
return new AVLNode(data, treeNodeID, heightLabelID, bfLabelID, 0, 0);
412428
}
413429
this.cmd(act.setHighlight, curr.graphicID, 1);
414-
if (data < curr.data) {
430+
// if (data < curr.data) {
431+
if (this.compare(data, curr.data) < 0) {
415432
this.cmd(act.setText, 0, `${data} < ${curr.data}. Looking at left subtree`);
416433
this.cmd(act.step);
417434
curr.left = this.addH(data, curr.left);
418435
curr.left.parent = curr;
419436
this.resizeTree();
420437
const connected = this.connectSmart(curr.graphicID, curr.left.graphicID);
421438
connected && this.cmd(act.step);
422-
} else if (data > curr.data) {
439+
// } else if (data > curr.data) {
440+
} else if (this.compare(data, curr.data) > 0) {
423441
this.cmd(act.setText, 0, `${data} > ${curr.data}. Looking at right subtree`);
424442
this.cmd(act.step);
425443
curr.right = this.addH(data, curr.right);
@@ -653,7 +671,8 @@ export default class AVL extends Algorithm {
653671
return;
654672
}
655673
this.cmd(act.setHighlight, curr.graphicID, 1);
656-
if (data < curr.data) {
674+
// if (data < curr.data) {
675+
if (this.compare(data, curr.data) < 0) {
657676
this.cmd(act.setText, 0, `${data} < ${curr.data}. Looking left`);
658677
this.cmd(act.step);
659678
curr.left = this.removeH(curr.left, data);
@@ -662,7 +681,8 @@ export default class AVL extends Algorithm {
662681
this.connectSmart(curr.graphicID, curr.left.graphicID);
663682
this.resizeTree();
664683
}
665-
} else if (data > curr.data) {
684+
// } else if (data > curr.data) {
685+
} else if (this.compare(data, curr.data) < 0) {
666686
this.cmd(act.setText, 0, `${data} > ${curr.data}. Looking right`);
667687
this.cmd(act.step);
668688
curr.right = this.removeH(curr.right, data);

src/algo/BST.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,22 @@ export default class BST extends Algorithm {
688688
this.toClear = [];
689689
}
690690

691+
compare(a, b) {
692+
const numA = parseInt(a);
693+
const numB = parseInt(b);
694+
695+
const isNumA = !isNaN(numA);
696+
const isNumB = !isNaN(numB);
697+
698+
if (isNumA && isNumB) {
699+
return numA - numB;
700+
} else if (!isNumA && !isNumB) {
701+
return a.localeCompare(b);
702+
} else {
703+
return isNumA ? -1 : 1;
704+
}
705+
}
706+
691707
add(data, skipPseudocode) {
692708
this.commands = [];
693709
this.clearOldObjects();
@@ -733,7 +749,8 @@ export default class BST extends Algorithm {
733749
return new BSTNode(data, treeNodeID, 0, 0, 'add');
734750
}
735751
this.cmd(act.setHighlight, curr.graphicID, 1);
736-
if (data < curr.data) {
752+
// if (data < curr.data) {
753+
if (this.compare(data, curr.data) < 0) {
737754
this.highlight(8, 0, 'add');
738755
this.highlight(9, 0, 'add');
739756
this.cmd(act.setText, 0, `${data} < ${curr.data}. Looking at left subtree`);
@@ -746,7 +763,8 @@ export default class BST extends Algorithm {
746763
this.resizeTree();
747764
const connected = this.connectSmart(curr.graphicID, curr.left.graphicID);
748765
connected && this.cmd(act.step);
749-
} else if (data > curr.data) {
766+
// } else if (data > curr.data) {
767+
} else if (this.compare(data, curr.data) > 0) {
750768
this.highlight(10, 0, 'add');
751769
this.highlight(11, 0, 'add');
752770
this.cmd(act.setText, 0, `${data} > ${curr.data}. Looking at right subtree`);

0 commit comments

Comments
 (0)