Advertisement
Guest User

Untitled

a guest
May 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. //Raymond Kim 862042213
  2. //Naid Ibarra nibar004
  3.  
  4. //---------------------------------------------------------------------------------------PrintJob.h
  5. #ifndef __PRINTJOB_H
  6. #define __PRINTJOB_H
  7.  
  8. using namespace std;
  9.  
  10. class PrintJob {
  11. private:
  12. int priority;
  13. int jobNumber;
  14. int numPages;
  15.  
  16. public:
  17. PrintJob ( int, int, int);
  18. int getPriority ( );
  19. int getJobNumber ( );
  20. int getPages ( );
  21. //You can add additional functions here
  22. };
  23. #endif
  24.  
  25. //----------------------------------------------------------------------------------PrintJob.cpp
  26.  
  27. //Raymond Kim 862042213
  28. //Naid Ibarra nibar004
  29. #include "PrintJob.h"
  30.  
  31. PrintJob::PrintJob ( int setP, int setJ, int numP ):priority(setP), jobNumber(setJ), numPages(numP){}
  32.  
  33. int PrintJob::getPriority ( ){
  34. return priority;
  35. }
  36.  
  37. int PrintJob::getJobNumber ( ){
  38. return jobNumber;
  39. }
  40.  
  41. int PrintJob::getPages ( ){
  42. return numPages;
  43. }
  44.  
  45. //------------------------------------------------------------------------------------Heap.h
  46.  
  47. //Raymond Kim 862042213
  48. //Naid Ibarra nibar004
  49. #ifndef __HEAP_H
  50. #define __HEAP_H
  51.  
  52. #include "PrintJob.h"
  53.  
  54. const int MAX_HEAP_SIZE = 10;
  55.  
  56. class Heap {
  57. private:
  58. PrintJob* arr[MAX_HEAP_SIZE]; // Notice this is an array of PrintJob pointers
  59. int numItems; //current number of items in heap
  60.  
  61. public:
  62. /*Initializes an empty heap.*/
  63. Heap();
  64.  
  65. /*Inserts a PrintJob to the heap without
  66. violating max-heap properties.*/
  67. void enqueue ( PrintJob* );
  68.  
  69. /*Removes the node with highest priority from the heap.
  70. Follow the algorithm on priority-queue slides. */
  71. void dequeue ( );
  72.  
  73. /*Returns the node with highest priority.*/
  74. PrintJob* highest ( );
  75.  
  76. /*Prints the PrintJob with highest priority in the following format:
  77. Priority: priority, Job Number: jobNum, Number of Pages: numPages
  78. (Add a new line at the end.)*/
  79. void print ( );
  80.  
  81. // void graphiz(const string &);
  82. // void visualizeTree(ofstream &outFS, int i);
  83.  
  84. private:
  85. /*This function is called by dequeue function
  86. to move the new root down the heap to the
  87. appropriare location.
  88. check:
  89. 1) i should be valid
  90. Possibilities:
  91. 1) has left child
  92. 2) has left and right children
  93. 3) has no children
  94. */
  95. void trickleDown(int);
  96.  
  97. void trickeUp(int, PrintJob*);
  98.  
  99. int getParent(int);
  100.  
  101. int getLeft(int);
  102.  
  103. int getRight(int);
  104.  
  105.  
  106. //You can include additional private helper functions here
  107. };
  108. #endif
  109.  
  110. //----------------------------------------------------------------------------------Heap.cpp
  111.  
  112. //Raymond Kim 862042213
  113. //Naid Ibarra nibar004
  114. #include<cstdlib>
  115. #include<iostream>
  116. #include<algorithm>
  117. #include<fstream>
  118. #include"Heap.h"
  119. using namespace std;
  120.  
  121. Heap::Heap() : numItems(0) {}
  122.  
  123. void Heap::enqueue (PrintJob* job) {
  124. if (numItems == 10) {
  125. return;
  126. }
  127. trickeUp(numItems, job);
  128. ++numItems;
  129.  
  130. }
  131.  
  132. void Heap::dequeue () {
  133. if (numItems == 0) {
  134. return;
  135. }
  136. swap(arr[0], arr[numItems - 1]);
  137. --numItems;
  138. trickleDown(0);
  139. }
  140.  
  141. PrintJob *Heap::highest () {
  142. if (numItems == 0) {
  143. return NULL;
  144. }
  145. return arr[0];
  146. }
  147.  
  148. void Heap::print() {
  149. if (numItems == 0) {
  150. return;
  151. }
  152. cout << "Priority: " << arr[0]->getPriority()
  153. << ", Job Number: " << arr[0]->getJobNumber()
  154. << ", Number of Pages: " << arr[0]->getPages() << endl;
  155. }
  156.  
  157. //PRIVATE--------------------------------------------------
  158.  
  159. void Heap::trickleDown(int i) {
  160. if (i >= numItems) {
  161. return;
  162. }
  163. else if (i < 0) {
  164. return;
  165. }
  166.  
  167. if (getLeft(i) < numItems && getRight(i) < numItems) {
  168. if (arr[getLeft(i)]->getPriority() > arr[i]->getPriority()) {
  169. if (arr[getRight(i)]->getPriority() > arr[getLeft(i)]->getPriority()) {
  170. swap(arr[getRight(i)], arr[i]);
  171. trickleDown(getRight(i));
  172. }
  173. else {
  174. swap(arr[getLeft(i)], arr[i]);
  175. trickleDown(getLeft(i));
  176. }
  177. }
  178. else if (arr[getRight(i)]->getPriority() > arr[i]->getPriority()) {
  179. swap(arr[getRight(i)], arr[i]);
  180. trickleDown(getRight(i));
  181. }
  182. }
  183. else if (getLeft(i) < numItems) {
  184. if (arr[getLeft(i)]->getPriority() > arr[i]->getPriority()) {
  185. swap(arr[getLeft(i)], arr[i]);
  186. trickleDown(getLeft(i));
  187. }
  188. }
  189. }
  190.  
  191. void Heap::trickeUp(int i, PrintJob* job) {
  192. if (i == 0) {
  193. arr[i] = job;
  194. return;
  195. }
  196. if (job->getPriority() < arr[getParent(i)]->getPriority()) {
  197. arr[i] = job;
  198. return;
  199. }
  200. else {
  201. arr[i] = arr[getParent(i)];
  202. trickeUp(getParent(i), job);
  203. }
  204. }
  205.  
  206. int Heap::getParent(int i) {
  207. return (i - 1) / 2;
  208. }
  209.  
  210. int Heap::getLeft(int i) {
  211. return (2 * i) + 1;
  212. }
  213.  
  214. int Heap::getRight(int i) {
  215. return (2 * i) + 2;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement