Guest User

Untitled

a guest
Feb 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. class PQ{
  2. private:
  3. int *arr;
  4. int capacity;
  5. int size = 0;
  6.  
  7. PQ(const PQ& other) {}
  8. PQ& operator=(const PQ& other) {}
  9.  
  10. public:
  11. PQ(int cap):capacity(cap){
  12. arr = new int [++capacity];
  13. for (int i= 0; i <= capacity; i++) arr[i] = {0};
  14. }
  15.  
  16. ~PQ(){
  17. delete []arr;
  18. }
  19.  
  20. int top(){
  21. return arr[1];
  22. }
  23. void pop(){
  24. arr[1] = arr[size];
  25. size--;
  26. int idx = 1;
  27. int childidx;
  28.  
  29. while (2*idx < size && (arr[idx] < arr[2*idx] || arr[idx] < arr[2*idx+1])){
  30. if (arr[2*idx] > arr[2*idx+1]) childidx = 2*idx;
  31. else childidx = 2*idx+1;
  32.  
  33. int temp = arr[idx];
  34. arr[idx] = arr[childidx];
  35. arr[childidx] = temp;
  36. idx = childidx;
  37. }
  38. }
  39.  
  40. void push(int data){
  41. arr[++size] = data;
  42. int idx = size;
  43. while(arr[idx] > arr[idx/2] && idx/2 !=0 ) { // && stop when arr[0] reached.
  44. int temp = arr[idx];
  45. arr[idx] = arr[idx/2];
  46. arr[idx/2] = temp;
  47. idx = idx/2;
  48. }
  49. }
  50.  
  51. bool empty(){
  52. return !size;
  53. }
Add Comment
Please, Sign In to add comment