Assignment 5: Priority Queues - Data Structures, Algorithms | COP 4530, Assignments of Computer Science

Material Type: Assignment; Class: DATA STR ALG GEN PRO; Subject: COMPUTER PROGRAMMING; University: Florida State University; Term: Fall 2006;

Typology: Assignments

Pre 2010

Uploaded on 08/30/2009

koofers-user-84c
koofers-user-84c 🇺🇸

5

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
COP 4530/CGS5425: Data Structures, Algorithms, and Generic Programming
Florida State University, Dept. of Comp. Sci., Fall 2006
Instructor: Breno de Medeiros
TA: Ling Toh
Assignment 5: Priority Queues
Goals:
In this assignment, you will be required to implement a template class for a priority
queue object, i.e. you are required to complete the implementation for the
Priority_Queue class. You will also be required to implement the heap sort
algorithm on the priority queue to rearrange the items of a container in descending order.
Requirements for Priority_Queue class
A partial interface and implementation of the Priority_Queue class is provided in the
distributed priority_queue.h files.
The underlying Priority_Queue data structure is any container type that has its
operator [] overloaded.
A Priority_Queue object could be declared as ,
Priority_Queue <vector<int> > Q1; //underlying vector stores integer values
//underlying deque stores ch aracter values, and uses the greater function
//object for comparison
Priority_Queue <deque<char>, greater<char> > Q2;
//underlyingBasicContainer s tores floating-point valu es and uses the less
//function object for compar ison
Priority_Queue <BasicContain er<float>, less<float> > Q3;
The partially distributed priority_queue.h file contain the following implementations:
template <typename T>
class LessThan
{
public:
bool operator()( const T &x, const T &y ) { return x < y;}
};
template <class C, class P = class LessThan<typename C::value_type> >
class Priority_Queue
{
public:
typedef typename C::value_type value_type;
pf3
pf4

Partial preview of the text

Download Assignment 5: Priority Queues - Data Structures, Algorithms | COP 4530 and more Assignments Computer Science in PDF only on Docsity!

COP 4530/CGS5425: Data Structures, Algorithms, and Generic Programming

Florida State University, Dept. of Comp. Sci., Fall 2006

Instructor: Breno de Medeiros

TA: Ling Toh

Assignment 5: Priority Queues

Goals:

In this assignment, you will be required to implement a template class for a priority queue object, i.e. you are required to complete the implementation for the Priority_Queue class. You will also be required to implement the heap sort algorithm on the priority queue to rearrange the items of a container in descending order.

Requirements for Priority_Queue class

A partial interface and implementation of the Priority_Queue class is provided in the distributed priority_queue.h files. The underlying Priority_Queue data structure is any container type that has its operator [] overloaded. A Priority_Queue object could be declared as , Priority_Queue <vector > Q1; //underlying vector stores integer values //underlying deque stores character values, and uses the greater function //object for comparison Priority_Queue <deque, greater > Q2; //underlyingBasicContainer stores floating-point values and uses the less //function object for comparison Priority_Queue <BasicContainer, less > Q3; The partially distributed priority_queue.h file contain the following implementations: template class LessThan { public: bool operator()( const T &x, const T &y ) { return x < y;} }; template <class C, class P = class LessThan > class Priority_Queue { public: typedef typename C::value_type value_type;

void PushHeap(const value_type& value) { c.push_back(value); RestoreHeap(c.size()-1); } void PopHeap() { swap(c[0], c[c.size()-1]); c.pop_back(); RestoreHeap(0); } protected: C c; P Compare; }; The full implementation of the priority_queue.h file MUST contain at least the following declarations: Priority_Queue(): Constructor for the Priority_Queue object. Priority_Queue(const C& container): Constructor for the Priority_Queue object that initializes the container type c to container. In addition, this method should also call the MakeHeap method to ensure that c adheres to its heap property. void Push(const value_type& value): Adds item of type value to priority queue. This method should call the PushHeap method to add item to the underlying container and maintain its heap property. void Pop(): Removes the item from the top of the priority queue. This method should call the PopHeap method to remove the item from the underlying container and maintain its heap property. unsigned int Size(): Returns the size of the priority queue. bool Empty(): Returns true if the priority queue is empty and false otherwise. value_type& Top(): Returns the element located at the top of the priority queue. void Display(std::ostream&, char ofc = '\0') const: Displays all the elements in the priority queue. void MakeHeap(): Rearranges the elements within the container c, in-place so as to maintain the heap property of the priority queue. Must have cost at most O(N). In particular, using generic sorting algorithms such as quicksort, which has cost O(N log N), will be marked down for inefficiency.

  1. fucntionTest.cpp : general functionality test for Priority_Queue object.
  2. fpq.cpp: partial implementation.

Deliverables

  1. Your implementation must be entirely contained in the following files, which MUST be named, a. priority_queue.h b. fpq.cpp
  2. Your program must compile on linprog.cs.fsu.edu or program.cs.fsu.edu. If you program does not compile on either machines, the grader cannot test your submission. To compile in program.cs.fsu.edu, use the commands: g++ - I. fpq.cpp g++ - I. functionTest.cpp Points will be deducted for not complying with these requirements. For instructions on how to submit your project, consult the first assignment, which is posted online. Remember to substitute "4" for "1" in all the command and path names which have the form "projectX" or "projX