Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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))
Пример #5
0
 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
Пример #6
0
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
Пример #7
0
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
Пример #8
0
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)
Пример #9
0
# 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))))
Пример #10
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()
    print("Post-order traversal:", end=" ")
    post_order(root)
    print()
Пример #11
0
	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
		target: the target value
	Post condition: None
	Return: the number of times the given target value appears in the given tree
	"""
Пример #12
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))