avl树

 

  具体的执行过程,摘自维基百科,很容易懂。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;
}

 

转载于:https://www.cnblogs.com/bracken/archive/2013/04/12/3016085.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值