Develop mastery of object-oriented polymorphism.
Beginning of class session 11.
PythonEtude, from your SVN repository. All your work for this assignment should be done within that project. Put your code in the given binary_tree.py file.
To help you develop this mastery, on this assignment you may use conditional statements only where specifically indicated. All other control must be done using polymorphic method dispatch or iterators. You may not use break or continue within loops.
If you write a loop that iterates over a built-in Python list object, you may not mutate that list object within the loop. Per the Python Tutorial, Section 4.2, “It is not safe to modify the sequence being iterated over in the loop.” However, you may (and probably should) create your own iterable datatype that can safely be mutated during iteration. Such a custom datatype isn’t necessary for preorder(), inorder(), and postorder() below, but I suspect it is for levelorder().
You’ll get a zero on this assignment if you don’t follow these restrictions!
t is a binary tree object, then the following loops would perform the appropriate traversals:
for a in t.preorder():
print(a)
for b in t.inorder():
print(b)
for c in t.postorder():
print(c)
for d in t.levelorder():
print(d)
Hints:
EmptyIterator, that implements an empty iterator. So, for example, the following loop would print nothing:
for e in EmptyIterator():
print("Hi!")
collections.deques while iterating over them, but you can make your own iterable queue class. (Perhaps you might want a deque field in instances of your class?)
Include a top-level factory function, build_tree(preord, inord), that takes two strings representing a pre-order and in-order traversal of a tree. Based on these strings, your function should return an object representing a binary tree with one character stored in each node. Valid inputs will not have repeated characters. If the pair of strings do not represent a valid tree, your function should raise an appropriate exception. You may use conditional statements in your implementation of this function.
Below are some valid function calls for your testing, though you should come up with some simpler ones on your own:
build_tree('', '')
build_tree('A', 'A')
build_tree('ABC', 'BAC')
build_tree('ABC', 'BCA')
build_tree('THEQUICKLAZYFOX', 'QIUCEHLAZKFYTXO')
build_tree('AQUICHE', 'UQIAHCE')
build_tree('UNCOPYRIGHTABLE', 'OCPNRYIUTHAGLBE')
build_tree('AMBIDEXTROUS', 'MIDBEARTUOXS')
build_tree('FLOWCHARTING', 'OWARNGITHCLF')
build_tree('LUMBERJACK', 'EBMRULCAKJ')
Turn in your work by committing it to your SVN repository.