Skip to content

Commit d882deb

Browse files
committed
pqueue-heap
1 parent 0a2f01e commit d882deb

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

Assignment5/pqueue-heap.cpp

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*************************************************************
2+
* File: pqueue-heap.cpp
3+
*
4+
* Implementation file for the HeapPriorityQueue
5+
* class.
6+
*/
7+
8+
#include "pqueue-heap.h"
9+
#include "error.h"
10+
11+
HeapPriorityQueue::HeapPriorityQueue() {
12+
capacity = INITIAL_CAPACITY;
13+
count = 0;
14+
array = new string[capacity];
15+
}
16+
17+
HeapPriorityQueue::~HeapPriorityQueue() {
18+
delete[] array;
19+
}
20+
21+
int HeapPriorityQueue::size() {
22+
return count;
23+
}
24+
25+
bool HeapPriorityQueue::isEmpty() {
26+
return count == 0;
27+
}
28+
29+
void HeapPriorityQueue::enqueue(string value) {
30+
if (count == capacity) expandCapacity();
31+
array[count] = value;
32+
count++;
33+
if (count > 1) bubbleup(count);
34+
}
35+
36+
string HeapPriorityQueue::peek() {
37+
if (isEmpty()) error("peek: Attempting to peek into an empty pqueue");
38+
return array[0];
39+
}
40+
41+
string HeapPriorityQueue::dequeueMin() {
42+
if (isEmpty()) error("dequeueMin: Attempting to dequeue and empty pqueue");
43+
string first = array[0];
44+
array[0] = array[count - 1];
45+
count--;
46+
if (count > 1) bubbledown(1); // bubbledown first element
47+
return first;
48+
}
49+
50+
void HeapPriorityQueue::expandCapacity() {
51+
string *oldArray = array;
52+
capacity *= 2;
53+
array = new string[capacity];
54+
for (int i = 0; i < count; i++) {
55+
array[i] = oldArray[i];
56+
}
57+
delete[] oldArray;
58+
}
59+
60+
void HeapPriorityQueue::bubbleup(int index) {
61+
if (array[index - 1] < array[(index / 2) - 1]) {
62+
string temp = array[index - 1];
63+
array[index - 1] = array[(index / 2) - 1];
64+
array[(index / 2) - 1] = temp;
65+
if (index / 2 - 1 >= 1) bubbleup(index / 2);
66+
}
67+
}
68+
69+
void HeapPriorityQueue::bubbledown(int index) {
70+
if (count > (2 * index) - 1 && array[index - 1] > array[(2 * index) - 1]) {
71+
string temp = array[index - 1];
72+
array[index - 1] = array[(2 * index) - 1];
73+
array[(2 * index) - 1] = temp;
74+
bubbledown(2 * index);
75+
}
76+
77+
if (count > 2 * index && array[index - 1] > array[2 * index]) {
78+
string temp = array[index - 1];
79+
array[index - 1] = array[2 * index];
80+
array[2 * index] = temp;
81+
bubbledown(2 * index + 1);
82+
}
83+
}

Assignment5/pqueue-heap.h

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**********************************************
2+
* File: pqueue-heap.h
3+
*
4+
* A priority queue class backed by a binary
5+
* heap.
6+
*/
7+
#ifndef PQueue_Heap_Included
8+
#define PQueue_Heap_Included
9+
10+
#include <string>
11+
using namespace std;
12+
13+
/* A class representing a priority queue backed by an
14+
* binary heap.
15+
*/
16+
class HeapPriorityQueue {
17+
public:
18+
/* Constructs a new, empty priority queue backed by a binary heap. */
19+
HeapPriorityQueue();
20+
21+
/* Cleans up all memory allocated by this priority queue. */
22+
~HeapPriorityQueue();
23+
24+
/* Returns the number of elements in the priority queue. */
25+
int size();
26+
27+
/* Returns whether or not the priority queue is empty. */
28+
bool isEmpty();
29+
30+
/* Enqueues a new string into the priority queue. */
31+
void enqueue(string value);
32+
33+
/* Returns, but does not remove, the lexicographically first string in the
34+
* priority queue.
35+
*/
36+
string peek();
37+
38+
/* Returns and removes the lexicographically first string in the
39+
* priority queue.
40+
*/
41+
string dequeueMin();
42+
43+
private:
44+
static const int INITIAL_CAPACITY = 10;
45+
46+
// Instance variables
47+
string *array;
48+
int count;
49+
int capacity;
50+
51+
// Private method prototypes
52+
void expandCapacity();
53+
void bubbleup(int index);
54+
void bubbledown(int index);
55+
};
56+
57+
#endif

0 commit comments

Comments
 (0)