Hey...so i have been workin on this code for about 3 days....and am really havin troubles figuring it out >.<
My teacher told me that what I need to do to make the rebalance work (im not asking for help on this, just solving the stack over flow..)
anyways, what my teacher told me is that I need to make the recursive call RIGHT AT the point of the insert...i need to figure out my stopping condition, but I just cant seem to figure that out... and im not sure if a while loop is the best thing to do in this case, it was just an idea that I had. I have tried it a milliom ways :(
Here is the code, any insight would be great...
public void add(Comparable item) throws Exception {
TreeNode newNode = new TreeNode(item, null, null);
if (treeRoot == null) {
treeRoot = newNode;
} else {
add(treeRoot, newNode);
}
}
private TreeNode add(TreeNode r, TreeNode n) throws Exception {
boolean check = false;
while (!check) {
if (n.getData().compareTo(r.getData()) < 0) {
if (r.getLeft() == null) {
check = true;
r.setLeft(add(r, n));
} else {
r = r.getLeft();
}
} else if (n.getData().compareTo(r.getData()) >= 0) {
if (r.getRight() == null) {
check = true;
r.setRight(add(r, n));
} else {
r = r.getRight();
}
}
}
r.setBalance(r.getBalance() + 1);
if (r != treeRoot && getParent(treeRoot, r) != null) {
TreeNode temp = getParent(treeRoot, r);
temp.setBalance(temp.getBalance() + 1);
}
return r;
}
Thanks!
Paul