Пример #1
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
Пример #2
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))
Пример #3
0
def sum_leaf_values(tnode):
	"""
	Purpose: returns the sum of all the leaf values in the given tree
	Pre-conditions:
		tnode: the given tree node
	Post condition: None
	Return: the sum of all the leaf values in the tree
	"""
	count = 0
	if tnode is None:
		pass
	elif tf.is_leaf(tnode):
		count += 1
	else:
		count += sum_leaf_values(tn.get_left(tnode))
		count += sum_leaf_values(tn.get_right(tnode))
	return count
Пример #4
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
Пример #5
0
def count_node_types(tnode):
    """
    Purpose:
        Returns a tuple containing the number of leaf nodes in the tree,
        and the number of non-leaf nodes in the tree.
    Pre-conditions:
        :param tnode: a treenode
    return: a tuple (number of leaf nodes, number of non-leaf nodes)
    """
    if tnode is None:
        return 0, 0
    else:
        left = count_node_types(tn.get_left(tnode))
        right = count_node_types(tn.get_right(tnode))
        if tf.is_leaf(tnode):
            return (1 + left[0] + right[0], 0 + left[1] + right[1])
        else:
            return (0 + left[0] + right[0], 1 + left[1] + right[1])
Пример #6
0
def count_node_types(tnode):
    """
    Purpose: count the number of leaf nodes and non-leaf nodes in a tree.
    Preconditions
    :param tnode: a tree node
    Postconditions: nothing
    :return: a tuple(# leaf, # non-leaf)
    """
    if tnode == None:
        return (0, 0)
    leaf_count = 0
    root_count = 0
    if tf.is_leaf(tnode) is True:     #base case, 1 leaf
        return (leaf_count + 1, root_count)
    else:           #recursive case
        leaf_count += count_node_types(tn.get_left(tnode))[0] + count_node_types(tn.get_right(tnode))[0]
        root_count += (count_node_types(tn.get_left(tnode))[1] + count_node_types(tn.get_right(tnode))[1]) + 1
    return (leaf_count, root_count)
Пример #7
0
def subst(tnode, t, r):
    """
    Purpose: replace target value with replacement value in a tree
    Preconditions:
    :param tnode: a tree
    :param t: target value
    :param r: replacement value
    Postconditions: values are replaced, if match
    :return: None
    """
    if tnode == None:
        return None
    if tf.is_leaf(tnode) is True:       #base
        if tn.get_data(tnode) == t:
            tn.set_data(tnode, r)
    if tn.get_data(tnode) == t:
        tn.set_data(tnode, r)
    subst(tn.get_left(tnode), t, r)
    subst(tn.get_right(tnode), t, r)
Пример #8
0
def complete(tnode):
    """ Purpose: Determine if the given tree is complete.
    Pre-conditions:
    :param tnode: a primitive binary tree
    :return: A tuple (True, height) if the tree is complete, A tuple (False, None) otherwise.
    """
    if tnode is None:
        return False, None
    if tf.is_leaf(tnode) is True:  #base 1: if leaf
        return True, 1
    elif tn.get_left(tnode) is None or tn.get_right(
            tnode) is None:  #base 2: if incomplete
        return False, None
    left_flag, left_height = complete(
        tn.get_left(tnode))  #check left side first
    if left_flag is False:
        return False, None
    right_flag, right_height = complete(
        tn.get_right(tnode))  #check right side first
    if right_flag is False:
        return False, None
    if left_height == right_height:  #if height is equal, then tree is complete
        return True, left_height + 1