AVL

来源:互联网 发布:客户分布地图制作软件 编辑:程序博客网 时间:2024/06/06 02:38
template <typename Comparable>struct AvlNode{Comparable element;AvlNode *left;AvlNode *right;int height;AvlNode( const Comparable & theElement,AvlNode *lt, AvlNode *rt, int = 0 ):element(theElement),left(lt),right(rt),height(h){}}int height( AvlNode *t )const{return t == NULL ? -1 : t->height;}void insert( const Comparable & x, AvlNode * & t ){if( t == NULL ){t =new AvlNode( x, NULL, NULL );}else if( x < t->element ){insert( x, t->left );if( height( t->left ) - height( t->right ) == 2 )if( x < t->left->element )rotateWithLeftChild( t );elsedoubleWithLeftChild( t );}else if( t->element < x ){insert( x, t->right );if( height( t->right ) - height( t->left ) == 2 )if( t->right->element < x )rotateWithRightChild( t );elsedoubleWithRightChild( t );}else return;}void rotateWithLeftChild( AvlNode * &k2 ){AvlNode *k1 = k2->left;k2->left = k1->right;k1->right = k2;k2->height = max( height( k2->left ),height( k2->right ) ) + 1;k1->height = max( height( k1->left ),k2->height ) + 1;k2 = k1;}void rotateWithrightChilf( AvlNode * & k1){AvlNode *k2 = k1->right;k2 ->left =k1;k1->right = k2->left;k1 = k2;}void doubleWithLeftChild( AvlNode * & k3 ){rotateWithChild( k3->left );rotateWithLeftChild( k3 );}void doubleWithRightChild( k3->left ){rotateWithRightChild( k3->left );rotateWithLeftChild( k3 );}

0 0
原创粉丝点击