Summary: In this assignment, you will implement trees with accompanying iterators.
Note: This assignment is optional. Do it only if you feel it will significantly impact your learning or that you have previous poor grades that you need to ameliorate. If you do this assignment, please note that I've given you a long time for this assignment, so don't wait until the last moment to begin it.
Collaboration: You can work on this assignment in groups of up to size three. You may discuss your design with any size group. You may also work with each other on general debugging issues.
If you work as part of a group, you are responsible for ensuring that all members of the group understand all sections of the assignment.
Implement a cursor-based Tree class that supports the
following methods
Tree() -- Create a new tree with no elements.
void reset() -- Move the cursor to the root of the tree,
if there is one.
void down(int childnum) -- Move the cursor down to one of
the subtrees, given that the subtree exists.
void up() -- Move the cursor up in the tree, providing
there is a way to go up.
Object getContents() -- Get the contents of the current node,
if there is one.
Object getArity() -- Get the arity of the current node,
if there is one.
Iterator elements(emphasis, order, direction) --
returns an interator that traverses the tree starting at the root,
using the desired
priority (depth-first or breadth-first), order (preorder or postorder),
and direction (left-to-right or right-to-left).
void setArity(int arity) -- Set the arity of the current
node.
void setChild(int childnum, Object value) -- Set one of the
children of the current node to a new leaf with the given value.
void setContents(Object value) -- Set the contents of the
current
node to the given value. If the tree is empty, sets the value of the
root node to the given value.
void removeChild(int childnum) -- Remove one of the children
of a node.
Demonstrate that your traversal methods work by building an appropriate tree and showing the results of each traversal method.
Note that you will most likely need to implement a TreeNode
class to use as the basis of your Tree class.
For extra credit, develop a print method that prints the
tree in a nice, two-dimensional, fashion.
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Tue Nov 25 10:25:14 1997.
This page generated on Tue Nov 25 10:26:59 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu