Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define cin fin
- #define cout fout
- #define sp << " " <<
- #define nl << "\n"
- ifstream fin("prob.in");
- ofstream fout("prob.out");
- int n, i, j, h[100], k, y, a;
- void insert(int a) {
- n++;
- k++;
- h[k] = a;
- }
- void sort(int nn) {
- int x;
- x = h[nn];
- while (nn > 1 && x > h[nn / 2]) {
- h[nn] = h[nn / 2];
- nn = nn / 2;
- }
- h[nn] = x;
- }
- void jos(int n) {
- int fiu = -1;
- while (fiu != 0) {
- if (2 * n <= k)
- fiu = 2 * n;
- if (2 * n + 1 <= k && h[2 * n + 1] >= h[fiu])
- fiu = 2 * n + 1;
- if (h[fiu] <= h[n])
- fiu = 0;
- if (fiu) {
- swap(h[n], h[fiu]);
- n = fiu;
- }
- }
- }
- void elimin() {
- if (k < 1) return;
- cout<<h[1]<<" ";
- h[1] = h[k];
- k--;
- n--;
- jos(1);
- }
- int main() {
- int t;
- k = 0;
- while (cin >> t) {
- insert(t);
- sort(k);
- }
- cout << "Max-heapul dat: " << endl;
- for (i = 1; i <= k; i++)
- cout << h[i] << " ";
- cout nl;
- cout << "Elementele max :" nl ;
- for (int i = 1; i <= 3; i++) {
- elimin();
- }
- cout nl;
- if (k == 0) {
- cout << "Heapul nu are elemente" nl;
- } else {
- cout << "Max-heapul nou: " << endl;
- for (i = 1; i <= k; i++)
- cout << h[i] << " ";
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement