summaryrefslogtreecommitdiff
path: root/Year_2/Algorithms/build-max-heap.cc
blob: 40c0893004b3f449e6d8f76f701d0e822f7c9eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <cstdlib>
#include <fstream>
#include <iostream>

using namespace std;

template <class H>
class MaxHeap {
public:
    void
    max_heapify(int i)
    {
        if (i > _size)
            return;

        size_t l = left(i);
        size_t r = right(i);
        size_t max = i;

        if (l <= _size && _A[l] > _A[i])
            max = l;
        if (r <= _size && _A[r] > _A[max])
            max = r;

        if (max != i) {
            swap(i, max);
            max_heapify(max);
        }
    }

    size_t
    left(int i)
    {
        return i << 1;
    }
    size_t
    right(int i)
    {
        return (i << 1) | 1;
    }

    ~MaxHeap()
    {
        delete _A;
    }

    MaxHeap(H* A, int size, ostream& out)
        : _size(size + 1)
    {
        _A = new H[_size + 1];
        for (int i = 1; i <= size; ++i) {
            _A[i] = A[i - 1];
        }

        for (int i = _size / 2; i > 0; --i)
            max_heapify(i);

        for (int i = 1; i <= size; ++i) {
            out << _A[i] << ' ';
        }
        out << endl;
    }

    void
    swap(int x, int y)
    {
        auto t = _A[x];
        _A[x] = _A[y];
        _A[y] = t;
    }

    H
    extract_max()
    {
        swap(1, _size);
        --_size;
        max_heapify(1);

        return _A[_size + 1];
    }

private:
    H* _A;
    size_t _size;
    size_t _n;
};

int
main(int argc, char** argv)
{
    ifstream fin("input.txt");
    ofstream fout("output.txt");
    string t;
    int n, j;

    for (int i = 0; i < atoi(argv[1]); ++i) {
        fin >> t;
        fin >> n;

        if (t == "bool") {
            bool* arr = new bool[n];
            for (j = 0; j < n; ++j)
                fin >> arr[j];

            MaxHeap<bool>* mh = new MaxHeap<bool>(arr, n, fout);

            delete mh;
        } else if (t == "double") {
            double* arr = new double[n];
            for (j = 0; j < n; ++j)
                fin >> arr[j];

            MaxHeap<double>* mh = new MaxHeap<double>(arr, n, fout);

            delete mh;
        } else if (t == "char") {
            char* arr = new char[n];
            for (j = 0; j < n; ++j)
                fin >> arr[j];

            MaxHeap<char>* mh = new MaxHeap<char>(arr, n, fout);

            delete mh;
        } else if (t == "int") {
            int* arr = new int[n];
            for (j = 0; j < n; ++j)
                fin >> arr[j];

            MaxHeap<int>* mh = new MaxHeap<int>(arr, n, fout);

            delete mh;
        }
    }

    fin.close();
    fout.close();
    return 0;
}