Guest User

Untitled

a guest
Dec 11th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. class slownik
  6. {
  7.    struct wezel
  8.     {
  9.         wezel * p, * left, * right;
  10.         int  bf,key;
  11.         string s,ang;
  12.     };
  13.   public:
  14.  
  15.     wezel * root;  // korzeñ drzewa
  16.     int count;       // liczba wêz³ów
  17.  
  18. // **********************************************
  19. // *** Definicje funkcji sk³adowych klasy AVL ***
  20. // **********************************************
  21.  
  22. // Konstruktor klasy AVL
  23. //----------------------
  24.  
  25. slownik()
  26. {
  27.   root = NULL;
  28.   count = 0;
  29. }
  30.  
  31. // Destruktor klasy AVL
  32. //---------------------
  33.  
  34. ~slownik()
  35. {
  36.   while(root) delete(remove(root));
  37. }
  38.  
  39. // Rotacja RR
  40. //-----------
  41.  
  42. wezel * rotationRR(wezel * A)
  43. {
  44.   wezel * B = A->right, * P = A->p;
  45.  
  46.   A->right = B->left;
  47.   if(A->right) A->right->p = A;
  48.   B->left = A;
  49.   B->p = P;
  50.   A->p = B;
  51.   if(P)
  52.   {
  53.     if(P->left == A) P->left = B; else P->right = B;
  54.   }
  55.   else root = B;
  56.  
  57.   if(B->bf == -1)
  58.   {
  59.     A->bf = B->bf = 0;
  60.   }
  61.   else
  62.   {
  63.     A->bf = -1; B->bf = 1;
  64.   }
  65.   return B;
  66. }
  67.  
  68. // Rotacja LL
  69. //-----------
  70.  
  71. wezel * rotationLL(wezel * A)
  72. {
  73.   wezel * B = A->left, * P = A->p;
  74.  
  75.   A->left = B->right;
  76.   if(A->left) A->left->p = A;
  77.   B->right = A;
  78.   B->p = P;
  79.   A->p = B;
  80.   if(P)
  81.   {
  82.     if(P->left == A) P->left = B; else P->right = B;
  83.   }
  84.   else root = B;
  85.  
  86.   if(B->bf == 1)
  87.   {
  88.     A->bf = B->bf = 0;
  89.   }
  90.   else
  91.   {
  92.     A->bf = 1; B->bf = -1;
  93.   }
  94.  
  95.   return B;
  96. }
  97.  
  98. // Rotacja RL
  99. //-----------
  100.  
  101. wezel * rotationRL(wezel * A)
  102. {
  103.   wezel * B = A->right, * C = B->left, * P = A->p;
  104.  
  105.   B->left = C->right;
  106.   if(B->left) B->left->p = B;
  107.   A->right = C->left;
  108.   if(A->right) A->right->p = A;
  109.   C->left = A;
  110.   C->right = B;
  111.   A->p = B->p = C;
  112.   C->p = P;
  113.   if(P)
  114.   {
  115.     if(P->left == A) P->left = C; else P->right = C;
  116.   }
  117.   else root = C;
  118.  
  119.   A->bf = (C->bf == -1) ?  1 : 0;
  120.   B->bf = (C->bf ==  1) ? -1 : 0;
  121.   C->bf = 0;
  122.  
  123.   return C;
  124. }
  125.  
  126. // Rotacja LR
  127. //-----------
  128.  
  129. wezel * rotationLR(wezel * A)
  130. {
  131.   wezel * B = A->left, * C = B->right, * P = A->p;
  132.  
  133.   B->right = C->left;
  134.   if(B->right) B->right->p = B;
  135.   A->left = C->right;
  136.   if(A->left) A->left->p = A;
  137.   C->right = A;
  138.   C->left = B;
  139.   A->p = B->p = C;
  140.   C->p = P;
  141.   if(P)
  142.   {
  143.     if(P->left == A) P->left = C; else P->right = C;
  144.   }
  145.   else root = C;
  146.  
  147.   A->bf = (C->bf ==  1) ? -1 : 0;
  148.   B->bf = (C->bf == -1) ?  1 : 0;
  149.   C->bf = 0;
  150.  
  151.   return C;
  152. }
  153.  
  154. // Wstawia element do struktury AVL
  155. //---------------------------------
  156.  
  157. bool insert(wezel * n)
  158. {
  159.   wezel * x = root, * y, * z;
  160.  
  161.   y = n->left = n->right = NULL;
  162.   n->bf = 0;
  163.  
  164.   while(x)
  165.   {
  166.     if(x->s.compare(n->s) == 0)
  167.     {
  168.       delete n;
  169.       return false;
  170.     }
  171.     y = x;
  172.     x = (n->s.compare(x->s)<0) ? x->left : x->right;
  173.   }
  174.  
  175.   count++;
  176.  
  177.   if(!(n->p = y))
  178.   {
  179.     root = n;
  180.     return true;
  181.   }
  182.   if(n->s.compare(y->s) < 0) y->left = n; else y->right = n;
  183.   if(y->bf)
  184.   {
  185.     y->bf = 0;
  186.     return true;
  187.   }
  188.   y->bf = (y->left == n) ? 1 : -1;
  189.   z = y->p;
  190.   while(z)
  191.   {
  192.     if(z->bf) break;
  193.     z->bf = (z->left == y) ? 1 : -1;
  194.     y = z; z = z->p;
  195.   }
  196.  
  197.   if(!z) return true;
  198.  
  199.   if(z->bf == 1)
  200.   {
  201.     if(z->right == y)
  202.     {
  203.       z->bf = 0;
  204.       return true;
  205.     }
  206.     if(y->bf == -1) rotationLR(z); else rotationLL(z);
  207.     return true;
  208.   }
  209.   else
  210.   {
  211.     if(z->left == y)
  212.     {
  213.       z->bf = 0;
  214.       return true;
  215.     }
  216.     if(y->bf == 1) rotationRL(z); else rotationRR(z);
  217.     return true;
  218.   }
  219. }
  220.  
  221. // Wyszukuje element wg wartoœci klucza
  222. //-------------------------------------
  223.  
  224. wezel * search(string slowo)
  225. {
  226.   wezel * x = root;
  227.  
  228.   while((x) && (x->s.compare(slowo) != 0))
  229.     x = (x->s.compare(slowo) >0) ? x->left : x->right;
  230.  
  231.   return x;
  232. }
  233.  
  234. wezel * minNode(wezel * x)
  235. {
  236.   while(x->left) x = x->left;
  237.   return x;
  238. }
  239.  
  240. wezel * maxNode(wezel * x)
  241. {
  242.   while(x->right) x = x->right;
  243.   return x;
  244. }
  245.  
  246. // Zwraca wêze³ poprzednika
  247. //-------------------------
  248.  
  249. wezel * pred(wezel * x)
  250. {
  251.   if(x->left) return maxNode(x->left);
  252.  
  253.   wezel * y;
  254.  
  255.   do
  256.   {
  257.     y = x;
  258.     x = x->p;
  259.   } while(x && (x->right != y));
  260.  
  261.   return x;
  262. }
  263.  
  264. // Zwraca wêze³ nastêpnika
  265. //------------------------
  266.  
  267. wezel * succ(wezel * x)
  268. {
  269.   if(x->right) return minNode(x->right);
  270.  
  271.   wezel * y;
  272.  
  273.   do
  274.   {
  275.     y = x;
  276.     x = x->p;
  277.   } while(x && (x->left != y));
  278.  
  279.   return x;
  280. }
  281.  
  282. // Usuwa element x ze struktury AVL. Zwraca usuniêty wêze³
  283. //--------------------------------------------------------
  284.  
  285. wezel * remove(wezel * x)
  286. {
  287.   wezel * t, * y, * z;
  288.   bool nest;
  289.  
  290.   if((x->left) && (x->right))
  291.   {
  292.     y = remove(pred(x));
  293.     count++;
  294.     nest = false;
  295.   }
  296.   else
  297.   {
  298.     if(x->left)
  299.     {
  300.       y = x->left; x->left = NULL;
  301.     }
  302.     else
  303.     {
  304.       y = x->right; x->right = NULL;
  305.     }
  306.     x->bf = 0;
  307.     nest = true;
  308.   }
  309.  
  310.   if(y)
  311.   {
  312.     y->p = x->p;
  313.     if(y->left  = x->left)  y->left->p  = y;
  314.     if(y->right = x->right) y->right->p = y;
  315.     y->bf = x->bf;
  316.   }
  317.  
  318.   if(x->p)
  319.   {
  320.     if(x->p->left == x) x->p->left = y; else x->p->right = y;
  321.   }
  322.   else root = y;
  323.  
  324.   if(nest)
  325.   {
  326.     z = y;
  327.     y = x->p;
  328.     while(y)
  329.     {
  330.       if(!(y->bf))
  331.       {
  332.  
  333. // Przypadek nr 1
  334.  
  335.         y->bf = (y->left == z) ? -1 : 1;
  336.         break;
  337.       }
  338.       else
  339.       {
  340.         if(((y->bf ==  1) && (y->left  == z)) || ((y->bf == -1) && (y->right == z)))
  341.         {
  342.  
  343. // Przypadek nr 2
  344.  
  345.           y->bf = 0;
  346.           z = y; y = y->p;
  347.         }
  348.         else
  349.         {
  350.           t = (y->left == z) ? y->right : y->left;
  351.           if(!(t->bf))
  352.           {
  353.  
  354. // Przypadek 3A
  355.  
  356.             if(y->bf == 1) rotationLL(y); else rotationRR(y);
  357.             break;
  358.           }
  359.           else if(y->bf == t->bf)
  360.           {
  361.  
  362. // Przypadek 3B
  363.  
  364.             if(y->bf == 1) rotationLL(y); else rotationRR(y);
  365.             z = t; y = t->p;
  366.           }
  367.           else
  368.           {
  369.  
  370. // Przypadek 3C
  371.  
  372.             if(y->bf == 1) rotationLR(y); else rotationRL(y);
  373.             z = y->p; y = z->p;
  374.           }
  375.         }
  376.       }
  377.     }
  378.   }
  379.   count--;
  380.   return x;
  381. }
  382.  
  383. // Przechodzi przez drzewo AVL metod¹ preorder
  384. //--------------------------------------------
  385.  
  386. void preorder(wezel * x)
  387. {
  388.   if(x)
  389.   {
  390.     cout << x->s <<" "<< x->ang << endl;  // tutaj przetwarzamy bie¿¹cy wêze³
  391.     preorder(x->left);
  392.     preorder(x->right);
  393.   }
  394. }
  395.  
  396. // Przechodzi przez drzewo AVL metod¹ inorder
  397. //-------------------------------------------
  398.  
  399. void inorder(wezel * x)
  400. {
  401.   if(x)
  402.   {
  403.     inorder(x->left);
  404.     cout << x->s <<" "<< x->ang << endl;  // tutaj przetwarzamy bie¿¹cy wêze³
  405.     inorder(x->right);
  406.   }
  407. }
  408.  
  409. // Przechodzi przez drzewo AVL metod¹ postorder
  410. //---------------------------------------------
  411.  
  412. void postorder(wezel * x)
  413. {
  414.   if(x)
  415.   {
  416.     postorder(x->left);
  417.     postorder(x->right);
  418.     cout << x->s <<" "<< x->ang <<endl;  // tutaj przetwarzamy bie¿¹cy wêze³
  419.   }
  420. }
  421.  
  422. // Przechodzi przez drzewo wypisuj¹c zawartoœæ wêz³ów
  423. void walk(wezel * x)
  424. {
  425.   cout << x->s << " : bf = " << setw(2) << x->bf << " : Left-> ";
  426.   if(x->left) cout << setw(3) << x->left->s;
  427.   else        cout << "NIL";
  428.   cout << ", Right-> ";
  429.   if(x->right) cout << setw(3) << x->right->s;
  430.   else         cout << "NIL";
  431.   cout << " : parent -> ";
  432.   if(x->p) cout << setw(3) << x->p->s;
  433.   else     cout << "NIL";
  434.   cout << endl;
  435.   if(x->left)  walk(x->left);
  436.   if(x->right) walk(x->right);
  437. }
  438.  
  439. int wypisz(slownik *T)
  440. {
  441.     int z;
  442.    z=T->coutAVLcount();
  443.    if (z==0)
  444.     cout<<"Slownik jest pusty"<<endl;
  445.   else
  446.     T->walk(T->root);
  447.  
  448. return 0;
  449. }
  450. // Wypisuje do cout liczbê wêz³ów drzewa AVL
  451. //------------------------------------------
  452.  
  453. int coutAVLcount()
  454. {
  455.   return count;
  456. }
  457.  
  458. void add(slownik * T)
  459. {
  460.   cout << "Dodawanie nowych slow\n"
  461.           "-------------------------------------\n\n";
  462.   cout << "Podaj liczbe slow do dodania."<<endl;
  463.  
  464.   int i,n;
  465.  
  466.   wezel * x;
  467.  
  468.   cin >> n;
  469.   for(i = 0; i < n; i++)
  470.   {
  471.     x = new wezel;
  472.     cout<<"Podaj slowo"<<endl;
  473.     cin >> x->s;
  474.     cout<<"Podaj tlumaczenie \n"<<"Gdy go brak wstaw '-'"<<endl;
  475.     cin>>x->ang;
  476.     T->insert(x);
  477.   }
  478.  
  479.   cout << endl;
  480.   T->walk(T->root);
  481.  
  482. }
  483.  
  484. // Usuwa wêze³ o zadanym kluczu
  485. //-----------------------------
  486.  
  487. void del(slownik * T)
  488. {
  489.   cout << "Usuwanie wezla drzewa AVL o zadanym kluczu\n"
  490.           "------------------------------------------\n\n";
  491.  
  492.   wezel * x;
  493.   string key;
  494.   cout << "Podaj slowo do usuniecia : "; cin >> key;
  495.  
  496.   x = T->search(key);
  497.  
  498.   if(x)
  499.   {
  500.     delete T->remove(x);
  501.     cout << endl;
  502.     if(T->root) T->walk(T->root);
  503.   }
  504.   else cout << "\nBrak takiego slowa\n";
  505. }
  506.  
  507. void szukaj(slownik * T)
  508. {
  509.   cout << "Sprawdzenie obecnosci slowa w slowniku\n"
  510.           "--------------------------------------------------------\n\n"
  511.           "Podaj slowo : "<<endl;
  512.  
  513.   string slowo;
  514.   cin >> slowo;
  515.   if(T->search(slowo)) cout << "Slowo znalezione."<<endl<<T->search(slowo)->s<<" - "<<T->search(slowo)->ang;
  516.   else               cout << "Nie znaleziono takiego slowa\n";
  517. }
  518.  
  519. // Przechodzi przez drzewo algorytmem preorder
  520. void preorder(slownik * T)
  521. {
  522.   cout << "Przechodzenie drzewa AVL algorytmem preorder\n"
  523.           "--------------------------------------------\n\n";
  524.   T->preorder(T->root);
  525. }
  526.  
  527. // Przechodzi przez drzewo algorytmem inorder
  528. void inorder(slownik * T)
  529. {
  530.   cout << "Przechodzenie drzewa AVL algorytmem inorder\n"
  531.           "-------------------------------------------\n\n";
  532.   T->inorder(T->root);
  533. }
  534.  
  535. // Przechodzi przez drzewo algorytmem postorder
  536. void postorder(slownik * T)
  537. {
  538.   cout << "Przechodzenie drzewa AVL algorytmem postorder\n"
  539.           "---------------------------------------------\n\n";
  540.   T->postorder(T->root);
  541. }
  542. };
  543.  
  544. main()
  545. {
  546.   slownik * T = new slownik();
  547.   int choice;
  548.   slownik a;
  549.   do
  550.   {
  551.     cout << "Slownik MENU\n"
  552.             "==============================\n\n"
  553.             "Wybor Funkcja\n"
  554.             "-----------------------------\n"
  555.             " [0]  Koniec\n"
  556.             " [1]  Dodaj slowo\n"
  557.             " [2]  Usun slowo\n"
  558.             " [3]  Znajdz slowo\n"
  559.             " [4]  Wyswietl drzewo\n"
  560.             " [5]  pusto\n"
  561.             " [6]  pusto\n"
  562.             " [7]  Preorder\n"
  563.             " [8]  Inorder\n"
  564.             " [9]  Postorder\n"
  565.             "-----------------------------\n"
  566.             "Twoj wybor : ";
  567.     cin >> choice;
  568.  
  569.     switch(choice)
  570.     {
  571.       case 1 : a.add(T);       break;
  572.       case 2 : a.del(T);       break;
  573.       case 3 : a.szukaj(T);     break;
  574.       case 4 : a.wypisz(T);      break;
  575.       case 5 :     break;
  576.       case 6 :       break;
  577.       case 7 : a.preorder(T);  break;
  578.       case 8 : a.inorder(T);   break;
  579.       case 9 : a.postorder(T); break;
  580.     }
  581.     if((choice >= 1) && (choice <= 9))
  582.     {
  583.       cout << endl;
  584.     }
  585.  
  586.   } while(choice);
  587.  
  588.   delete T;
  589. }
Advertisement
Add Comment
Please, Sign In to add comment