向BST添加元素会导致错误

时间:2013-09-24 00:46:06

标签: python

我已经使用python实现了BST,但是在向树中添加元素时会出现一些错误

class Node:
    def __init__(self,word,meaning):
        self.left=None
        self.right=None
        self.word=word
        self.meaning=meaning



class BST:
    def __init__(self,word,meaning):
        self.root=Node(word,meaning)

    def add_word(self,word,meaning):
        if(self.root.word==None):
            self.root.word=word
            self.root.meaning=meaning
            return "Create root"

        else:
            current=self.root
            while(1):
                if(word<current.word):
                    if(current.left):
                        self.add_word(word,meaning)
                    else:
                        current.left=Node(word,meaning)
                        break
                elif(word>current.word):
                    if(current.right):
                        self.add_word(word,meaning)
                    else:
                        current.right=Node(word,meaning)
                        break
                else:
                    break

    def in_order(self,node):
        if(node!=None):
            self.in_order(node.root.left)
            print(node.root.word,node.root.meaning)
            self.in_order(node.root.right)

2 个答案:

答案 0 :(得分:1)

这个怎么样?


def add_word(self, word, meaning):
    self._add_word(self.root, word, meaning)

def _add_word(self, node, word, meaning): if node is None: node = Node(word, meaning) return if word < node.word: self._add_word(node.left, word, meaning) elif word > node.word: self._add_word(node.right, word, meaning)

答案 1 :(得分:0)

您的add_word方法不能用作递归函数,因为它没有任何参数来指示它应该对哪个节点进行操作。当您从self.add_word内调用add_word时(使用未修改的参数),它会运行到递归限制。

只需更改current

的值,而不是进行递归
def add_word(self,word,meaning):
    if(self.root.word==None):
        self.root.word=word
        self.root.meaning=meaning
        return "Create root"

    else:
        current=self.root
        while(1):
            if(word<current.word):
                if(current.left):
                    current = current.left # changed here!
                else:
                    current.left = Node(word,meaning)
                    break
            elif(word>current.word):
                if(current.right):
                    current = current.right # and here too!
                else:
                    current.right = Node(word,meaning)
                    break
            else:     # you might want to raise an exception or something here
                break # since this branch indicates that the word alread exists

您的in_order方法也存在问题,该方法需要node值,但会尝试访问该root属性(如果node则无效}是Node实例)。唯一具有root属性的对象是BST本身,因此您只需要node.leftnode.right等。

要启动递归,您应该使node参数可选,使用默认值作为哨兵,然后将node设置为self.root,如果您获得了哨兵功能:

_sentinel = object()
def in_order(self, node=_sentinel):
    if(node is not None):
        if node == BST._sentinel:
            node = self.root
        self.in_order(node.left)
        print(node.word, node.meaning)
        self.in_order(node.right)