首页 > 代码库 > AVL

AVL

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 struct node;
  4 node *null;
  5 struct node{
  6     node *lc, *rc;
  7 
  8     int dep;
  9     int key, value;
 10 
 11     inline void init(int _key, int _value)
 12     {
 13         lc=rc=null;
 14         dep=1;
 15         key = _key;
 16         value =http://www.mamicode.com/ _value;
 17     }
 18     inline void up(){ dep = max(lc->dep, rc->dep)+1; }
 19 };
 20 class AVL{
 21     node *root;
 22 public:
 23     AVL(){
 24         null=new node;
 25         null->lc=null->rc=null;
 26         null->dep=0;
 27         root=null;
 28     }
 29     void RightRotate(node *&x)
 30     {
 31         node *t=x->lc;
 32         x->lc = t->rc;
 33         t->rc = x;
 34         x->up();
 35         t->up();
 36         x = t;
 37     }
 38     void LeftRotate(node *&x)
 39     {
 40         node *t=x->rc;
 41         x->rc = t->lc;
 42         t->lc = x;
 43         x->up();
 44         t->up();
 45         x = t;
 46     }
 47     void Insert(int key, int value)
 48     {
 49         Insert(root, key, value);
 50     }
 51     void Insert(node *&x, int key, int value)
 52     {
 53         if(x==null){
 54             x=new node;
 55             x->init(key, value);
 56             return ;
 57         }
 58         if(key < x->key){
 59             Insert(x->lc, key, value);
 60             if(x->lc->dep - x->rc->dep == 2){
 61                 if(key < x->lc->key)    RightRotate(x);
 62                 else    LeftRotate(x->lc), RightRotate(x);
 63             }
 64         }
 65         else{
 66             Insert(x->rc, key, value);
 67             if(x->rc->dep - x->lc->dep == 2){
 68                 if(key >= x->rc->key)    LeftRotate(x);
 69                 else    RightRotate(x->rc), LeftRotate(x);
 70             }
 71         }
 72         x->up();
 73     }
 74     int find(int key)const
 75     {
 76         return find(root, key);
 77     }
 78     int find(const node *x, const int key)const
 79     {
 80         if(x == null)    return 0;
 81         if(x->key == key)    return x->value;
 82         else if(x->key > key)    return find(x->lc, key);
 83         else    return find(x->rc, key);
 84     }
 85     void ReBalance(node *&x)
 86     {
 87         if(x->lc->dep - x->rc->dep == 2){
 88             if(x->lc->lc->dep >= x->lc->rc->dep)    RightRotate(x);
 89             else    LeftRotate(x->lc), RightRotate(x);
 90         }
 91         else if(x->rc->dep - x->lc->dep == 2){
 92             if(x->rc->rc->dep >= x->rc->lc->dep)    LeftRotate(x);
 93             else    RightRotate(x->rc), LeftRotate(x);
 94         }
 95     }
 96     bool DelNode(int key)
 97     {
 98         return DelNode(root, key);
 99     }
100     node* DelMax(node *&x)
101     {
102         if(x->rc != null){
103             node *t=DelMax(x->rc);
104             x->up();
105             ReBalance(x);
106             return t;
107         }
108         else{
109             node *t=x;
110             x = x->lc;
111             return t;
112         }
113     }
114     bool DelNode(node *&x, int key)
115     {
116         if(x==null)    return false;
117         if(x->key == key){
118             if(x->lc==null || x->rc==null){
119                 x = x->lc==null ? x->rc : x->lc;
120                 return true;
121             }
122             else{
123                 node *t=DelMax(x->lc);
124                 t->lc = x->lc;
125                 t->rc = x->rc;
126                 delete x;
127                 x = t;
128             }
129         }
130         if(key < x->key)    DelNode(x->lc, key);
131         else    DelNode(x->rc, key);
132         if(x!=null)
133             x->up();
134         ReBalance(x);
135     }
136 };
137 AVL A;
138 A.Insert(key, value);
139 A.find(key);
140 A.DelNode(key);

 

AVL