iterator pattern we keep the node class exactly as we defined it earlieronly now we have added container classnamely mytree this class implements the iterator protocoland as such can be used in normal python for loopas we have seen in the list iterator we also included convenience function that allows us to build binary tree by simply calling the add_node method on the tree instance of mytree this allows us to forget about how exactly the tree is implemented the process of inserting new node into the tree simply involves looking at the current node and going left if the node to be added' data value is less than or equal to that of the current node in the tree we keep doing this until an empty spot is foundand the new node is placed at this empty spot to traverse the treewe keep stack of nodesthen we start pushing repeatedlymoving left and pushing the current node onto the stack this happens until there are no more left children thenwe pop the top node from the stack and check if it has right child if it doesthe right child gets pushed onto the stackalong with the leftmost branch of the tree from that node we then return the value of the node popped from the stack the stack is maintained as an instance variable of the tree objectand as such we are able to continue where we left off every time the __next__(method is called this results in an ordered sequence being printedas follows we could use existing python functions and constructs to interface with the binary treelike for loops and max and sum functions just for funlook at how easy it is to use these constructs with completely custom data type you have seen that for loops work as expectednowwe will use the same code as we did in bin_tree_iterator pybut we will add max and sum call to the last if statement as suchonly the if statement is included in the code snippet