Lab 9

Code for this lab

For this lab you need to create a class called TreeClass

First you need to create your tree object. This will be held in an ArrayList of Strings (ArrayList).

Initialize the tree in the constructor and add a dummy value to the list ("DUMMY" should be your value).

Write a method called buildTree that takes a Scanner parameter and, while it has data (or has next :p), add the next piece of data to the list.

Next you should write a toString method that uses a for loop to append all elements in the tree to an empty string and then return it.

Now we need 3 traversal methods. For this lab we are doing pre, post, and inorder traversal. The method names, as you can see in the driver are: toStringPreorderTraversal(String, int), toStringPostorderTraversal(String, int), toStringInorderTraversal(String, int).

Each of these methods have the same code basically, it is just the order you do it in the changes it, so we will start with preorder traversal.

Preorder takes the String input and appends the int index of the tree you made to it. So basically StringName += " " + treename.get(intname)

After that we have 2 branches, the left and right leaves of the node. To get the left node we multiply our index by 2, to get the right we multiply our index by 2 and add 1. These should both be conditions, checking if the next value is larger than the size of the list. If it is, do nothing, but if it isn't then you should call the method again with the new values.

Once you have that you will return your trimmed String input. See what you get from that, if you get the right answer, great, you are almost done! Now all you need to do is move when you append to the String input. Since we just did Preorder and it was before the conditions...well, I think you can figure that part out now.

Zip up the folder and submit to dropbox!