I'm trying to construct a binary search tree but have a problem when creating my own method to remove a node.
I know how to deal with removing a node when it has two children, one child or no child, which I have taken into account in my code. But when I am really trying to delete it and set it to null, nothing happens. The node is still there, and the smallest number isn't replacing it when the node has two children. (one technique to deal with removing a node with two children). Where's the problem?
Below is the code:
public static void main(String[] args) {
BST root=new BST(10);
root.insert(5);
root.insert(15);
root.insert(2);
root.insert(5);
BST node=root.insert(13);
root.insert(22);
root.insert(1);
root.insert(14);
BST smallestNode=root.insert(12);
System.out.println(node.left.value);
System.out.println(root.contains(4));
System.out.println(root.remove(12));
System.out.println(root.contains(12));
}
public static class BST {
public int value;
public BST left;
public BST right;
public BST(int root) {
this.value = root;
}
public BST insert(int value) {
BST currentNode=this;
while(true){
if(value>= currentNode.value){
if(currentNode.right ==null){
currentNode.right=new BST(value);
currentNode=currentNode.right;
break;
}
currentNode= currentNode.right;
}else{
if(currentNode.left==null){
currentNode.left=new BST(value);
currentNode=currentNode.left;
break;
}
currentNode= currentNode.left;
}
}
return currentNode;
}
public boolean contains(int value) {
BST currentNode=this;
while(true){
if(value>= currentNode.value){
if(currentNode.right ==null){
return false;
}
currentNode= currentNode.right;
if(currentNode.value==value){
return true;
}
}else{
if(currentNode.left==null){
return false;
}
currentNode= currentNode.left;
if(currentNode.value==value){
return true;
}
}
}
}
public BST remove(int value) {
BST currentNode=this;
if(currentNode.right==null &¤tNode.left==null){
return this;
}
while(true){
if(value>= currentNode.value){
if(currentNode.right==null){
return this;
}
currentNode= currentNode.right;
if(currentNode.value==value){
if(currentNode.left!=null&¤tNode.right==null){
currentNode=currentNode.left;
return currentNode;
}else if(currentNode.right!=null&¤tNode.left==null){
currentNode=currentNode.right;
return currentNode;
}else if(currentNode.right==null&¤tNode.left==null){
currentNode=null;
return currentNode;
}else{
currentNode.value=smallestValueSearchService(this);
currentNode.right=this.right;
currentNode.left=this.left;
return currentNode;
}
}
}else{
if(currentNode.left==null){
return this;
}
currentNode= currentNode.left;
if(currentNode.value==value){
if(currentNode.left!=null&¤tNode.right==null){
currentNode=currentNode.left;
return currentNode;
}else if(currentNode.right!=null&¤tNode.left==null){
currentNode=currentNode.right;
return currentNode;
}else if(currentNode.right==null&¤tNode.left==null){
currentNode=null;
return currentNode;
}else{
currentNode.value=smallestValueSearchService(this);
currentNode.right=this.right;
currentNode.left=this.left;
return currentNode;
}
}
}
}
}
}
//?: conditional
public static int smallestValueSearchService(BST binaryTree){
if(binaryTree.left==null){
int temp=binaryTree.value;
binaryTree=null;
return temp;
}else{
return smallestValueSearchService(binaryTree.left);
}
}
- This is a visual illustration of what I am supposed to achieve and what my code produces:

- This is the binary tree:
