Programming Assignment #7 (50 points)
Due: Wednesday night, April 23, at 11:59 p.m.
This assignment is to be done INDIVIDUALLY. Working together is not permitted.
You may discuss general concepts but are not to even look at each others' code.
Your name on your program is your pledge that the program is your own work.
If you receive help from anyone else it must be documented at the beginning
of your program.
Assignment: Design and implement a class, PostorderIterator, which
implements a post-order Iterator, but does not use any container
for storing the tree. The file name is PostorderIterator.java.
You will also implement a class, extendBT, given below.
The purpose of extendBT is to provide a way to call your
postorder iterator.
Traversals are covered in Section 10.5 of Bailey, pages 201
- 210. Bailey's Iterators store tree information in a stack
(or queue or vector) to make the nextElement() method easier
to write. Since you have the parent reference available, the
Iterator does not need any information except a reference to
the root of the tree and a reference to the current BinaryTreeNode.
Your constructor will be similar to Bailey's in that you
will pass the root of the BinaryTree (a reference to a BinaryTreeNode)
to the constructor. But, you don't load the tree information into
any other data structure. Space requirements of the iterator are O(1),
storing only the root and the current reference.
Iterator methods must be of best-case complexity. That is, current
always references the BinaryTreeNode from which nextElement() extracts
the Object to be returned. The only times that root is used are in the
constructor and in the reset() method.
Note: This is a somewhat difficult assignment -- a good test of your
problem solving and ability to design algorithms. It is unlikely
that everyone will complete this assignment correctly.
The extendBT class (required):
import structure.BinaryTree;
import structure.Iterator;
public class extendBT extends BinaryTree
{
public Iterator postorderElements( )
// over-rides postorderElements method in BinaryTree class
{
return new PostorderIterator ( root );
}
}
Outline of PostorderIterator class:
import structure.Iterator;
import structure.BinaryTree;
import structure.BinaryTreeNode;
public class PostorderIterator implements Iterator
{
protected BinaryTreeNode root;
protected BinaryTreeNode current;
public PostorderIterator ( BinaryTreeNode root )
// post: constructs an iterator to traverse in postorder
{
...
}
...
public static void main ( String [] args )
{
// if you create a binary tree named myBT and insert information
extendBT myBT = new extendBT();
...
// then you invoke the postorderElements method of the
// extendBT class to return an Iterator. BinaryTree code is at
// http://www.mhhe.com/engcs/compsci/bailey/
Iterator it1 = (Iterator) myBT.postorderElements();
}
}
Grading Rubric:
5 pts. reasonable attempt to solve problem as assigned
5 pts. also compiles
5 pts. style standards (includes clarity)
The following 50 points are available only for code that compiles
and runs without error.
20 pts. quality of your algorithms
5 pts. quality of your testDriver
10 pts. works for our testDriver