Просмотр исходного кода

figured out my mysterious zeros problem is coming from heapifyDown(). Have to figure out exactly from where.

Natalie Pueyo 8 лет назад
Родитель
Сommit
f43160cb0b
2 измененных файлов с 6 добавлено и 5 удалено
  1. 3 2
      program2/priorityqueue.cpp
  2. 3 3
      program2/testCode.cxx

+ 3 - 2
program2/priorityqueue.cpp

@@ -56,9 +56,8 @@ void PriorityQueue::removeMax(){
   } else {
     // remove the max value (at root)
     heapArray[1] = heapArray[heap_size + 1];
-    heap_size--;
-    // heapify down until the heap properties are restored
     heapifyDown(1);
+    heap_size--; // causing that zero to appear?
   }
 }
 
@@ -106,6 +105,8 @@ void PriorityQueue::heapifyDown(int index){
 
   if (largerKeyIndex != index) {
     change(&heapArray[index], &heapArray[largerKeyIndex]);
+    // apparently heapArray[largerKeyIndex] doesn't have a value?!?! fix this later
+    std::cout << "when does zero appear? " << heapArray[largerKeyIndex] << std::endl;
     heapifyDown(largerKeyIndex);
     //heap_size--;
   }

+ 3 - 3
program2/testCode.cxx

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