python - 在实现二叉搜索树时,Python 中的最大递归深度超出了错误

标签 python algorithm recursion binary-search-tree

我正在使用Python来学习BST并尝试实现插入和查找方法。但我在 insertNode 方法中遇到了最大递归深度超出错误。我对 BST 数据结构很陌生,因此很难在 Python 中实现这些方法。我尝试研究并使我的代码与互联网上的代码相似,但我仍然收到错误。

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

class BST:
    def __init__(self):
        self.root = None  
    def insert(self,data):
        temp = Node(data)
        if self.root is None:
            self.root = temp
        else:
            self.insertNode(self.root,data)  
    def insertNode(self,root,data):
        temp = Node(data)
        if data < self.root.data:
            if self.root.left is None:
                self.root.left = temp
            else:
                self.insertNode(self.root.left,data)
        elif data > self.root.data:
            if self.root.right is None:
                self.root.right = temp
            else:
                self.insertNode(self.root.right,data)
    def findinTree(self,root,data):
        if self.root is None:
            return False
        if data == self.root.data:
            return True
        if data < self.root.data:
            self.findinTree(self.root.left,data)
        else:
            self.findinTree(self.root.right,data)
        return False


bst = BST()
bst.insert(30)
bst.insert(10)
bst.insert(50)
bst.insert(90)
bst.insert(100)
bst.insert(15)

请帮助调试错误,以便功能按预期工作。

最佳答案

insertNode您引用self.root这是树的根。相反,您应该引用root ,函数参数。

事实上,在创建第一个子级别( bst.insert(50) )后,树根的右分支不是 None不再这样了,所以它一直打电话self.insertNode(self.root.right,data)

def insertNode(self,root,data):                                                                                                                                                                          
    temp = Node(data)                                                                                                                                                                                    
    if data < root.data:                                                                                                                                                                                 
        if root.left is None:                                                                                                                                                                            
            print("Inserting {} on the left branch of {}".format(data, root.data))                                                                                                                       
            root.left = temp                                                                                                                                                                             
        else:                                                                                                                                                                                            
            print("Inserting {} on the left branch of {}".format(data, root.data))                                                                                                                       
            self.insertNode(root.left,data)                                                                                                                                                              
    elif data > root.data:                                                                                                                                                                               
        if root.right is None:                                                                                                                                                                           
            print("Inserting {} on the right branch of {}".format(data, root.data))                                                                                                                      
            root.right = temp                                                                                                                                                                            
        else:                                                                                                                                                                                            
            print("Inserting {} on the right branch of {}".format(data, root.data))                                                                                                                      
            self.insertNode(root.right,data) 

关于python - 在实现二叉搜索树时,Python 中的最大递归深度超出了错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46473429/

相关文章:

python - 从元组列表中查找空间顺序的最有效方法?

python - 现有的 python 函数循环遍历列表并获取具有匹配元素的项目?

Python - 如果或如果无效语法错误

algorithm - 这个数据结构的名称是什么?

algorithm - 一个特定输入的奇偶链接列表运行时错误。其他一切运行良好

algorithm - 在计算树的直径时,为什么仅计算高度是不够的

c++ - 汉诺塔C++(使用递归)

algorithm - 是否可以将所有递归函数重写为尾递归?

python - 对Python字典列表进行排序

java - 从一个 100 万无法读入内存的文件中随机选择 100 行