简体   繁体   English

二叉搜索树删除方法删除整个子树

[英]Binary Search Tree Delete method Deletes Whole Subtree

I have been learning data structures and have been trying to implement the delete method in my BST class.我一直在学习数据结构,并一直在尝试在我的 BST class 中实现删除方法。 What I have noticed is that instead of deleting one node it will delete the whole subtree that the node is a part of.我注意到的是,它不会删除一个节点,而是删除该节点所属的整个子树。

    class BST:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

    def addchild(self, child):
        if child == self.data:
            return
        if child < self.data:
            if self.left:
                self.left.addchild(child)
            else:
                self.left = BST(child)
        else:
            if self.right:
                self.right.addchild(child)
            else:
                self.right = BST(child)

    def search(self, data):
        if self.data == data:
            return True
        if data < self.data:
            if self.left:
                return self.left.search(data)
            else:
                return False
        else:
            if self.right:
                return self.right.search(data)
            else:
                return False

    def iot(self):
        vals = []

        if self.left:
            vals += self.left.iot()

        vals.append(self.data)

        if self.right:
            vals += self.right.iot()

        return vals

    def findmax(self):
        if self.right:
            return self.right.findmax()
        else:
            return self.data

    def findmin(self):
        if self.left:
            return self.left.findmin()
        else:
            return self.data

    def delete(self, data):
        if data < self.data:
            if self.left:
                self.left = self.left.delete(data)
        elif data > self.data:
            if self.right:
                self.right = self.right.delete(data)
        else:
            if self.left is None and self.right is None:
                return None
            elif self.left is None:
                return self.right
            elif self.right is None:
                return self.left

            minval = self.right.findmin()
            self.data = minval
            self.right = self.right.delete(minval)


def buildtree(arr):
    r = BST(arr[0])

    for i in range(1, len(arr)):
        r.addchild(arr[i])
    return r

So when input with these values and deleting the number 1 from the tree it will print this因此,当输入这些值并从树中删除数字 1 时,它将打印此

bst = buildtree([34, 7, 8, 1, 3, 4, 5, 10, 65, 98, 100, 203])

print(bst.iot())

bst.delete(1)

print(bst.iot())

1st print statement output: [1, 3, 4, 5, 7, 8, 10, 34, 65, 98, 100, 203]
2nd print statement output: [34, 65, 98, 100, 203]

When I debug in Pycharm, before leaving the delete method and after all steps are executed the tree will show that the left subtree is intact and everything is how it should be.当我在 Pycharm 中调试时,在离开删除方法之前和执行所有步骤之后,树将显示左子树完好无损,一切都应该是这样。 However once I leave the method the left subtree becomes None.但是,一旦我离开该方法,左子树就会变为无。

Any help would be appreciated.任何帮助,将不胜感激。

Here's a strong hint:这是一个强烈的提示:

You are missing a return statement on final block of you code:您在代码的最后一块缺少 return 语句:

        minval = self.right.findmin()
        self.data = minval
        self.right = self.right.delete(minval)

A debug answer: In your example 1 is going to be the leftmost leaf in the tree.调试答案:在您的示例中,1 将成为树中最左边的叶子。 So look what's happening: When you start the method delete you say that所以看看发生了什么:当你开始删除方法时,你说

if data < self.data:
        if self.left:
            self.left = self.left.delete(data)

So basically the left son of the root will hold whatever comes at the end of the process.所以基本上根的左儿子将持有过程结束时出现的任何东西。 If you will follow the left branch until you get 1, you will see that you get the the case of如果你会沿着左分支直到你得到 1,你会看到你得到的情况是

else:
        if self.left is None and self.right is None:
            return None

This means that the root left son is None, and this explains your bug这意味着根左子是无,这解释了你的错误

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

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