• + 0 comments

    Solution using 2 heapify mehods and maintaining a heap through vector

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    //used to bubble up the newly added element 
    void heapifyUp(vector<long long int>& heap) {
        int index = heap.size()-1;
        int parent = (index-1)/2;
        while (parent >= 0) {
            if(heap[parent] > heap[index]) {
                swap(heap[parent], heap[index]);
                index = parent;
                parent = (index-1)/2;
            }
            else break;
        }
    }
    
    //used to structure the heap after element deletion
    void heapifyDown(vector<long long int>& heap, long long int index = 0) {
        int left = 2 * index + 1;
        int right = 2 * index + 2;
        int parent = index;
        
        if(left < heap.size() && heap[parent] > heap[left]) 
            parent = left;
        if(right < heap.size() && heap[parent] > heap[right]) 
            parent = right;
            
        if(parent != index) {
            swap(heap[index], heap[parent]);
            heapifyDown(heap, parent);
        } 
    }
    
    void insertToHeap(vector<long long int>& heap, long long int element) {
        heap.push_back(element);
        heapifyUp(heap);
    }
    
    void deleteFromHeap(vector<long long int>& heap, long long int element) {
        int elementIndex = -1;
        for(int i=0; i<heap.size(); i++) {
            if(heap[i] == element){
                elementIndex = i;
            }
        }
        swap(heap[elementIndex], heap[heap.size()-1]);
        heap.resize(heap.size()-1);
        
        //problem we were facing 
        /*
        Fix Heapification After Deletion:
        1.  After removing an element by swapping it with the last element, instead of just calling heapifyDown, you should check whether the element needs to be "bubbled up" or "bubbled down."
        2. If the element that was swapped into the position is smaller than its parent, you should call heapifyUp.
        3. If it's larger than its children, you should call heapifyDown.
    */
        if(elementIndex > 0 && heap[elementIndex] < heap[(elementIndex-1) / 2]) {
            heapifyUp(heap);
        }
        else 
            heapifyDown(heap, elementIndex);
    }
    
    void print(vector<long long int> heap) {
            for(int i:heap) 
        cout<<i<<' ';
        cout<<endl;
    }
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        vector<long long int> heap;
        long long int cases = -1, querry, element = -1;
        cin>>cases;
        
        while(cases--) {
            cin>>querry;
            switch (querry) {
                case 1:
                    cin>>element;
                    insertToHeap(heap, element);
                    break;
                case 2:
                    cin>>element;
                    deleteFromHeap(heap, element);
                    break;
                case 3:
                    if(heap.size() > 0)
                        cout<<heap[0]<<endl;
                    break;
            }
        }
    
        return 0;
    }