On term test 2, there were three questions, and I believe I did not do very good.
The first question was about the basic concepts of tree class, drawing trees with the height of 3 and arity of 3 and list the tree by postorder. Because I only wrote down and memorized inorder _visit, I had problems about solving postorder question. I re-visted the previous lab, which contains preorder_visit code, and reviewed it about the test for preparing the finals.
def postorder_visit(root, perform):
''' (BTNode, function) -> NoneType
Visit each node of binary tree rooted at root in postorder
and perform.
>>> b = BTNode(8)
>>> b = insert(b, 4)
>>> b = insert(b, 2)
>>> b = insert(b, 6)
>>> b = insert(b, 12)
>>> b = insert(b, 14)
>>> b = insert(b, 10)
>>> def f(node): print(node.data)
>>> postorder_visit(b, f)
2
6
4
10
14
12
8
'''
if root is None:
pass
else:
postorder_visit(root.left, perform)
postorder_visit(root.right, perform)
perform(root)
The second question was pretty simple since we had the similar method on previous lab.
def list_between(node, start, end):
''' (BTNode, object, object) -> list
Return a Python list of all values in the binary search tree
rooted at node that are between start and end (inclusive).
>>> list_between(None, 3, 13)
[]
>>> b = BTNode(8)
>>> b = insert(b, 4)
>>> b = insert(b, 2)
>>> b = insert(b, 6)
>>> b = insert(b, 12)
>>> b = insert(b, 14)
>>> b = insert(b, 10)
>>> list_between(b, 2, 3)
[2]
>>> L = list_between(b, 3, 11)
>>> L.sort()
>>> L
[4, 6, 8, 10]
'''
if node is None:
return []
else:
left_list = (list_between(node.left, start, end)
if node.data > start
else [])
right_list = (list_between(node.right, start, end)
if node.data < end
else [])
node_list = ([node.data]
if (start <= node.data <= end)
else [])
return left_list + node_list + right_list
The question 2 was almost same as the above code, but it has little difference (I do not remember exactly, but I added more code on node_list.
The difficulty of this term test was the third question: I totally had no idea how to insert the new node that is half of previous node.back.value. I am going to review about LLNode and LinkedList after the sample solution is posted.
I hope the strike will end soon. I have a lot of questions and need help from the lab since piazza is not enough for me. Until the strike ends, I will use office hour and piazza and also self-study more using the sample solution of labs.
No comments:
Post a Comment