迭代地从二叉搜索树中删除节点

时间:2018-05-12 18:51:54

标签: c# recursion iteration binary-search-tree

我有三种方法用于删除BST中的节点。 RemoveRecRemoveNonRec方法以递归和迭代方式搜索节点,Remove删除找到的节点。

private void RemoveRec(int value, ref TreeNode node)
{
    if (node != null)
    {
        if (value < node.Value)
        {
            RemoveRec(value, ref node.Left);
        }
        else if (value > node.Value)
        {
            RemoveRec(value, ref node.Right);
        }
        else
        {
            Remove(value, ref node);
        } 
    } 
}

public void RemoveNonRec(int value)
{
    ref TreeNode node = ref this.Root;
    while (node != null)
    {
        if (value < node.Value)
        {
            node = node.Left;
        }
        else if (value > node.Value)
        {
            node = node.Right;
        }
        else
        {
            Remove(value, ref node);
            break;
        }
    }
}

private void Remove(int value, ref TreeNode node)
{
    if (node != null)
    {
        if (node.Counter > 1)
        {
            --node.Counter;
            Console.WriteLine("Deleted element: {0}, elements in the node: {1}", node.Value, node.Counter);
        }
        else
        {
            int vMes = node.Value;

            if (node.Left == null && node.Right == null)
            {
                node = null;
            }
            else if (node.Left != null && node.Right == null)
            {
                node = node.Left;
            }
            else if (node.Left == null && node.Right != null)
            {
                node = node.Right;
            }
            else
            {
                if (node.Right.Left == null)
                {
                    node.Right.Left = node.Left;
                    node = node.Right;
                }
                else
                {
                    TreeNode p = node.Right;
                    while (p.Left.Left != null)
                       p = p.Left;
                    TreeNode q = p.Left;
                    p.Left = q.Right;
                    q.Left = node.Left;
                    q.Right = node.Right;
                    node = q;
                }
            }

            Console.WriteLine("Deleted node: {0}", vMes);
        }
    }
}

问题是RemoveNonRec无效,我稍微知道原因。但是我不知道如何让它发挥作用。

2 个答案:

答案 0 :(得分:0)

作为解决方案,您可以在TreeNode类中添加这两个返回引用的方法。

private TreeNode left;
private TreeNode right;

public ref TreeNode GetLeft()
{
    return ref left;
}

public ref TreeNode GetRight()
{
    return ref right;
}

所以你不再访问这些属性了。相反,你称之为以下两种方法:

private void RemoveRec(int value, ref TreeNode node)
{
    if (node != null)
    {
        if (value < node.Value)
        {
            RemoveRec(value, ref node.GetLeft());
        }
        else if (value > node.Value)
        {
            RemoveRec(value, ref node.GetRight());
        }
        else
        {
            Remove(value, ref node);
        }
    }
}

public void RemoveNonRec(int value)
{
    if(value == Root.Value)
    {
        Remove(value, ref Root);
    }
    else
    {
        TreeNode node = Root;

        while (node != null)
        {
            if (value < node.Value)
            {
                node = node.GetLeft();
            }
            else if (value > node.Value)
            {
                node = node.GetRight();
            }
            else
            {
                Remove(value, ref node);
                break;
            }
        }
    }
}

private void Remove(int value, ref TreeNode node)
{
    if (node != null)
    {
        if (node.Counter > 1)
        {
            --node.Counter;
            Console.WriteLine("Deleted element: {0}, elements in the node: {1}", node.Value, node.Counter);
        }
        else
        {
            int vMes = node.Value;
            ref TreeNode left = ref node.GetLeft();
            ref TreeNode right = ref node.GetRight();

            if (left == null && right == null)
            {
                node = null;
            }
            else if (left != null && right == null)
            {
                node = left;
            }
            else if (left == null && right != null)
            {
                node = right;
            }
            else
            {
                if (node.GetRight().GetLeft() == null)
                {
                    node.GetRight().GetLeft() = left;
                    node = right;
                }
                else
                {
                    TreeNode p = right;
                    ref TreeNode leftLeft = ref p.GetLeft().GetLeft();
                    while (leftLeft != null)
                        p = p.GetLeft();
                    ref TreeNode q = ref p.GetLeft();
                    p.GetLeft() = q.GetRight();
                    q.GetLeft() = node.GetLeft();
                    q.GetRight() = node.GetRight();
                    node = q;
                }
            }

            Console.WriteLine("Deleted node: {0}", vMes);
        }
    }
}

答案 1 :(得分:0)

我设法解决了这个问题。现在它工作正常

public void RemoveNonRec(int value)
{
    TreeNode node = Root;
    while (node != null)
    {
        if (value < node.Value)
        {
            if (node.Left != null && node.Left.Value == value)
            {
                Remove(value, ref node.Left);
                break;
            }
            node = node.Left;
        }
        else if (value > node.Value)
        {
            if (node.Right != null && node.Right.Value == value)
            {
                Remove(value, ref node.Right);
                break;
            }
            node = node.Right;
        }
        else
        {
            Remove(value, ref Root);
            break;
        }
    }
}