方法isEmpty用于二叉树

时间:2015-05-30 11:34:27

标签: python python-3.x binary-tree

我已经在python中实现了二叉树,并希望看看它是否适用于isEmpty函数。当我测试代码并插入一些我已经注意到的值时,该python以某种方式从树中删除值,因为如果我检查root是否等于None我得到True。我究竟做错了什么?下面是我的代码:

class BinTree():

    def __init__(self, item = None):

        self.item = item
        self.left = None
        self.right = None

class Tree():

    def __init__(self):

        self.root = None

    def put(self, indata):

        p = self.root
        tree = BinTree(indata)

        if p == None:
            print("yey")
            p = tree
            return p
        else:
            while True:

                if indata > p.item:
                    #if bigger, go right
                    if p.right == None:
                        #if right slot is empty, make one
                        p.right = p
                        return
                        #return to go back to the base level
                    elif p.right != None:
                        #if right slot is full, go deeper
                        p = p.right
                        #do not return to keep same level and repeat the loop
                elif indata < p.item:
                    #if smaller, go left
                    if p.left == None:
                        #if left slot is empty, make one
                        p.left = p
                        return
                        #return to go back to the base level
                    elif p.left != None:
                        #if left slot is full, go deeper
                        p = p.left
                        #do not return to keep same level and repeat the loop
                else:
                    #if equal
                    return False
                    #return False if the value already exist in the tree

    def isempty(self):

        if self.root == None:
            return True
        else:
            print("yey2")
            return False

然后是我在shell中写的值:

>>> Tree().put(9)
yey
<__main__.BinTree object at 0x105a57eb8>
>>> Tree().isempty()
True

1 个答案:

答案 0 :(得分:0)

你永远不会设置self.root;你只有反弹 pp是一个单独的变量,设置它不会设置self.root

未设置self.root,您的树仍为空。

请注意,因为None是单例,所以在Python中通常使用is来测试对象。您在put()方法中犯了其他一些想要纠正的错误,例如使用p.right = p创建循环引用,而不是插入新的tree节点。

我选择了一些不同的变量名称,以便更清楚地发生了什么。 newnode代替treecurrent代替p

def put(self, indata):
    newnode = BinTree(indata)

    if self.root is None:
        self.root = newnode
        return self.root

    current = self.root
    while True:
        if indata > current.item:
            # if bigger, go right
            if current.right is None:
                # if right slot is empty, make one
                current.right = newnode
                return newnode
            else:
                current = current.right
        elif indata < current.item:
            # if smaller, go left
            if current.left is None:
                # if left slot is empty, make one
                current.left = newnode
                return newnode
            else:
                # if left slot is full, go deeper
                current = current.left
        else:
            # if equal
            return False

如果节点已经存在于树中,我可能会返回None,而不是False;这样你可以更好地测试条件:

newnode = tree.put(datapoint)
if newnode is None:
    # already present in the tree, do something with that info

您的isempty方法正确无误;它可以简化,因为==is运算符已经生成TrueFalse;回到那个结果:

def isempty(self):
    return self.root is None

通过这些更改,isempty()方法适用于我:

>>> class BinTree():
...     def __init__(self, item = None):
...         self.item = item
...         self.left = None
...         self.right = None
... 
>>> class Tree():
...     def __init__(self):
...         self.root = None
...     def put(self, indata):
...         newnode = BinTree(indata)
...         if self.root is None:
...             self.root = newnode
...             return self.root
...         current = self.root
...         while True:
...             if indata > current.item:
...                 # if bigger, go right
...                 if current.right is None:
...                     # if right slot is empty, make one
...                     current.right = newnode
...                     return newnode
...                 else:
...                     current = current.right
...             elif indata < current.item:
...                 # if smaller, go left
...                 if current.left is None:
...                     # if left slot is empty, make one
...                     current.left = newnode
...                     return newnode
...                 else:
...                     # if left slot is full, go deeper
...                     current = current.left
...             else:
...                 # if equal
...                 return False
...     def isempty(self):
...         return self.root is None
... 
>>> tree = Tree()
>>> tree.isempty()
True
>>> tree.put(5)
<__main__.BinTree object at 0x10a520cf8>
>>> tree.isempty()
False