I was hoping someone could clarify how the call stack works in this recursive call for me please. I think I am starting to wrap my head around recursion and backtracking which has been an issue for me and I think I have dwindled the problem down to one specific area.
So, the confusion came up when I was practicing my BST Inorder Traversals in Python.
Here is the implementation of a BST Tree I used to represent the following array [10, 14, 19, 27, 31, 35, 42].
I provide 3 Inorder Traversal solutions because I was trying to understand how the recursive call stack worked. For options 1 and 2 when I keep the result outside of the recursive call I get how the solution works. But for Option 3 when we do res = and res = res + I am confused how the call stack keeps track of this to return an answer. I think once I get how the res is saved in the call stack I will be able to understand how recursion works. Could someone walk me through how this works please.
FYI: I don't run the solution with all three functions named the same it was for the sake of the example.
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def insert(self, data):
node = Node(data)
if self.data:
if data < self.data:
if self.left is None:
self.left = node
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = node
else:
self.right.insert(data)
else:
self.data = data
def printTree(self):
if self.left:
self.left.printTree()
print(self.data)
if self.right:
self.right.printTree()
# Option 1) I can follow the call stack if I implement it like this keeping the result out of the helper function
def inorderTraversalRecursive(self, root):
res = []
def inorder(root):
if not root:
return
inorder(root.left)
res.append(root.data)
inorder(root.right)
inorder(root)
return res
# Option 2) I can also follow the stack when I implement it like this and print the root with end=''
def inorderTraversalRecursive(self, root):
if root is None:
return
self.inorderTraversalRecursive(root.left)
print(root.data, end=' ')
self.inorderTraversalRecursive(root.right)
# Option 3) I have an issue understanding once I put the result into the if root statement
def inorderTraversalRecursive(self, root):
res = []
if root:
res = self.inorderTraversalRecursive(root.left)
res.append(root.data)
res = res + self.inorderTraversalRecursive(root.right)
return res
#Creating the Tree
root = Node(27)
root.insert(14)
root.insert(35)
root.insert(10)
root.insert(19)
root.insert(31)
root.insert(42)
print(root.inorderTraversalRecursive(root))
Here's how I rewrote the function in the hope that it will help to understand it better:
def inorderTraversalRecursive(self, root):
if root:
return self.inorderTraversalRecursive(root.left)\
+[root.data]\
+self.inorderTraversalRecursive(root.right)
return []
P.S.1. In your code the name of the function (variant 3) is inorderTraversal
- probably a spelling mistake.
P.S.2. I wonder why there is if self.data:
block in the insert
function. If self.data
is set to 0
it won't be recorded?
P.S.3. I hope i did everything right. I dare say, Recursion is a slippery subject.