Advertisement
huutho_96

AVL Tree

Jun 4th, 2015
5,829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.36 KB | None | 0 0
  1. //https://www.facebook.com/pages/C%C3%B9ng-h%E1%BB%8Dc-l%E1%BA%ADp-tr%C3%ACnh/632038696941833
  2. //Xin lỗi mọi người code sẽ không giống trong video vì mình không nhìn hết các trường hợp, mọi thắc mắc hay phản hồi các bạn vui lòng inbox lên page trên nha
  3. #include <iostream>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. #define LH -1
  8. #define EH 0
  9. #define RH 1
  10.  
  11. struct AVLNode
  12. {
  13.     int x;
  14.     char CSCB;
  15.     AVLNode *pLeft;
  16.     AVLNode *pRight;
  17. };
  18.  
  19. typedef AVLNode * AVLTree;
  20.  
  21. void CreateAVLTree(AVLTree &Root)
  22. {
  23.     Root = NULL;
  24. }
  25.  
  26. AVLNode *CreateAVLNode(int x)
  27. {
  28.     AVLNode *p = new AVLNode;
  29.  
  30.     if (!p) exit(1);
  31.  
  32.     p->CSCB = EH;
  33.     p->x = x;
  34.     p->pLeft = NULL;
  35.     p->pRight = NULL;
  36.  
  37.     return p;
  38. }
  39.  
  40. //cay con trai lech trai
  41. void Rotate_Left_Left(AVLTree &Root)
  42. {
  43.     AVLNode *p;
  44.  
  45.     p = Root->pLeft;
  46.     Root->pLeft = p->pRight;
  47.     p->pRight = Root;
  48.  
  49.     switch (p->CSCB)
  50.     {
  51.     case LH:
  52.         Root->CSCB = EH;
  53.         p->CSCB = EH;
  54.         break;
  55.     case EH:
  56.         p->CSCB = RH;
  57.         Root->CSCB = LH;
  58.         break;
  59.     }
  60.  
  61.     Root = p;
  62. }
  63.  
  64. //cay con phai lech phai
  65. void Rotate_Right_Right(AVLTree &Root)
  66. {
  67.     AVLNode *p;
  68.  
  69.     p = Root->pRight;
  70.     Root->pRight = p->pLeft;
  71.     p->pLeft = Root;
  72.  
  73.     switch (p->CSCB)
  74.     {
  75.     case EH:
  76.         Root->CSCB = RH;
  77.         p->CSCB = EH;
  78.         break;
  79.     case RH:
  80.         p->CSCB = EH;
  81.         Root->CSCB = EH;
  82.         break;
  83.     }
  84.  
  85.     Root = p;
  86. }
  87.  
  88. //cay con phai lech trai
  89. void Rotate_Right_Left(AVLTree &Root)
  90. {
  91.     AVLNode *p1, *p2;
  92.  
  93.     p1 = Root->pRight;
  94.     p2 = p1->pLeft;
  95.     Root->pRight = p2->pLeft;
  96.     p1->pLeft = p2->pRight;
  97.     p2->pLeft = Root;
  98.     p2->pRight = p1;
  99.  
  100.     switch (p2->CSCB)
  101.     {
  102.     case LH:
  103.         Root->CSCB = EH;
  104.         p1->CSCB = RH;
  105.         break;
  106.     case EH:
  107.         Root->CSCB = EH;
  108.         p1->CSCB = EH;
  109.         break;
  110.     case RH:
  111.         Root->CSCB = LH;
  112.         p1->CSCB = EH;
  113.         break;
  114.     }
  115.  
  116.     p2->CSCB = EH;
  117.     Root = p2;
  118. }
  119.  
  120. //cay con trai lech phai
  121. void Rotate_Left_Right(AVLTree &Root)
  122. {
  123.     AVLNode *p1, *p2;
  124.  
  125.     p1 = Root->pLeft;
  126.     p2 = p1->pRight;
  127.     Root->pLeft = p2->pRight;
  128.     p1->pRight = p2->pLeft;
  129.     p2->pRight = Root;
  130.     p2->pLeft = p1;
  131.  
  132.     switch (p2->CSCB)
  133.     {
  134.     case LH:
  135.         p1->CSCB = EH;
  136.         Root->CSCB = RH;
  137.         break;
  138.     case EH:
  139.         Root->CSCB = EH;
  140.         p1->CSCB = EH;
  141.         break;
  142.     case RH:
  143.         Root->CSCB = EH;
  144.         p1->CSCB = LH;
  145.         break;
  146.     }
  147.  
  148.     p2->CSCB = EH;
  149.     Root = p2;
  150. }
  151.  
  152. //Can bang khi cay lech trai
  153. int BalanceLeft(AVLTree &Root)
  154. {
  155.     AVLNode *p;
  156.  
  157.     p = Root->pLeft;
  158.  
  159.     switch (p->CSCB)
  160.     {
  161.     case LH:
  162.         Rotate_Left_Left(Root);
  163.         return 2;
  164.     case EH:
  165.         Rotate_Left_Left(Root);
  166.         return 1;
  167.     case RH:
  168.         Rotate_Left_Right(Root);
  169.         return 2;
  170.     }
  171. }
  172.  
  173. //can bang cay lech phai
  174. int BalanceRight(AVLTree &Root)
  175. {
  176.     AVLNode *p;
  177.  
  178.     p = Root->pRight;
  179.  
  180.     switch (p->CSCB)
  181.     {
  182.     case RH:
  183.         Rotate_Right_Right(Root);
  184.         return 2;
  185.     case EH:
  186.         Rotate_Right_Right(Root);
  187.         return 1;
  188.     case LH:
  189.         Rotate_Right_Left(Root);
  190.         return 2;
  191.     }
  192. }
  193.  
  194. //Chen 1 node vao cay AVL
  195. int InsertNode(AVLTree &Root, int x)
  196. {
  197.     int Res;
  198.     if (Root)
  199.     {
  200.         //gia tri da co trong cay
  201.         if (Root->x == x) return 0;
  202.  
  203.         //Root->x > x
  204.         //chen vao ben trai
  205.         if (Root->x > x)
  206.         {
  207.             Res = InsertNode(Root->pLeft, x);
  208.             if (Res < 2) return Res;
  209.  
  210.             //Res >= 2
  211.             switch (Root->CSCB)
  212.             {
  213.             case RH:
  214.                 Root->CSCB = EH;
  215.                 return 1;
  216.             case EH:
  217.                 Root->CSCB = LH;
  218.                 return 2;
  219.             case LH:
  220.                 BalanceLeft(Root);
  221.                 return 1;
  222.             }
  223.         }
  224.  
  225.         //Root->x < x
  226.         //chen vao ben phai
  227.         else
  228.         {
  229.             Res = InsertNode(Root->pRight, x);
  230.  
  231.             if (Res < 2) return Res;
  232.  
  233.             //Res >= 2
  234.             switch (Root->CSCB)
  235.             {
  236.             case LH:
  237.                 Root->CSCB = EH;
  238.                 return 1;
  239.             case EH:
  240.                 Root->CSCB = RH;
  241.                 return 2;
  242.             case RH:
  243.                 BalanceRight(Root);
  244.                 return 1;
  245.             }
  246.         }
  247.     }
  248.  
  249.     Root = CreateAVLNode(x);
  250.     return 2;
  251. }
  252.  
  253. //Tim node the mang
  254. int SearchStandFor(AVLTree &Root, AVLNode *&p)
  255. {
  256.     int Res;
  257.  
  258.     if (p->pLeft)
  259.     {
  260.         Res = SearchStandFor(Root, p->pLeft);
  261.  
  262.         if (Res < 2) return Res;
  263.  
  264.         switch (p->CSCB)
  265.         {
  266.         case LH:
  267.             p->CSCB = EH;
  268.             return 1;
  269.         case EH:
  270.             p->CSCB = RH;
  271.             return 2;
  272.         case RH:
  273.             return BalanceRight(Root);
  274.         }
  275.     }
  276.  
  277.     Root->x = p->x;
  278.     Root = p;
  279.     p = p->pRight;
  280.     return 2;
  281. }
  282.  
  283. //Xoa 1 node tren cay
  284. int DelNode(AVLTree &Root, int x)
  285. {
  286.     int Res;
  287.  
  288.     //Khong ton tai node nay tren cay
  289.     if (!Root) return 0;
  290.  
  291.     //Qua duoc if tren tuc la ton tai
  292.     //Root->x > x => Sang ben trai tim xoa
  293.     if (Root->x > x)
  294.     {
  295.         Res = DelNode(Root->pLeft, x);
  296.  
  297.         //Neu co xoa thi Res = 1 hoac 2. 2 tuc thay doi chieu cao cay
  298.         if (Res < 2) return Res;
  299.  
  300.         //Chieu cao bi thay doi
  301.         switch (Root->CSCB)
  302.         {
  303.         case LH:
  304.             Root->CSCB = EH;
  305.             return 2;
  306.         case EH:
  307.             Root->CSCB = RH;
  308.             return 1;
  309.         case RH:
  310.             return BalanceRight(Root);
  311.         }
  312.     }
  313.  
  314.     if (Root->x < x)
  315.     {
  316.         Res = DelNode(Root->pRight, x);
  317.  
  318.         if (Res < 2) return Res;
  319.  
  320.         switch (Root->CSCB)
  321.         {
  322.         case LH:
  323.             return BalanceLeft(Root);
  324.         case EH:
  325.             Root->CSCB = LH;
  326.             return 1;
  327.         case RH:
  328.             Root->CSCB = EH;
  329.             return 2;
  330.         }
  331.     }
  332.     else
  333.     {
  334.         //Root->x = x
  335.         AVLNode *p1 = Root;
  336.  
  337.         if (Root->pLeft == NULL)
  338.         {
  339.             Root = Root->pRight;
  340.             Res = 2;
  341.         }
  342.         else
  343.         {
  344.             if (Root->pRight == NULL)
  345.             {
  346.                 Root = Root->pLeft;
  347.                 Res = 2;
  348.             }
  349.             else
  350.             {
  351.                 Res = SearchStandFor(p1, Root->pRight);
  352.  
  353.                 if (Res < 2) return Res;
  354.                 switch (Root->CSCB)
  355.                 {
  356.                 case RH:
  357.                     Root->CSCB = EH;
  358.                     return 2;
  359.                 case EH:
  360.                     Root->CSCB = LH;
  361.                     return 1;
  362.                 case LH:
  363.                     return BalanceRight(Root);
  364.                 }
  365.             }
  366.             delete p1;
  367.             return Res;
  368.         }
  369.     }
  370.  
  371. }
  372.  
  373. //Duyet theo muc
  374. void Level(AVLTree Root)
  375. {
  376.     queue<AVLTree> q;
  377.     AVLTree p;
  378.  
  379.     if (Root == NULL) return;
  380.  
  381.     p = Root;
  382.     q.push(p);
  383.  
  384.     while (!q.empty())
  385.     {
  386.         p = q.front();
  387.         q.pop();
  388.         cout << p->x << endl;
  389.  
  390.         if (p->pLeft) q.push(p->pLeft);
  391.         if (p->pRight) q.push(p->pRight);
  392.     }
  393. }
  394.  
  395. void Input(AVLTree &Root)
  396. {
  397.     int x;
  398.     do
  399.     {
  400.         cout << "x = 0 de thoat: x = ";
  401.         cin >> x;
  402.         if (x == 0) break;
  403.         InsertNode(Root, x);
  404.     } while (1);
  405. }
  406.  
  407. void main()
  408. {
  409.     AVLTree Root;
  410.     CreateAVLTree(Root);
  411.     InsertNode(Root, 866);
  412.     InsertNode(Root, 790);
  413.     InsertNode(Root, 879);
  414.     InsertNode(Root, 777);
  415.     InsertNode(Root, 864);
  416.     InsertNode(Root, 870);
  417.     InsertNode(Root, 900);
  418.     InsertNode(Root, 475);
  419.     InsertNode(Root, 863);
  420.     InsertNode(Root, 865);
  421.     InsertNode(Root, 867);
  422.     InsertNode(Root, 875);
  423.     InsertNode(Root, 888);
  424.     InsertNode(Root, 915);
  425.     DelNode(Root, 777);
  426.     DelNode(Root, 790);
  427.     Level(Root);
  428.     system("pause");
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement