您的位置:首页 > 理论基础 > 数据结构算法

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

2012-12-14 16:08 525 查看
#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 Dataint 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;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: