In this assignment you will be adding to the classes Node and Tree that we developed in class and testing them. There are several short methods that you will have to write.
Write a function get_height() that returns the height of a binary tree from any starting node aNode. The height of a tree is the number of edges from the starting node to the furthest leaf. The height of the leaf node is 0. The height of an empty tree is -1. If aNode is the root node then the function returns the height of the binary search tree.
def get_height (self, aNode):
Write a function num_nodes() that returns the number of nodes in the left subtree from the root and the number of nodes in the right subtree from the root and the root itself.
def num_nodes (self, aNode):
Write a function that computes the balance factor of any given node.
balance_factor = height (left sub tree) - height (right sub tree)The balance_factor of a leaf node is zero.
def balance_factor (self, aNode):
Write a function that returns True if a binary search tree is balanced and False otherwise. A binary search tree is balanced if every node in the binary search has a balance factor of -1, 0, or 1.
def is_balanced (self, aNode):
Write a function that takes as input parameter a sorted list of integers in ascending order and returns a balanced binary search tree. This function returns a new Tree object.
def create_tree (self, a_list):
Write a function that prints the nodes breadth first from a given starting node aNode. If aNode is the root node then it prints all the nodes in the binary search tree breadth first. Each level is printed on a separate line.
def print_level (self, aNode):
In this assignment you will be writing helper methods for the Tree class that we developed and test them. The following is the outline of the code that you will be submitting. You may include other helper functions as needed.
class Node (object): ... class Tree (object): # Returns the height of the tree def get_height (self, aNode): # Returns the number of nodes in the left subtree and # the number of nodes in the right subtree and the root def num_nodes (self, aNode): # returns the difference between the height of the left # sub tree and the height of the right sub tree def balance_factor (self, aNode): # returns True if the tree is balanced and False otherwise # in a balanced tree every node has a balance factor of -1, 0, 1 def is_balanced (self, aNode): # create a balanced binary search tree from a sorted list def create_tree (self, a_list): # prints the nodes breadth first def print_level (self, aNode): def main(): # Create at least two binary search trees - one balanced and # the other not # Test your function get_height() for those trees # Test your function num_nodes() for those trees # Find the balance factors of the roots of those trees # Find if the trees are balanced or not # Create a balanced binary search tree from a sorted list # check that it is balanced # Print all the trees that you have breadth first
In the class TestBinaryTree you will create several trees and show convincingly that your methods are working. For example you can create a tree by inserting the following integers in this order: 50, 30, 70, 10, 40, 60, 80, 7, 25, 38, 47, 58, 65, 77, 96. There should be enough documentation in your code that explains to the teaching assistants what you are testing and how.
The file that you will be turning in will be called TestBinaryTree.py. The file will have a header of the following form:
# File: TestBinaryTree.py # Description: # Student Name: # Student UT EID: # Partner Name: # Partner UT EID: # Course Name: CS 313E # Unique Number: # Date Created: # Date Last Modified:For this assignment you may work with a partner. If you do, both of you must read the paper on Pair Programming and submit the same copy of the program but make sure that you have your partner's name and eid in your program. If you are doing this assignment by yourself, then remove the Partner Name and Partner UT EID from the header.
Use the Canvas system to submit your TestBinaryTree.py file. We should receive your work by 11 PM on Friday, 26 Apr 2019. There will be substantial penalties if you do not adhere to the guidelines. Remember Python is case sensitive. The name of your file must match exactly what we have specified.