Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- using namespace std;
- //Zadanie 1
- struct el {
- int v;
- el* rodzic = NULL;
- el* lewy = NULL;
- el* prawy = NULL;
- };
- el* root = NULL;
- void dodaj(int wartosc)
- {
- el *nowy = NULL; //nowy element, ktory chcemy dodac
- nowy = new el;
- nowy->v = wartosc;
- el *y = NULL; //wskazuje przegladanego rodzica
- el *x = root; //kopia root
- while (x) //przechodzenie przez drzewo
- {
- if (nowy->v == x->v)
- {
- delete nowy;
- exit(1);
- }
- y = x;
- if (nowy->v < x->v)
- x = x->lewy;
- else
- x = x->prawy;
- }
- nowy->rodzic = y;
- if (!y) //jeli puste drzewo
- root = nowy;
- else
- if (nowy->v < y->v)
- y->lewy = nowy;
- else
- y->prawy = nowy;
- }
- void inorder(el* w)
- {
- if (w)
- {
- inorder(w->lewy);
- cout << w->v << " ";
- inorder(w->prawy);
- }
- }
- void zadanie1() {
- dodaj(15);
- inorder(root);
- cout << endl;
- dodaj(12);
- inorder(root);
- cout << endl;
- dodaj(18);
- inorder(root);
- cout << endl;
- dodaj(26);
- inorder(root);
- cout << endl;
- dodaj(20);
- inorder(root);
- cout << endl;
- dodaj(7);
- inorder(root);
- cout << endl;
- dodaj(3);
- inorder(root);
- cout << endl;
- dodaj(25);
- inorder(root);
- }
- //Zadanie 2
- const int N = 15;
- int kopiec1[N] = { 15,12,11,10,11,8,5,3,7,2,20,4,7,5,1 };
- int kopiec2[N] = { 15,12,11,10,11,8,5,3,7,2,9,4,7,12,1 };
- void wynurz(int i, int kp[])
- {
- if(i>0)
- {
- int ojciec=(i-1)/2;
- if(kp[i]>kp[ojciec])
- {
- swap(kp[i],kp[ojciec]);
- wynurz(ojciec, kp);
- }
- }
- }
- void print(int kp[])
- {
- for (int i = 0; i < N; i++)
- cout << kp[i] << " ";
- cout << endl;
- } //wydruk stanu kolejki
- void zadanie2() {
- cout<<"Kopiec przed wynurzeniu: ";
- print(kopiec1);
- wynurz(10, kopiec1);
- cout<<"Kopiec po wynurzeniu: ";
- print(kopiec1);
- cout<<"Kopiec przed wynurzeniu: ";
- print(kopiec2);
- wynurz(13, kopiec2);
- cout<<"Kopiec po wynurzeniu: ";
- print(kopiec2);
- }
- //MAIN
- int main(int argc, char** argv) {
- //cout << "Zadanie 1" << endl; zadanie1();
- cout << "Zadanie 2" << endl; zadanie2();
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement