1 #include<iostream> 2 using namespace std; 3 4 class AVL 5 { 6 public: 7 enum BF { RH = 1 , EH , LH }; 8 class Node 9 { 10 public: 11 Node* lchild; 12 Node* rchild; 13 int data; 14 BF bf; 15 Node(): lchild(NULL) , rchild(NULL) , data(0) , bf(EH) {} 16 }; 17 typedef Node* PNode; 18 PNode root; 19 public: 20 AVL():root(NULL) {} 21 ~AVL(){ DestoryTree(); } 22 int Insert(int value ); 23 int InsertAVL( PNode& T , int value , int &taller ); 24 void R_Rotate( PNode &T ); 25 void L_Rotate( PNode &T ); 26 void LeftBalance( PNode &T); 27 void RightBalance( PNode &T); 28 void print(); 29 void DFS(PNode &T); 30 void DestoryTree(); 31 void Destory( PNode &T ); 32 }; 33 34 int AVL::Insert( int value) 35 { 36 int taller; 37 int res = InsertAVL( root , value , taller ); 38 cout << "============Insert " << value << "============" << endl; 39 print(); 40 cout << endl; 41 return res; 42 } 43 44 void AVL::R_Rotate( PNode &T ) 45 { 46 PNode lc = T->lchild; 47 T->lchild = lc->rchild ; 48 lc->rchild = T; 49 T = lc; 50 } 51 52 void AVL::L_Rotate( PNode& T ) 53 { 54 PNode rc = T->rchild ; 55 T->rchild = rc->lchild; 56 rc->lchild = T ; 57 T = rc; 58 } 59 60 void AVL::LeftBalance( PNode& T ) 61 { 62 PNode &temp = T->lchild ; 63 switch( temp->bf ) 64 { 65 case LH: 66 T->bf = EH; 67 temp->bf = EH; 68 R_Rotate( T ); 69 break; 70 case RH: 71 PNode temp2 = temp->rchild; 72 switch( temp2->bf ) 73 { 74 case LH: 75 temp2->bf = EH; 76 T->bf = RH; 77 temp->bf = EH; 78 break; 79 case EH: 80 temp2->bf = T->bf = temp->bf = EH; 81 break; 82 case RH: 83 T->bf = EH; 84 temp->bf = LH; 85 temp2->bf = EH; 86 break; 87 } 88 L_Rotate( temp ); 89 R_Rotate( T ); 90 break; 91 } 92 } 93 94 void AVL::RightBalance( PNode& T ) 95 { 96 PNode &temp = T->rchild ; 97 switch( temp->bf ) 98 { 99 case RH: 100 T->bf = temp->bf = EH; 101 L_Rotate( T ); 102 break; 103 case LH: 104 PNode temp2 = temp->lchild; 105 switch( temp2->bf ) 106 { 107 case LH: 108 T->bf = EH; 109 temp2->bf = EH; 110 temp->bf = RH; 111 break; 112 case RH: 113 T->bf = LH; 114 temp2->bf = EH; 115 temp->bf = EH; 116 break; 117 case EH: 118 T->bf = temp2->bf = temp->bf = EH; 119 break; 120 } 121 R_Rotate( temp ); 122 L_Rotate( T ); 123 break; 124 } 125 } 126 127 int AVL::InsertAVL( PNode& T , int value , int& taller ) 128 { 129 if( !T ) 130 { 131 PNode temp = new Node; 132 temp->data = value; 133 temp->bf = EH; 134 T = temp; 135 taller = true; 136 } 137 else 138 { 139 if( T->data == value ) 140 { 141 taller = false; 142 return false; 143 } 144 else if( T->data > value ) 145 { 146 if( InsertAVL( T->lchild , value , taller ) ) 147 { 148 if( taller ) 149 { 150 switch( T->bf ) 151 { 152 case LH: 153 LeftBalance( T ); 154 taller = false; 155 break; 156 case EH: 157 T->bf = LH; 158 taller = true; 159 break; 160 case RH: 161 T->bf = EH; 162 taller = false; 163 break; 164 } 165 } 166 } 167 else 168 return false; 169 } 170 else 171 { 172 if( InsertAVL( T->rchild , value , taller ) ) 173 { 174 if ( taller ) 175 { 176 switch( T->bf ) 177 { 178 case LH: 179 T->bf = EH; 180 taller = false; 181 break; 182 case EH: 183 T->bf = RH; 184 taller = true; 185 break; 186 case RH: 187 RightBalance(T); 188 taller = false; 189 break; 190 } 191 } 192 } 193 else 194 return false; 195 } 196 } 197 return true; 198 } 199 200 void AVL::DFS(PNode &T) 201 { 202 if( T ) 203 { 204 if( T->lchild ) 205 DFS( T->lchild ); 206 cout << "visit " << T->data << " The bf is " << T->bf - 2 << endl; 207 if( T->rchild ) 208 DFS( T->rchild ); 209 } 210 } 211 212 void AVL::print() 213 { 214 DFS( root ); 215 } 216 217 void AVL::DestoryTree() 218 { 219 Destory( root ); 220 } 221 222 void AVL::Destory( PNode& T ) 223 { 224 if( T ) 225 { 226 PNode q = T ; 227 if( T->lchild ) 228 Destory( T->lchild ); 229 if( T->rchild ) 230 Destory( T->rchild ); 231 free( q ); 232 T = NULL; 233 } 234 } 235 236 int main() 237 { 238 AVL avl; 239 avl.Insert( 10 ); 240 avl.Insert( 12 ); 241 avl.Insert( 2 ); 242 avl.Insert( -1 ); 243 avl.Insert( -2 ); 244 avl.Insert( 3 ); 245 return 0; 246 }
时间: 2024-10-09 08:47:40