Here's the last code that I got for "
insert" implementation, please take a look and see if there's anything wrong. For some reason, I don't get ordered number when I did "
inorder" to my BinarySearchTrees
PHP Code:
public void insert(BinaryNode node) {
BinaryNode n = this.root;
if (Double.parseDouble(node.getValue().toString()) < Double.parseDouble(n.getValue().toString())) {
while (n != null) {
if (Double.parseDouble(node.getValue().toString()) < Double.parseDouble(n.getValue().toString()) && n.getLeft() == null) {
n.setLeft(node);
} else if (Double.parseDouble(node.getValue().toString()) > Double.parseDouble(n.getValue().toString()) && n.getRight() == null) {
n.setRight(node);
}
n = n.getLeft();
}
} else if (Double.parseDouble(node.getValue().toString()) > Double.parseDouble(n.getValue().toString())) {
while (n != null) {
if (Double.parseDouble(node.getValue().toString()) > Double.parseDouble(n.getValue().toString()) && n.getRight() == null) {
n.setRight(node);
} else if (Double.parseDouble(node.getValue().toString()) < Double.parseDouble(n.getValue().toString()) && n.getLeft() == null) {
n.setLeft(node);
}
n = n.getRight();
}
} else {
System.out.println("SAME NUMBER EXCEPTION!");
}
}
here's the
inorder implementation
PHP Code:
public void inorder(BinaryNode node) {
if (node == null) {
return;
}
inorder(node.getLeft());
System.out.println(node.getValue()); // visit the code
inorder(node.getRight());
}