Anda di halaman 1dari 3

Data Structures Assignment 3

(Heaps)
________________________________________
This assignment is designed to get you comfortable with heaps and tree traversals.
The commonwealth games is round the corner and you would like to develop some
simple software which will help you keep track of statistics about Athletic events. We
would like to keep a database of athletes and their scores in various athletic events.
Assume that there is some standardized scoring mechanism for different athletic events
so that we can compare the performance of athletes across different events (e.g., if A gets
a score 7 in event E1 and B gets a score 8 in event E2, then B has better performance than
A). For each individual sports event, we would like to have quick access to the details of
best sportsperson in that event. We would also like to have quick access to the details of
the best sportsperson across all the athletic events. For this we maintain a heap of heaps
which is explained next.

Heap of Heaps:
For each individual athletic event like 100m, 200m etc., we maintain a max heap where
the key is the standardized score and the value is the details about the athlete. We
maintain another max heap where each node corresponds to an athletic event. For each
node, the key is the best score of an athlete in that event and the value is the max heap for
that event. We would like to perform the following operations on our data structure.

1. removeMaxFromEvent(event_id): Remove and return the athlete with the best


score in the event denoted by event_id. The athlete should be removed only from the
heap corresponding to this event. The athlete remains in the heap corresponding to all
other events he participates in.
2. removeMax(): Remove and return the athlete with the best score across all events.
Suppose this athlete’s best performance is in event E, then this athlete should be removed
from the heap corresponding to event E and not from any other event heaps.
3. insertAthlete(athlete): Insert the information about an athlete into our data
structure.

Note that when you add an athlete into an event heap you also have to update the top-
level heap (the heap of event heaps). The same is true for all the other operations.

Following are the main classes you should write. Figure out if you need to write any
other classes or methods.

class Athlete{
String Name;
String Country;
int Age;
double height;
double weight;
int NumEvents;
String EventID[] ;
int score[];
}

Class myHeap{
...
insert(){...}
RemoveMax();
}

Class HeapsOfHeaps{
...
RemoveMaxFromEvent(){...}
RemoveMax(){...}
InsertAthlete(){...}
PrintHeap(){...}
}

Your class HeapOfHeaps should support an operation PrintHeap which basically prints
the entire heap by traversing it in pre-order. This means that we do a pre-order traversal
of the top level heap and whenever there is a visit operation for any node, we do a pre-
order traversal of the heap associated with that node.

Any inconsistency in the input file should be detected and reported. Your program should
print ERROR IN INPUT FILE and abort.
If any of the heaps is empty when a RemoveMax operation is performed, an appropriate
exception should be thrown. This exception should be handled and the program should
print
EMPTY HEAP EXCEPTION.

Example of an input file:

INSERT Usain Bolt, Jamaica, 24, 1.95, 93.9, 3, 100M, 200M, 400M, 9, 8, 7
INSERT Asafa Powell, Jamaica, 23, 1.90, 93, 3, 100M, 200M, 400M, 8, 7, 6
INSERT Ivan Sergeyevich Ukhov, Russia, 24, 1.92, 83, 1, HIGHJUMP, 10
REMOVEMAX HIGHJUMP
REMOVEMAX HIGHJUMP
REMOVEMAX
PRINTHEAP
INSERT Steven Hooker, Australia, 28, 1.87, 82, 1, POLEVAULT, 15
PRINTHEAP

For each athlete, you are given the name, country, age, height (in meters), weight (in
kgs), the number of events he participates in, the event ids and the standardized score on
these events respectively.
Construct all the event heaps and the heap of event heaps top-down, i.e., insert an
element into the heap as soon as you see the element in the input file. For example, for
the following first input line:
Usain Bolt, Jamaica, 24, 1.95, 93.9, 3, 100M, 200M, 400M, 9, 8, 7
You should insert this athlete in the 100M, 200M, and 400M heaps and then insert these
heaps (in that order into the top level heap).

You should write a program named Simulate.java. The input file is given on the
command line, i.e., you will run
java Simulate <inputfilename>
Your program should produce the output on standard output.

For the above input file, your program should print the following:

Ivan Sergeyevich Ukhov, Russia, 24, 1.92, 83, 1, HIGHJUMP, 10


EMPTY HEAP EXCEPTION
Usain Bolt, Jamaica, 24, 1.95, 93.9, 3, 100M, 200M, 400M, 9, 8, 7
Asafa Powell, Jamaica, 23, 1.90, 93, 3, 100M, 200M, 400M, 8, 7, 6
Usain Bolt, Jamaica, 24, 1.95, 93.9, 3, 100M, 200M, 400M, 9, 8, 7
Asafa Powell, Jamaica, 23, 1.90, 93, 3, 100M, 200M, 400M, 8, 7, 6
Usain Bolt, Jamaica, 24, 1.95, 93.9, 3, 100M, 200M, 400M, 9, 8, 7
Asafa Powell, Jamaica, 23, 1.90, 93, 3, 100M, 200M, 400M, 8, 7, 6
Steven Hooker, Australia, 28, 1.87, 82, 1, POLEVAULT, 15
Asafa Powell, Jamaica, 23, 1.90, 93, 3, 100M, 200M, 400M, 8, 7, 6
Usain Bolt, Jamaica, 24, 1.95, 93.9, 3, 100M, 200M, 400M, 9, 8, 7
Asafa Powell, Jamaica, 23, 1.90, 93, 3, 100M, 200M, 400M, 8, 7, 6
Usain Bolt, Jamaica, 24, 1.95, 93.9, 3, 100M, 200M, 400M, 9, 8, 7
Asafa Powell, Jamaica, 23, 1.90, 93, 3, 100M, 200M, 400M, 8, 7, 6

Anda mungkin juga menyukai