![]() |
Assignment 4Submission
RequirementsAs stated on the assignment handout, you must:
SuggestionsAssignment 4 talks about revising Assignment 3, but they really are not very related, except conceptually. To avoid confusion, I'd start from scratch. Just write the following two classes. TreeNode
In order to build a tree, you'll need some nodes. Have a look at the Node class on p. 408 - 410 of the book for ideas. Ideally, you should practice writing generic and reusable code like this. However, for this assignment, your nodes only need to hold Later, we're going to need a way to compare nodes, so they should implement the Comparable interface. Here's a skeleton of the essential features of this class: public class TreeNode implements Comparable<TreeNode> { //instance members (could be public or protected, if you're lazy) private Integer data; private TreeNode left; private TreeNode right; //constructors public TreeNode(Integer data); public TreeNode(Integer data, TreeNode left, TreeNode right); //methods //if above variables are private, include a get and set for each. public boolean isLeaf(); //handy, but not required public int compareTo(TreeNode node); //see java.lang.Comparable in API //a toString() and equals() method is good practice, but not used here } BinaryTreeFor the binary tree, you don't need to follow what the book is doing. The only instance variable you need is a pointer to the root of the tree. Then I recommend the following methods:
A Constructor. This should take a set of numbers as its only parameter. This could be an array of
I'd recommend you use a
When you're done looping, only one node will remain in the PriorityQueue, but it will be contain all the other nodes as children. Remove this last node from the queue, and set
(In order to use this technique, your nodes must implement
So this method will take a string of Ls and Rs that give directions through the tree to particular leaf node. (It is an error if the given string contains other letters, or if it does not end at a leaf node.) It will then return the value in this leaf node (or throw an exception if there was an error).
You do not need to add special labeling to your nodes, as you already know whether a subtree is on the left or the right--you call either
public class BinaryTree { //the only instance variable private TreeNode root; //constructor public BinaryTree(int[] set) { /* --load a PriorityQueue<TreeNode> with one node for each element of set (that is, each node contains an element of set) --then go through the queue, creating internal nodes and putting them back in the queue --when the queue has only one node left: this.root = queue.poll(); */ } //methods public void display() { System.out.println(this.display(this.root)); } private String display(TreeNode node) { //(including proper formatting) build a string that contains: //node's left subtree //node's contents //node's right subtree } public int find(String searchCode) throws IllegalArgumentException { //start at this.root and follow the directions in searchCode //return the contents of the designated leaf node } public static void main(String[] args) { //test the other methods here. For example: int[] testSet = {5, 2, 3, 1, 4}; BinaryTree tree = new BinaryTree(testSet); //display the tree //try some different find strings //try some legitimate directions. But also test: //--a search string that has other letters in it //--a search string that's too long (passes a leaf node) //--a search string that's too short (doesn't reach a leaf) //you may also want to try testing a tree with a different input set } } In Assignment 5, we'll add another method to this BinaryTree class. Grading:Out of 100 points:
|
~ztomasze Index :
TA Details: ICS211:
Assignment 2 http://www2.hawaii.edu/~ztomasze |
Last Edited: 09 Oct 2006 ©2006 by Z. Tomaszewski. |