Cod sursa(job #2895562)

Utilizator Teodor_AxinteAxinte Teodor-Ionut Teodor_Axinte Data 29 aprilie 2022 11:17:26
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.61 kb
/*
#include <bits/stdc++.h>

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

class MaxHeap{
private:
    int Size= 0;
    vector<int> vec ={-1};
public:
    bool isEmpty() const{
        return Size==0;
    }
    int getMax() const{
        return vec[1];
    }
    void shiftUp(int i){
        if(i>=Size)
            return;
        if(i==1)
            return;
        if(vec[i] >vec[i/2]){
            swap(vec[i],vec[i/2]);
        }
        shiftUp(i/2);
    }
    void insertItem(int val){
        if(Size + 1 >=vec.size())
            vec.push_back(0);
        vec[++Size] =val;
        shiftUp(Size);
   }

    void shiftDown(int i){
        if(i> Size)
            return;
        int swapId = i;
        if(i*2 <= Size && vec[i]<vec[i*2]){
            swapId = i;
        }
        if(i*2 + 1 <=Size && vec[swapId] < vec[i*2 +1]){
            swapId = i*2 + 1;
        }
        if(swapId !=i){
            swap(vec[i],vec[swapId]);
            shiftDown(swapId);
        }
    }

    int extractMax(){
        int maxNum = vec[1];
        swap(vec[1],vec[Size--]);
        shiftDown(1);
        return maxNum;
    }

    void display(){
        for(int i=0;i<=Size;i++)
            cout<<vec[i]<<" ";
        cout<<'\n';
    }

};
MaxHeap *Pq = new MaxHeap;
int main(){
/*
    if(Pq->isEmpty()){
        cout<<"Yes, this is the correct answer\n";
    }
    else {
        cout<<"Problem with empty function\n";
    }
*/
    Pq->insertItem(324);
    Pq->insertItem(123);
    Pq->insertItem(345);
    Pq->insertItem(54);
    Pq->insertItem(532);

    Pq->display();
    cout << Pq->getMax()<<'\n';



}