Advertisement
majczel23000

AVL

Jan 18th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct wezel{
  5.     wezel *up;
  6.     wezel *left;
  7.     wezel *right;
  8.     int val;
  9.     int bf;
  10. };
  11.  
  12. void RR(wezel *&root, wezel *A)
  13. {
  14.     wezel *B = A->right, *p = A->up;
  15.     A->right = B->left;
  16.     if(A->right)
  17.         A->right->up = A;
  18.     B->left = A;
  19.     B->up = p;
  20.     A->up = B;
  21.     if(p)
  22.     {
  23.         if(p->left == A)
  24.             p->left = B;
  25.         else
  26.             p->right = B;
  27.     }
  28.     if(B->bf == -1)
  29.         A->bf = B->bf = 0;
  30.     else
  31.     {
  32.         A->bf = -1;
  33.         B->bf = 1;
  34.     }
  35. }
  36.  
  37. void LL(wezel *&root, wezel *A){
  38.     wezel *B = A->left, *p = A->up;
  39.     A->left = B->right;
  40.     if(A->left)
  41.         A->left->up = A;
  42.     B->right = A;
  43.     B->up = p;
  44.     A->up = B;
  45.     if(p){
  46.         if(p->left == A)
  47.             p->left = B;
  48.         else
  49.             p->right = B;
  50.     }
  51.     if(B->bf == 1)
  52.         A->bf = B->bf = 0;
  53.     else{
  54.         A->bf = 1;
  55.         B->bf = -1;
  56.     }
  57. }
  58. void RL(wezel *&root, wezel *A){
  59.     wezel *B = A->right, *C = B->left, *p = A->up;
  60.     B->left = C->right;
  61.     if(B->left)
  62.         B->left->up = C;
  63.     A->right = C->left;
  64.     if(A->right)
  65.         A->right->up = C;
  66.     C->left = A;
  67.     C->right = B;
  68.     A->up = B->up = C;
  69.     C->up = p;
  70.     if(p){
  71.         if(p->left == A)
  72.             p->left = C;
  73.         else
  74.             p->right = C;
  75.     }
  76.     if(C->bf == -1)
  77.         A->bf = 1;
  78.     else
  79.         A->bf = 0;
  80.     if(C->bf == 1)
  81.         B->bf = -1;
  82.     else
  83.         B->bf = 0;
  84.     C->bf = 0;
  85.  
  86. }
  87. void LR(wezel *&root, wezel *A){
  88.     wezel *B = A->left, *C = B->right, *p = A->up;
  89.     B->right = C->left;
  90.     if(B->right)
  91.         B->right->up = C;
  92.     A->left = C->right;
  93.     if(A->left)
  94.         A->left->up = C;
  95.     C->right = A;
  96.     C->left = B;
  97.     A->up = B->up = C;
  98.     C->up = p;
  99.     if(p){
  100.         if(p->left == A)
  101.             p->left = C;
  102.         else
  103.             p->right = C;
  104.     }
  105.     if(C->bf == 1)
  106.         A->bf = -1;
  107.     else
  108.         A->bf = 0;
  109.     if(C->bf == -1)
  110.         B->bf = 1;
  111.     else
  112.         B->bf = 0;
  113.     C->bf = 0;
  114.  
  115. }
  116. void dodaj(wezel *&root, int k){
  117.     wezel * y,* p,* x;
  118.     bool t;
  119.     y = new wezel;
  120.     y->left = y->right = y->up = NULL;
  121.     y->val  = k;
  122.     y->bf  = 0;
  123.     p = root;
  124.     if(!p)
  125.         root = y;
  126.     else{
  127.     while(true)
  128.       if(k < p->val){
  129.         if(!p->left){
  130.           p->left = y;
  131.           break;
  132.         }
  133.         p = p->left;
  134.       }
  135.       else{
  136.         if(!p->right){
  137.           p->right = y;
  138.           break;
  139.         }
  140.         p = p->right;
  141.       }
  142.     y->up = p;
  143.  
  144. /////////////
  145.          if(p->bf)
  146.             p->bf = 0;
  147.          else{
  148.             if(p->left == y)
  149.                 p->bf = 1;
  150.             else
  151.                 p->bf = -1;
  152. ////////////
  153.          x = p->up;
  154.          t = false;
  155.          while(x){
  156.             if(x->bf){
  157.                 t = true;
  158.                 break;
  159.             };
  160.             if(x->left == p)
  161.                 x->bf =  1;
  162.             else
  163.                 x->bf = -1;
  164.             p = x;
  165.             x = x->up;
  166.          }
  167.  
  168.       if(t){
  169.         if(x->bf == 1){
  170.           if(x->right == p)
  171.              x->bf = 0;
  172.           else
  173.             if(p->bf == -1)
  174.                     LR(root,x);
  175.           else
  176.             LL(root,x);
  177.         }
  178.         else{
  179.           if(x->left == p)
  180.             x->bf = 0;
  181.           else if(p->bf == 1)
  182.             RL(root,x);
  183.           else
  184.             RR(root,x);
  185.         }
  186.       }
  187.     }
  188.   }
  189. }
  190. void post(wezel *p){
  191.     if(p){
  192.         cout << p->val << " ";
  193.         post(p->left);
  194.         post(p->right);
  195.     }
  196. }
  197. int main(){
  198.     wezel *root = NULL;
  199.     dodaj(root, 6);
  200.     dodaj(root, 10);
  201.     dodaj(root, 5);
  202.     dodaj(root, 3);
  203.     dodaj(root, 7);
  204.     dodaj(root, 2);
  205.     dodaj(root, 8);
  206.     dodaj(root, 9);
  207.     post(root);
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement