import java.util.ArrayList; import java.util.Iterator; public class Tree { private Node root; int numNodes; // Create a tree with just one node. public Tree(Item value) { root = new Node(value); numNodes = 1; } // A more useful constructor that will create a new tree with a // given node as the root, along with 2 existing subtrees as its children. // Note that either subtree may be null! public Tree(Item value, Tree leftSubtree, Tree rightSubtree) { root = new Node(value); numNodes = 1; root.left = leftSubtree.root; leftSubtree.root.parent = root; root.right = rightSubtree.root; rightSubtree.root.parent = root; numNodes = leftSubtree.numNodes + rightSubtree.numNodes + 1; } public int size() { return numNodes; } // Let's use the preorder iterator to print the tree! public String toString() { StringBuilder build = new StringBuilder(); build.append("The tree has " + numNodes + " node(s):\n"); Iterator iter = preorderIterator(root); while(iter.hasNext()) build.append("\t" + ((Node) iter.next()).data + "\n"); return build.toString(); } ////////////////////////////////////////////////////////////////////// // ADD CODE HERE that will define a class called EulerTourIterator, // which implements the built-in Iterator interface. // As a guide, use the code for the prorder iterator class below. // Note: Your addNext() method will need to take parameters // indicating what it should do (if anything) for its left action, // underneath action, and its right action. // // Here is one possible approach, as indicated by the way I've // written the iterator access methods at the bottom of this // source file: // Encode the 3 actions as strings. If an action string says // "myself", then we are to add the node we are at to the ArrayList. // If the action string is the empty string, then this indicates that // we perform no action. Otherwise, the action string indicates // we need to insert some special node/string in our iterator output. ////////////////////////////////////////////////////////////////////// /* public class EulerTourIterator implements Iterator { } */ // For reference: here is a stand-alone preorder iterator. // This code should be commented out because we don't need it! // The Euler tour should take care of all 3 traversals. public class PreorderIterator implements Iterator { // Initialize our attributes to keep track of nodes in preorder, // and start the recursive process from the 'start' node. protected ArrayList list = new ArrayList(); protected int currentIndex; public PreorderIterator(Node start) { list = new ArrayList(); addNext(start); currentIndex = 0; } // Recursive function to add nodes in a preorder manner public void addNext(Node n) { if (n == null) return; list.add(n); addNext(n.left); addNext(n.right); } public boolean hasNext() { return currentIndex < list.size(); } public Object next() { return list.get(currentIndex++); } public void remove() { throw new UnsupportedOperationException(); } } // Here is our depricated way to accessing the preorder iterator: // Eventually, you need to comment-out these 2 methods. public Iterator preorderIterator(Node start) { return new PreorderIterator(start); } public Iterator preorderIterator() { return new PreorderIterator(root); } // When you are ready to test, un-comment-out this code. /* public Iterator preorderIterator(Node start) { return new EulerTourIterator(start, "myself", "", ""); } public Iterator inorderIterator(Node start) { return new EulerTourIterator(start, "", "myself", ""); } public Iterator postorderIterator(Node start) { return new EulerTourIterator(start, "", "", "myself"); } public Iterator eulerTourIterator(Node start, String left, String below, String right) { return new EulerTourIterator(start, left, below, right); } */ // Let's say that if they don't tell us where to start, the default // is to start traversing from the root. // Please un-comment-out this method when you are ready to test. /* public Iterator preorderIterator() { return new EulerTourIterator(root, "myself", "", ""); } public Iterator inorderIterator() { return new EulerTourIterator(root, "", "myself", ""); } public Iterator postorderIterator() { return new EulerTourIterator(root, "", "", "myself"); } public Iterator eulerTourIterator(String left, String below, String right) { return new EulerTourIterator(root, left, below, right); } */ }