【问题标题】:Maximum recursion depth exceeded error in Python while implementing Binary Search Tree实现二叉搜索树时 Python 中的最大递归深度超出错误
【发布时间】:2018-03-10 10:48:16
【问题描述】:

我在使用 Python 学习 BST 并尝试实现 insert 和 find 方法。但是我在 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)

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

【问题讨论】:

  • 确切的错误是什么,它发生在哪一行?你能把错误跟踪贴在这里吗?
  • 您可以使用sys.setrecursionlimit 增加递归限制。请注意,python 堆栈帧可能会变得非常大。
  • Traceback(最近一次调用最后一次):文件“python”,第 47 行,在 文件“python”,第 16 行,插入文件“python”,第 29 行,insertNode 文件中“ python”,第 29 行,insertNode 文件中的“python”,第 29 行,insertNode 文件中的“python”,第 29 行,insertNode 文件中的“python”,第 19 行,insertNode RuntimeError:最大递归深度超出@ShreyasG
  • 请使用堆栈跟踪更新您的问题 - 它会更容易阅读。我还可以在您的代码中看到问题,例如:def insertNode(self,root,data): 你有一个 root 参数,你没有在方法中使用它。
  • 另外,您正在创建太多临时节点。仅在真正需要时才可以创建节点。

标签: python algorithm recursion binary-search-tree


【解决方案1】:

这些方法可能是问题的根源 ;-) 原因:

def insertNode(self, root, data):
    if data < root.data:  # <- use the root parameter ...
        if root.left is None:
            root.left = Node(data)
        else:
            self.insertNode(root.left, data)
    else:
        if root.right is None:
            root.right = Node(data)
        else:
            self.insertNode(root.right, data)

def findinTree(self, root, data):
    if root is None:
        return False
    if data == root.data:
        return True
    if data < root.data:
        return self.findinTree(root.left, data)
    else:
        return self.findinTree(root.right, data)

NB 代码未测试

更新:采纳了 VPfB 的建议,并没有创建不必要的节点。

【讨论】:

  • 谢谢,但我还有另一个问题。如果我以这种方式制作“findinTree”方法,我将如何在通过对象调用此方法时传入参数“root”?
  • insert 方法相同。你给自己写了一个find 包装方法。任何赞成或接受的答案将不胜感激。
【解决方案2】:

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) 

【讨论】:

    猜你喜欢
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多