AVL树(数据结构与算法分析代码)
AVL树是带有平衡条件的二叉查找树。
一棵AVL树是其每个结点的左右子树和右子树的高度最多差1的二叉查找树。(空树的高度定义为-1)。
AVL树是通过单旋转或者双旋转保持平衡性质。
PS:只是贴上代码(严格来说是保存代码。。。。),日后会贴上图片,方便理解。。。。。。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 using namespace std; 3 4 template <typename Object> 5 class AVLTree 6 { 7 private: 8 struct AVLNode 9 { 10 Object data; 11 AVLNode * left; 12 AVLNode * right; 13 int height; 14 15 AVLNode( const Object & d = Object(), AVLNode *l = NULL , AVLNode *r = NULL, int h = 0) 16 : data( d ), left( l ), right( r ), height( h ) 17 { } 18 }; 19 20 21 public: 22 AVLTree( ) 23 { 24 root = NULL; 25 } 26 27 AVLTree( const AVLTree & rhs ) 28 { 29 root = rhs.root; 30 } 31 ~AVLTree( ) 32 { 33 makeEmpty( ); 34 } 35 36 bool isEmpty( ) const 37 { 38 if( root == NULL ) 39 return true; 40 else 41 return false; 42 } 43 44 45 const Object & findMin( ) const 46 { 47 return findMin( root ); 48 } 49 50 const Object & findMax( ) const 51 { 52 return findMax( root ); 53 } 54 55 bool contains( const Object & x ) const 56 { 57 return contains( x, root ); 58 } 59 60 void insert( const Object & x ) 61 { 62 insert( x, root ); 63 } 64 65 void remove( const Object & x ) 66 { 67 remove( x, root ); 68 } 69 70 void makeEmpty( ) 71 { 72 makeEmpty( root ); 73 } 74 75 void preRecursive( ) 76 { 77 preRecursive( root ); 78 } 79 80 private: 81 AVLNode * root; 82 83 int height( AVLNode * t ) const 84 { 85 //return t == NULL ? -1 : t->height; 86 if(t == NULL) 87 return -1; 88 89 return t->height; 90 } 91 92 AVLNode * findMin( AVLNode * t ) const 93 { 94 if( t == NULL ) 95 return NULL; 96 if( t->left == NULL ) 97 return t; 98 return findMin( t->left ); 99 } 100 101 AVLNode * findMax( AVLNode * t ) const 102 { 103 if( t == NULL ) 104 return NULL; 105 if( t->right == NULL ) 106 return t; 107 return findMax( t->right ); 108 } 109 110 bool contains ( const Object & x, AVLNode * t ) const 111 { 112 if( t == NULL ) 113 return false; 114 else if( x < t->data ) 115 return contains( x, t->left ); 116 else if( x > t->data ) 117 return contains( x, t->right ); 118 else 119 return true; 120 } 121 122 void insert( const Object & x, AVLNode * & t ) 123 { 124 if( t == NULL ) 125 { 126 t = new AVLNode( ); 127 t->data = x; 128 t->left = t->right = NULL; 129 130 t = new AVLNode( x, NULL, NULL ); 131 } 132 133 else if ( x < t->data ) 134 { 135 insert( x, t->left ); 136 if( height( t->left ) - height( t->right ) == 2 ) 137 { 138 if( x < t->left->data ) 139 rotateWithLeftChild( t ); 140 else 141 doubleWithLeftChild( t ); 142 } 143 } 144 145 else if ( x > t->data ) 146 { 147 insert( x, t->right ); 148 if( height( t->right) - height( t->left ) == 2 ) 149 { 150 if( x > t->right->data ) 151 rotateWithRightChild( t ); 152 else 153 doubleWithRightChild( t ); 154 } 155 } 156 157 else 158 ; 159 160 t->height = max( height( t->left ), height( t->right ) ) + 1; 161 } 162 163 void rotateWithLeftChild( AVLNode * & x ) 164 { 165 AVLNode * y = x->left; 166 x->left = y->right; 167 y->right = x; 168 x->height = max( height(x->left), height(x->right) ) + 1; 169 y->height = max( height(y->left), x->height ) + 1; 170 x = y; 171 } 172 173 void rotateWithRightChild( AVLNode * & x ) 174 { 175 AVLNode * y = x->right; 176 x->right = y->left; 177 y->left = x; 178 x->height = max( height(x->left), height(x->right) ) + 1; 179 y->height = max( height(y->right), x->height ) + 1; 180 x = y; 181 } 182 183 void doubleWithLeftChild( AVLNode * & x ) 184 { 185 rotateWithRightChild(x->left); 186 rotateWithLeftChild(x); 187 } 188 189 void doubleWithRightChild( AVLNode * & x ) 190 { 191 rotateWithLeftChild(x->right); 192 rotateWithRightChild(x); 193 } 194 195 void remove( const Object & x, AVLNode * & t ) const 196 { 197 if( t == NULL ) 198 return ; 199 if( x < t->data ) 200 remove( x, t->left ); 201 else if( x > t->data ) 202 remove( x, t->right ); 203 else if( t->left != NULL && t->right != NULL ) 204 { 205 t->data = findMin( t->right )->data; 206 remove( t->data, t->right ); 207 } 208 else 209 { 210 AVLNode * oldNode = t; 211 t = ( t->left != NULL ) ? t->left : t->right; 212 delete oldNode; 213 } 214 } 215 216 void makeEmpty( AVLNode * & t ) const 217 { 218 if( t != NULL ) 219 { 220 makeEmpty( t->left ); 221 makeEmpty( t->right ); 222 delete t; 223 } 224 t = NULL; 225 } 226 227 void preRecursive( AVLNode * &T ) const 228 { 229 if( T != NULL ) 230 { 231 cout << T->data << " "; 232 preRecursive( T->left ); 233 preRecursive( T->right ); 234 } 235 } 236 237 }; 238 239 int main() 240 { 241 AVLTree<int> tree; 242 int n, m; 243 cin >> n; 244 cout << endl; 245 for(int i = 1; i <= n; ++i) 246 { 247 cin >> m; 248 tree.insert( m ); 249 //tree.preRecursive(); 250 //cout << endl; 251 } 252 tree.preRecursive(); 253 /*tree.insert(7); 254 tree.insert(13); 255 tree.insert(3); 256 tree.insert(2); 257 tree.preRecursive();*/ 258 return 0; 259 }