Binary Trees ( Due 15 Apr 2022 )

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

def is_similar (self, pNode):

Write a method get_level() that takes as input the level and returns a list of all the nodes at that level from left to right. If that level does not exist for that binary search tree return an empty list . Use the convention that the root is at level 0.

def get_level (self, level):

Write a method get_height() 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 get_height (self):

Write a method num_nodes() that returns the sum of 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. You have to implement this function recursively, and not have a num_nodes parameter in the Tree class.

def num_nodes (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.

import sys

class Node (object):
  ...

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

  # Returns a list of nodes at a given level from left to right
  def get_level (self, level): 

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

  # 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):

def main():
    # Create three trees - two are the same and the third is different
	line = sys.stdin.readline()
	line = line.strip()
	line = line.split()
	tree1_input = list (map (int, line)) 	# converts elements into ints

	line = sys.stdin.readline()
	line = line.strip()
	line = line.split()
	tree2_input = list (map (int, line)) 	# converts elements into ints

	line = sys.stdin.readline()
	line = line.strip()
	line = line.split()
	tree3_input = list (map (int, line)) 	# converts elements into ints

    # Test your method is_similar()

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

    # Get the height of the two trees that are different

    # Get the total number of nodes a binary search tree

if __name__ == "__main__":
  main()

To run this code on the Mac you will do:
python3  TestBinaryTree.py < bst.in

On a Windows machine you will do
python TestBinaryTree.py < bst.in

In the class TestBinaryTree you will create several trees and show convincingly that your methods are working. Here is an example of the file bst.in:

50 30 70 10 40 60 80 7 25 38 47 58 65 77 96
50 30 70 10 40 60 80 7 25 38 47 58 65 77 96
58 77 65 30 38 50 7 25 47 96 80 10 60 70 40
There should be enough documentation in your code that explains to the student 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 only one 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, 15 Apr 2022. 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.