简体   繁体   English

Java:二叉搜索树

[英]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: 我决定使用java创建一个二进制搜索树 ,我想要做的是从树中删除Max元素,所以我创建了这部分代码:

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. 该方法返回Max元素,但不会从树中删除它。 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. 编辑:我真正想要做的是删除Max节点并返回它,以便我现在可以删除哪一个节点。

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. 要做到这一点,你必须使对它的引用无效,即,使其父对应的指针指向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. 当root是树的最大元素时,您还需要注意这种情况。 So, if you have some reference to the root of BST, add this case before the above case: 因此,如果您对BST的根有一些参考,请在上述情况之前添加此案例:

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. 如果您没有对BST根目录的引用,那么您可以做的是深层复制根目录的左侧节点,然后删除左侧节点。 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. 处理root是最大元素的情况的另一种简单方法是在实际调用此函数之前检查它。 Simply check if root has a right node, and if it doesn't have it, reallocate the root reference. 只需检查root是否有正确的节点,如果没有,请重新分配根引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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