Jelajahi Sumber

worked on change() to make it follow the assignment's directives more correctly

Natalie Pueyo 7 tahun lalu
induk
melakukan
01982ec44c
2 mengubah file dengan 18 tambahan dan 13 penghapusan
  1. 1 7
      program1/insertionsort.h
  2. 17 6
      program2/priorityqueue.cpp

+ 1 - 7
program1/insertionsort.h

@@ -4,12 +4,6 @@
 // ECS60, UC Davis
 // Adapted from: Lysecky & Vahid "Data Structures Essentials", zyBooks
 
-// include guard
-#ifndef INSSORT_H
-#define INSSORT_H
-
 #include <vector>
 
-int* InsertionSort(std::vector<int>* numbers);
-
-#endif
+void InsertionSort(std::vector<int>* numbers);

+ 17 - 6
program2/priorityqueue.cpp

@@ -68,14 +68,25 @@ void PriorityQueue::removeKey(int index){
 }
 
 void PriorityQueue::change(int *key, int *newK){
-  if (heapArray[*key] == NULL) {
-    std::cout << "PriorityQueue::change key " << *key << " not found" << std::endl;
-    return;
+// needs to change. I'm looking for the key, not the index?
+  for (int i = 0; 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(i);
+      } else if (*key < *newK) {
+        heapifyDown(i);
+      }
+
+    } else {
+      std::cout << "PriorityQueue::change key " << *key << " not found" << std::endl;
+      return;
+    }
   }
 
-  int temp = *key;
-  *key = *newK;
-  *newK = temp;
 }
 
 void PriorityQueue::heapifyDown(int node){