Python 向BST添加元素会导致错误

Python 向BST添加元素会导致错误,python,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.r

我已经使用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)
类节点:
定义初始(自我、单词、意义):
self.left=无
self.right=无
self.word=word
自我意义=意义
BST级:
定义初始(自我、单词、意义):
self.root=节点(单词,含义)
def添加单词(自我、单词、含义):
如果(self.root.word==None):
self.root.word=word
self.root.meaning=意义
返回“创建根目录”
其他:
当前=自根
而(一):
如果(wordcurrent.word):
如果(当前右侧):
添加单词(单词,意思)
其他:
current.right=节点(单词,含义)
打破
其他:
打破
按顺序定义(自身、节点):
如果(节点!=无):
self.in_顺序(node.root.left)
打印(node.root.word、node.root.means)
self.in_顺序(node.root.right)
这个怎么样?

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)

def添加单词(自我、单词、含义):
self.\u添加单词(self.root、单词、含义)

定义添加单词(自我、节点、单词、含义): 如果节点为无: node=节点(单词、含义) 返回 如果单词node.word:self.\u添加单词(node.right,单词,意思)

这个怎么样?

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)

def添加单词(自我、单词、含义):
self.\u添加单词(self.root、单词、含义)

定义添加单词(自我、节点、单词、含义): 如果节点为无: node=节点(单词、含义) 返回 如果单词node.word:self.\u添加单词(node.right,单词,意思)

您的
add\u word
方法不能作为递归函数使用,因为它没有任何参数来指示应该在哪个节点上操作。当您从
add\u word
中调用
self.add\u 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

您的
add\u word
方法不能作为递归函数工作,因为它没有任何参数来指示应该在哪个节点上操作。当您从
add\u word
中调用
self.add\u 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

您同时使用迭代和递归。选择一个或另一个当我删除while循环时,出现运行时错误。您会遇到什么错误?(您也必须删除break语句)我已经删除了break语句。在执行递归时,您总是这样做
current=self.root
。您同时使用迭代和递归。选择一个或另一个当我删除while循环时,出现运行时错误。您会遇到什么错误?(你也必须删除break语句)我必须删除break语句。在执行递归时,你总是这样做
current=self.root
。更改current的值是什么意思?我不知道如何回答,@Kasunlakmal。你有没有看过我在代码中评论我改变了事情的地方?通过分配给
current
,循环是有意义的,因为每次迭代要么将
current
引用向上移动,要么添加一个新的叶节点和
break
s。你有没有看过我在代码中评论我改变了事情的地方?通过分配给
current
,循环是有意义的,因为每次迭代要么将
current
引用向上移动到树上,要么添加一个新的叶节点和
break
s。