数据结构学习笔记-平衡二叉树实现

#ifndef _CAVLTREE_H_
#define _CAVLTREE_H_

const int LH = 1;
const int EH = 0;
const int RH = -1;

typedef struct BiTNode
{
    int nData;
    int nBalanceFactor;
    struct BiTNode* pLChild, *pRChild;
} BiTNode, *BiTree;

class CAVLTree  
{
public:
	            CAVLTree();
	virtual    ~CAVLTree();
    
public:
    bool InsertAVL( int e, bool* taller );
    void TraverseTree();

private:
    bool InsertAVL( BiTree& pBiTree, int e, bool* taller );
    void TraverseTree( BiTree& pBiTree );
    void DestroyTree( BiTree& pBiTree );
    
private:
    void LeftBalance( BiTree& pBiTree );
    void RightBalance( BiTree& pBiTree );

    void R_Rotate( BiTree& pBiTree );
    void L_Rotate( BiTree& pBiTree );

private:
    BiTree m_pBiTree;

};

#endif // _CAVLTREE_H_
#include <stdio.h>
#include "AVLTree.h"

CAVLTree::CAVLTree()
: m_pBiTree( NULL )
{

}

CAVLTree::~CAVLTree()
{
    DestroyTree( m_pBiTree );
}

void CAVLTree::R_Rotate( BiTree& pBiTree )
{
    BiTree pLTree;

    pLTree = pBiTree->pLChild;
    pBiTree->pLChild = pLTree->pRChild;
    pLTree->pRChild = pBiTree;

    pBiTree = pLTree;
}

void CAVLTree::L_Rotate( BiTree& pBiTree )
{
    BiTree pRTree;

    pRTree = pBiTree->pRChild;
    pBiTree->pRChild = pRTree->pLChild;
    pRTree->pLChild = pBiTree;

    pBiTree = pRTree;
}

void CAVLTree::LeftBalance( BiTree& pBiTree )
{
    BiTree pLeft, pLeftR;
    
    pLeft = pBiTree->pLChild;
    
    switch( pLeft->nBalanceFactor )
    {
    case LH:
        pBiTree->nBalanceFactor = pLeft->nBalanceFactor = EH;
        R_Rotate( pBiTree );
        break;
    case RH:
        pLeftR = pLeft->pRChild;
        switch( pLeftR->nBalanceFactor )
        {
        case LH:
            pBiTree->nBalanceFactor = RH;
            pLeft->nBalanceFactor = EH;    
            break;
        case EH:
            pBiTree->nBalanceFactor = pLeft->nBalanceFactor = EH;    
            break;
        case RH:
            pBiTree->nBalanceFactor = EH;
            pLeft->nBalanceFactor = LH;
            break;
        }
        pLeftR->nBalanceFactor = EH;
        L_Rotate( pBiTree->pLChild );
        R_Rotate( pBiTree );
        break;
    }
}

void CAVLTree::RightBalance( BiTree& pBiTree )
{
    BiTree pRight, pRightL;
    
    pRight = pBiTree->pRChild;
    
    switch( pRight->nBalanceFactor )
    {
    case RH:
        pBiTree->nBalanceFactor = pRight->nBalanceFactor = EH;
        L_Rotate( pBiTree );
        break;
    case LH:
        pRightL = pRight->pLChild;
        switch( pRightL->nBalanceFactor )
        {
        case RH:
            pBiTree->nBalanceFactor = RH;
            pRight->nBalanceFactor = EH;    
            break;
        case EH:
            pBiTree->nBalanceFactor = pRight->nBalanceFactor = EH;    
            break;
        case LH:
            pBiTree->nBalanceFactor = EH;
            pRight->nBalanceFactor = LH;
            break;
        }
        pRightL->nBalanceFactor = EH;
        R_Rotate( pBiTree->pRChild );
        L_Rotate( pBiTree );
        break;
    }
}

bool CAVLTree::InsertAVL( BiTree& pBiTree, int e, bool* taller )
{
    if ( !pBiTree )
    {
        pBiTree = new BiTNode;
        pBiTree->nData = e;
        pBiTree->pLChild = pBiTree->pRChild = NULL;
        pBiTree->nBalanceFactor = EH;
        *taller = true;
    }
    else
    {
        if ( e == pBiTree->nData )
        {
            *taller = false;
            return false;
        }
        if ( e < pBiTree->nData )
        {
            if ( !InsertAVL( pBiTree->pLChild, e, taller ))
            {
                return false;
            }

            if ( *taller )
            {
                switch( pBiTree->nBalanceFactor )
                {
                case LH:
                    LeftBalance( pBiTree );
                    *taller = false;
                    break;
                case EH:
                    pBiTree->nBalanceFactor = LH;
                    *taller = true;
                    break;
                case RH:
                    pBiTree->nBalanceFactor = EH;
                    *taller = false;
                    break;
                }
            }
        }
        else
        {
            if ( !InsertAVL( pBiTree->pRChild, e, taller ))
            {
                return false;
            }
            if ( *taller )
            {
                switch( pBiTree->nBalanceFactor )
                {
                case RH:
                    RightBalance( pBiTree );
                    *taller = false;
                    break;
                case EH:
                    pBiTree->nBalanceFactor = RH;
                    *taller = true;
                    break;
                case LH:
                    pBiTree->nBalanceFactor = EH;
                    *taller = false;
                    break;
                }
            }
        }
    }
    return true;
}

bool CAVLTree::InsertAVL( int e, bool* taller )
{
    return InsertAVL( m_pBiTree, e, taller );
}

void CAVLTree::DestroyTree( BiTree& pBiTree )
{
    if ( NULL == pBiTree )
    {
        return;
    }

    if ( NULL != pBiTree->pLChild )
    {
        DestroyTree( pBiTree->pLChild );
    }

    if ( NULL != pBiTree->pRChild )
    {
        DestroyTree( pBiTree->pRChild );
    }

    delete pBiTree;
    pBiTree = NULL;
}

void CAVLTree::TraverseTree( BiTree& pBiTree )
{
    if ( NULL == pBiTree )
    {
        return;
    }
    
    if ( NULL != pBiTree->pLChild )
    {
        TraverseTree( pBiTree->pLChild );
    }

    printf("%d ", pBiTree->nData );
    
    if ( NULL != pBiTree->pRChild )
    {
        TraverseTree( pBiTree->pRChild );
    }
}

void CAVLTree::TraverseTree()
{
    TraverseTree( m_pBiTree );    
}

#include <stdio.h>
#include "AVLTree.h"

int main(int argc, char* argv[])
{
    // Test Data
    int nSrcArry[20] = 
    {8,3,6,5,9,56,32,21,48,57,16,25,23,29,35,36,50,1,2,4};

    printf("Source Data Array: \n");
    for ( int i = 0; i < 20; i++ )
    {
        printf("%d, ", nSrcArry[i]);
        if ( 9 == i%10 )
        {
            printf("\n");
        }
    }
    
    CAVLTree oAVLTree;
    bool bHeigh = false;
    for ( i = 0; i < 20; i++ )
    {
        oAVLTree.InsertAVL( nSrcArry[i], &bHeigh );
    }

    printf("Sorted Data Array: \n");
    oAVLTree.TraverseTree();
    printf("\n");
    
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值