I want my Binary tree to be iterable so that I can loop though it to visit every node once. Also, inorder
is a generator function which re
The problem is that you call self.inorder(node.left)
, but don't do anything with the result, and as a result, the code is simply executed (well executed lazily, which means not executed), and the runtime environment continues to the next line.
You need to resolve it by propagating (i.e. re-yielding) the elements that are generated by the calls to inorder
:
def inorder(self, node):
if node is None:
return
if node.left is not None:
for x in self.inorder(node.left) :
# you need to *re-yield* the elements of the left and right child
yield x
yield node
if node.right is not None:
for x in self.inorder(node.right) :
yield x