Very quick question. I am working on the deletion method of a trinary tree. My approach is simple, I delete and join by replacing the node object with the node.left object.
public void delete(int key, Node node) {
if (node == null) {
throw new RuntimeException();
} else if (key < node.key) {
delete(key, node.left) ;
} else if (key > node.key) {
delete(key, node.right);
} else {
//found the node to delete, delete from here.
if (node.mid != null) {
node = node.mid;
}
else if (node.left == null) {
node = node.right;
}
else if (node.right == null) {
node = node.left;
//node.key=node.left.key;
}
else {
node= findMin(node.right);
}
}
}
This is part of the code. However, it doesn't work, that is, its key node.key is still the same.
else if (node.right == null){
node = node.left;
}
But if I write node.key explicitly, then it works.
else if (node.right == null){
node.key=node.left.key;
}
Why was that? I can never understand the object and reference thing, geez... Thanks guys.