Advertisement
huutho_96

AVL

Dec 1st, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define Left_higher -1;
  4. #define Equal_height 0;
  5. #define Right_higher 1;
  6. typedef struct node
  7. {
  8.     char balance_factor;
  9.     int data;
  10.     node*left;
  11.     node*right;
  12. }AVL_NODE;
  13. /*Question 1*/
  14. AVL_NODE *CreateAVLNode(int x)
  15. {
  16.     AVL_NODE *p = new AVL_NODE;
  17.  
  18.     if (!p) exit(1);
  19.  
  20.     p->balance_factor = Equal_height;
  21.     p->data = x;
  22.     p->left = NULL;
  23.     p->right = NULL;
  24.  
  25.     return p;
  26. }
  27. /*Cay con trai lech trai*/
  28. void Rotate_Left_Left(AVL_NODE*&root)
  29. {
  30.     AVL_NODE*p;
  31.     p = root->left;
  32.     root->left = p->right;
  33.     p->right = root;
  34.     switch (p->balance_factor)
  35.     {
  36.     case -1:
  37.     {
  38.                root->balance_factor = Equal_height;
  39.                p->balance_factor = Equal_height;
  40.                break;
  41.     }
  42.     case 0:
  43.         p->balance_factor = Right_higher;
  44.         root->balance_factor = Left_higher;
  45.         break;
  46.     }
  47.     root = p;
  48. }
  49. /*Cay con phai lech phai*/
  50. void Rotate_Right_Right(AVL_NODE*&root)
  51. {
  52.     AVL_NODE*p;
  53.     p = root->right;
  54.     root->right = p->left;
  55.     p->left = root;
  56.     switch (p->balance_factor)
  57.     {
  58.     case 0:
  59.         root->balance_factor = Right_higher;
  60.         p->balance_factor = Equal_height;
  61.         break;
  62.     case 1:
  63.         p->balance_factor = Equal_height;
  64.         root->balance_factor = Equal_height;
  65.         break;
  66.     }
  67.     root = p;
  68. }
  69. //Cay con phai lech trai
  70. void Rotate_Right_Left(AVL_NODE* &root)
  71. {
  72.     AVL_NODE *p1, *p2;
  73.  
  74.     p1 = root->right;
  75.     p2 = p1->left;
  76.     root->right = p2->left;
  77.     p1->left = p2->right;
  78.     p2->left = root;
  79.     p2->right = p1;
  80.  
  81.     switch (p2->balance_factor)
  82.     {
  83.     case -1:
  84.         root->balance_factor = Equal_height;
  85.         p1->balance_factor = Right_higher;
  86.         break;
  87.     case 0:
  88.         root->balance_factor = Equal_height;
  89.         p1->balance_factor = Equal_height;
  90.         break;
  91.     case 1:
  92.         root->balance_factor = Left_higher;
  93.         p1->balance_factor = Equal_height;
  94.         break;
  95.     }
  96.  
  97.     p2->balance_factor = Equal_height;
  98.     root = p2;
  99. }
  100. /*Cay con trai lech phai*/
  101. void Rotate_Left_Right(AVL_NODE* &root)
  102. {
  103.     AVL_NODE *p1, *p2;
  104.  
  105.     p1 = root->left;
  106.     p2 = p1->right;
  107.     root->left = p2->right;
  108.     p1->right = p2->left;
  109.     p2->right = root;
  110.     p2->left = p1;
  111.  
  112.     switch (p2->balance_factor)
  113.     {
  114.     case -1:
  115.         p1->balance_factor = Equal_height;
  116.         root->balance_factor = Right_higher;
  117.         break;
  118.     case 0:
  119.         root->balance_factor = Equal_height;
  120.         p1->balance_factor = Equal_height;
  121.         break;
  122.     case 1:
  123.         root->balance_factor = Equal_height;
  124.         p1->balance_factor = Left_higher;
  125.         break;
  126.     }
  127.  
  128.     p2->balance_factor = Equal_height;
  129.     root = p2;
  130. }
  131. /*Can bang khi cay lech trai*/
  132. int BalanceLeft(AVL_NODE* &root)
  133. {
  134.     AVL_NODE *p;
  135.  
  136.     p = root->left;
  137.  
  138.     switch (p->balance_factor)
  139.     {
  140.     case -1:
  141.         Rotate_Left_Left(root);
  142.         return 2;
  143.     case 0:
  144.         Rotate_Left_Left(root);
  145.         return 1;
  146.     case 1:
  147.         Rotate_Left_Right(root);
  148.         return 2;
  149.     }
  150. }
  151. /*Can bang khi cay lech phai*/
  152. int BalanceRight(AVL_NODE* &root)
  153. {
  154.     AVL_NODE *p;
  155.  
  156.     p = root->right;
  157.  
  158.     switch (p->balance_factor)
  159.     {
  160.     case 1:
  161.         Rotate_Right_Right(root);
  162.         return 2;
  163.     case 0:
  164.         Rotate_Right_Right(root);
  165.         return 1;
  166.     case -1:
  167.         Rotate_Right_Left(root);
  168.         return 2;
  169.     }
  170. }
  171. /*Insert node to the tree*/
  172. int InsertNode(AVL_NODE* &root, int x)
  173. {
  174.     int Res;
  175.     if (root)
  176.     {
  177.         //gia tri da co trong cay
  178.         if (root->data == x) return 0;
  179.  
  180.         //Root->x > x
  181.         //chen vao ben trai
  182.         if (root->data > x)
  183.         {
  184.             Res = InsertNode(root->left, x);
  185.             if (Res < 2) return Res;
  186.  
  187.             //Res >= 2
  188.             switch (root->balance_factor)
  189.             {
  190.             case 1:
  191.                 root->balance_factor = Equal_height;
  192.                 return 1;
  193.             case 0:
  194.                 root->balance_factor = Left_higher;
  195.                 return 2;
  196.             case -1:
  197.                 BalanceLeft(root);
  198.                 return 1;
  199.             }
  200.         }
  201.  
  202.         //Root->x < x
  203.         //chen vao ben phai
  204.         else
  205.         {
  206.             Res = InsertNode(root->right, x);
  207.  
  208.             if (Res < 2) return Res;
  209.  
  210.             //Res >= 2
  211.             switch (root->balance_factor)
  212.             {
  213.             case -1:
  214.                 root->balance_factor = Equal_height;
  215.                 return 1;
  216.             case 0:
  217.                 root->balance_factor = Right_higher;
  218.                 return 2;
  219.             case 1:
  220.                 BalanceRight(root);
  221.                 return 1;
  222.             }
  223.         }
  224.     }
  225.  
  226.     root = CreateAVLNode(x);
  227.     return 2;
  228. }
  229. void Duyet_NLR(AVL_NODE*root)
  230. {
  231.     if (root != NULL)
  232.     {
  233.         Duyet_NLR(root->left);
  234.         cout << root->data << " ";
  235.         //Duyet_NLR(root->left);
  236.         Duyet_NLR(root->right);
  237.     }
  238. }
  239. /*Question 2*/
  240. int SearchStandFor(AVL_NODE*&root, AVL_NODE*&p)
  241. {
  242.     int Res;
  243.  
  244.     if (p->left)
  245.     {
  246.         Res = SearchStandFor(root, p->left);
  247.  
  248.         if (Res < 2) return Res;
  249.  
  250.         switch (p->balance_factor)
  251.         {
  252.         case -1:
  253.             p->balance_factor = Equal_height;
  254.             return 1;
  255.         case 0:
  256.             p->balance_factor = Right_higher;
  257.             return 2;
  258.         case 1:
  259.             return BalanceRight(p);
  260.         }
  261.  
  262.     }
  263.  
  264.     root->data = p->data;
  265.     root = p;
  266.     p = p->right;
  267.     return 2;
  268. }
  269. int DelNode(AVL_NODE* &root, int x)
  270. {
  271.     int Res;
  272.  
  273.     //Khong ton tai node nay tren cay
  274.     if (!root) return 0;
  275.  
  276.     //Qua duoc if tren tuc la ton tai
  277.     //Root->x > x => Sang ben trai tim xoa
  278.     if (root->data > x)
  279.     {
  280.         Res = DelNode(root->left, x);
  281.  
  282.         //Neu co xoa thi Res = 1 hoac 2. 2 tuc thay doi chieu cao cay
  283.         if (Res < 2) return Res;
  284.  
  285.         //Chieu cao bi thay doi
  286.         switch (root->balance_factor)
  287.         {
  288.         case -1:
  289.             root->balance_factor = Equal_height;
  290.             return 2;
  291.         case 0:
  292.             root->balance_factor = Right_higher;
  293.             return 1;
  294.         case 1:
  295.             return BalanceRight(root);
  296.         }
  297.     }
  298.  
  299.     if (root->data < x)
  300.     {
  301.         Res = DelNode(root->right, x);
  302.  
  303.         if (Res < 2) return Res;
  304.  
  305.         switch (root->balance_factor)
  306.         {
  307.         case -1:
  308.             return BalanceLeft(root);
  309.         case 0:
  310.             root->balance_factor = Left_higher;
  311.             return 1;
  312.         case 1:
  313.             root->balance_factor = Equal_height;
  314.             return 2;
  315.         }
  316.     }
  317.     else
  318.     {
  319.         //Root->x = x
  320.         AVL_NODE *p1 = root;
  321.  
  322.         if (root->left == NULL)
  323.         {
  324.             root = root->right;
  325.             Res = 2;
  326.         }
  327.         else
  328.         {
  329.             if (root->right == NULL)
  330.             {
  331.                 root = root->left;
  332.                 Res = 2;
  333.             }
  334.             else
  335.             {
  336.                 Res = SearchStandFor(p1, root->right);
  337.  
  338.                 if (Res < 2) return Res;
  339.                 switch (root->balance_factor)
  340.                 {
  341.                 case 1:
  342.                     root->balance_factor = Equal_height;
  343.                     return 2;
  344.                 case 0:
  345.                     root->balance_factor = Left_higher;
  346.                     return 1;
  347.                 case -1:
  348.                     return BalanceRight(root);
  349.                 }
  350.             }
  351.             delete p1;
  352.             return Res;
  353.         }
  354.     }
  355. }
  356. int main()
  357. {
  358.     AVL_NODE*root = NULL;
  359.     int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
  360.     int d1;
  361.     x1 = InsertNode(root, 4);
  362.     cout << x1 << endl;
  363.     x2 = InsertNode(root, 5);
  364.     cout << x2 << endl;
  365.     x3 = InsertNode(root, 7);
  366.     cout << x3 << endl;
  367.     x4 = InsertNode(root, 10);
  368.     cout << x4 << endl;
  369.     x5 = InsertNode(root, 20);
  370.     cout << x5 << endl;
  371.     x6 = InsertNode(root, 9);
  372.     cout << x6 << endl;
  373.     x7 = InsertNode(root, 11);
  374.     cout << x7 << endl;
  375.     x8 = InsertNode(root, 13);
  376.     cout << x8 << endl;
  377.  
  378.     cout << "delete\n";
  379.     d1 = DelNode(root, 7);
  380.     cout << d1 << endl;
  381.  
  382.     Duyet_NLR(root);
  383.     system("pause");
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement