binary-search-Tree'删除函数不适合2个孩子节点

发布于 2025-01-23 01:18:42 字数 2005 浏览 0 评论 0原文

我正在学习二进制搜索树和递归。我可以通过叶子和单个子节点获得预期的结果。但是,每当我尝试删除带有2个子节点的节点,但没有收到预期的结果。删除rootnode(12)时,如何将rootnode设置为18 ...因为18是root右子右节点的值?

/* Using Pre-Order Traversal
* Original Tree: [12,7,3,7,14,18]
* Expected Result: [14,7,3,7,18]
* Result Tree: [18,7,3,7,18] */

public void DeleteNode(Node rootNode, int deleteValue)
{
    SearchBinaryTree(DeleteNodeHelper, rootNode, deleteValue);
}

public void DeleteNodeHelper(Node node)
{
    if(node != null)
    {
        // 0 child? unlink the node from its parent
        if (node.Left == null && node.Right == null)
        {
            node.Value = 0;
            node.Name = "deleted";
            node.Left = null;
            node.Right = null;
        }
        // 2 child? replace root node with right child's left-most value
        if (node.Left != null && node.Right != null)
        {
            Node rootNodeReplacement = FindLeftMostNode(node.Right);
            SearchBinaryTree(DeleteNodeHelper, node.Right, rootNodeReplacement.Value);
            node.Value = rootNodeReplacement.Value;
        }
        //1 child? replace root node with only child
        else
        {
            node.Value = node.Left != null ? node.Left.Value : node.Right.Value;
            node.Name = node.Left != null ? node.Left.Name : node.Right.Name;
            node.Left = null;
            node.Right = null;
        }
    }
}

public void SearchBinaryTree(Action<Node> action, Node node, int searchValue)
{
    if (node != null)
    {
        if ( node.Value == searchValue)
        {
            action.Invoke(node);
        }
        if (node.Value < searchValue)
        {
            SearchBinaryTree(action, node.Right, searchValue);
        }
        if (node.Value > searchValue)
        {
            SearchBinaryTree(action, node.Left, searchValue);
        }     
    }
}

public Node FindLeftMostNode(Node node)
{
    while (node.Left != null)
    {
        node = node.Left;
    }
    return node;
}

I'm learning about Binary Search Trees and Recursion. I'm able to get expected results with leaf and single child nodes. However, whenever I try to delete a node with 2 child nodes but am not receiving the expected result. When deleting the rootNode (12), How is it possible that the rootNode is being set as 18... since 18 is the value of the root's right child's right node?

/* Using Pre-Order Traversal
* Original Tree: [12,7,3,7,14,18]
* Expected Result: [14,7,3,7,18]
* Result Tree: [18,7,3,7,18] */

public void DeleteNode(Node rootNode, int deleteValue)
{
    SearchBinaryTree(DeleteNodeHelper, rootNode, deleteValue);
}

public void DeleteNodeHelper(Node node)
{
    if(node != null)
    {
        // 0 child? unlink the node from its parent
        if (node.Left == null && node.Right == null)
        {
            node.Value = 0;
            node.Name = "deleted";
            node.Left = null;
            node.Right = null;
        }
        // 2 child? replace root node with right child's left-most value
        if (node.Left != null && node.Right != null)
        {
            Node rootNodeReplacement = FindLeftMostNode(node.Right);
            SearchBinaryTree(DeleteNodeHelper, node.Right, rootNodeReplacement.Value);
            node.Value = rootNodeReplacement.Value;
        }
        //1 child? replace root node with only child
        else
        {
            node.Value = node.Left != null ? node.Left.Value : node.Right.Value;
            node.Name = node.Left != null ? node.Left.Name : node.Right.Name;
            node.Left = null;
            node.Right = null;
        }
    }
}

public void SearchBinaryTree(Action<Node> action, Node node, int searchValue)
{
    if (node != null)
    {
        if ( node.Value == searchValue)
        {
            action.Invoke(node);
        }
        if (node.Value < searchValue)
        {
            SearchBinaryTree(action, node.Right, searchValue);
        }
        if (node.Value > searchValue)
        {
            SearchBinaryTree(action, node.Left, searchValue);
        }     
    }
}

public Node FindLeftMostNode(Node node)
{
    while (node.Left != null)
    {
        node = node.Left;
    }
    return node;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2025-01-30 01:18:42

弄清楚了。我交换了两行代码。在更新根节点之前,我试图删除右儿童的最左节点。

public void DeleteNodeHelper(Node node)
{
    if (node != null)
    {
        // 0 child? unlink the node from its parent
        if (node.Left == null && node.Right == null)
        {
            node.Value = 0;
            node.Name = "deleted";
            node.Left = null;
            node.Right = null;
        }
        // 2 child? replace root node with right child's left-most value
        if (node.Left != null && node.Right != null)
        {
            Node rootNodeReplacement = FindLeftMostNode(node.Right);
            node.Value = rootNodeReplacement.Value; //this line was swapped with the one below
            SearchBinaryTree(DeleteNodeHelper, node.Right, rootNodeReplacement.Value);
        }
        //1 child? replace root node with only child
        else
        {
            node.Value = node.Left != null ? node.Left.Value : node.Right.Value;
            node.Name = node.Left != null ? node.Left.Name : node.Right.Name;
            node.Left = null;
            node.Right = null;
        }
    }
}

Figured it out. I had two lines of code swapped. I tried to delete the right-child's left-most node before I updated the root node.

public void DeleteNodeHelper(Node node)
{
    if (node != null)
    {
        // 0 child? unlink the node from its parent
        if (node.Left == null && node.Right == null)
        {
            node.Value = 0;
            node.Name = "deleted";
            node.Left = null;
            node.Right = null;
        }
        // 2 child? replace root node with right child's left-most value
        if (node.Left != null && node.Right != null)
        {
            Node rootNodeReplacement = FindLeftMostNode(node.Right);
            node.Value = rootNodeReplacement.Value; //this line was swapped with the one below
            SearchBinaryTree(DeleteNodeHelper, node.Right, rootNodeReplacement.Value);
        }
        //1 child? replace root node with only child
        else
        {
            node.Value = node.Left != null ? node.Left.Value : node.Right.Value;
            node.Name = node.Left != null ? node.Left.Name : node.Right.Name;
            node.Left = null;
            node.Right = null;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文