具体的执行过程,摘自维基百科,很容易懂。http://zh.wikipedia.org/wiki/AVL%E6%A0%91
#include <stdio.h> #include <malloc.h> #include <assert.h> typedef int ElementType; typedef struct AvlNode { /* data */ ElementType element; struct AvlNode* leftChild; struct AvlNode* rightChild; } AvlNode; int max( int a, int b ) { return a > b ? a : b; } int Height( AvlNode* node ) { int height = 0; if ( node != NULL ) { /* code */ height = 1 + max( Height( node->leftChild ), Height( node->rightChild ) ); } return height; } int GetHeight( AvlNode* node ) { int height = 0; height = Height( node ); return height - 1; } void RotateLeft( AvlNode** root ) { AvlNode* pMiddleNode = ( *root )->rightChild; ( *root )->rightChild = pMiddleNode->leftChild; pMiddleNode->leftChild = *root; *root = pMiddleNode; } void RotateRight( AvlNode** root ) { AvlNode* pMiddleNode = ( *root )->leftChild; ( *root )->leftChild = pMiddleNode->rightChild; pMiddleNode->rightChild = *root; *root = pMiddleNode; } void RotateLeftRight( AvlNode** root ) { RotateLeft( &( *root )->leftChild ); RotateRight( root ); } void RotateRightLeft( AvlNode** root ) { RotateRight( &( *root )->rightChild ); RotateLeft( root ); } void InsertNode( ElementType element, AvlNode** root ) { AvlNode** pNode = root; AvlNode* pTreeNode = NULL; if ( *pNode == NULL ) { /* code */ pTreeNode = ( AvlNode* )malloc( sizeof( AvlNode ) ); pTreeNode->element = element; pTreeNode->rightChild = NULL; pTreeNode->leftChild = NULL; *pNode = pTreeNode; return; } else if ( element < (*pNode)->element ) { InsertNode( element, &( (*pNode)->leftChild )); if ( GetHeight( (*pNode)->leftChild ) - GetHeight( (*pNode)->rightChild ) > 1 ) { if ( element < (*pNode)->leftChild->element ) { RotateRight( root ); } else { RotateLeftRight( root ); } } } else if ( element > (*pNode)->element ) { InsertNode( element, &( (*pNode)->rightChild ) ); if ( GetHeight( (*pNode)->rightChild ) - GetHeight( (*pNode)->leftChild ) > 1 ) { if ( element > ( *pNode )->rightChild->element ) { RotateLeft( root ); } else { RotateRightLeft( root ); } } } } void TestGetHeightInTree() { AvlNode* root = ( AvlNode* )malloc( sizeof( AvlNode ) ); int testHeight = 0; root->element = 4; root->leftChild = NULL; root->rightChild = NULL; root->leftChild = ( AvlNode* )malloc( sizeof( AvlNode ) ); root->leftChild->element = 3; root->leftChild->leftChild = NULL; root->leftChild->rightChild = NULL; testHeight = GetHeight( root->leftChild ); assert( 0 == testHeight ); testHeight = GetHeight( root ); assert( 1 == testHeight ); root->leftChild->leftChild = ( AvlNode* )malloc( sizeof( AvlNode ) ); root->leftChild->leftChild->leftChild = NULL; root->leftChild->leftChild->rightChild = NULL; root->leftChild->leftChild->element = 2; testHeight = GetHeight( root->leftChild ); assert( 1 == testHeight ); testHeight = GetHeight( root->leftChild->leftChild ); assert( 0 == testHeight ); testHeight = GetHeight( root ); assert( 2 == testHeight ); } void TestRotateRight() { AvlNode* root = ( AvlNode* )malloc( sizeof( AvlNode ) ); root->rightChild = NULL; root->leftChild = NULL; root->element = 4; root->leftChild = ( AvlNode* )malloc( sizeof( AvlNode ) ); root->leftChild->element = 2; root->leftChild->rightChild = NULL; root->leftChild->leftChild = NULL; root->leftChild->leftChild = ( AvlNode* )malloc( sizeof( AvlNode ) ); root->leftChild->leftChild->element = 1; RotateRight( &root ); assert( 2 == root->element ); assert( 1 == root->leftChild->element ); assert( 4 == root->rightChild->element ); } void TestRotateLeft() { AvlNode* root = ( AvlNode* )malloc( sizeof( AvlNode ) ); root->rightChild = NULL; root->leftChild = NULL; root->element = 1; root->rightChild = ( AvlNode* )malloc( sizeof( AvlNode ) ); root->rightChild->element = 2; root->rightChild->rightChild = NULL; root->rightChild->leftChild = NULL; root->rightChild->rightChild = ( AvlNode* )malloc( sizeof( AvlNode ) ); root->rightChild->rightChild->element = 4; RotateLeft( &root ); assert( 2 == root->element ); assert( 1 == root->leftChild->element ); assert( 4 == root->rightChild->element ); } void TestInsertRightRotateNode() { AvlNode* root = ( AvlNode* )malloc( sizeof( AvlNode ) ); AvlNode* testNode = NULL; root->element = 1; root->leftChild = NULL; root->rightChild = NULL; InsertNode( 2, &root ); assert( 2 == root->rightChild->element ); assert( NULL == root->rightChild->leftChild ); assert( NULL == root->rightChild->rightChild ); InsertNode( 4, &root ); assert( 2 == root->element ); assert( 4 == root->rightChild->element ); assert( 1 == root->leftChild->element ); testNode = root->rightChild; InsertNode( 6, &root ); assert( 6 == testNode->rightChild->element ); InsertNode( 8, &root ); testNode = root->rightChild->rightChild; assert( 2 == root->element ); assert( 8 == testNode->element ); InsertNode( 10, &root ); assert( 6 == root->element ); assert( 2 == root->leftChild->element ); assert( 4 == root->leftChild->rightChild->element ); assert( 1 == root->leftChild->leftChild->element ); assert( 8 == root->rightChild->element ); assert( 10 == root->rightChild->rightChild->element ); } void TestInsertLeftRotateNode() { AvlNode* root = ( AvlNode* )malloc( sizeof( AvlNode ) ); AvlNode* testNode = NULL; root->element = 20; root->leftChild = NULL; root->rightChild = NULL; InsertNode( 18, &root ); assert( 18 == root->leftChild->element ); assert( NULL == root->leftChild->leftChild ); assert( NULL == root->leftChild->rightChild ); InsertNode( 14, &root ); assert( 18 == root->element ); assert( 20 == root->rightChild->element ); assert( 14 == root->leftChild->element ); testNode = root->leftChild; InsertNode( 10, &root ); assert( 10 == testNode->leftChild->element ); InsertNode( 6, &root ); assert( 18 == root->element ); assert( 10 == root->leftChild->element ); assert( 20 == root->rightChild->element ); assert( 14 == root->leftChild->rightChild->element ); assert( 6 == root->leftChild->leftChild->element ); InsertNode( 2, &root ); assert( 10 == root->element ); assert( 6 == root->leftChild->element ); assert( 2 == root->leftChild->leftChild->element ); assert( 18 == root->rightChild->element ); assert( 20 == root->rightChild->rightChild->element ); assert( 14 == root->rightChild->leftChild->element ); } void TestInsertRotateLeftRight() { AvlNode* root = ( AvlNode* )malloc( sizeof( AvlNode ) ); AvlNode* testNode = NULL; root->element = 20; root->leftChild = NULL; root->rightChild = NULL; InsertNode( 18, &root ); InsertNode( 14, &root ); InsertNode( 8, &root ); InsertNode( 10, &root ); assert( 18 == root->element ); assert( 10 == root->leftChild->element ); assert( 20 == root->rightChild->element ); assert( 8 == root->leftChild->leftChild->element ); assert( 14 == root->leftChild->rightChild->element ); } void TestInsertRotateRightLeft() { AvlNode* root = ( AvlNode* )malloc( sizeof( AvlNode ) ); AvlNode* testNode = NULL; root->element = 10; root->leftChild = NULL; root->rightChild = NULL; InsertNode( 8, &root ); InsertNode( 14, &root ); InsertNode( 18, &root ); InsertNode( 16, &root ); assert( 10 == root->element ); assert( 8 == root->leftChild->element ); assert( 16 == root->rightChild->element ); assert( 14 == root->rightChild->leftChild->element ); assert( 18 == root->rightChild->rightChild->element ); } void TestAvlTree() { TestRotateRight(); TestRotateLeft(); TestGetHeightInTree(); TestInsertRightRotateNode(); TestInsertLeftRotateNode(); TestInsertRotateLeftRight(); TestInsertRotateRightLeft(); } int main() { TestAvlTree(); return 0; }