AVL树详解

来源:互联网 发布:2012中国网络购物规模 编辑:程序博客网 时间:2024/05/17 06:54

AVL树C++代码实现

1、源文件tree.h

1   2 #include <iostream>  3 using namespace std;  4   5 template<class T>  6 struct TreeNode  7 {  8     T data;  9     T df; 10     struct TreeNode<T> *leftchild,*rightchild; 11     TreeNode(const T &d=T(),const T &df=T()) 12         :data(d),df(0),leftchild(NULL),rightchild(NULL) 13     {} 14 }; 15  16 template<class T> 17 class Tree 18 { 19     public: 20         //插入算法 21         void insert(const T &key) 22         { 23             insert(root,key); 24         } 25     private: 26         void insert(TreeNode<T> *&pcur,const T &key) 27         { 28             TreeNode<T> *cur=pcur; 29             TreeNode<T> *tmp=NULL; 30             T d; 31             stack<TreeNode<T> *> s; 32             while(cur !=NULL) 33             { 34                 if(cur->data==key) 35                     return; 36                 tmp=cur; 37                 s.push(tmp);//用栈保存查找记录 38                 if(cur->data >key) 39                     cur=cur->leftchild; 40                 else 41                     cur=cur->rightchild; 42             } 43             cur=new TreeNode<T>(key); 44             if(pcur==NULL) //当一个节点都没有的时候 45             { 46                 pcur=cur; 47                 return; 48             } 49             if(key < tmp->dat) 50                 tmp->leftchild=cur; 51             else 52                 tmp->rightchild=cur; 53             ///////////////插入成功//////////////// 54             while(!s.empty()) 55             { 56                 s.pop(); 57                 //平衡因子=右孩子-左孩子 58                 //如果在tmp的左孩子中插入节点,平衡因子--, 59                 //如果在tmp的右孩子中插入节点,平衡因子++ 60                  61                 if(cur==tmp->leftchild) 62                     tmp->df--; 63                 else 64                     tmp->df++; 65                 //////////////////// 66                 if(tmp->df==0) 67                     break; 68                 if(tmp->df==1 || tmp->df==-1) 69                     cur=tmp; 70                 else 71                 { 72                     d=(tmp->df <0)?-1:1; 73                     if(cur->df==d) 74                     { 75                         if(d==-1) 76                             RotateR(tmp); 77                         else 78                             RotateL(tmp); 79                     }else 80                     { 81                         if(d==-1) 82                             RotateLR(tmp); 83                         else 84                             RotateLR(tmp); 85                     } 86                     break; 87                 } 88             } 89             ///////////////// 90             if(s.empty()) 91                 pcur=tmp; 92             else 93             { 94                 TreeNode<T> *pre=NULL; 95                 s.get_top(pre); 96                 if(pre->data > tmp->data) 97                     pre->leftchild=tmp; 98                 else 99                     pre->rightchild=tmp;100             }101         }102     private:103         //左单旋转法104         //因为两个节点的平衡因子值的(正负)号相同,均为正,因此用左单旋105         void RotateL(TreeNode<T> *&cur)106         {107             TreeNode<T> *tmp=cur;108             cur=cur->rightchild;109             tmp->rightchild=cur->leftchild;110             cur->leftchild=tmp;111             cur->df=tmp->df=0;112         }113         //右单旋转法,114         //因为两节点的平衡因子的值(正负)号相同,均为负(-),所以用右单旋115         void RotateR(TreeNode<T> *&cur)116         {117             TreeNode<T> *tmp=cur;118             cur=cur->leftchild;119             tmp->leftchild=cur->rightchild;120             cur->rightchild=tmp;121             cur->df=tmp->df=0;122         }123         //先左后右双旋转124         void RotateLR(TreeNode<T> *&cur)125         {126             TreeNode<T> *Rtmp=cur;127             TreeNode<T> *Ltmp=cur->leftchild;128             cur=Ltmp->rightchild;129             //先进行左单旋130             Ltmp->rightchild=cur->leftchild;131             cur->leftchild=Ltmp;132             if(cur->df <=0)133                 Ltmp->df=0;134             else135                 Ltmp->df=-1;136             //再进行右单旋137             Rtmp->leftchild=cur->rightchild;138             cur->rightchild=Rtmp;139             if(cur->df==-1)140                 Rtmp->df=1;141             else142                 Rtmp->df=0;143             cur->df=0;144         }145         //先右后左双旋转146         void RotateRL(TreeNode<T> *&cur)147         {148             TreeNode<T> *Ltmp=cur;149             TreeNode<T> *Rtmp=cur->rightchild;150             cur=Rtmp->leftchild;151             //先进行右单旋152             Rtmp->leftchild=cur->rightchild;153             cur->rightchild=Rtmp;154             if(cur->df >=0)155                 Rtmp->df=0;156             else157                 Rtmp->df=1;158             //再进行左单旋159             Ltmp->rightchild=cur->leftchild;160             cur->leftchild=Ltmp;161             if(cur->df ==1)162                 Ltmp->df=-1;163             else164                 Ltmp->df=0;165             cur->df=0;166         }167     public:168         //先右后左双旋转169         void RotateRL()170         {171             RotateRL(root);172         }173         //先左后右双旋转174         void RotateLR()175         {176             RotateLR(root);177         }178         //左单旋转法179         void RotateL()180         {181             RotateL(root);182         }183         //右单旋转法184         void RotateR()185         {186             RotateR(root);187         }188     public:189         Tree(const T v=T()):value(v),root(NULL)190         {}191     private:192         T value;193         TreeNode<T> *root;194 };  




0 0
原创粉丝点击