简体   繁体   中英

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. But I was getting Maximum recursion depth exceeded error in insertNode method. I am new to BST data structure and hence struggling to implement the methods in 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

Update: took suggestion of VPfB and did not create unnecessary nodes.

In insertNode you refer to self.root which is the tree's root. Instead, you should refer root , the function parameter.

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)

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM