def build_lecture_example(): """ Purpose: returns the example found in the lecture slides """ atree = tn.create(2) a = tn.create(7) b = tn.create(5) tn.set_left(atree, a) tn.set_right(atree, b) c = tn.create(11) d = tn.create(6) tn.set_left(a, c) tn.set_right(a, d) return atree
def insert_prim(tnode, value): """ Insert a new value into the binary tree. Preconditions: :param tnode: a binary search tree, created by create() :param value: a value Postconditions: If the value is not already in the tree, it is added to the tree Return :return: flag, tree Flag is True is insertion succeeded; tree is the tree with value in it Flag is False if the value is already in the tree, tree is returned unchanged """ if tnode is None: return True, tn.create(value) else: cval = tn.get_data(tnode) if cval == value: return False, tnode elif value < cval: flag, subtree = insert_prim(tn.get_left(tnode), value) if flag: tn.set_left(tnode, subtree) return flag, tnode else: flag, subtree = insert_prim(tn.get_right(tnode), value) if flag: tn.set_right(tnode, subtree) return flag, tnode
def copy(tnode): """ Purpose: create a new object that is the same as tnode Preconditions: :param tnode: a tree Postconditions: a new object created, tnode not changed :return: reference to the newly created tree """ if tnode == None: return None if tf.is_leaf(tnode) is True: #base: if leaf, create tree with data return tn.create(tn.get_data(tnode)) new_tree = tn.create(tn.get_data(tnode)) #recursive: set left and right to copy() child tn.set_left(new_tree, copy(tn.get_left(tnode))) tn.set_right(new_tree, copy(tn.get_right(tnode))) return new_tree
def build_fibtree(n): """ Purpose: Build a tree whose structure represents the Fibonacci numbers. The root of the tree has data value Fib(n). This function can return very large trees. Pre-conditions: :param n: a non-negative integer Return: :return: a primitive binary tree whose structure reflects the calculation of fib(n) """ if n <= 1: return tn.create(n) else: ltree = build_fibtree(n-1) rtree = build_fibtree(n-2) return tn.create(tn.get_data(ltree)+tn.get_data(rtree), ltree, rtree)
def reflection(tnode): """ Purpose: create a new tree that is the mirror of the original precoditions: :param tnode: a tree Postconditions: a new object is created :return: the new tree that is the mirror of the original """ if tnode == None: return if tf.is_leaf(tnode) is True: return tn.create(tn.get_data(tnode)) mirrored = tn.create(tn.get_data( tnode)) # recursive: set left and right to right and left of tnode tn.set_left(mirrored, reflection(tn.get_right(tnode))) tn.set_right(mirrored, reflection(tn.get_left(tnode))) return mirrored
def build_turtle(): """ Purpose: returns a simple tree with single letter data values """ atree = tn.create('t', tn.create('u', tn.create('t'), tn.create('r')), tn.create('e', tn.create('l'), None)) return atree
def refection(tnode): """ Purpose: Copy the inputted tree and return a reflected copy Preconditions: :param tnode: A tree node to be reflected and copied Post-conditions: None :return: The reflected and copied tree """ if tnode is None: # Special case if tree is None return None else: # a new tree with subtrees swapped return tn.create(tn.get_data(tnode), refection(tn.get_right(tnode)), refection(tn.get_left(tnode)))
def copy(tnode): """ Purpose: Copy a tree and return the copy Preconditions: :param tnode: A tree node to be copied Post-conditions: None :return: The reference to the copied tree """ if tnode is None: # Special case if tree is None return None else: return tn.create( tn.get_data(tnode), copy(tn.get_left(tnode)), copy(tn.get_right(tnode))) # return a copy of items in tree
def copy(tnode): """ Purpose: Make a copy of the tree rooted at the given tnode. Pre-conditions: :param tnode: A treenode Return: :return: A copy of the tree. """ if tnode is None: return None else: return tn.create(tn.get_data(tnode), copy(tn.get_left(tnode)), copy(tn.get_right(tnode)))
def build_tuttle(): """ Purpose: returns a simple tree with single letter data values this tree is slightly different from the previous tree """ atree = tn.create('t', tn.create('u', tn.create('t'), tn.create('t')), tn.create('e', tn.create('l'), None)) return atree
def treeify(alist): """ Purpose: Create a tree using the given list. The first node in the tree is the data value of the root. The first half of the remaining nodes form the left subtree. The second half of the remaining nodes form the right subtree. If the list has an even length, the left subtree will be slightly bigger then the right Pre: :param alist: a Python list Return: :return: a primitive tree structure (tnode) """ if len(alist) == 0: # if there are no values, return empty tree return None elif len(alist) == 1: # if just one value, create a leaf return tn.create(alist[0]) else: # split the list in half, and build two roughly equal subtrees mid = 1 + len(alist)//2 return tn.create(alist[0], treeify(alist[1:mid]), treeify(alist[mid:]))
def build_complete(height, d=0): """ Purpose: Create a complete binary tree of the given height. The data value for the root of the tree is d Pre-conditions: :param height: a non-negative integer :param d: (optional) an integer, used as the value for the root of the tree Return: :return: a complete primitive binary tree whose root has data value d """ if height == 0: return None else: return tn.create(d,build_complete(height-1,2*d+1), build_complete(height-1,2*d+2))
def copy(tnode): """ Purpose: To create an exact copy of the given tree, with completely new treenodes, but exactly the same data values, in exactly the same places Pre-conditions: :param tnode: a treenode Post-conditions: None return: A copy of tnode """ if tnode is None: return None else: cur_val = tn.get_data(tnode) new_tnode = tn.create(cur_val) tn.set_left(new_tnode, copy(tn.get_left(tnode))) tn.set_right(new_tnode, copy(tn.get_right(tnode))) return new_tnode
Lab Section: 14 """ import a8q3 as complt import treenode as Tn testcasesComplete = [ #0 { "Input" : None, "Output" : True, "Reason" : "There is no tree to evaluate" }, #1 { "Input" : Tn.create(1), "Output" : True, "Reason" : "The tree is only a root" }, #2 { "Input" : Tn.create(1, Tn.create(2)), "Output" : False, "Reason" : "The tree is incomplete" }, #3 { "Input" : Tn.create(1, Tn.create(2), Tn.create(3)), "Output" : True, "Reason" : "The tree is complete with a depth of 2" },
- very long data values might cause the presentation to get messy - subtrees appear below a parent - left subtree immediately - right subtree after the entire left subtree Pre-conditions: :param tnode: a Binary tree (treenode or None) :param level: the level of the tnode (default value 0) Return: A string with the hierarchy of the tree. """ if tnode is None: return 'EMPTY' else: result = '\t' * level result += str(TN.get_data(tnode)) if TN.get_left(tnode) is not None: result += '\n' + to_string(TN.get_left(tnode), level + 1) if TN.get_right(tnode) is not None: result += '\n' + to_string(TN.get_right(tnode), level + 1) return result root = TN.create(7) a = TN.create(4) b = TN.create(5) c = TN.create(9) TN.set_left(root, a) TN.set_right(root, b) TN.set_right(b, c) print(root) print(to_string(root))
return True elif len(left_list) == 0 and min(right_list) > cur_data: return ordered(right_sub) elif len(right_list) == 0 and max(left_list) < cur_data: return ordered(left_sub) if max(left_list) < cur_data and min(right_list) > cur_data: return ordered(left_sub) and ordered(right_sub) else: return False ############################################################################### if __name__ == '__main__': expr_tree = extree.fibonatree fibTree = extree.fibonatree my_tree = tn.create( 0, tn.create(0, tn.create(0, tn.create(0), tn.create(0)), tn.create(0, tn.create(0), tn.create(0))), tn.create(0, tn.create(0, tn.create(0), tn.create(0)), tn.create(0, tn.create(0), tn.create(0)))) muh_tree = tn.create(10, tn.create(5, tn.create(1), tn.create(7)), tn.create(15, tn.create(11), tn.create(17))) muh_tree1 = tn.create(10) muh_tree2 = tn.create(10, tn.create(5), tn.create(3)) print(tf.to_string(muh_tree)) print(ordered(muh_tree))
:param tnode: a treenode Post-conditions: none Return :return: the number of nodes as an integer """ if tnode == None: return 0 else: return 1 + count(tn.get_left(tnode)) \ + count(tn.get_right(tnode)) if __name__ == '__main__': # Create the tree we've been using in class atree = tn.create(2) a = tn.create(7) b = tn.create(5) tn.set_left(atree, a) tn.set_right(atree, b) c = tn.create(11) d = tn.create(6) tn.set_left(a, c) tn.set_right(a, d) # test height test_suite1 = [{ "inputs": [None], "outputs": 0, "reason": "empty tree" }, { "inputs": [tn.create(5)],
#NSID: jat687 #Student Number: 1101081 #Course: CMPT 145-01 #Lab: L03 import exampletrees as exampletrees import treenode as tn import a8q1 as a8q1 ##################################################################### # test a8q1.count_node_types # Unit testing # Trees for testing empty_tree = None single_tree = tn.create(1776) example_tree = tn.create( 5, tn.create(1, None, tn.create(4, tn.create(3, tn.create(2, None, None), None), None)), tn.create(9, tn.create(8, tn.create(7, tn.create(6, None, None), None), None), tn.create(1, tn.create(3, None, None), tn.create(3, None, None)))) fibona_tree = tn.create( 5, tn.create(2, tn.create(1, None, None), tn.create(1, tn.create(0, None, None), tn.create(1, None, None))), tn.create(
#a8q1_testing.py enz889 Enhan Zhao cmpt145 import a8q2 import treenode as tn #test mirriored()#################### mirrored = [ { 'inputs': [None, None], 'outputs': True, 'reason': 'empty trees' }, { 'inputs': [tn.create(1), tn.create(1)], 'outputs': True, 'reason': 'singleton trees mirrored' }, { 'inputs': [tn.create(1), tn.create(2)], 'outputs': False, 'reason': 'singleton trees not mirrored' }, { 'inputs': [ tn.create(1, tn.create(2, tn.create(3))), tn.create(1, None, tn.create(2, None, tn.create(3))) ], 'outputs': True, 'reason': 'only left children in t1 and only right children in t2 mirrored '
#a8q3_testing.py enz889 Enhan Zhao cmpt145 import a8q3 import treenode as tn #test complete()############################################### complete = [ { 'inputs': None, 'outputs': (False, None), 'reason': 'empty tree' }, { 'inputs': tn.create(1), 'outputs': (True, 1), 'reason': 'singleton tree' }, { 'inputs': tn.create(1, tn.create(2)), 'outputs': (False, None), 'reason': 'incomplete height 2' }, { 'inputs': tn.create(1, tn.create(2), tn.create(3)), 'outputs': (True, 2), 'reason': 'complete height 2' }, { 'inputs': tn.create(1, tn.create(2, tn.create(4), tn.create(5)), tn.create(3, tn.create(6), tn.create(7))),
def int_cmplt(tnode): if tnode is None: return (True, 0) else: flag_left, ldepth = int_cmplt(tn.get_left(tnode)) flag_right, rdepth = int_cmplt(tn.get_right(tnode)) if ldepth == rdepth: return (flag_left and flag_right, rdepth + 1) else: return (False, 0) flag, height = int_cmplt(tnode) return flag ############################################################################### if __name__ == '__main__': expr_tree = exampletrees.fibonatree fibTree = exampletrees.fibonatree my_tree = tn.create( 0, tn.create(0, tn.create(0, tn.create(0), tn.create(0)), tn.create(0, tn.create(0), tn.create(0))), tn.create(0, tn.create(0, tn.create(0), tn.create(0)), tn.create(0, tn.create(0), tn.create(0)))) muh_tree = tn.create(1, tn.create(3), tn.create(4)) print(tf.to_string(muh_tree)) print(cmplt(muh_tree))
tnode: the given tree node t: target value r: replacement value Post conditions: the target value is replaced with the replacement value Return: None """ if tnode is not None and tn.get_data(tnode) == t: tn.set_data(tnode, r) elif tnode is None: pass else: subst(tn.get_right(tnode), t, r) subst(tn.get_left(tnode), t, r) root = tn.create(7) a = tn.create(5) b = tn.create(5) c = tn.create(5) tn.set_left(root, a) tn.set_right(root, b) tn.set_right(b, c) print(root) def count_target(tnode, target): """ Purpose: Counts the number of times the given target appears in the given tree Pre-condition: tnode: the given tree node
#a8q1_testing.py enz889 Enhan Zhao cmpt145 import a8q1 import treenode as tn #test count_node_types()############################################### count_node = [ {'inputs' : None, 'outputs' : (0, 0), 'reason' : 'empty tree'}, {'inputs' : tn.create(1), 'outputs' : (1, 0), 'reason' : 'singleton tree'}, {'inputs' : tn.create(1, tn.create(2, tn.create(3))), 'outputs' : (1, 2), 'reason' : 'tree with only left child'}, {'inputs' : tn.create(1, None, tn.create(2, None, tn.create(3))), 'outputs' : (1, 2), 'reason' : 'tree with only right child'}, {'inputs' : tn.create(1, tn.create(2, tn.create(4, tn.create(6)), tn.create(5)), tn.create(3, tn.create(7))), 'outputs' : (3, 4), 'reason' : 'tree with assorted children'}, ] for t in count_node: inputs = t['inputs'] expected = t['outputs']
def build_xtree_me(): """ Purpose: builds a slightly larger, more or less undistinguished tree """ xtree = tn.create(5, tn.create(1,None, tn.create(4, tn.create(3,tn.create(2,None,None),None), None)), tn.create(9,tn.create(8,tn.create(7,tn.create(6,None,None),None),None), tn.create(1,tn.create(3,None,None),tn.create(3,None,None)))) return xtree
:param value: A value to delete :param expected: The expected result of delete_prim(atree,value) :param reason: A string about the reason for the test Return: :return: none """ flag, result = delete_prim(atree, value) if flag is not expected: print("Unit Test error: delete_prim returned:", flag, reason) ################################################# # unit test all functions # member unit_member(None, 1, False, 'on empty tree') unit_member(tn.create(1), 1, True, 'on one node tree containing data value') unit_member(tn.create(1), 2, False, 'on one node tree not containing data value') unit_member(tn.create(10, tn.create(5), tn.create(15)), 1, False, 'on three node tree without data value') unit_member(tn.create(10, tn.create(5), tn.create(15)), 7, False, 'on three node tree without data value') unit_member(tn.create(10, tn.create(5), tn.create(15)), 12, False, 'on three node tree without data value') unit_member(tn.create(10, tn.create(5), tn.create(15)), 17, False, 'on three node tree without data value') unit_member(tn.create(10, tn.create(5), tn.create(15)), 5, True, 'on three node tree containing data value') unit_member(tn.create(10, tn.create(5), tn.create(15)), 10, True, 'on three node tree containing data value')
#a8q4_testing enz889 Enhan Zhao cpt145 import treenode as tn import a8q4 #test path_to()############################################### path_to = [ { 'inputs': [None, 1], 'outputs': (False, None), 'reason': 'empty tree' }, { 'inputs': [tn.create(1), 1], 'outputs': (True, [1]), 'reason': 'singleton tree' }, { 'inputs': [tn.create(1, tn.create(2)), 2], 'outputs': (True, [2, 1]), 'reason': 'simple tree left' }, { 'inputs': [tn.create(1, None, tn.create(2)), 2], 'outputs': (True, [2, 1]), 'reason': 'simple tree right' }, { 'inputs': [ tn.create(1, tn.create(2, tn.create(4), tn.create(5)), tn.create(3, tn.create(6), tn.create(7))), 7 ],
MyQueue.enqueue(explore, tnode) while MyQueue.size(explore) > 0: current = MyQueue.dequeue(explore) print(tn.get_data(current), end=" ") left = tn.get_left(current) if left is not None: MyQueue.enqueue(explore, left) right = tn.get_right(current) if right is not None: MyQueue.enqueue(explore, right) if __name__ == '__main__': # Create the tree we've been using in class root = tn.create(2) a = tn.create(7) b = tn.create(5) tn.set_left(root, a) tn.set_right(root, b) c = tn.create(11) d = tn.create(6) tn.set_left(a, c) tn.set_right(a, d) print("Pre-order traversal:", end=" ") pre_order(root) print() print("In-order traversal:", end=" ") in_order(root) print()
Lab Section: 14 """ import treenode as Tn import a8q4 as T_ordered orderedTestCases = [ #0 { "Input": None, "Output": True, "Reason": "The tree is empty and therefore ordered" }, #1 { "Input": Tn.create(1), "Output": True, "Reason": "A single leaf tree is ordered" }, #2 { "Input": Tn.create(1, Tn.create(2)), "Output": False, "Reason": "The tree is disordered because the left leaf is greater than the root" }, #3 { "Input": Tn.create(2, Tn.create(1), Tn.create(3)),
# CMPT 145: Binary trees # Defines a few example trees import treenode as tn atree = tn.create(2) a = tn.create(7) b = tn.create(5) tn.set_left(atree, a) tn.set_right(atree, b) c = tn.create(11) d = tn.create(6) tn.set_left(a, c) tn.set_right(a, d) # an empty tree with a bad pun: it's empty mtree = None # a tree with one node only. Yes, a bad pun too. ctree = tn.create('si') # a larger more e-xtree-me tree xtree = tn.create( 5, tn.create(1, None, tn.create(4, tn.create(3, tn.create(2, None, None), None), None)), tn.create(9, tn.create(8, tn.create(7, tn.create(6, None, None), None), None), tn.create(1, tn.create(3, None, None), tn.create(3, None, None))))
#Name: Jason Tran #NSID: jat687 #Student Number: 1101081 #Course: CMPT 145-01 #Lab: L03 import treenode as tn import a8q4 as a8q4 ##################################################################### # test a8q4.ordered # Trees for testing empty_tree = None single_tree = tn.create(1776) xtree = tn.create( 5, tn.create(1, None, tn.create(4, tn.create(3, tn.create(2, None, None), None), None)), tn.create(9, tn.create(8, tn.create(7, tn.create(6, None, None), None), None), tn.create(1, tn.create(3, None, None), tn.create(3, None, None)))) fibona_tree = tn.create( 5, tn.create(2, tn.create(1, None, None), tn.create(1, tn.create(0, None, None), tn.create(1, None, None))), tn.create(