简体   繁体   English

在二叉树中进行递归操作时,如何删除根节点?

[英]How can I delete the root node during recursive operation in Binary tree?

I am writing the delete function of Binary tree. 我正在编写二叉树的删除功能。 I have divided my cases into 3. One with both child null. 我将案例分为3个。一个孩子均为空。 One with one child null and one with both childs not null. 一个带一个孩子为空的孩子,另一个带两个孩子都不为空的孩子。 I am recursively calling the delete operation after case 3. For ex, as you can see I have called delete operation on node 50. This would replace the parent node 50 with 75. Now I have to delete the node 75 from right subtree. 我将在情况3之后递归调用delete操作。例如,如您所见,我在节点50上调用了delete操作。这将用75替换父节点50。现在我必须从右侧子树中删除节点75。 So I ran the delete procedure recursively. 因此,我以递归方式运行删除过程。 But I am not getting the desired output because 75 is the root node in right subtree of 50. How do I fix it so that I am able to delete the root 但是我没有得到所需的输出,因为75是50的右子树中的根节点。如何修复它,以便能够删除根

class BST {
    public static void main(String args[]) {
         Tree tr;
         tr = new Tree(100);
         tr.insert(50);
         tr.insert(125);
         tr.insert(150);
         tr.insert(25);
         tr.insert(75);
         tr.insert(20); 
         tr.insert(90);
         tr.delete(50);
    }
}

class Tree {

    public Tree(int n) {
        value = n;
        left = null;
        right = null;
    }

    public void insert(int n) {
        if (value == n)
            return;
        if (value < n)
            if (right == null)
                right = new Tree(n);
            else
                right.insert(n);
        else if (left == null)
            left = new Tree(n);
        else
            left.insert(n);
    }

    public Tree min() {
        if(left == null)
            return this;
        else
            return left.min();
    }

    public Tree max(){
        if(right == null)
            return this;
        else
            return right.max();
    }

    public Tree find(int n)
    {
        if(n == value)
            return this;
        else if(n > value)
            return right.find(n);
        else if(n < value)
            return left.find(n);
        else
            return null;
    }

    public Tree findParent(int n, Tree parent)
    {
        if(n == value)
            return parent;
        else if(n > value)
            return right.findParent(n, this);
        else if(n < value)
            return left.findParent(n, this);
        else
            return null;
    }

    public void case1(int n, Tree tr, Tree parent)
    {
        if(parent.left.value == n)
            parent.left = null;
        else
            parent.right = null;
    }

    public void case2(int n, Tree tr, Tree parent)
    {

        if(parent.left!=null && parent.left.value == n)
            parent.left = parent.left.left;
        else

            parent.right = parent.right.right;

    }

    public void case3(int n, Tree tr, Tree parent)
    {
        int min = tr.right.min().value;
        tr.value = min;
        tr.right.delete(min);
    }


    public void delete(int n) {  

    // fill in the code for delete

        Tree tr = find(n);
        Tree parent = findParent(n, this);

        if(tr == null)
        {
            System.out.println("The tree is not present in Binary Tree");
            return;
        }
        if(tr.left == null && tr.right == null)
        {
            case1(n, tr, parent);

        }
        else if((tr.left == null) || (tr.right == null))
        {
            System.out.print(tr.right.value);
            System.out.print(parent.right.value);
            case2(n, tr, parent);
        }
        else
        {
            case3(n, tr, parent);
        }

    }

    protected int value;
    protected Tree left;
    protected Tree right;
}

Well, you have two options: 好吧,您有两种选择:

First way: you can wrap the tree structure in an object that hides the implementation details of this datastructure. 第一种方式:您可以将树结构包装在一个对象中,该对象隐藏此数据结构的实现细节。 Forward all the relevant modifying calls to the root node, and implement the delete operation to handle the case when the root node should be deleted: in this case you can just replace the reference in the wrapper object. 将所有相关的修改调用转发到根节点,并执行delete操作以处理应删除根节点的情况:在这种情况下,您可以只替换包装对象中的引用。

Second way: Reuse what you have. 第二种方式:重用您拥有的东西。 Don't delete the root node, but overwrite it with the desired new contents. 不要删除根节点,而要用所需的新内容覆盖它。 That way you don't have to find and alter the parent of a node, and the problem is solved. 这样,您不必查找和更改节点的父代,问题就解决了。 This seems much easier though. 不过,这似乎容易得多。

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

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