Преглед изворни кода

fixed a number of class methods which had minor bugs, especially in the for loops. Heapify down having some issues with zeros not disappearing as they should.

Natalie Pueyo пре 7 година
родитељ
комит
1e94963547
4 измењених фајлова са 43 додато и 45 уклоњено
  1. 2 2
      program2/Makefile
  2. 35 38
      program2/priorityqueue.cpp
  3. 3 3
      program2/priorityqueue.h
  4. 3 2
      program2/testCode.cxx

+ 2 - 2
program2/Makefile

@@ -19,7 +19,7 @@ PRIORITY_DEV_OBJ=$(PRIORITY_SOURCES:.cpp=.o)
 PRIORITY_OPT_OBJ=$(PRIORITY_SOURCES:.cpp=.o3)
 
 .PHONY: all
-all: priority_cla $(CREATE_DATA_EXE) $(TEST_DATA_EXE) 
+all: priority_cla $(CREATE_DATA_EXE) $(TEST_DATA_EXE)
 
 # Priority Queue
 .PHONY: $(PRIORITY_CLA)
@@ -37,7 +37,7 @@ $(PRIORITY_OPT_OBJ): %.o3: %.cpp %.h
 
 # test code executable
 $(TEST_DATA_EXE): %.exe: %.cxx $(PRIORITY_OPT_OBJ)
-	$(CC) $(OPT) $< -o $@
+	$(CC) $(OPT) $^ -o $@
 
 
 # Build

+ 35 - 38
program2/priorityqueue.cpp

@@ -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;
 }

+ 3 - 3
program2/priorityqueue.h

@@ -24,13 +24,13 @@ public:
 };
 
 
-void insert(int newNode);
+void insert(int key);
 void removeMax();
 void removeKey(int key);
-void change(int* key, int* newK);
+void change(int* key, int* newKey);
 // helpful functions
 void heapifyUp(int key, int index);
-void heapifyDown(int node);
+void heapifyDown(int index);
 //nlohmann::json JSON();
 // other required functions (for now)
 void initiateHeap(int capacity);

+ 3 - 2
program2/testCode.cxx

@@ -11,12 +11,13 @@ int main(){
   h.initiateHeap(20);
   h.insert(3);
   h.insert(2);
-  h.removeKey(1);
   h.insert(15);
   h.insert(5);
+  h.removeKey(2);
   h.insert(4);
   h.insert(45);
-  h.removeMax();
+  h.removeKey(15);
+  //h.removeMax();
   //h.decreaseKey(2, 1);
   h.printArray();
   return 0;