|
|
@@ -29,21 +29,24 @@ public:
|
|
|
void PriorityQueue::initiateHeap(int capacity){
|
|
|
heap_size = 0;
|
|
|
max_size = capacity;
|
|
|
- heapArray = new int[max_size]; // try to change this to vector... please!!!
|
|
|
+ heapArray = new int[max_size];
|
|
|
}
|
|
|
|
|
|
-void PriorityQueue::insert(int newNode){
|
|
|
+void PriorityQueue::insert(int key){
|
|
|
if(heap_size == max_size){
|
|
|
std::cout << "PriorityQueue::insert called on full priority queue" << std::endl;
|
|
|
return;
|
|
|
}
|
|
|
+ // heap starts at index 1 so when I want to insert a new node, it should be at the
|
|
|
+ // nex index?
|
|
|
+ int keyIndex = heap_size + 1;
|
|
|
// heapify up until the heap properties are restored
|
|
|
- heapifyUp(newNode, heap_size);
|
|
|
+ heapifyUp(key, keyIndex);
|
|
|
}
|
|
|
|
|
|
void PriorityQueue::removeMax(){
|
|
|
// check if heap is empty
|
|
|
- if (heap_size <= 0) {
|
|
|
+ if (heap_size <= 1) {
|
|
|
std::cout << "PriorityQueue::removeMax called on an empty priority queue" << std::endl;
|
|
|
return;
|
|
|
}
|
|
|
@@ -52,18 +55,18 @@ void PriorityQueue::removeMax(){
|
|
|
heap_size--;
|
|
|
} else {
|
|
|
// remove the max value (at root)
|
|
|
- heapArray[0] = heapArray[heap_size - 1];
|
|
|
+ heapArray[1] = heapArray[heap_size + 1];
|
|
|
heap_size--;
|
|
|
// heapify down until the heap properties are restored
|
|
|
- heapifyDown(0);
|
|
|
+ heapifyDown(1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void PriorityQueue::removeKey(int key){
|
|
|
- for (int i = 0; i >= heap_size; i++) {
|
|
|
+ for (int i = 1; i <= heap_size; i++) {
|
|
|
if (heapArray[i] == key) {
|
|
|
// change the key at correct index into something larger than maxValue
|
|
|
- heapArray[i] = heapArray[0] + 10;
|
|
|
+ heapArray[i] = heapArray[1] + 10;
|
|
|
heapifyUp(heapArray[i], i);
|
|
|
removeMax();
|
|
|
return;
|
|
|
@@ -73,20 +76,13 @@ void PriorityQueue::removeKey(int key){
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
-void PriorityQueue::change(int *key, int *newK){
|
|
|
- for (int i = 0; i >= heap_size; i++) {
|
|
|
+void PriorityQueue::change(int *key, int *newKey){
|
|
|
+ for (int i = 1; i <= heap_size; i++) {
|
|
|
if (heapArray[i] == *key) {
|
|
|
- // if the key is found exchange the old key for the new
|
|
|
- heapArray[i] = *newK;
|
|
|
-
|
|
|
- // figure out the case when heapify up/down are needed
|
|
|
- if (*key > *newK) {
|
|
|
- heapifyUp(heapArray[i], i);
|
|
|
- return;
|
|
|
- } else if (*key < *newK) {
|
|
|
- heapifyDown(i);
|
|
|
- return;
|
|
|
- }
|
|
|
+ int temp = *newKey;
|
|
|
+ *newKey = *key;
|
|
|
+ *key = temp;
|
|
|
+ return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -94,43 +90,44 @@ void PriorityQueue::change(int *key, int *newK){
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
-void PriorityQueue::heapifyDown(int node){
|
|
|
- int lBranchValue = 2*node;
|
|
|
- int rBranchValue = 2*node + 1;
|
|
|
+void PriorityQueue::heapifyDown(int index){
|
|
|
+ int lBranchIndex = 2*index;
|
|
|
+ int rBranchIndex = 2*index + 1;
|
|
|
|
|
|
- int largerNode = node;
|
|
|
+ int largerKeyIndex = index;
|
|
|
|
|
|
- if (lBranchValue < heap_size && heapArray[lBranchValue] > heapArray[node]) {
|
|
|
- largerNode = lBranchValue;
|
|
|
+ if (lBranchIndex < heap_size && heapArray[lBranchIndex] > heapArray[index]) {
|
|
|
+ largerKeyIndex = lBranchIndex;
|
|
|
}
|
|
|
|
|
|
- if (rBranchValue < heap_size && heapArray[rBranchValue] > heapArray[largerNode]) {
|
|
|
- largerNode = rBranchValue;
|
|
|
+ if (rBranchIndex < heap_size && heapArray[rBranchIndex] > heapArray[largerKeyIndex]) {
|
|
|
+ largerKeyIndex = rBranchIndex;
|
|
|
}
|
|
|
|
|
|
- if (largerNode != 1) {
|
|
|
- change(&heapArray[node], &heapArray[largerNode]);
|
|
|
- heapifyDown(largerNode);
|
|
|
+ if (largerKeyIndex != index) {
|
|
|
+ change(&heapArray[index], &heapArray[largerKeyIndex]);
|
|
|
+ heapifyDown(largerKeyIndex);
|
|
|
+ //heap_size--;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void PriorityQueue::heapifyUp(int key, int index){
|
|
|
int curIndex = index;
|
|
|
- int parentIndex = (curIndex-1)/2;
|
|
|
+ int parentIndex = (curIndex)/2;
|
|
|
heap_size++;
|
|
|
// insert new key into the end of the array
|
|
|
heapArray[curIndex] = key;
|
|
|
|
|
|
- while (curIndex != 0 && heapArray[parentIndex] < heapArray[curIndex]) {
|
|
|
+ while (curIndex != 1 && heapArray[parentIndex] < heapArray[curIndex]) {
|
|
|
change(&heapArray[curIndex], &heapArray[parentIndex]);
|
|
|
- curIndex = (curIndex-1)/2;
|
|
|
- parentIndex = (parentIndex-1)/2;
|
|
|
+ curIndex = (curIndex)/2;
|
|
|
+ parentIndex = (parentIndex)/2;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void PriorityQueue::printArray(){
|
|
|
- for (int i = 0; i >= heap_size; i++) {
|
|
|
- std::cout << heapArray[i];
|
|
|
+ for (int i = 1; i <= heap_size; i++) {
|
|
|
+ std::cout << heapArray[i] << " , ";
|
|
|
}
|
|
|
std::cout << std::endl;
|
|
|
}
|