def preorder(self)"""generate preorder iteration of positions in the tree ""if not self is empty)for in self subtree preorder(self root))start recursion yield def subtree preorder(selfp)"""generate preorder iteration of positions in subtree rooted at ""yield visit before its subtrees for in self children( )for each child do preorder of ' subtree for other in self subtree preorder( )yield other yielding each to our caller code fragment support for performing preorder traversal of tree this code should be included in the body of the tree class formallyboth preorder and the utility subtree preorder are generators rather than perform "visitaction from within this codewe yield each position to the caller and let the caller decide what action to perform at that position the subtree preorder method is the recursive one howeverbecause we are relying on generators rather than traditional functionsthe recursion has slightly different form in order to yield all positions within the subtree of child cwe loop over the positions yielded by the recursive call self subtree preorder( )and reyield each position in the outer context note that if is leafthe for loop over self children(pis trivial (this is the base case for our recursionwe rely on similar technique in the public preorder method to re-yield all positions that are generated by the recursive process starting at the root of the treeif the tree is emptynothing is yielded at this pointwe have provided full support for the preorder generator user of the class can therefore write code such as for in preorder)"visitposition the official tree adt requires that all trees support positions method as well to use preorder traversal as the default order of iterationwe include the definition shown in code fragment within our tree class rather than loop over the results returned by the preorder callwe return the entire iteration as an object def positions(self)"""generate an iteration of the tree positions ""return self preorderreturn entire preorder iteration code fragment an implementation of the positions method for the tree class that relies on preorder traversal to generate the results