Homework Assignment # 8

Due Thursday November 7, 2013 by 11:59 PM. See the following link for turnin instructions:

https://piazza.com/class/hkuo0jreo4q3wa?cid=39

[10 Points] Please have the following at the top of HW8.py (Please don't put you name on the homework for privacy reasons.)

```# HW8.py
# Homework 8
#
# UT EID: XXX999, UTCS username: XXXXXXXX
# CS313e, Fall 2013, Dr. P. Cannata
# Department of Computer Science, The University of Texas at Austin --

Below you will find a different implementation of Binary Search Tree which will be discussed in class on November 5, 2013.

[10 Points, 2.5 points each] The homework assignment consists of the following:
1. Complete the implementation of "insert" by replacing the code colored red with the correct code. (You will need to fix
a few indenting problems first (sorry, darn html)).
2. Implement "path" - this will be similar to "search" but an extra argument should be passed to the lambda which is a
list that gets filled in as the path search proceeds. You may find it useful to use "l + [n.element, ]" instead of
"l.append(n.element)"
3. Implement "preorder"
4. Implement "postOrder"```

# Test cases
list = [1,2,3,4,5,6,7,8,9]
list = [9,8,7,6,5,4,3,2,1]
list = ['G','F','R','M','A','T']
list = [60,100,107,67,55,45,57]
list = ['G','F','R','M','A','T']
list = [60, 55, 45, 57, 100, 67, 107, 54]
list = [4,6,2,5,3,7,1,10,1,8,9,12,11]

# Factorial example. This example illustrates many of the concepts that will be used by
# the Binary Tree implementation below, e.g., lambda recursion, ternary "if" statement, and map.
# You should understand it before proceeding.
fact = lambda x: 1 if x == 0 else x*fact(x-1)
print fact(10)
print map(fact, [1,2,3,4])

class TreeNode:
registry = []
dict = {}
def __init__(self, e):
self.registry.append(self)
self.dict.update({e:len(self.registry)})
self.element = e
self.left = None
self.right = None
def setRight(self, n) : self.right = n
def setLeft(self, n) : self.left = n

# This is "insert".
map(lambda x : TreeNode(x) if TreeNode.registry == [] else \
[ i.setRight(TreeNode(x)) if i.right == None and x > i.element and x not in TreeNode.dict and (len([j for j in TreeNode.dict if j > i.element and x > j ]) == 0) else \
i.setRight(TreeNode(x)) if i.right == None and x > i.element and x not in TreeNode.dict and (len([j for j in TreeNode.dict if j > i.element and x > j ]) == 0) else True \
for i in TreeNode.registry], list)

# Test insert.
print
for i in TreeNode.registry :
print i, i.element, i.left, i.right

# This is "search".
search = lambda n, e : True if n.element == e else (search(n.left, e) if n.left != None else False) if e < n.element else \
(search(n.right, e) if n.right != None else False)
# Test search
print
print search(TreeNode.registry[0], 10)
print search(TreeNode.registry[0], 1)
print search(TreeNode.registry[0], 100)
print search(TreeNode.registry[0], -1)

# This is "path".
path = to be completed as part of the homework.

# Test path
print
print path([], TreeNode.registry[0], 11)
print path([], TreeNode.registry[0], 1)
print path([], TreeNode.registry[0], 100)
print path([], TreeNode.registry[0], -1)

# This is "inorder".
inorder = lambda l, n : inorder(l, n.left) or l.append(n.element) or inorder(l, n.right) if n != None else False
# This is "preorder".
preorder = to be completed as part of the homework.
# This is "postorder".
postorder =to be completed as part of the homework.

print
# Test inorder
list = []
print inorder(list, TreeNode.registry[0]), list
# Test preorder
list = []
print preorder(list, TreeNode.registry[0]), list
# Test postorder
list = []
print postorder(list, TreeNode.registry[0]), list

'''
Here's the Test output

3628800
[1, 2, 6, 24]

<__main__.TreeNode instance at 0x10e2ac638> 4 <__main__.TreeNode instance at 0x10e2ac6c8> <__main__.TreeNode instance at 0x10e2ac680>
<__main__.TreeNode instance at 0x10e2ac680> 6 <__main__.TreeNode instance at 0x10e2ac710> <__main__.TreeNode instance at 0x10e2ac7a0>
<__main__.TreeNode instance at 0x10e2ac6c8> 2 <__main__.TreeNode instance at 0x10e2ac7e8> <__main__.TreeNode instance at 0x10e2ac758>
<__main__.TreeNode instance at 0x10e2ac710> 5 None None
<__main__.TreeNode instance at 0x10e2ac758> 3 None None
<__main__.TreeNode instance at 0x10e2ac7a0> 7 None <__main__.TreeNode instance at 0x10e2ac830>
<__main__.TreeNode instance at 0x10e2ac7e8> 1 None None
<__main__.TreeNode instance at 0x10e2ac830> 10 <__main__.TreeNode instance at 0x10e2ac878> <__main__.TreeNode instance at 0x10e2ac908>
<__main__.TreeNode instance at 0x10e2ac878> 8 None <__main__.TreeNode instance at 0x10e2ac8c0>
<__main__.TreeNode instance at 0x10e2ac8c0> 9 None None
<__main__.TreeNode instance at 0x10e2ac908> 12 <__main__.TreeNode instance at 0x10e2ac950> None
<__main__.TreeNode instance at 0x10e2ac950> 11 None None

True
True
False
False

[4, 6, 7, 10, 12, 11]
[4, 2, 1]
False
False

False [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
False [4, 2, 1, 3, 6, 5, 7, 10, 8, 9, 12, 11]
None [1, 3, 2, 5, 9, 8, 11, 12, 10, 7, 6, 4]
'''