本文解决的问题:
随机产生20个树,构建平衡二叉排序树。
实现代码如下:
#include<stdio.h>
#include<stdlib.h>
#define EH 0 /*等高*/
#define LH 1 /*左高*/
#define RH -1 /*右高*/
typedef struct btnode
{
int data;
int BF;
struct btnode *lchild,*rchild;
}Btnode,*bitree;
int insert_avl_left(bitree *broot,int key,bool *taller);
int insert_avl(bitree *broot,int key,bool *taller);
int avl_left_balance(bitree *broot);
int avl_right_balance(bitree *broot);
int insert_avl_right(bitree *broot,int key,bool *taller);
//右单旋转,解决左左
int avl_left_balance_ll(bitree *broot)
{
bitree t;
/*
int temp=(*broot)->data;
t=(bitree)malloc(sizeof(btnode));
t->data=temp;
(*broot)->data=(*broot)->lchild->data;
(*broot)->lchild=(*broot)->lchild->lchild;
(*broot)->rchild=t;
*/
t=(*broot)->lchild;
(*broot)->lchild=NULL;
if(t->rchild != NULL)
{
(*broot)->lchild=t->rchild;
}
t->rchild=(*broot);
(*broot)=t;
(*broot)->BF=EH;
(*broot)->lchild->BF=EH;
return 0;
}
//左右旋转,解决左右
int avl_left_balance_lr(bitree *broot)
{
bitree t;
/*
int temp=(*broot)->lchild->data;
t=(bitree)malloc(sizeof(btnode));
t->data=temp;
(*broot)->lchild->data=(*broot)->lchild->rchild->data;
(*broot)->lchild->lchild=t;
*/
switch((*broot)->lchild->rchild->BF)
{
case LH:
{
(*broot)->BF=RH;
(*broot)->lchild->BF=EH;
(*broot)->lchild->rchild->BF=EH;
break;
}
case EH:
{
(*broot)->BF=EH;
(*broot)->lchild->BF=EH;
(*broot)->lchild->rchild->BF=EH;
break;
}
case RH:
{
(*broot)->BF=EH;
(*broot)->lchild->BF=LH;
(*broot)->lchild->rchild->BF=EH;
break;
}
}
t=(*broot)->lchild;
(*broot)->lchild=(*broot)->lchild->rchild;
t->rchild=t->rchild->lchild;
(*broot)->lchild->lchild=t;
avl_left_balance_ll(broot);
return 0;
}
//左单,解决右右
int avl_left_balance_rr(bitree *broot)
{
bitree t;
/*
int temp=broot->data;
t=(bitree)malloc(sizeof(btnode));
t->data=temp;
broot->data=broot->rchild->data;
broot->rchild=broot->rchild->rchild;
broot->lchild=t;
*/
t=(*broot)->rchild;
(*broot)->rchild=NULL;
if(t->lchild != NULL)
{
(*broot)->rchild=t->lchild;
}
t->lchild=(*broot);
(*broot)=t;
(*broot)->BF=EH;
(*broot)->lchild->BF=EH;
return 0;
}
//右左旋转,解决右子树插入左侧
int avl_left_balance_rl(bitree *broot)
{
bitree t;
/*
int temp=broot->rchild->data;
t=(bitree)malloc(sizeof(btnode));
t->data=temp;
broot->rchild->data=broot->rchild->lchild->data;
broot->rchild->rchild=t;
*/
switch((*broot)->rchild->lchild->BF)
{
case LH:
{
(*broot)->BF=EH;
(*broot)->rchild->BF=RH;
(*broot)->rchild->lchild->BF=EH;
break;
}
case EH:
{
(*broot)->BF=EH;
(*broot)->rchild->BF=EH;
(*broot)->rchild->lchild->BF=EH;
break;
}
case RH:
{
(*broot)->BF=LH;
(*broot)->rchild->BF=EH;
(*broot)->rchild->lchild->BF=EH;
break;
}
}
t=(*broot)->rchild;
(*broot)->rchild=(*broot)->rchild->lchild;
t->lchild=NULL;
(*broot)->rchild->rchild=t;
avl_left_balance_rr(broot);
return 0;
}
//对左子树失衡进行处理
int avl_left_balance(bitree *broot)
{
bitree t=(*broot)->lchild;
switch(t->BF){
case LH://LL型
return avl_left_balance_ll(broot);
case RH://LR型
return avl_left_balance_lr(broot);
}
return 0;
}
//对右子树失衡进行处理
int avl_right_balance(bitree *broot)
{
bitree t=(*broot)->rchild;
switch(t->BF){
case LH://RL型
return avl_left_balance_rl(broot);
case RH://RR型
return avl_left_balance_rr(broot);
}
return 0;
}
//查找新结点插入的子树
int insert_avl(bitree *broot,int key,bool *taller)
{
if(key<(*broot)->data)
{
//进入左子树操作
insert_avl_left(broot,key,taller);
return 0;
}else
{
//进入右子树操作
insert_avl_right(broot,key,taller);
return 0;
}
}
//插入结点,判断平衡
int insert_avl_left(bitree *broot,int key,bool *taller)
{
if((*broot)->lchild == NULL)
{
//如果当前节点的左子树为空,则直接插入
bitree add;
add=(bitree)malloc(sizeof(btnode));
add->BF=EH;
add->data=key;
add->lchild=NULL;
add->rchild=NULL;
(*broot)->lchild=add;
(*taller)=true;
}else{
insert_avl(&((*broot)->lchild),key,taller);
}
if((*taller) == false)
{
return 0;
}
switch((*broot)->BF){
case EH:
{
(*broot)->BF=LH;
(*taller)=true;
return 0;
}
case LH:
{//左高,插入左,失衡
avl_left_balance(broot);
(*taller)=false;
return 0;
}
case RH:
{//右高,插入左,均衡
(*broot)->BF=EH;
(*taller)=false;
return 0;
}
}
return 0;
}
int insert_avl_right(bitree *broot,int key,bool *taller)
{
if((*broot)->rchild == NULL)
{
//如果当前节点的左子树为空,则直接插入
bitree add;
add=(bitree)malloc(sizeof(btnode));
add->BF=EH;
add->data=key;
add->lchild=NULL;
add->rchild=NULL;
(*broot)->rchild=add;
(*taller)=true;
}else{
insert_avl(&((*broot)->rchild),key,taller);
}
if((*taller) == false)
{
return 0;
}
switch((*broot)->BF){
case EH:
{
(*broot)->BF=RH;
(*taller)=true;
return 0;
}
case LH:
{//左高,插入右,均衡
(*broot)->BF=EH;
(*taller)=false;
return 0;
}
case RH:
{//右高,插入右,失衡
avl_right_balance(broot);
(*taller)=false;
return 0;
}
}
return 0;
}
//插入新的结点
int creat_avl(bitree *broot,int key)
{
bool taller=false;
if((*broot)->data == NULL)
{
(*broot)->data=key;
(*broot)->BF=EH;
(*broot)->lchild=NULL;
(*broot)->rchild;
return 0;
}else{
return insert_avl(broot,key,&taller);
}
}
void preErgodic_recu(bitree root)
{
if(root)
{
printf("%d ",root->data);
preErgodic_recu(root->lchild);
preErgodic_recu(root->rchild);
}
}
int main(){
bitree broot;
int i;
broot=(bitree)malloc(sizeof(btnode));
broot->BF=EH;
broot->data=NULL;
broot->lchild=NULL;
broot->rchild=NULL;
//int a[20]={52,28,45,29,36,35,51,46,47,100,75,59,63,72,68,73,85,76,98,92};
int a[]={52,28,45,29,36,35,51,46,47,100,75,59,63,72,68,73,85,76,98,92};
for(i=0;i<20;i++)
{
creat_avl(&broot,a[i]);
}
preErgodic_recu(broot);
return 0;
}
如有疑问,请及时提出,谢谢!
———————————————————————————————————————————