Programming Assignment
Due:
Please make sure you adhere to the policies on
academic honesty.
Please see the general programming webpage for details about
the programming environment for this course, and specifically for
directions in how to submit your programming assignment electronically.
The files you need for this assignment can be
downloaded here.
Contents:
Your goal is to implement (2,4) trees, supporting insertion,
deletion and searching. Chapter 9.4 of the text gives a high-level
description of all of these operations. Your job will be to turn
these ideas into a real program. Implementing deletion will be
considered extra credit - worth 2 points.
Your program will need to make use of the following types of
structures:
- TFTree - (the ``TF''stands for Two-Four).
This is an interface for representing the overall (2,4) tree
structure. Rather than using the general tree interface, we will
design a special representation suited particularly for (2,4) trees.
You must implement this interface in a file
TFImplementation.java, which is the only file you should modify or
submit. The tree will be a linked structure, where each node of the
tree (including external nodes) will be represented with an object
from class TFNode (defined below). The methods which you will
have to support are the following:
- int size();
returns the number of keys stored in the tree (not to be
confused with the number of nodes)
- boolean isEmpty();
returns true if the tree contains zero keys, and false otherwise
(again, not to be confused with the number of nodes).
- TFNode getRoot();
returns the root node of the tree.
- void insertItem(int key);
This routine is to insert the integer `key' into the tree
(there will not be any other associated `element' for this program).
- boolean find(int key);
This returns true if `key' is found in the tree, and false otherwise.
- boolean remove(int key); // EXTRA CREDIT
This routine is asked to delete an item `key' from the tree. If no
such `key' is found in the tree, nothing is removed. If there happen
to be multiple items in the tree equal to the value `key' your routine
is free to remove an arbitrary such `key'. The routine is to return
true if it successfully found and deleted an item, or false if no such
item was found.
- TFNode -
This class is used to represent a single node which is part of a
(2,4) tree. In order to be able to represent a node which may have a
variable number of keys and children, we will make use of the
VectorADT as defined in Chapter 5.1.1 of the text.
Specifically, the class TFNode defines the following methods:
- TFNode getParent() - returns reference to node's parent
(null if its the root).
- void setParent(TFNode newParent) - modifies the parent
link.
- boolean isInternal() - an internal node has one or more
children
- boolean isExternal() - an external node has no children
- VectorADT getKeys() - returns a VectorADT containing
the keys, starting with the leftmost key at rank 0. The keys in the
vector are represented using the Integer object defined by
Java.
- VectorADT getChildren() - returns a vectorADT of the children,
starting with the leftmost child at rank 0. The Object's on
this list should be TFNode's, references to the children nodes.
To modify the parent, you will use the setParent method. If you
wish to modify a node's keys or children, you will use the
getKeys or getChildren methods to get a reference to the
VectorADT. At that point, you can effectively modify the contents of
the vector through that ADT's methods.
The constructor for TFNode returns a node with an empty VectorADT of keys
and an empty VectorADT of children. Such a node should be used to
represent an external node in the Two-Four tree. In general, an
internal node will have a number of children which is exactly
one more than the number of keys.
- VectorADT - We will provide you with a working
implementation of the VectorADT interface as defined in
Chapter 5.1.1. Please note that the method names are not the same as
with java.util.Vector.
- Integer's - As we have seen, our general data structures
that deal with Object's in Java cannot actually store basic data
types such as int's which we are using as keys. Instead, Java
has predefined several wrapper classes which can be used. You may
read the full class definition in the Java documentation, but
essentially you need only concern yourself with two functions:
- If you have a variable, int value, and you want to create
a new Integer with that value, you may type something such as:
Integer i = new Integer(value);
- If you have a variable, Integer i, and you want to know
what value it represents (for instance to compare to another value),
you can type something such as:
int value = i.intValue();
We will be providing you with two different drivers which you may use
in developing and testing your data structures.
The first is a ``no frills'' text-based driver (TFDriver.java)
which will simply offer a menu which lets you reinitialize a tree,
search in a tree, or insert or delete items. The only output it will
give is an echo of any return values provided by the operations. As
we have done in the past, the default for this driver is to read
commands from the keyboard. However, the program will accept an
optional command line argument which is the name of a file to be used
for specifying commands rather than the keyboard. The main advantage
of this is that it allows you, when debugging, to type in a long
combination of commands into a file, and then to run your program
repeatedly on this set of commands (as opposed to having to type in
all the commands from scratch each time you retest your program)
The second driver we are providing is an applet (TFApplet.java)
which can display a picture of your tree. Specifically, the applet
will redraw the entire tree once, after each insertion or deletion is
complete. It does not, by default, draw your tree in any of the
intermediate stages which take place from within your routines. (see
below for a way to do this!)
This applet should be a great tool for you in developing your program
as it allows you to see a view of your data structure. Of course,
this view may show that your data structure is wrong or that you have
not set pointers up correctly. If the picture does not look like you
think it should have, then there is almost certainly a problem with
your tree structure. If your program works correctly, you should be
able to get pictures like those given in the book.
As mentioned earlier, the applet only redraws the tree each time an
insertion or deletion is completed. However it might be interesting
if you are able to see a picture of your data structure at various
points from within your code. This may help when developing your
program, and it may make a more interesting animation when complete.
We are providing you with a special function,
Animation.Checkpoint() which provides exactly this feature. If you
place a call to this method at various places within your code, and
then you run the applet, the following behavior will occur. At each
such call, the applet will redraw your tree in its current state, and
then it will force a pause to the entire process, waiting for the user
to click a button to continue. The text-based driver completely
ignores these calls.
- We make the strongest possible recommendation that you design
your programs making good use of subroutines. That is, the text
describes operations such as search, split,
transfer, and fusion. Separating out the ugly work into
separate routines will make your program much easier to understand and
debug. Do not use duplicate code in multiple places.
- Treat external nodes carefully. Although they seem unimportant,
they are there to help standardize the rest of your code. Please make
sure that you incorporate these nodes properly.
- Whenever you create or change a parent-child relationship, make
sure that you update both the parent's child pointer and the child's
parent pointer.
- You may need to be exceptionally careful with special cases such
as inserting a key into an empty tree or dealing properly with
duplicate keys.
- Test your code by comparing your results to the sequence of
operations shown in Figures 9.16-9.17 of the text. Insert: 4, 6, 12,
15, 3, 5, 10, 8, 11, 13, 14, 17; if you implement the extra credit,
continue by removing: 4, 12 (using predecessor), 13, 14, and compare
with Figures 9.18-9.19.
The files you need for this assignment can be
downloaded here.
You should submit:
- TFImplementation.java
- readme - a brief summary of your program, and
any further comments you wish to make to the grader. If you do the
extra credit, please make this clear.
If you define any additional classes, please submit the
additional source code.
Last modified: 15 November 2002