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 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 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 reflect(tnode): """ Purpose: swap every left and right sub tree in a tree preconditions: :param tnode: a tree postconditions: left and right in tree and sub tree swapped :return: nothing """ if tnode is None or tf.is_leaf(tnode) is True: #base: do nothing if end return left = tn.get_left(tnode) right = tn.get_right(tnode) tn.set_left(tnode, right) #swap left and right. tn.set_right(tnode, left) reflect(tn.get_left(tnode)) reflect(tn.get_right(tnode))
def delete_bst(tnode): if tnode is None: return False, tnode else: cval = tn.get_data(tnode) if cval == value: return reconnect(tnode) elif value < cval: flag, subtree = delete_bst(tn.get_left(tnode)) if flag: tn.set_left(tnode, subtree) return flag, tnode else: flag, subtree = delete_bst(tn.get_right(tnode)) if flag: tn.set_right(tnode, subtree) return flag, tnode
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 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
def reconnect(delthis): if tn.get_left(delthis) is None \ and tn.get_right(delthis) is None: # the deleted node has no children return True, None elif tn.get_left(delthis) == None: # the deleted node has one right child return True, tn.get_right(delthis) elif tn.get_right(delthis) == None: # the deleted node has one left child return True, tn.get_left(delthis) else: # the deleted node has 2 children left = tn.get_left(delthis) right = tn.get_right(delthis) walker = left # walk all the way to the right from left while tn.get_right(walker) != None: walker = tn.get_right(walker) tn.set_right(walker, right) return True, left
def reflect(tnode): """ Purpose: Copy the inputted tree and reflect it Preconditions: :param tnode: A tree node to be reflected and copied Post-conditions: reflects the inputted tree fulfilling the reflection property :return: None """ if tnode is None: # Special case if tree is None return None else: right = tn.get_right(tnode) # simplify right and left left = tn.get_left(tnode) reflect(left) # Iterate through the tree reflect(right) tn.set_left( tnode, right) # Set each tree on the on right to tree on the left ect. tn.set_right(tnode, left)
# 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))))
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() print("Post-order traversal:", end=" ") post_order(root) print() print("Breadth-first order traversal:", end=" ")
""" 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 target: the target value Post condition: None Return: the number of times the given target value appears in the given tree """ count = 0
- 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))