Sunteți pe pagina 1din 2

Data Structures Assignment 4

(2-4 Tree)
Due: Oct 29, 2010

FAQ

You need to implement a 2-4 tree. In this assignment you will also learn about maintaining your own "pointer" system. You only need to build a 2-4 tree to hold integers. What is unusual is that you may not use any reference variables (including System.out) in your class Two4Tree (i.e., in the file Two4Tree.java). Thus you may not use any external methods either.
public class Two4Tree { public Two4Tree(int numNodes) { /* = maximum number of nodes to support */ ... } public boolean insert(int value) { /* return false if no more space */ ... } public int deleteAll(int value) { /* delete all occurrences of value, return the count */ ... } public int search(int value) { /* return number of occurrences of value */... } ... private nodesArray[]; ... } In effect, you need to yourself allocate space for nodes inside the internal array nodesArray. Thus all references are in the form of indices to locations within nodesArray. In other words, you store the values as well as "indices" of parent/child/sibling nodes in nodesArray. Keep all

integers belonging to a node contiguously in the array. To keep things simple, you do not need to implement full-fledged node allocation and deallocation routines. For allocation you may use the lowest unused index so far. Deallocation is not needed. Garbage collection is not required to be implemented. Your main class TestTree in file TestTree.java, will read input from the console and write output to console. The input consists of a sequence of insertions followed by a sequence of search and delete operations. The format is as follows (again, variables are in bold and all are integers):
numInsertions

followed by numInsertions lines of the form:


value

followed by any number of lines, each of which are in one of these two formats
FIND value DELETE value Delete deletes

all ocurrences of the value by deleting them in order. It is equivalent to performing deletion (and any tree adjustment) in a loop until there is no more to delete. The output of the program will report the results of the operations along with a few tree statistics as follows. For insertion:
INSERTED value AT LEVEL level AT INDEX index numSplits SPLITS

For search:
FOUND numFound OCCURRENCES OF value

And for deletions:


DELETED numDeleted OCCURRENCES OF value numMerges MERGES

Note the root is at level 0. The index is of the node in which the value first went (before any splits). For consistency, you borrow from your left sibling if possible. If not, you borrow from your right sibling. If that is also not possible, you merge with you left sibling if it exists, and with your right sibling otherwise. Also, different occurrences of an integer are to be treated as different and stored separately as two different values in the same or different nodes.

Example
WITH INPUT:
1 100 FIND 100 DELETE 100

OUTPUT IS:
INSERTED 100 AT LEVEL 0 AT INDEX 0 0 SPLITS FOUND 1 OCCURRENCES OF 100 DELETED 1 OCCURRENCES OF 100 0 MERGES

S-ar putea să vă placă și