C307 / I308 Data Representation

C307 I308 Homework 8

Due date: Monday, March 21, 2022.

Ex. 1. Tree Implementation

a. Project

Create a project called hw8. Add a class called Node that is part of a package called tree. Copy the code from the following file into it:

Node.java

Create another class in the same package called Interface, containing the main function. Copy the code from the following file into it:

Interface.java

b. Node Methods

For this homework, you must supply the code for six of the functions in the class Node. The outline of these functions is already in the class, you just need to fill in the code. The description for these functions is given in the comments above them.

In addition, add an instance method in the class Node that prints the tree in symmetric order, called printInOrder. Use the function from the lecture slides, but convert it from static to instance. You can use the function weight from the lecture as a model for this. In the interface class, add a menu option for this operation and a call to this function in perform action.

c. Interface

Add a method in the class Interface building the tree that you can find on slide 11 in the lecture. You can start with the code given on slide 5, and complete it by adding the nodes that are missing. Make the function return the tree it created. Then in the main, assign the result of a call to this function to the variable tree when it is declared instead of null. That way, the program will start with a tree that is not empty and you can test the functions you wrote.

As shown in the lecture, you can save the commands used to create a tree in a text file and then copy and paste them into the program to avoid typing them each time. For example, the following file:

tree.txt

contains the commands to build the tree below:

To use this file during the execution of the program, first delete the current tree with the command d, then copy and paste the content of the file into the running program. This should give you a second tree that you can test your functions on.

Homework Submission

Upload the files Node.java and Interface.java to Canvas - Assignments - Homework 8.