平衡二叉树的创建

在下为了这可平衡二叉树呕心沥血,差点把屎都呕出来了,下面是源代码,如果有错,请见谅!!

///////////////////////////////////////    head.h   ///////////////////////////////////////////
#include <iostream>

using namespace std;

#define EQ( a , b )  ( (a) == (b) ) #define LT( a , b )  ( (a) < (b) ) #define LQ( a , b )  ( (a) > (b) )

#define LH +1     //left side is higher #define EH 0       // the same high #define RH -1     //ringt side is higher

typedef struct BSTNode {
int data;
int bf;
struct BSTNode *lchild, *rchlid; }BSTNode, *BSTree;

void R_Rotate( BSTree &p ) {
BSTree lc;
lc=p->lchild;
p->lchild=lc->rchlid;
lc->rchlid=p;
p=lc; }

void L_Rotate( BSTree &p ) {
BSTree rc;
rc=p->rchlid;
p->rchlid=rc->lchild;
rc->lchild=p;
p=rc; }

void LeftBalance( BSTree &T ) {
BSTree lc,rd;
lc=T->lchild;
switch( lc->bf )
{
case LH :
T->bf=lc->bf=EH;
R_Rotate(T);
break;

case RH :
rd=lc->rchlid;
switch( rd->bf )
{
case LH:
T->bf=RH;
lc->bf=EH;
break;
case EH:
T->bf=lc->bf=EH;
break;
case RH:
T->bf=EH;
lc->bf=LH;
break;
}

rd->bf=EH;
L_Rotate( T->lchild );
R_Rotate( T );
} }

void RightBalance( BSTree &T ) {
BSTree rc, ld;
rc=T->rchlid;
switch( rc->bf )
{
case RH:
T->bf=rc->bf=EH;
L_Rotate( T );
break;
case LH:
ld=rc->lchild;
switch( ld->bf )
{
case LH:
T->bf=EH;
rc->bf=RH;
break;
case EH:
T->bf=rc->bf=EH;
break;
case RH:
T->bf=LH;
rc->bf=EH;
break;
}

ld->bf=EH;
R_Rotate( T->rchlid );
L_Rotate( T );
} }

bool InsertAVL( BSTree &T , int e , bool &taller ) {
if( !T )
{
T=( BSTree )malloc( sizeof( BSTNode) );
T->data=e;
T->lchild=T->rchlid=NULL;
T->bf=EH;
taller=true;
}

else
{
if( EQ( e, T->data ) )
{
taller=false;
cout<<“the key has existed”<<endl;
return false ;
}
if( LT(e,T->data) )
{
if( !InsertAVL(T->lchild, e, taller) )
{
return false;
}
if( taller )
{
switch( T->bf )
{
case LH:
LeftBalance( T );
taller=false;
break;
case EH:
T->bf=LH;
taller=true;
break;
case RH:
T->bf=EH;
taller=false;
break;
}//switch
}//if(taller)
}//if

else
{
if( !InsertAVL(T->rchlid,e,taller) )
{
return false;
}
if( taller )
{
switch( T->bf )
{
case LH:
T->bf=EH;
taller=false;
break;
case EH:
T->bf=RH;
taller=true;
break;
case RH:
RightBalance(T);
taller=false;
break;
}
}//if(taller)
}//else

}//else
return true; }

void PrintBST( BSTree T, int m ) {
int i;
if( T->rchlid )
PrintBST(T->rchlid,m+1);

for( i=1; i<=m; i++ )
cout<<”    “;
cout<<T->data<<endl;

if( T->lchild )
PrintBST(T->lchild,m+1); }

void CreateBST( BSTree &T ) {
int e, m;
bool taller =false ;
T=NULL;

cout<<“Please input keys ( End with -1) :”;
cin>>e;
while( e!=-1 )
{
InsertAVL(T,e,taller);
cout<<“Please input keys (End with -1):”;
cin>>e;
taller=false ;
}

m=0;
cout<<“Now it will show you this BSTree by hengxiang:”<<endl;
if( T )
PrintBST(T,m);
else
cout<<“This is a empty tree !”<<endl; }

//////////////////////////////////////main.cpp   //////////////////////////////////////////////
#include “head.h”

void R_Rotate( BSTree &p ); void L_Rotate( BSTree &p ); void LeftBalance( BSTree &T ); void RightBalance( BSTree &T ); bool InsertAVL( BSTree &T , int e , bool &taller ); void PrintBST( BSTree T, int m ); void CreateBST( BSTree &T );

void main() {
BSTree T;
CreateBST(T);

}

上面是创建这棵树的原理,在下还是糊里糊涂的,你们还是去看看别的参考资料把!!!!
    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/MFCdestoryer/article/details/7037576
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞