Binary Search Trees ( Due 05 Apr 2013 )

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 method isSimilar() that takes as input two binary trees and returns true if the nodes have the same key values and are arranged in the same order and false otherwise.

def isSimilar (self, pNode):

Write a method printLevel() that takes as input the level and prints out all the nodes at that level. If that level does not exist for that binary search tree it prints nothing. Use the convention that the root is at level 1.

def printLevel (self, level):

Write a method getHeight() that returns the height of a binary search tree. Recall that the height of a tree is the longest path length from the root to a leaf.

def getHeight (self):

Write a method numNodes() 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. This function will be useful to determine if the tree is balanced.

def numNodes (self):

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 the other functions that we developed for completeness.

class Node (object):
  ...

class Tree (object):
  # Returns true if two binary trees are similar
  def isSimilar (self, pNode):

  # Prints out all nodes at the given level
  def printLevel (self, level): 

  # Returns the height of the tree
  def getHeight (self): 

  # Returns the number of nodes in the left subtree and
  # the number of nodes in the right subtree
  def numNodes (self):

def main():
    # Create three trees - two are the same and the third is different

    # Test your method isSimilar()

    # Print the various levels of two of the trees that are different

    # Get the height of the two trees that are different

    # Get the number of nodes in the left and right subtree

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 proctor 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:

#  Course Name: CS 313E

#  Unique Number: 53260

#  Date Created:

#  Date Last Modified:

Use the turnin program to submit your TestBinaryTree.py file. The proctor should receive your work by 11 PM on Friday, 05 Apr 2013. There will be substantial penalties if you do not adhere to the guidelines.