Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using namespace std;
- ifstream cin("prob5.in");
- ofstream cout("prob5.out");
- int h[100001], n;
- bool comp(int a, int b) {
- return (a > b);
- }
- void show(string s = "") {
- cout << s << ":\n";
- for(int i = 1; i <= n; ++i)
- cout << h[i] << ' ';
- cout << '\n';
- }
- void read() {
- while(cin >> h[++n]);
- --n;
- }
- void heapifyUp(int pos) {
- while(pos > 1 && comp(h[pos], h[pos >> 1])) {
- swap(h[pos], h[pos >> 1]);
- pos >>= 1;
- }
- }
- void heapifyDown(int pos) {
- int son = 0;
- while(pos != son) {
- son = pos;
- if(2 * son <= n && !comp(h[pos], h[2 * son]))
- pos = 2 * son;
- if(2 * son + 1 <= n && !comp(h[pos], h[2 * son + 1]))
- pos = 2 * son + 1;
- swap(h[pos], h[son]);
- }
- }
- void buildHeap() {
- for(int i = 1; i <= n; ++i)
- heapifyUp(i);
- }
- void del(int pos = 1) {
- swap(h[pos], h[n--]);
- heapifyDown(pos);
- }
- int main() {
- read();
- show("Avem tabloul");
- buildHeap();
- show("Avem MaxHeapul");
- del();
- show("Heapul dupa prima stergere");
- del();
- show("Heapul dupa a doua stergere");
- del();
- show("Heapul dupa a treia stergere");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement