python provides us with very powerful function to use when creating an iterator the function is called yield yield is similar to return in that it returns value to the caller howeveryield also takes the additional step of freezing the state of the function so that the next time the function is called it continues executing from the exact point it left off earlier functions that create objects that can be iterated are called generator functions the code for an inorder iterator of binary tree is shown below look at this code carefullyat first glance you might think that the code is not recursive howeverremember that __iter__ overrides the for in operation for iterationso it really is recursivebecause it is recursive over treenode instances the __iter__ method is defined in the treenode class def __iter__(self)if selfif self has_left_child()for elem in self left_childyield elem yield self key if self has_right_child()for elem in self right_childyield elem at this point you may want to download the entire file containing the full version of the binarysearchtree and treenode classes complete binarysearchtree implementation class treenodedef __init__(selfkeyvalleft noneright noneparent none)self key key self payload val self left_child left self right_child right self parent parent def has_left_child(self)return self left_child def has_right_child(self)return self right_child def is_left_child(self)return self parent and self parent left_child =self def is_right_child(self)return self parent and self parent right_child =self def is_root(self)return not self parent trees and tree algorithms