0

Could anybody explain how it is that the computer ever gets to walkTree(tree['right'])? I believe that the function keeps calling itself until None and then recursively pops off all of the "left" stacks and printing them, but when the function calls walkTree(tree['right']), what is the computer doing when it passes by walkTree(tree['left']) again?

def walkTree(tree):
    if tree == None:
        return None
    walkTree(tree['left']
    print(tree['data'])
    walkTree(tree['right'])
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
DrJessop
  • 462
  • 6
  • 26
  • 1
    Once it gets to the bottom of the `left` it will return up the call stack printing the data in the current node and go down the `right`. A simple Tree of `2<-1->3` would print `2, 1, 3`, assuming you correctly terminate the recursion. – AChampion Aug 11 '17 at 15:57
  • 1
    shouldn't you be *return*ing from the function `if tree == None` – Oluwafemi Sule Aug 11 '17 at 15:57
  • I just fixed that now, thank you @OluwafemiSule – DrJessop Aug 11 '17 at 15:58
  • @AChampion I know what the program does, I'm still confused about how it does it – DrJessop Aug 11 '17 at 15:59

2 Answers2

2

BST is a recursive data structure. When you're calling the function with the 'left' value, it is going to the left half of the BST. Then this recurses and continues till it reaches the leftmost node in the tree. Then it starts travelling back up and goes to the immediate right subtree of it's parent (the parent of the leftmost node). Then again the same process continues and it visits the left subtrees first and proceeds in that fashion. When it finally reaches the root of the whole tree while travelling back up (after ALL nodes in the left half is visited) it's time to visit the right subtree. Then again it goes to the left subtree of that root (right of the absolute root) if any is present. This left subtree is not the left subtree of the main tree but that of the right node of the main root.

Your code basically would print the values in the entire BST in ascending order. Also, I think it should be

if tree == None:
    return
Debanik Dawn
  • 797
  • 5
  • 28
  • I believe that the behaviour of return and return None are identical, but thank you for such a good explanation. – DrJessop Aug 11 '17 at 16:18
  • Yeah I didn't see your edit until I had posted my answer. Yes, it's the same. And also if you want to know how recursion works, in short, stack frames are created in memory. The address of the calling function is pushed into a stack each time the recursive control goes one more level deep. This is to know where to return to when traveling back up. Each address is popped from the stack in a last-in-first-out fashion. The control from the main root goes to the left and so on. Finally when it comes back to the main root again, it can now go to the right subtrees and so on... – Debanik Dawn Aug 11 '17 at 16:33
  • @DrJessop you are correct, a bare `return` uses an implicit `None`, however convention would say to use a bare `return` if the `None` is not meaningful, which in this case it isn't: https://stackoverflow.com/questions/15300550/python-return-return-none-and-no-return-at-all – AChampion Aug 11 '17 at 16:34
2

It sounds like you do not understand how the call stack works. Take a simple tree:

   2
  / \
 1   3

The call stack would look like (using indentation to indicate level in the call stack):

walkTree(tree)
    walkTree(left)
        walkTree(left)
            return
        print(1)
        walkTree(right)
            return
        return (implicit)
    print(2)
    walkTree(right)
        walkTree(left)
            return
        print(3)
        walkTree(right)
            return
        return (implicit)
    return (implicit)

A return at the bottom of the call stack only returns to the call stack one higher, not all the way to the top.

AChampion
  • 29,683
  • 4
  • 59
  • 75