简体   繁体   English

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

[英]Maximum recursion depth exceeded error in Python while implementing Binary Search Tree

I was using Python to learn BST and tried implementing insert and find methods. 我使用Python学习BST,并尝试实现insert和find方法。 But I was getting Maximum recursion depth exceeded error in insertNode method. 但是我在insertNode方法中遇到了最大递归深度超过错误。 I am new to BST data structure and hence struggling to implement the methods in Python. 我是BST数据结构的新手,因此很难在Python中实现这些方法。 I tried researching and making my code similar to ones on the internet, but I am still getting the error. 我尝试研究并使代码类似于互联网上的代码,但是仍然出现错误。

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)

Please help debug the error so that the functions work as intended. 请帮助调试错误,以便功能按预期工作。

These methods might be the root ;-) cause of the problem: 这些方法可能是问题的根源 ;-)原因:

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 code not tested NB 代码未经测试

Update: took suggestion of VPfB and did not create unnecessary nodes. 更新:建议使用VPfB,并且未创建不必要的节点。

In insertNode you refer to self.root which is the tree's root. insertNode您引用的是self.root ,这是树的根。 Instead, you should refer root , the function parameter. 相反,您应该引用root (函数参数)。

In fact, after the 1st sub-level is created ( bst.insert(50) ), the right branch of the tree's root is not None anymore and so it keeps calling self.insertNode(self.root.right,data) 实际上,在创建第一个子级别( 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecursionError:超过最大递归深度 - 二叉树 - RecursionError: maximum recursion depth exceeded - Binary Tree 为什么在此 Python 二进制搜索中出现“RecursionError:比较中超出最大递归深度”错误? - Why am i getting “RecursionError: maximum recursion depth exceeded in comparison” error on this Python Binary Search? 找到二叉搜索树的深度时超出了最大递归深度 - Maximum recursion depth exceeded when finding the depth of binary-search-tree Python 错误“调用 Python 对象时超出了最大递归深度” - Python error “maximum recursion depth exceeded while calling a Python object” 出现错误“调用Python对象时超出了最大递归深度” - Getting error “Maximum recursion depth exceeded while calling a Python object” 调用Python对象时超出了最大递归深度-运行时错误 - maximum recursion depth exceeded while calling a Python object -runtime error Python:最大递归深度超出错误 - Python: Maximum recursion depth exceeded error python中的运行时错误:“超出了最大递归深度” - Runtime Error in python: “Maximum recursion depth exceeded” 在比较错误python中超过了最大递归深度? - Maximum recursion depth exceeded in comparison error python? Python 错误:递归超出最大深度 cmp - Python error: recursion exceeded maximum depth cmp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM