Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PQ{
- private:
- int *arr;
- int capacity;
- int size = 0;
- PQ(const PQ& other) {}
- PQ& operator=(const PQ& other) {}
- public:
- PQ(int cap):capacity(cap){
- arr = new int [++capacity];
- for (int i= 0; i <= capacity; i++) arr[i] = {0};
- }
- ~PQ(){
- delete []arr;
- }
- int top(){
- return arr[1];
- }
- void pop(){
- arr[1] = arr[size];
- size--;
- int idx = 1;
- int childidx;
- while (2*idx < size && (arr[idx] < arr[2*idx] || arr[idx] < arr[2*idx+1])){
- if (arr[2*idx] > arr[2*idx+1]) childidx = 2*idx;
- else childidx = 2*idx+1;
- int temp = arr[idx];
- arr[idx] = arr[childidx];
- arr[childidx] = temp;
- idx = childidx;
- }
- }
- void push(int data){
- arr[++size] = data;
- int idx = size;
- while(arr[idx] > arr[idx/2] && idx/2 !=0 ) { // && stop when arr[0] reached.
- int temp = arr[idx];
- arr[idx] = arr[idx/2];
- arr[idx/2] = temp;
- idx = idx/2;
- }
- }
- bool empty(){
- return !size;
- }
Add Comment
Please, Sign In to add comment