class _BSTIterator : def __init__( self, root ): # Creates the Python list and fills it with the keys. self._theNodes = list() self._curItem = 0 # Keep track of the next location in the array. self._bstTraversal( root ) self._curItem = 0 # Reset the current item index. def __iter__( self ): return self # Returns the next key from the array of keys def __next__( self ): if self._curItem < len( self._theNodes ) : node = self._theNodes[ self._curItem ] self._curItem += 1 return node else : raise StopIteration # Performs an inorder traversal used to build the array of keys. def _bstTraversal( self, subtree ): if subtree is not None : self._bstTraversal( subtree.left ) self._theNodes.append( subtree ) self._curItem += 1 self._bstTraversal( subtree.right )