I have a method to insert nodes in a BST according to the alphabetical order, but I have an infinite loop when I compare the 2 strings I think that the value never changes when it passes the comparison so it's comparing again with the same values resulting in an infinite loop. I think that the aux
and T
nodes are not updating the values with the recursive method so it's comparing the same values over and over.
class BST {
BSTNode root;
public BST() {
root = null;
}
BSTNode aux = new BSTNode();
BSTNode insertNames(BSTNode T , int data, String name, double salary) {
if (root == null) {
T = new BSTNode();
T.setName(name);
root = T;
} else {
aux = root;
if (name.compareTo(aux.getName()) < 0)
aux.setLeft(insertNames(aux.getLeft(),data, name, salary));
else if (name.compareTo(aux.getName()) >= 0)
aux.setRight(insertNames(aux.getRight(),data, name, salary));
}
return T;
}
}
class Main{
public static void main(String[] args){
BST alpha=new BST();
BSTNode root = new BSTNode();
alpha.insertNames(root, 0, "Roy", 0);
alpha.insertNames(root, 0, "Joseph", 0);
}
}