Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- //*******************************************************************************************************************************
- class Lisc
- {
- public:
- int wartosc;
- Lisc *prawy;
- Lisc *lewy;
- Lisc();
- };
- Lisc::Lisc()
- {
- prawy = 0;
- lewy = 0;
- }
- //********************************************************************************************************************************
- class Drzewo
- {
- public:
- Lisc *pierwszy;
- Drzewo();
- void dodajLisc(int liczbaWLisciu);
- void odejmijLisc(int Wartosc_do_usuniecia);
- };
- Drzewo::Drzewo()
- {
- pierwszy = 0;
- }
- //********************************************************************************************************************************
- void Drzewo::dodajLisc(int liczbaWLisciu)
- {
- Lisc *nowylisc = new Lisc;
- nowylisc->wartosc = liczbaWLisciu;
- if (pierwszy == 0)
- {
- pierwszy = nowylisc;
- }
- else
- {
- Lisc *temp = pierwszy;
- if (temp->wartosc == nowylisc->wartosc)
- {
- Lisc *doUsuniecia = temp;
- pierwszy = nowylisc;
- nowylisc->lewy = temp->lewy;
- nowylisc->prawy = temp->prawy;
- temp = nowylisc;
- delete doUsuniecia;
- }
- while (temp)
- {
- if (temp->wartosc > nowylisc->wartosc)
- {
- if (temp->lewy == 0 )
- {
- temp->lewy = nowylisc;
- break;
- }
- else
- {
- temp = temp->lewy;
- }
- }
- if (temp->wartosc < nowylisc->wartosc)
- {
- if ( temp->prawy == 0)
- {
- temp->prawy = nowylisc;
- break;
- }
- else
- {
- temp = temp->prawy;
- }
- }
- if (temp->lewy->wartosc == nowylisc->wartosc)
- {
- Lisc *doUsuniecia = temp->lewy;
- nowylisc->lewy = temp->lewy->lewy;
- nowylisc->prawy = temp->lewy->prawy;
- temp->lewy = nowylisc;
- delete doUsuniecia;
- }
- if (temp->prawy->wartosc == nowylisc->wartosc)
- {
- Lisc *doUsuniecia = temp->lewy;
- nowylisc->lewy = temp->prawy->lewy;
- nowylisc->prawy = temp->prawy->prawy;
- temp->prawy = nowylisc;
- delete doUsuniecia;
- }
- }
- }
- }
- //********************************************************************************************************************************
- void Drzewo::odejmijLisc(int Wartosc_do_usuniecia)
- {
- if (pierwszy == 0)
- {
- return;
- }
- else
- {
- Lisc *temp = pierwszy;
- while (1)
- {
- if (temp->wartosc > Wartosc_do_usuniecia)
- {
- if (temp->lewy->wartosc == Wartosc_do_usuniecia)
- {
- Lisc *liscDoUsuniecia = temp->lewy;
- Lisc *tempLewy = temp->lewy->lewy;
- if (temp->lewy->prawy != 0)
- {
- temp->lewy = temp->lewy->prawy;
- }
- else
- {
- if (temp->lewy->lewy != 0)
- {
- temp->lewy = temp->lewy->lewy;
- delete liscDoUsuniecia;
- break;
- }
- else
- {
- temp->lewy = 0;
- delete liscDoUsuniecia;
- break;
- }
- }
- if (tempLewy != 0)
- {
- while (temp->lewy != 0)
- {
- temp = temp->lewy;
- }
- temp->lewy = tempLewy;
- }
- else
- {
- temp->lewy->lewy = 0;
- }
- delete liscDoUsuniecia;
- break;
- }
- temp = temp->lewy;
- }
- if (temp->wartosc < Wartosc_do_usuniecia)
- {
- if (temp->prawy->wartosc == Wartosc_do_usuniecia)
- {
- Lisc *tempLewy2 = temp->prawy->lewy;
- Lisc *liscDoUsuniecia = temp->prawy;
- if (temp->lewy->prawy != 0)
- {
- temp->prawy = temp->prawy->prawy;
- }
- else
- {
- if (temp->prawy->lewy != 0)
- {
- temp->prawy = temp->prawy->lewy;
- delete liscDoUsuniecia;
- break;
- }
- else
- {
- temp->prawy = 0;
- delete liscDoUsuniecia;
- break;
- }
- }
- if (tempLewy2 != 0)
- {
- temp = temp->prawy;
- while (temp->lewy != 0)
- {
- temp = temp->lewy;
- }
- temp->lewy = tempLewy2;
- delete liscDoUsuniecia;
- break;
- }
- else
- {
- temp->prawy->lewy = 0;
- }
- delete liscDoUsuniecia;
- break;
- }
- temp = temp->prawy;
- }
- if (temp->wartosc == Wartosc_do_usuniecia)
- {
- Lisc *liscDoUsuniecia = temp;
- Lisc *tempLewy = temp->lewy;
- if (temp->prawy != 0)
- {
- temp = temp->prawy;
- pierwszy = temp;
- if (temp->lewy != 0)
- {
- while (temp->lewy != 0)
- {
- temp = temp->lewy;
- }
- temp->lewy = tempLewy;
- delete liscDoUsuniecia;
- break;
- }
- else
- {
- temp->lewy == 0;
- delete liscDoUsuniecia;
- break;
- }
- }
- else
- {
- if (temp->lewy != 0)
- {
- temp = temp->lewy;
- pierwszy = temp;
- delete liscDoUsuniecia;
- break;
- }
- }
- }
- }
- }
- }
- int main()
- {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment