Pi0trek

Drzewo Binarne

Oct 6th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.85 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. //*******************************************************************************************************************************
  5. class Lisc
  6. {
  7. public:
  8.     int wartosc;
  9.     Lisc *prawy;
  10.     Lisc *lewy;
  11.     Lisc();
  12. };
  13. Lisc::Lisc()
  14. {
  15.     prawy = 0;
  16.     lewy = 0;
  17. }
  18. //********************************************************************************************************************************
  19. class Drzewo
  20. {
  21. public:
  22.     Lisc *pierwszy;
  23.     Drzewo();
  24.     void dodajLisc(int liczbaWLisciu);
  25.     void odejmijLisc(int Wartosc_do_usuniecia);
  26. };
  27. Drzewo::Drzewo()
  28. {
  29.     pierwszy = 0;
  30. }
  31. //********************************************************************************************************************************
  32. void Drzewo::dodajLisc(int liczbaWLisciu)
  33. {
  34.  
  35.     Lisc *nowylisc = new Lisc;
  36.     nowylisc->wartosc = liczbaWLisciu;
  37.     if (pierwszy == 0)
  38.     {
  39.         pierwszy = nowylisc;
  40.     }
  41.     else
  42.     {
  43.         Lisc *temp = pierwszy;
  44.  
  45.         if (temp->wartosc == nowylisc->wartosc)
  46.         {
  47.             Lisc *doUsuniecia = temp;
  48.             pierwszy = nowylisc;
  49.             nowylisc->lewy = temp->lewy;
  50.             nowylisc->prawy = temp->prawy;
  51.             temp = nowylisc;
  52.             delete doUsuniecia;
  53.         }
  54.         while (temp)
  55.         {          
  56.             if (temp->wartosc > nowylisc->wartosc)
  57.             {
  58.                 if (temp->lewy == 0 )
  59.                 {
  60.                     temp->lewy = nowylisc;
  61.                     break;
  62.                 }
  63.                 else
  64.                 {
  65.                     temp = temp->lewy;
  66.                 }
  67.             }
  68.             if (temp->wartosc < nowylisc->wartosc)
  69.             {
  70.                 if ( temp->prawy == 0)
  71.                 {
  72.                     temp->prawy = nowylisc;
  73.                     break;
  74.                 }
  75.                 else
  76.                 {
  77.                     temp = temp->prawy;
  78.                 }
  79.             }
  80.  
  81.             if (temp->lewy->wartosc == nowylisc->wartosc)
  82.             {
  83.                 Lisc *doUsuniecia = temp->lewy;
  84.                 nowylisc->lewy = temp->lewy->lewy;
  85.                 nowylisc->prawy = temp->lewy->prawy;
  86.                 temp->lewy = nowylisc;
  87.                 delete doUsuniecia;
  88.             }
  89.             if (temp->prawy->wartosc == nowylisc->wartosc)
  90.             {
  91.                 Lisc *doUsuniecia = temp->lewy;
  92.                 nowylisc->lewy = temp->prawy->lewy;
  93.                 nowylisc->prawy = temp->prawy->prawy;
  94.                 temp->prawy = nowylisc;
  95.                 delete doUsuniecia;
  96.             }
  97.            
  98.         }
  99.     }
  100. }
  101. //********************************************************************************************************************************
  102. void Drzewo::odejmijLisc(int Wartosc_do_usuniecia)
  103. {
  104.     if (pierwszy == 0)
  105.     {
  106.         return;
  107.     }
  108.     else
  109.     {
  110.         Lisc *temp = pierwszy;
  111.         while (1)
  112.         {          
  113.                 if (temp->wartosc > Wartosc_do_usuniecia)
  114.                 {
  115.                     if (temp->lewy->wartosc == Wartosc_do_usuniecia)
  116.                     {
  117.                         Lisc *liscDoUsuniecia = temp->lewy;
  118.                         Lisc *tempLewy = temp->lewy->lewy;
  119.  
  120.                         if (temp->lewy->prawy != 0)
  121.                         {
  122.                             temp->lewy = temp->lewy->prawy;
  123.                         }
  124.                         else
  125.                         {
  126.                             if (temp->lewy->lewy != 0)
  127.                             {
  128.                                 temp->lewy = temp->lewy->lewy;
  129.                                 delete liscDoUsuniecia;
  130.                                 break;
  131.                             }
  132.                             else
  133.                             {
  134.                                 temp->lewy = 0;
  135.                                 delete liscDoUsuniecia;
  136.                                 break;
  137.                             }                          
  138.                         }
  139.                         if (tempLewy != 0)
  140.                         {
  141.                             while (temp->lewy != 0)
  142.                             {
  143.                                 temp = temp->lewy;
  144.                             }
  145.                             temp->lewy = tempLewy;
  146.                         }
  147.                         else
  148.                         {
  149.                             temp->lewy->lewy = 0;
  150.                         }
  151.  
  152.                         delete liscDoUsuniecia;
  153.                         break;
  154.                     }
  155.                     temp = temp->lewy;
  156.                 }
  157.  
  158.                 if (temp->wartosc < Wartosc_do_usuniecia)
  159.                 {
  160.                     if (temp->prawy->wartosc == Wartosc_do_usuniecia)
  161.                     {
  162.                         Lisc *tempLewy2 = temp->prawy->lewy;
  163.                         Lisc *liscDoUsuniecia = temp->prawy;
  164.  
  165.                         if (temp->lewy->prawy != 0)
  166.                         {
  167.                             temp->prawy = temp->prawy->prawy;
  168.                         }
  169.                         else
  170.                         {
  171.                             if (temp->prawy->lewy != 0)
  172.                             {
  173.                                 temp->prawy = temp->prawy->lewy;
  174.                                 delete liscDoUsuniecia;
  175.                                 break;
  176.                             }
  177.                             else
  178.                             {
  179.                                 temp->prawy = 0;
  180.                                 delete liscDoUsuniecia;
  181.                                 break;
  182.                             }
  183.                         }
  184.                         if (tempLewy2 != 0)
  185.                         {
  186.                             temp = temp->prawy;
  187.                             while (temp->lewy != 0)
  188.                             {
  189.                                 temp = temp->lewy;
  190.                             }
  191.                             temp->lewy = tempLewy2;
  192.                             delete liscDoUsuniecia;
  193.                             break;
  194.                         }
  195.                         else
  196.                         {
  197.                             temp->prawy->lewy = 0;
  198.                         }
  199.                         delete liscDoUsuniecia;
  200.                         break;
  201.                     }
  202.                     temp = temp->prawy;
  203.                 }
  204.                 if (temp->wartosc == Wartosc_do_usuniecia)
  205.                 {
  206.                     Lisc *liscDoUsuniecia = temp;
  207.                     Lisc *tempLewy = temp->lewy;
  208.                     if (temp->prawy != 0)
  209.                     {
  210.                         temp = temp->prawy;
  211.                         pierwszy = temp;
  212.                         if (temp->lewy != 0)
  213.                         {
  214.                             while (temp->lewy != 0)
  215.                             {
  216.                                 temp = temp->lewy;
  217.                             }
  218.                             temp->lewy = tempLewy;
  219.                             delete liscDoUsuniecia;
  220.                             break;
  221.                         }
  222.                         else
  223.                         {
  224.                             temp->lewy == 0;
  225.                             delete liscDoUsuniecia;
  226.                             break;
  227.                         }
  228.                     }              
  229.                     else
  230.                     {
  231.                         if (temp->lewy != 0)
  232.                         {
  233.                             temp = temp->lewy;
  234.                             pierwszy = temp;
  235.                             delete liscDoUsuniecia;
  236.                             break;
  237.                         }
  238.                     }
  239.                 }
  240.            
  241.         }
  242.     }
  243. }
  244. int main()
  245. {
  246.     return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment