简体   繁体   中英

Java: Binary Search Tree

I decided to create a Binary Search Tree using java, and what I want to do is delete the Max element from the tree, so I created this part of code:

public Node<T> removeMax(Node<T> node) {
    if (node == null)
        return null;
    if (node.right == null) {
        Node<T> n = node;
        node = null;
        return n;
    }

    return removeMax(node.right);
}

The method returns the Max element, but it doesn't remove it from the tree. As you can see, I tried to remove it in this part:

Node<T> n = node;
node = null;
return n;

But, when I print the elements in the tree, it shows the "removed" ones too. What am I doing wrong?

EDIT: What I'm really trying to do is delete the Max node and return it so I can now which one was deleted.

Now I noticed you wanted to know which node gets deleted. All you can do is to delete the maximum node and print the tree:

 public void deleteMax(Node<T> root) { 
         Node<T> current = root;
         while (current.right.right != null) { 
             current = current.right; 
         } 
         if(current.right.left!=null) {//max has 1 child to left
             current.right=current.right.left;
         }
         else {//max has no child
             current.right=null;
         }
     }

public String printInfix(Node<T> root) {//prints all the data in the tree in infix order

   if(root==null) {
        return "";
   }

 return printAll(root.left+" "+root.data+" "+printAll(root.right);
}

You want to delete a node in a binary search tree. So, basically what you want to do is to make it inaccessible. To do that, you have to nullify the reference to it, ie, make its parent's corresponding pointer to it as null.

Change this:

if (node.right == null) {
    Node<T> n = node;
    node = null;
    return n;
}

To this:

if (node.right.right == null) {
    Node<T> n = node.right;
    node.right = node.right.left;
    return n;
}

Also you need to take care of the case when the root is the maximum element of the tree. So, if you have some reference to the root of BST, add this case before the above case:

if (node.right == null) {
    Node<T> n = node;
    referenceToTheRootOfBST = node.left;
    return n;
}

If you don't have a reference to the root of the BST, what you can do is deep copy the left node of the root and then remove the left node. So, the above case changes to:

if (node.right == null) {
    Node<T> n = node;
    //I'll assume you don't call this function if root is the only element.
    //if root is the only element.If that's the case, then just make the 
    //root null before calling this function.
    Node<T> leftNode = node.left;
    node.value = leftNode.value;
    node.left = leftNode.left;
    node.right = leftNode.right;
    return n;
}

Another simple way to handle the case that root is the maximum element is to check it before actually calling this function. Simply check if root has a right node, and if it doesn't have it, reallocate the root reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM