2

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.

asked Sep 28, 2017 at 15:50
5
  • What is the exact error and on what line does it occur? Can you paste the error trace here? Commented Sep 28, 2017 at 15:52
  • you could increase the recursion limit with sys.setrecursionlimit. just be careful, python stackframes can get pretty big. Commented Sep 28, 2017 at 15:53
  • Traceback (most recent call last): File "python", line 47, in <module> File "python", line 16, in insert File "python", line 29, in insertNode File "python", line 29, in insertNode File "python", line 29, in insertNode File "python", line 29, in insertNode File "python", line 19, in insertNode RuntimeError: maximum recursion depth exceeded @ShreyasG Commented Sep 28, 2017 at 15:58
  • 2
    Please update your question with the stack trace - it will be easier to read. Also I can see problems in your code, for instance in: def insertNode(self,root,data): you have a root parameter which you don't use inside the method. Commented Sep 28, 2017 at 16:38
  • Also, you are creating way too many temp Nodes. You could create a Node only when really needed. Commented Sep 28, 2017 at 17:02

2 Answers 2

2

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) 
answered Sep 28, 2017 at 17:01
Sign up to request clarification or add additional context in comments.

1 Comment

General programming advice - do NOT use the same name for a function parameter as for an object property. It's too easy to make the mistake which Daniele Bacarella has caught.
2

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.

answered Sep 28, 2017 at 16:42

2 Comments

Thank you but I also have another question. If I am making "findinTree" method this way, how would I pass in the parameter "root" while calling this method via object ?
The same way you've done the insert method. You write yourself a find wrapper method. Any upvotes or accepted answer would be much appreciated.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.