07.08.2013 Views

CS 2420 ? Fall 2010 Priority Queues (Heaps) Priority Queue

CS 2420 ? Fall 2010 Priority Queues (Heaps) Priority Queue

CS 2420 ? Fall 2010 Priority Queues (Heaps) Priority Queue

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Objective: Implement a priority queue.<br />

<strong>Priority</strong> <strong>Queue</strong><br />

<strong>CS</strong> <strong>2420</strong> – <strong>Fall</strong> <strong>2010</strong><br />

Program 5 – 20 points<br />

<strong>Priority</strong> <strong><strong>Queue</strong>s</strong> (<strong>Heaps</strong>)<br />

A max priority queue is a data structure that allows at least two operations: insert which does the<br />

obvious thing and deleteMax, that finds, returns, and removes the maximum element in the<br />

priority queue. The max priority queue will be implemented as a binary heap (a table which<br />

logically represents an almost complete binary tree).<br />

The data you will use to test this priority queue is the concordance you produced in program 4.<br />

If you didn’t complete that program, you can just input a copy of the output file from program 4<br />

(prog5.txt). Make sure your array is large enough to hold all the words.<br />

You are to insert all the data (word and occurrenceCt) into the priority queue and then delete<br />

them one at a time (to produce a sorted list). (This is actually an unimpressive way of using a<br />

priority queue, as the priority queue can handle interleaved insertions and deletions.)<br />

The output will be the entire concordance with the most commonly used word(s) and number of<br />

occurrences listed first, and the least commonly used word(s) and number of occurrences listed<br />

last. Be sure to send the output to a file, prog5out.txt.<br />

When you have completed the assignment, zip your entire project and submit the zip file to<br />

Eagle.<br />

Hints:<br />

While combining this program with your hash table code is "logically appealing", just reading in<br />

prog5.txt is simpler.<br />

prog5.txt is HUGE. We need to see output created from this file, but test your program with a<br />

very small file.<br />

To do reasonable debugging, I like to have every data structure know how to print itself. In<br />

particular, the entries in the queue need to be able to print themselves. In Java, a function which<br />

prints the data structure is called "toString" – so I use that convention. In C++, being able to<br />

create a string from various data types is not easy. Below is a clever trick to accomplish this<br />

goal. Basically, you create a string stream which acts like a stream (using regular


class ItemType<br />

{ public:<br />

string word;<br />

int occurrenceCt;<br />

int key;<br />

ItemType(string aword = "", int ct=0) { word = aword; occurrenceCt=ct; key = ct;}<br />

string toString() {<br />

stringstream out;<br />

out

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!