“理解二叉搜索树中具有两个子节点的节点的删除过程”

huangapple go评论73阅读模式
英文:

"Understanding the deletion process for a node with two child nodes in a binary search tree"

问题

I have been working on a binary search tree implementation using the nodes and binary classes. I'm currently trying to understand the deletion process for a node that has two child nodes.

I have examined the code and the _delete_recursive function. From my understanding, the function handles the cases where the node to be deleted has no children or only one child node. However, I'm having difficulty comprehending how it handles the situation where the node has two child nodes.

I expected that the _delete_recursive function would provide a step-by-step explanation of how it handles the deletion process for a node with two child nodes. Specifically, I would like to know how it selects the successor node and updates the tree structure accordingly.

I would greatly appreciate any insights or explanations on how the _delete_recursive function works in this scenario. Thank you!

英文:

In the given code, there is a binary search tree implemented using the nodes and binary classes. The delete method in the binary class is used to delete a node from the tree. I would like to understand how the def _delete_recursive() function works when deleting a node that has two child nodes.

Could you please provide an explanation of the deletion process in this case? Specifically, how does the function handle the situation where the node to be deleted has two child nodes? I would appreciate a step-by-step breakdown of the process and the code is

class nodes:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

class binary:
    def __init__(self):
        self.root = None

    def insert(self, value):
        self.root = self.insert_recur(value, self.root)

    def insert_recur(self, value, curr):
        if curr is None:
            return nodes(value)
        elif curr.value < value:
            curr.right = self.insert_recur(value, curr.right)
        elif curr.value > value:
            curr.left = self.insert_recur(value, curr.left)
        return curr

    def trans(self):
        self.print_trans(self.root)

    def print_trans(self, node):
        if node is None:
            return
        self.print_trans(node.left)
        print("----->", node.value,end=" ")
        self.print_trans(node.right)

    def delete(self, value):
        self.root = self._delete_recursive(value, self.root)
    
    def _delete_recursive(self, value, node):
        if node is None:
            return node
        if value < node.value:
            node.left  = self._delete_recursive(value,node.left)
        elif value > node.value:
            node.right  = self._delete_recursive(value,node.right)
        else:
            if node.left is None and node.right is None :
                return None
            elif node.right is None:
                return node.left
            elif node.left is None:
                return node.right  
        return node
    
 
tree = binary()
tree.insert(1)
tree.insert(2)
tree.insert(3)
tree.insert(4)
tree.insert(5)
tree.insert(6)
tree.insert(7)
tree.insert(8)
tree.insert(9)
tree.insert(10)
tree.insert(11)
tree.insert(12)
tree.insert(13)
tree.insert(14)
tree.insert(15)
tree.insert(16)
tree.insert(17)
tree.delete(5)
tree.trans()

I plan to ask this question on Stack Overflow to seek a clear explanation.

"I have been working on a binary search tree implementation using the nodes and binary classes. I'm currently trying to understand the deletion process for a node that has two child nodes.

I have examined the code and the _delete_recursive function. From my understanding, the function handles the cases where the node to be deleted has no children or only one child node. However, I'm having difficulty comprehending how it handles the situation where the node has two child nodes.

I expected that the _delete_recursive function would provide a step-by-step explanation of how it handles the deletion process for a node with two child nodes. Specifically, I would like to know how it selects the successor node and updates the tree structure accordingly.

I would greatly appreciate any insights or explanations on how the _delete_recursive function works in this scenario. Thank you!"

答案1

得分: 0

> 我很难理解它如何处理节点有两个子节点的情况。

答案很简单:它不处理这种情况。

> 我想知道它如何选择继承者节点并相应地更新树结构。

在这种情况下,它不选择继承者节点,也不更新树结构。

您提供的驱动代码实际上并未测试这种情况。主要代码创建的树看起来像一条链:

   1
    \
     2
      \
       3
        \
         4
          \
           5
            \
            ...
              \
               17

因此,对 tree.delete(5) 的调用可以正常工作,因为它不是一个具有两个子节点的节点。

要真正测试这种情况,请创建一个像这样的树:

       4
      / \
     2   6
    / \
   1   3

...然后尝试删除值为 2 的节点:

tree = binary()
tree.insert(4)
tree.insert(2)
tree.insert(6)
tree.insert(1)
tree.insert(3)
tree.delete(2)

...现在你会看到节点 2 仍然在树中。

您需要寻找一个更好的实现方式。一些参考资料:

  • 维基百科 解释了节点具有两个子节点的情况下的处理过程,并提供了伪代码。
  • 这个网站上有许多Python实现。以下是我自己发布的一些答案,每次都有点不同,通常从提问者的代码开始:这里这里这里这里这里
英文:

> I'm having difficulty comprehending how it handles the situation where the node has two child nodes.

The answer is simple: it doesn't handle that case.

> I would like to know how it selects the successor node and updates the tree structure accordingly.

It doesn't select the successor node and doesn't update the tree in that case.

The driver code you have provided doesn't really test this case. The tree that the main code creates looks like one chain:

   1
    \
     2
      \
       3
        \
         4
          \
           5
            \
            ...
              \
               17

And so the call to tree.delete(5) works fine, as it is not a node that has two children.

To really test that case, create a tree like this:

       4
      / \
     2   6
    / \
   1   3

...and then try to delete the node with value 2:

tree = binary()
tree.insert(4)
tree.insert(2)
tree.insert(6)
tree.insert(1)
tree.insert(3)
tree.delete(2)

...and now you'll see the node 2 is still in the tree.

You'll need to look for a better implementation. Some references:

  • Wikipedia explains the process for the case where a node has two children, and provides pseudo code for it.
  • There are many Python implementations on this site. Here is a list of answers where I posted such myself -- each time a little different, as it usually starts from the asker's code: here, here, here, here, here...

huangapple
  • 本文由 发表于 2023年5月29日 00:51:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定