编程练习——平衡树(AVLTree)

AVLTree本身就是二叉查找树。为了保证查找效率在O(nlogn),所以有时要对树高进行必要的调整。

AVLTree拥有自己的法则使得插入、删除、查找都在O(nlogn)时间内完成。

下面写了一个AVLTreeNode类,好像和TreeNode类一样。

 

下面写AVLTree类,这个类重要的地方在与插入和删除操作。查找操作和BinarySearchTree一致。但是至今还没有找到一个有效的方法来使用BinarySearchTree中的Search。除非将Search方法抽象为一个接口。

暂时先不管太多。

先看左旋和右旋操作。

  1. // return the subRoot
  2.     //        a                   b
  3.     //       /                  /   /
  4.     //      b        ->        c     a
  5.     //     /
  6.     //    c
  7.     AVLNode<T>* LLRotate(AVLNode<T>* a, AVLNode<T>* b)
  8.     {
  9.         a->setLeft(b->getRight());
  10.         b->setRight(a);
  11.         
  12.         a->setAHeight(0);
  13.         b->setAHeight(0);
  14.         //a->setAHeight(getAVLHeight(a));
  15.         //b->setAHeight(getAVLHeight(b));
  16.         return b;
  17.     }
  18.     // return the subRoot
  19.     //        a                      b
  20.     //          /                  /   /
  21.     //            b        ->     a     c
  22.     //              /
  23.     //                c
  24.     AVLNode<T>* RRRotate(AVLNode<T>* a, AVLNode<T>* b)
  25.     {
  26.         a->setRight(b->getLeft());
  27.         b->setLeft(a);
  28.         
  29.         a->setAHeight(0);
  30.         b->setAHeight(0);
  31.         //a->setAHeight(getAVLHeight(a));
  32.         //b->setAHeight(getAVLHeight(b));
  33.         return b;
  34.     }
  35.     // return the subRoot
  36.     //        a                      c
  37.     //          /                  /   /
  38.     //            b        ->     a     b
  39.     //           /                 /      
  40.     //          c                   d  
  41.     //         /
  42.     //        d
  43.     AVLNode<T>* RLRotate(AVLNode<T>* a, AVLNode<T>* b, AVLNode<T>* c)
  44.     {
  45.         
  46.         b->setLeft(c->getRight());
  47.         c->setRight(b);
  48.         a->setRight(c->getLeft());
  49.         c->setLeft(a);
  50.         
  51.         
  52.         //a->setAHeight(getAVLHeight(a));
  53.         //c->setAHeight(getAVLHeight(c));
  54.         //b->setAHeight(getAVLHeight(b));
  55.         if(c->getAbsoluteAHeight() == 1)
  56.         {
  57.             if(b->getAHeight() == 0)
  58.             {
  59.                 b->setAHeight(-1);
  60.             }
  61.             else
  62.             {
  63.                 b->setAHeight(0);
  64.             }
  65.             a->setAHeight(1);
  66.             c->setAHeight(0);
  67.         
  68.         }
  69.         else
  70.         {
  71.             if(b->getAHeight() == 0)
  72.             {
  73.                 b->setAHeight(-1);
  74.             }
  75.             else
  76.             {
  77.                 b->setAHeight(0);
  78.             }
  79.             a->setAHeight(0);
  80.             c->setAHeight(0);
  81.             
  82.         }
  83.         
  84.         
  85.         return c;
  86.     }
  87.     // return the subRoot
  88.     //        a                      c
  89.     //       /                     /   /
  90.     //      b              ->     b     a
  91.     //       /                         /       
  92.     //        c                       d   
  93.     //         /
  94.     //          d
  95.     AVLNode<T>* LRRotate(AVLNode<T>* a, AVLNode<T>* b, AVLNode<T>* c)
  96.     {
  97.         b->setRight(c->getLeft());
  98.         c->setLeft(b);
  99.         a->setLeft(c->getRight());
  100.         c->setRight(a);
  101.         
  102.         
  103.         //a->setAHeight(getAVLHeight(a));
  104.         //c->setAHeight(getAVLHeight(c));
  105.         //b->setAHeight(getAVLHeight(b));
  106.         if(c->getAbsoluteAHeight() == 1)
  107.         {
  108.             if(b->getAHeight() == 0)
  109.             {
  110.                 b->setAHeight(1);
  111.             }
  112.             else
  113.             {
  114.                 b->setAHeight(0);
  115.             }
  116.             a->setAHeight(-1);
  117.             c->setAHeight(0);
  118.             
  119.         }
  120.         else
  121.         {
  122.             if(b->getAHeight() == 0)
  123.             {
  124.                 b->setAHeight(1);
  125.             }
  126.             else
  127.             {
  128.                 b->setAHeight(0);
  129.             }
  130.             a->setAHeight(0);
  131.             c->setAHeight(0);
  132.         
  133.         }
  134.         
  135.         
  136.         
  137.         return c;
  138.     }

这里旋转后调整平衡因子是关键部分。这是我自己总结的方法,经少量测试暂时没有出现问题。这个调整方法好像与大多数书本和网络程序的方法不一致。不清楚到底哪个是正确的。但是如果不使用这种直接赋值的方法,可以使用递归查找树高的方法保障万无一失的解决这个问题。这里使用的右树高减左树高得平衡因子的方法。

下面给出这样的两个函数,其中getAVLHeight是核心方法。

 

  1.     int getHeight(AVLNode<T>* node)
  2.     {
  3.         if(node == NULL)
  4.         {
  5.             return 0;
  6.         }
  7.         else
  8.         {
  9.             int leftHeight = getHeight(node->getLeft());
  10.             int rightHeight = getHeight(node->getRight());
  11.             if(leftHeight > rightHeight)
  12.             {
  13.                 return leftHeight+1;
  14.             }
  15.             else
  16.             {
  17.                 return rightHeight+1;
  18.             }
  19.         }
  20.     }
  21.     int getAVLHeight(AVLNode<T>* node)
  22.     {
  23.         if(node == NULL)
  24.         {
  25.             return 0;
  26.         }
  27.         else
  28.         {
  29.             int leftHeight = getHeight(node->getLeft());
  30.             int rightHeight = getHeight(node->getRight());
  31.             return rightHeight - leftHeight;
  32.         }
  33.     }

删除、插入操作有点复杂。

插入操作:

1.先找到可能要调整的那个节点A,这里的可能要调整的节点即为插入路径中平衡因子为+1或者为-1的点。(这里一定要记得不要自作聪明的认为如果向左插入,+1节点就不会是可能节点)

2.插入应该插入的节点,调整从A至目标节点的平衡因子。并且记录是否增长了子树树高(是否插入节点没有兄弟节点)。

3.插入后查看是否增长子树树高,如果增长树高,再查看A是否已经不平衡。如果不平衡,调整,否则,插入完成。

(这里要注意A节点为树根的情况,或者无A节点的情况)

因为下面的程序首先将树根设定为可能要调整的节点A,所以没有出现无A节点的情况。

以上三点,我看过很多教科书,发现讲解有些含糊,或者本身就是略写。这里并没有采用递归操作。

 

  1.     void insertNode(AVLNode<T>* node)
  2.     {
  3.         AVLNode<T>* subRoot = this->head;
  4.         if(subRoot == NULL)
  5.         {
  6.             subRoot = node;
  7.             return;
  8.         }
  9.         AVLNode<T>* ppoRoot = NULL;
  10.         AVLNode<T>* poRoot = NULL;
  11.         AVLNode<T>* pRoot = NULL;
  12.         
  13.     
  14.         poRoot = subRoot;
  15.         
  16.         while(subRoot != NULL)
  17.         {
  18.             pRoot = subRoot;
  19.             if(subRoot->getData() > node->getData())
  20.             {
  21.                 
  22.                 if(isPossibleNode(subRoot->getLeft(),node))
  23.                 {
  24.                     poRoot = subRoot->getLeft();
  25.                     ppoRoot = subRoot;
  26.                 }
  27.                 subRoot = subRoot->getLeft();
  28.             }
  29.             else if(subRoot->getData() < node->getData())
  30.             {
  31.                 
  32.                 if(isPossibleNode(subRoot->getRight(),node))
  33.                 {
  34.                     poRoot = subRoot->getRight();
  35.                     ppoRoot = subRoot;
  36.                 }
  37.                 subRoot = subRoot->getRight();
  38.             }
  39.             else
  40.             {
  41.                 throw "the same key";
  42.             }
  43.         }
  44.         bool isChangeHeight = false;
  45.         if(pRoot->getData() > node->getData())
  46.         {
  47.         
  48.             pRoot->setLeft(node);
  49.             if(pRoot->getRight() == NULL)
  50.             {
  51.                 isChangeHeight = true;
  52.             }
  53.             else
  54.             {
  55.                 pRoot->setAHeight(pRoot->getAHeight()-1);
  56.             }
  57.         }
  58.         else if(pRoot->getData() < node->getData())
  59.         {
  60.             
  61.             pRoot->setRight(node);
  62.             if(pRoot->getLeft() == NULL)
  63.             {
  64.                 isChangeHeight = true;
  65.             }
  66.             else
  67.             {
  68.                 pRoot->setAHeight(pRoot->getAHeight()+1);
  69.             }
  70.         }
  71.         if(poRoot != NULL && isChangeHeight)
  72.         {
  73.             // s->a update Height
  74.             subRoot = poRoot;
  75.             while(subRoot != node)
  76.             {
  77.                 if(subRoot->getData() > node->getData())
  78.                 {
  79.                     subRoot->setAHeight(subRoot->getAHeight()-1);
  80.                     
  81.                     subRoot = subRoot->getLeft();
  82.                 }
  83.                 else if(subRoot->getData() < node->getData())
  84.                 {
  85.                     subRoot->setAHeight(subRoot->getAHeight()+1);
  86.                     
  87.                     subRoot = subRoot->getRight();
  88.                 }
  89.             }
  90.             subRoot = poRoot;
  91.             AVLNode<T>* a = poRoot;
  92.             AVLNode<T>* b = NULL;
  93.             AVLNode<T>* c = NULL;
  94.             AVLNode<T>* tempRoot = NULL;
  95.             if(a->getAbsoluteAHeight() > 1)
  96.             {
  97.                 if(subRoot->getData() > node->getData())
  98.                 {
  99.                     b = subRoot->getLeft();
  100.                     if(a->getAHeight()* b->getAHeight() > 0)
  101.                     {
  102.                         //LLRotate
  103.                         tempRoot = LLRotate(a,b);
  104.                         
  105.                     }
  106.                     else
  107.                     {
  108.                         //LRRotate
  109.                         c = b->getRight();
  110.                         tempRoot =LRRotate(a,b,c);
  111.                     }
  112.                     
  113.                 }
  114.                 else if(subRoot->getData() < node->getData())
  115.                 {
  116.                     b = subRoot->getRight();
  117.                     if(a->getAHeight()* b->getAHeight() > 0)
  118.                     {
  119.                         //RRRotate
  120.                         tempRoot = RRRotate(a,b);
  121.                     }
  122.                     else
  123.                     {
  124.                         //RLRotate
  125.                         c = b->getLeft();
  126.                         tempRoot =RLRotate(a,b,c);
  127.                     }
  128.                 }
  129.                 if(ppoRoot!=NULL)
  130.                 {
  131.                     if(ppoRoot->getData() > tempRoot->getData())
  132.                     {
  133.                         ppoRoot->setLeft(tempRoot);
  134.                     }
  135.                     else
  136.                     {
  137.                         ppoRoot->setRight(tempRoot);
  138.                     }
  139.                 }
  140.                 else
  141.                 {
  142.                     this->head = tempRoot;
  143.                 }
  144.             }
  145.             
  146.         }
  147.     }

再看删除操作。

采用的是http://www.cs.uga.edu/~eileen/2720/Notes/AVLTrees-II.ppt这个ppt介绍的删除操作。

因为删除操作要反遍历到树根,所以这里无奈采用是递归操作。

bool deleteNode(AVLNode<T>* subRoot, AVLNode<T>* prev, T& node);

这个操作返回值:如果子树树高变化,那么返回true,否则返回false。

1.如果子树为Null,函数返回false

2.比较subRoot里的data值和node中的data值。

    A.如果subRoot->data< node.data,需要递归下右子树,如果这个递归返回true,将subRoot的平衡因子调整为-1,如果为false就return false。

    B.subRoot->data > node.data需要递归下左子树,如果这个递归返回true,将subRoot的平衡因子调整为+1,如果为false就return false。

    C.subRoot->data == node.data.

          1)如果此时subRoot只有一个孩子或者没有孩子,那么删除时树高一定会改变。另外做一个简单的删除操作即可。该左孩子代替此节点还是右孩子代替它,如何代替其实和BinarySearchTree一样。再return true

          2)如果有两个孩子,去左子树中找到替代品,然后将这个subRoot的数据域完全变为此替代品后,去左子树中用再用deleteNode方法将替代品删除。如果deleteNode操作返回true,那么显然subRoot的平衡因子要加1.

3.如果subRoot的平衡因子已经为2或者-2,那么以subRoot为根开始调整。

    A.如果调整后subRoot平衡因子为0,return true

    B.不为0,return false。

 

 下面是实际代码

 

  1.     bool deleteNode(AVLNode<T>* subRoot, AVLNode<T>* prev, T& node)
  2.     {
  3.         if(subRoot == NULL)
  4.         {
  5.             return false;
  6.         }
  7.         bool isChangeHeight = false;
  8.         if(subRoot->getData() > node)
  9.         {
  10.             isChangeHeight = deleteNode(subRoot->getLeft(),subRoot,node);
  11.             if(isChangeHeight)
  12.             {
  13.                 subRoot->setAHeight(subRoot->getAHeight()+1);
  14.             }
  15.             
  16.         }
  17.         else if(subRoot->getData() < node)
  18.         {
  19.             isChangeHeight = deleteNode(subRoot->getRight(),subRoot,node);
  20.             if(isChangeHeight)
  21.             {
  22.                 subRoot->setAHeight(subRoot->getAHeight()-1);
  23.             }
  24.             
  25.         }
  26.         else
  27.         {
  28.             AVLNode<T>* assignNode = NULL;
  29.             if(subRoot->getLeft() == NULL && subRoot->getRight() == NULL)
  30.             {
  31.                 // no child
  32.                 isChangeHeight = true;
  33.                 deleteNode(subRoot,prev,assignNode);
  34.                 return isChangeHeight;
  35.             }
  36.             else if(subRoot->getLeft() == NULL && subRoot->getRight() != NULL)
  37.             {
  38.                 // no left child
  39.                 isChangeHeight = true;
  40.                 assignNode = subRoot->getRight();
  41.                 deleteNode(subRoot,prev,assignNode);
  42.                 return isChangeHeight;
  43.                 
  44.             }           
  45.             else if(subRoot->getRight() == NULL && subRoot->getLeft() != NULL)
  46.             {
  47.                 // no right chlid
  48.                 isChangeHeight = true;
  49.                 assignNode = subRoot->getLeft();
  50.                 deleteNode(subRoot,prev,assignNode);
  51.                 return isChangeHeight;
  52.             }
  53.             else
  54.             {
  55.                 // have both children 
  56.                 assignNode = getMaxNode(subRoot->getLeft());
  57.                 T data = assignNode->getData();
  58.                 bool isChange = deleteNode(subRoot->getLeft(),subRoot,data);
  59.                 cout << "use "<< data<< " replace "<< subRoot->getData()<<endl;
  60.                 subRoot->setData(data);// copy all data to subRoot
  61.                 
  62.                 if(isChange)
  63.                 {
  64.                     subRoot->setAHeight(subRoot->getAHeight()+1);
  65.                 }
  66.             }
  67.             if(subRoot->getAbsoluteAHeight() == 2)
  68.             {
  69.                 AVLNode<T>* a = subRoot;
  70.                 AVLNode<T>* b = NULL;
  71.                 AVLNode<T>* c = NULL;
  72.                 AVLNode<T>* tempRoot = NULL;
  73.                 if(subRoot->getAHeight() == 2)
  74.                 {
  75.                     // move to left 
  76.                     b = subRoot->getRight();
  77.                     switch(b->getAHeight())
  78.                     {
  79.                         case 1:
  80.                             tempRoot = RRRotate(a,b);
  81.                             isChangeHeight = true;
  82.                             break;
  83.                         case -1:
  84.                             c = b->getLeft();
  85.                             tempRoot = RLRotate(a,b,c);
  86.                             isChangeHeight = true;
  87.                             break;
  88.                         case 0:
  89.                             tempRoot = RRRotate(a,b);   
  90.                             isChangeHeight = false;
  91.                             break;
  92.                     }
  93.                 }
  94.                 else if(subRoot->getAHeight() == -2)
  95.                 {
  96.                     // move to right
  97.                     b = subRoot->getLeft();
  98.                     switch(b->getAHeight())
  99.                     {
  100.                         case 1:
  101.                             c = b->getRight();
  102.                             tempRoot = LRRotate(a,b,c);
  103.                             isChangeHeight = true;
  104.                             break;
  105.                         case -1:
  106.                             tempRoot = LLRotate(a,b);
  107.                             isChangeHeight = true;
  108.                             break;
  109.                         case 0:
  110.                             tempRoot = LLRotate(a,b);   
  111.                             isChangeHeight = false;
  112.                             break;
  113.                     }   
  114.                 }
  115.                 if(prev == NULL)
  116.                 {
  117.                     this->head = tempRoot;
  118.                 }
  119.                 else
  120.                 {
  121.                     if(prev->getData() > tempRoot->getData())
  122.                     {
  123.                         prev->setLeft(tempRoot);
  124.                     }
  125.                     else
  126.                     {
  127.                         prev->setRight(tempRoot);
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.         return isChangeHeight;
  133.     }

最后贴上AVLTree的完整类代码

 

  1. #include "AVLNode.h"
  2. template<class T>
  3. class AVLTree
  4. {
  5. private:
  6.     AVLNode<T>* head;
  7.     int getHeight(AVLNode<T>* node)
  8.     {
  9.         if(node == NULL)
  10.         {
  11.             return 0;
  12.         }
  13.         else
  14.         {
  15.             int leftHeight = getHeight(node->getLeft());
  16.             int rightHeight = getHeight(node->getRight());
  17.             if(leftHeight > rightHeight)
  18.             {
  19.                 return leftHeight+1;
  20.             }
  21.             else
  22.             {
  23.                 return rightHeight+1;
  24.             }
  25.         }
  26.     }
  27.     int getAVLHeight(AVLNode<T>* node)
  28.     {
  29.         if(node == NULL)
  30.         {
  31.             return 0;
  32.         }
  33.         else
  34.         {
  35.             int leftHeight = getHeight(node->getLeft());
  36.             int rightHeight = getHeight(node->getRight());
  37.             return rightHeight - leftHeight;
  38.         }
  39.     }
  40.     void clearSubTree(AVLNode<T>* node)
  41.     {
  42.         if(node != NULL)
  43.         {
  44.             clearSubTree(node->getLeft());
  45.             clearSubTree(node->getRight());
  46.             delete node;
  47.         }
  48.     }
  49.     void printTree(AVLNode<T>* subTree, int count)
  50.     {
  51.         if(subTree!=NULL)
  52.         {
  53.             
  54.             printTree(subTree->getRight(), count+1);
  55.             for(int i = 0; i < count; i++)
  56.             {
  57.                 cout << "   ";
  58.             }
  59.             cout <<subTree->getData()<<endl;
  60.             printTree(subTree->getLeft(),count+1);
  61.         }
  62.     }
  63.     void printInMidOrder(AVLNode<T>* subTree)
  64.     {
  65.         if(subTree!=NULL)
  66.         {
  67.             printInMidOrder(subTree->getLeft());
  68.             
  69.             cout <<subTree->getData()<<endl;
  70.             printInMidOrder(subTree->getRight());
  71.         }
  72.     }
  73.     bool isPossibleNode(AVLNode<T>* node, AVLNode<T>* comNode)
  74.     {
  75.         if(node != NULL && node->getAbsoluteAHeight() == 1)
  76.         {
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81.     void insertNode(AVLNode<T>* node)
  82.     {
  83.         AVLNode<T>* subRoot = this->head;
  84.         if(subRoot == NULL)
  85.         {
  86.             subRoot = node;
  87.             return;
  88.         }
  89.         AVLNode<T>* ppoRoot = NULL;
  90.         AVLNode<T>* poRoot = NULL;
  91.         AVLNode<T>* pRoot = NULL;
  92.         
  93.     
  94.         poRoot = subRoot;
  95.         
  96.         while(subRoot != NULL)
  97.         {
  98.             pRoot = subRoot;
  99.             if(subRoot->getData() > node->getData())
  100.             {
  101.                 
  102.                 if(isPossibleNode(subRoot->getLeft(),node))
  103.                 {
  104.                     poRoot = subRoot->getLeft();
  105.                     ppoRoot = subRoot;
  106.                 }
  107.                 subRoot = subRoot->getLeft();
  108.             }
  109.             else if(subRoot->getData() < node->getData())
  110.             {
  111.                 
  112.                 if(isPossibleNode(subRoot->getRight(),node))
  113.                 {
  114.                     poRoot = subRoot->getRight();
  115.                     ppoRoot = subRoot;
  116.                 }
  117.                 subRoot = subRoot->getRight();
  118.             }
  119.             else
  120.             {
  121.                 throw "the same key";
  122.             }
  123.         }
  124.         bool isChangeHeight = false;
  125.         if(pRoot->getData() > node->getData())
  126.         {
  127.         
  128.             pRoot->setLeft(node);
  129.             if(pRoot->getRight() == NULL)
  130.             {
  131.                 isChangeHeight = true;
  132.             }
  133.             else
  134.             {
  135.                 pRoot->setAHeight(pRoot->getAHeight()-1);
  136.             }
  137.         }
  138.         else if(pRoot->getData() < node->getData())
  139.         {
  140.             
  141.             pRoot->setRight(node);
  142.             if(pRoot->getLeft() == NULL)
  143.             {
  144.                 isChangeHeight = true;
  145.             }
  146.             else
  147.             {
  148.                 pRoot->setAHeight(pRoot->getAHeight()+1);
  149.             }
  150.         }
  151.         if(poRoot != NULL && isChangeHeight)
  152.         {
  153.             // s->a update Height
  154.             subRoot = poRoot;
  155.             while(subRoot != node)
  156.             {
  157.                 if(subRoot->getData() > node->getData())
  158.                 {
  159.                     subRoot->setAHeight(subRoot->getAHeight()-1);
  160.                     
  161.                     subRoot = subRoot->getLeft();
  162.                 }
  163.                 else if(subRoot->getData() < node->getData())
  164.                 {
  165.                     subRoot->setAHeight(subRoot->getAHeight()+1);
  166.                     
  167.                     subRoot = subRoot->getRight();
  168.                 }
  169.             }
  170.             subRoot = poRoot;
  171.             AVLNode<T>* a = poRoot;
  172.             AVLNode<T>* b = NULL;
  173.             AVLNode<T>* c = NULL;
  174.             AVLNode<T>* tempRoot = NULL;
  175.             if(a->getAbsoluteAHeight() > 1)
  176.             {
  177.                 if(subRoot->getData() > node->getData())
  178.                 {
  179.                     b = subRoot->getLeft();
  180.                     if(a->getAHeight()* b->getAHeight() > 0)
  181.                     {
  182.                         //LLRotate
  183.                         tempRoot = LLRotate(a,b);
  184.                         
  185.                     }
  186.                     else
  187.                     {
  188.                         //LRRotate
  189.                         c = b->getRight();
  190.                         tempRoot =LRRotate(a,b,c);
  191.                     }
  192.                     
  193.                 }
  194.                 else if(subRoot->getData() < node->getData())
  195.                 {
  196.                     b = subRoot->getRight();
  197.                     if(a->getAHeight()* b->getAHeight() > 0)
  198.                     {
  199.                         //RRRotate
  200.                         tempRoot = RRRotate(a,b);
  201.                     }
  202.                     else
  203.                     {
  204.                         //RLRotate
  205.                         c = b->getLeft();
  206.                         tempRoot =RLRotate(a,b,c);
  207.                     }
  208.                 }
  209.                 if(ppoRoot!=NULL)
  210.                 {
  211.                     if(ppoRoot->getData() > tempRoot->getData())
  212.                     {
  213.                         ppoRoot->setLeft(tempRoot);
  214.                     }
  215.                     else
  216.                     {
  217.                         ppoRoot->setRight(tempRoot);
  218.                     }
  219.                 }
  220.                 else
  221.                 {
  222.                     this->head = tempRoot;
  223.                 }
  224.             }
  225.             
  226.         }
  227.     }
  228.     // return the subRoot
  229.     //        a                   b
  230.     //       /                  /   /
  231.     //      b        ->        c     a
  232.     //     /
  233.     //    c
  234.     AVLNode<T>* LLRotate(AVLNode<T>* a, AVLNode<T>* b)
  235.     {
  236.         a->setLeft(b->getRight());
  237.         b->setRight(a);
  238.         
  239.         a->setAHeight(0);
  240.         b->setAHeight(0);
  241.         //a->setAHeight(getAVLHeight(a));
  242.         //b->setAHeight(getAVLHeight(b));
  243.         return b;
  244.     }
  245.     // return the subRoot
  246.     //        a                      b
  247.     //          /                  /   /
  248.     //            b        ->     a     c
  249.     //              /
  250.     //                c
  251.     AVLNode<T>* RRRotate(AVLNode<T>* a, AVLNode<T>* b)
  252.     {
  253.         a->setRight(b->getLeft());
  254.         b->setLeft(a);
  255.         
  256.         a->setAHeight(0);
  257.         b->setAHeight(0);
  258.         //a->setAHeight(getAVLHeight(a));
  259.         //b->setAHeight(getAVLHeight(b));
  260.         return b;
  261.     }
  262.     // return the subRoot
  263.     //        a                      c
  264.     //          /                  /   /
  265.     //            b        ->     a     b
  266.     //           /                 /      
  267.     //          c                   d  
  268.     //         /
  269.     //        d
  270.     AVLNode<T>* RLRotate(AVLNode<T>* a, AVLNode<T>* b, AVLNode<T>* c)
  271.     {
  272.         
  273.         b->setLeft(c->getRight());
  274.         c->setRight(b);
  275.         a->setRight(c->getLeft());
  276.         c->setLeft(a);
  277.         
  278.         
  279.         //a->setAHeight(getAVLHeight(a));
  280.         //c->setAHeight(getAVLHeight(c));
  281.         //b->setAHeight(getAVLHeight(b));
  282.         if(c->getAbsoluteAHeight() == 1)
  283.         {
  284.             if(b->getAHeight() == 0)
  285.             {
  286.                 b->setAHeight(-1);
  287.             }
  288.             else
  289.             {
  290.                 b->setAHeight(0);
  291.             }
  292.             a->setAHeight(1);
  293.             c->setAHeight(0);
  294.         
  295.         }
  296.         else
  297.         {
  298.             if(b->getAHeight() == 0)
  299.             {
  300.                 b->setAHeight(-1);
  301.             }
  302.             else
  303.             {
  304.                 b->setAHeight(0);
  305.             }
  306.             a->setAHeight(0);
  307.             c->setAHeight(0);
  308.             
  309.         }
  310.         
  311.         
  312.         return c;
  313.     }
  314.     // return the subRoot
  315.     //        a                      c
  316.     //       /                     /   /
  317.     //      b              ->     b     a
  318.     //       /                         /       
  319.     //        c                       d   
  320.     //         /
  321.     //          d
  322.     AVLNode<T>* LRRotate(AVLNode<T>* a, AVLNode<T>* b, AVLNode<T>* c)
  323.     {
  324.         b->setRight(c->getLeft());
  325.         c->setLeft(b);
  326.         a->setLeft(c->getRight());
  327.         c->setRight(a);
  328.         
  329.         
  330.         //a->setAHeight(getAVLHeight(a));
  331.         //c->setAHeight(getAVLHeight(c));
  332.         //b->setAHeight(getAVLHeight(b));
  333.         if(c->getAbsoluteAHeight() == 1)
  334.         {
  335.             if(b->getAHeight() == 0)
  336.             {
  337.                 b->setAHeight(1);
  338.             }
  339.             else
  340.             {
  341.                 b->setAHeight(0);
  342.             }
  343.             a->setAHeight(-1);
  344.             c->setAHeight(0);
  345.             
  346.         }
  347.         else
  348.         {
  349.             if(b->getAHeight() == 0)
  350.             {
  351.                 b->setAHeight(1);
  352.             }
  353.             else
  354.             {
  355.                 b->setAHeight(0);
  356.             }
  357.             a->setAHeight(0);
  358.             c->setAHeight(0);
  359.         
  360.         }
  361.         
  362.         
  363.         
  364.         return c;
  365.     }
  366.     AVLNode<T>* getMaxNode(AVLNode<T>* subRoot)
  367.     {
  368.         if(subRoot == NULL)
  369.         {
  370.             return NULL;
  371.         }
  372.         if(subRoot->getRight() == NULL)
  373.         {
  374.             return subRoot;
  375.         }
  376.         else
  377.         {
  378.             return getMaxNode(subRoot->getRight());
  379.         }
  380.     }
  381.     void deleteNode(AVLNode<T>* subRoot, AVLNode<T>* prev,AVLNode<T>* assignNode)
  382.     {
  383.         if(prev == NULL)
  384.         {
  385.             this->head = assignNode;
  386.         }
  387.         else
  388.         {
  389.             if(prev->getLeft() == subRoot)
  390.             {
  391.                 prev->setLeft(assignNode);
  392.                 
  393.             }
  394.             else if(prev->getRight() == subRoot)
  395.             {
  396.                 prev->setRight(assignNode);
  397.                 
  398.             }
  399.         }
  400.         
  401.         delete subRoot;
  402.     }
  403.     bool deleteNode(AVLNode<T>* subRoot, AVLNode<T>* prev, T& node)
  404.     {
  405.         if(subRoot == NULL)
  406.         {
  407.             return false;
  408.         }
  409.         bool isChangeHeight = false;
  410.         if(subRoot->getData() > node)
  411.         {
  412.             isChangeHeight = deleteNode(subRoot->getLeft(),subRoot,node);
  413.             if(isChangeHeight)
  414.             {
  415.                 subRoot->setAHeight(subRoot->getAHeight()+1);
  416.             }
  417.             
  418.         }
  419.         else if(subRoot->getData() < node)
  420.         {
  421.             isChangeHeight = deleteNode(subRoot->getRight(),subRoot,node);
  422.             if(isChangeHeight)
  423.             {
  424.                 subRoot->setAHeight(subRoot->getAHeight()-1);
  425.             }
  426.             
  427.         }
  428.         else
  429.         {
  430.             AVLNode<T>* assignNode = NULL;
  431.             if(subRoot->getLeft() == NULL && subRoot->getRight() == NULL)
  432.             {
  433.                 // no child
  434.                 isChangeHeight = true;
  435.                 deleteNode(subRoot,prev,assignNode);
  436.                 return isChangeHeight;
  437.             }
  438.             else if(subRoot->getLeft() == NULL && subRoot->getRight() != NULL)
  439.             {
  440.                 // no left child
  441.                 isChangeHeight = true;
  442.                 assignNode = subRoot->getRight();
  443.                 deleteNode(subRoot,prev,assignNode);
  444.                 return isChangeHeight;
  445.                 
  446.             }           
  447.             else if(subRoot->getRight() == NULL && subRoot->getLeft() != NULL)
  448.             {
  449.                 // no right chlid
  450.                 isChangeHeight = true;
  451.                 assignNode = subRoot->getLeft();
  452.                 deleteNode(subRoot,prev,assignNode);
  453.                 return isChangeHeight;
  454.             }
  455.             else
  456.             {
  457.                 // have both children 
  458.                 assignNode = getMaxNode(subRoot->getLeft());
  459.                 T data = assignNode->getData();
  460.                 bool isChange = deleteNode(subRoot->getLeft(),subRoot,data);
  461.                 cout << "use "<< data<< " replace "<< subRoot->getData()<<endl;
  462.                 subRoot->setData(data);// copy all data to subRoot
  463.                 
  464.                 if(isChange)
  465.                 {
  466.                     subRoot->setAHeight(subRoot->getAHeight()+1);
  467.                 }
  468.             }
  469.             if(subRoot->getAbsoluteAHeight() == 2)
  470.             {
  471.                 AVLNode<T>* a = subRoot;
  472.                 AVLNode<T>* b = NULL;
  473.                 AVLNode<T>* c = NULL;
  474.                 AVLNode<T>* tempRoot = NULL;
  475.                 if(subRoot->getAHeight() == 2)
  476.                 {
  477.                     // move to left 
  478.                     b = subRoot->getRight();
  479.                     switch(b->getAHeight())
  480.                     {
  481.                         case 1:
  482.                             tempRoot = RRRotate(a,b);
  483.                             isChangeHeight = true;
  484.                             break;
  485.                         case -1:
  486.                             c = b->getLeft();
  487.                             tempRoot = RLRotate(a,b,c);
  488.                             isChangeHeight = true;
  489.                             break;
  490.                         case 0:
  491.                             tempRoot = RRRotate(a,b);   
  492.                             isChangeHeight = false;
  493.                             break;
  494.                     }
  495.                 }
  496.                 else if(subRoot->getAHeight() == -2)
  497.                 {
  498.                     // move to right
  499.                     b = subRoot->getLeft();
  500.                     switch(b->getAHeight())
  501.                     {
  502.                         case 1:
  503.                             c = b->getRight();
  504.                             tempRoot = LRRotate(a,b,c);
  505.                             isChangeHeight = true;
  506.                             break;
  507.                         case -1:
  508.                             tempRoot = LLRotate(a,b);
  509.                             isChangeHeight = true;
  510.                             break;
  511.                         case 0:
  512.                             tempRoot = LLRotate(a,b);   
  513.                             isChangeHeight = false;
  514.                             break;
  515.                     }   
  516.                 }
  517.                 if(prev == NULL)
  518.                 {
  519.                     this->head = tempRoot;
  520.                 }
  521.                 else
  522.                 {
  523.                     if(prev->getData() > tempRoot->getData())
  524.                     {
  525.                         prev->setLeft(tempRoot);
  526.                     }
  527.                     else
  528.                     {
  529.                         prev->setRight(tempRoot);
  530.                     }
  531.                 }
  532.             }
  533.         }
  534.         return isChangeHeight;
  535.     }
  536.     
  537. public:
  538.     AVLTree(T& data)
  539.     {
  540.         head = new AVLNode<T>(data);
  541.     }
  542.     AVLTree(AVLNode<T>* head)
  543.     {
  544.         clearSubTree(this->head);
  545.         this->head = head;
  546.     }
  547.     ~AVLTree()
  548.     {
  549.         clearSubTree(head);
  550.     }
  551.     void printTree()
  552.     {
  553.         printTree(this->head,0);
  554.     }
  555.     void printInMidOrder()
  556.     {
  557.         printInMidOrder(this->head);
  558.     }
  559.     void insertNode(T& data)
  560.     {
  561.         insertNode(new AVLNode<T>(data));
  562.     }
  563.     void deleteNode(T& data)
  564.     {
  565.         deleteNode(this->head,NULL,data);
  566.     }
  567.     /*bool isUnbalancePoint(AVLNode<T>* node, AVLNode<T>* compareNode)
  568.     {
  569.         if(node->getAbsoluteAHeight() == 1)
  570.         {
  571.             if(node->getAHeight == 1 && node->getData() < compareNode->getData() )
  572.             {
  573.                 return true;
  574.             }
  575.             else if(node->getAHeight == -1 && node->getData() > compareNode->getData())
  576.             {
  577.                 return true;
  578.             }
  579.         }
  580.         return false;
  581.     }*/
  582. };

你可能感兴趣的:(编程,c,网络,测试,null,search)