Cod sursa(job #2202215)

Utilizator vladalex01Vlad Alexandru vladalex01 Data 7 mai 2018 21:19:23
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.94 kb
#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <fstream>
//#include "Heap.h"

template <typename T>
class Heap
{ 
private:
    T* values;
    int numberOfElemets;
    int capacity;
public:
    const int max = 2147483640;
    Heap(int capacity) {
        // TODO BONUS
        this->capacity = capacity;
        numberOfElemets = 0;
        values = new T[capacity+1];
    }
    ~Heap() {
        // TODO BONUS
        delete[] values;
    }

    int parent(int poz) {
        // TODO BONUS
        if (poz / 2 > 1)
            return poz / 2;
        return -1;
    }
 
    int leftSubtree(int poz) {
        // TODO BONUS
        if (poz * 2 <= capacity)
            return poz*2;
        return -1;
    }
 
    int rightSubtree(int poz) {
        // TODO BONUS
        if (poz * 2 + 1 <= capacity)
            return poz * 2 + 1;
        return -1;
    }
 
    void pushUp(int poz) {
        // TODO BONUS
        int parent;
        T vaux;
            parent = poz/2;
            while (poz >1 && values[parent] > values[poz]) {
                vaux = values[parent];
                values[parent] = values[poz];
                values[poz] = vaux;
                poz = parent;
                parent = poz / 2;
            }

    }
 
    void pushDown(int poz) {
        // TODO BONUS
        int left = leftSubtree(poz);
        int right = rightSubtree(poz);
        T vaux;
        int child;
        while (left > 0)
        {
            child = left;
            if(right && values[right] < values[child])
                child = right;
            if (values[child] <values[poz])
            {
                vaux = values[child];
                values[child] = values[poz];
                values[poz] = vaux;
                poz = child;
            }
            else{
                break;
            }
        }
        left = leftSubtree(poz);
        right = rightSubtree(poz);

    }
 
    void insert(T x) {
        // TODO BONUS
        values[numberOfElemets + 1] = x;
        numberOfElemets++;
        pushUp(numberOfElemets);
    }
 
    T peek() {
        // TODO BONUS
        return values[1];
    }
 
    T extractMin() {
        // TODO BONUS
        T value =  values[1];
        values[1] = values[numberOfElemets];
        numberOfElemets--;
        pushDown(1);
        return value;
    }
    T remove(T x) {
        int k = x;
        x = max;
        return k;    

    }
};

int main()
{

    std::ifstream f;
    std::ofstream g;
    f.open("heapuri.in");
    g.open("heapuri.out");
    
    int n, i, x, y;
    f >> n;
    Heap<int> heap(n);

    for (i = 0; i < n; i++)
    {
        f >> x;
        if (x == 1)
        {
            f >> y;
            heap.insert(y);
        }
        if (x == 2)
        {
            f >> y;
            int k = heap.remove(y);
           // std::cout << k << "\n";
        }
        
        if (x == 3){
            int l = heap.extractMin();
            g << l << "\n";
        }

    }
}