| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1389 人关注过本帖
标题:[讨论]平衡二叉排序树的建立和增加.删除
只看楼主 加入收藏
nqq622
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-1-4
收藏
 问题点数:0 回复次数:6 
[讨论]平衡二叉排序树的建立和增加.删除

算法的建立和增加没问题了,但删除有问题,请广大楼主帮忙改改!
#include <stdio.h>
# define LH 1
# define EH 0
# define RH -1
# define TRUE 1
# define FALSE 0
# define MAX 30
int taller=0;
int shorter=0;

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

BSTree R_Rotate(BSTree p)
{
BSTNode *lc;
lc=p->lchild;
p->lchild=lc->rchild;
lc->rchild=p;p=lc;
return p;
} /*R_Rotate*/

BSTree L_Rotate(BSTree p)
{
BSTNode *rc;
rc=p->rchild;
p->rchild=rc->lchild;
rc->lchild=p;p=rc;
return p;
}/*L_Rotate*/

BSTree LeftBalance(BSTree T)
{
BSTNode *lc,*rd;
lc=T->lchild;
switch(lc->bf)
{
case LH: T->bf=lc->bf=EH;
T=R_Rotate(T);
break;
case RH: rd=lc->rchild;
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;
T->rchild=L_Rotate(T->lchild);
T=R_Rotate(T);
}
return T;
}

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

BSTree InsertAVL (BSTree T, int e)
{
BSTree p;
if(!T)
{
T=(BSTree)malloc(sizeof(BSTNode));
T->data=e;
T->lchild=T->rchild=NULL;
T->bf=EH;
taller=TRUE;
}
else
{
if(e==T->data)
{
taller=FALSE;
return NULL;
}
if(e < T->data)
{
p=InsertAVL(T->lchild,e);
if(p)
{
T->lchild=p;
if(taller)
switch(T->bf)
{
case LH:
T=LeftBalance(T);
taller=FALSE;
break;
case EH:
T->bf=LH;
taller=TRUE;
break;
case RH:
T->bf=EH;
taller=FALSE;
break;
}
}
}
else
{
p=InsertAVL(T->rchild,e);
if (p)
{
T->rchild=p;
if(taller)
{
switch(T->bf)
{
case LH: T->bf=EH;
taller=FALSE;
break;
case EH: T->bf=RH;
taller=TRUE;
break;
case RH: T=RightBalance(T);
taller=FALSE;
break;
}
}
}
}
}
return T;
}

void midorder(BSTree T)
{
if(T!=NULL)
{
midorder(T->lchild);
printf("%d ",T->data);
midorder(T->rchild);
}
}

BSTree RightBalance1(BSTree p)
{
BSTree p1,p2;
if(p->bf==-1)
{
p->bf=0;
shorter=1;
}
else if(p->bf==0)
{
p->bf=1;
shorter=0;
}
else
{
p1=p->lchild;
if(p1->bf==0)
{
p->lchild=p1->rchild;
p1->rchild=p;
p1->bf=-1;
p->bf=1;
p=p1;
shorter=0;
}
else if(p1->bf==1)
{
p->lchild=p1->rchild;
p1->rchild=p;
p1->bf=p->bf=0;
p=p1;
shorter=1;
}
else
{
p2=p1->rchild;
p1->rchild = p2->lchild;
p2->lchild = p1;
p->lchild = p2->rchild;
p2->rchild = p;
if(p2->bf == 0)
{
p->bf = 0;
p1->bf = 0;
}
else if(p2->bf == 1)
{
p->bf = -1;
p1->bf = 0;
}
else
{
p->bf=0;
p1->bf=1;
}
p2->bf=0;
p=p2;
shorter=1;
}
}
return p;
}

BSTree LeftBalance1(BSTree p)
{
BSTree p1,p2;
if(p->bf==1)
{ p->bf=0;
shorter=1;
}
else if(p->bf==0)
{
p->bf=-1;
shorter=0;
}
else
{
p1=p->rchild;
if(p1->bf==0)
{
p->rchild=p1->lchild;
p1->lchild=p;
p1->bf=1;
p->bf=-1;
p=p1;
shorter=0;
}
else if(p1->bf==-1)
{
p->rchild=p1->lchild;
p1->lchild=p;
p1->bf=p->bf=0;
p=p1;
shorter=1;
}
else
{
p2=p1->lchild;
p1->lchild=p2->rchild;
p2->rchild=p1;
p->rchild=p2->lchild;
p2->lchild=p;
if(p2->bf==0)
{
p->bf=0;
p1->bf=0;
}
else if(p2->bf==-1)
{
p->bf=1;
p1->bf=0;
}
else
{
p->bf=0;
p1->bf=-1;
}
p2->bf=0;
p=p2;
shorter=1;
}
}
return p;
}

BSTree Delete(BSTree q, BSTree r)
{
if(r->rchild==NULL)
{
q=r;
r=r->lchild;
free(q);
shorter=1;
}
else
{
Delete(q,r->rchild);
if(shorter==1)
RightBalance1(r);
}
return r;
}

BSTree DeleteAVL(BSTree p, int e)
{
int k;
BSTree q,temp;
if(p==NULL)
return NULL;
else if(e < p->data)
{
p->lchild=DeleteAVL(p->lchild,e);
if(shorter==1)
p=LeftBalance1(p);
return p;
}
else if(e < p->data)
{
p->rchild=DeleteAVL(p->rchild,e);
if(shorter==1)
p=RightBalance1(p);
return p;
}
else
{
q=p;
if(p->rchild==NULL)
{
p=p->lchild;
free(q);
shorter=1;
}
else if(p->lchild==NULL)
{
p=p->rchild;
free(q);
shorter=1;
}
else
{
q->lchild=Delete(q,q->lchild);
if(shorter==1)
p=LeftBalance1(p);
p=q;
}
}
return p;
}

main()
{
int i,n,a[MAX];
BSTree T,p;
T=NULL;
printf("Enter the Sum of Node:\n");
scanf("%d",&n);
printf("Enter the Node Data:\n");
for (i=0; i < n; i++)
{
scanf("%d",&a[i]);
}
for (i=0; i < n; i++)
{
T=InsertAVL (T,a[i]);
}
midorder(T);
printf("\n");
printf("Enter the num to insert, enter \"0\" to finish!!\n");
scanf("%d",&n);
while (n!=0)
{
T=InsertAVL (T, n);
scanf("%d",&n);
}
printf("\n");
midorder(T);
printf("\n");
printf("Enter the num to delete, enter \"0\" to finish!!\n");
scanf("%d",&n);
while (n != 0)
{
T=DeleteAVL (T, n);
scanf("%d",&n);
}
midorder(T);
}

搜索更多相关主题的帖子: 删除 
2006-01-10 11:13
nqq622
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-1-4
收藏
得分:0 
应该是这部分有问题,但我改不了,呵呵
BSTree DeleteAVL(BSTree p, int e)
{
BSTree q;
if(p==NULL)
return NULL;
else if(e < p->data)
{
p->lchild=DeleteAVL(p->lchild,e);
if(shorter==1)
p=LeftBalance1(p);
return p;
}
else if(e < p->data)
{
p->rchild=DeleteAVL(p->rchild,e);
if(shorter==1)
p=RightBalance1(p);
return p;
}
else
{
q=p;
if(p->rchild==NULL)
{
p=p->lchild;
free(q);
shorter=1;
}
else if(p->lchild==NULL)
{
p=p->rchild;
free(q);
shorter=1;
}
else
{
q->lchild=Delete(q,q->lchild);
if(shorter==1)
p=LeftBalance1(p);
p=q;
}
}
return p;
}
2006-01-10 11:20
sandywhy
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-3-4
收藏
得分:0 
应该是你Delete()函数的问题吧,赋值错误:
q,r,是指针
r=r->rchild;无法成功
2006-03-04 13:27
sandywhy
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-3-4
收藏
得分:0 
gf
2006-03-04 13:31
sandywhy
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-3-4
收藏
得分:0 

Delete()错误:
改为:
BSTree Delete(BSTree *q,BSTree *r); c
或者
BSTree Delete(BSTree &q,BSTree r);c++

2006-03-04 13:33
sandywhy
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-3-4
收藏
得分:0 
typedef struct tagNode
{
int Key;
int bf;
struct tagNode * left;
struct tagNode * right;
}NODE,*LPNODE;
LPNODE R_RotateTree(LPNODE lpTree)
{
LPNODE lpLeft;
lpLeft=lpTree->left;
lpTree->left=lpLeft->right;
lpLeft->right=lpTree;
return lpLeft;
}
LPNODE L_RotateTree(LPNODE lpTree)
{
LPNODE lpRight;
lpRight=lpTree->right;
lpTree->right=lpRight->left;
lpRight->left=lpTree;
return lpRight;
}

int DelRightBalance(LPNODE *Tree)
{
LPNODE lpTree=*Tree,lpRight,lpLeft;
int IsLower;
lpRight=lpTree->right;
switch(lpRight->bf)
{
case LH:
lpLeft=lpRight->left;
switch(lpLeft->bf)
{
case LH:lpTree->bf=EH;lpRight->bf=RH;break;
case EH:lpTree->bf=lpRight->bf=EH;break;
case RH:lpTree->bf=LH;lpRight->bf=EH;break;
}
lpLeft->bf=EH;
lpTree->right=R_RotateTree(lpRight);
IsLower=TRUE;
break;
case EH:lpTree->bf=RH;lpRight->bf=LH;
IsLower=FALSE;
break;
case RH:lpTree->bf=lpRight->bf=EH;
IsLower=TRUE;
break;
}
*Tree=L_RotateTree(lpTree);
return IsLower;
}
int DelLeftBalance(LPNODE *Tree)
{
LPNODE lpTree=*Tree,lpLeft,lpRight;
int IsLower;
lpLeft=lpTree->left;
switch(lpLeft->bf)
{
case LH:lpTree->bf=lpLeft->bf=EH;
IsLower=TRUE;
break;
case EH:lpTree->bf=LH;lpLeft->bf=RH;
IsLower=FALSE;
break;
case RH:
lpRight=lpLeft->right;
switch(lpRight->bf)
{
case LH:lpTree->bf=RH;lpLeft->bf=EH;break;
case EH:lpTree->bf=lpLeft->bf=EH;break;
case RH:lpTree->bf=EH;lpLeft->bf=LH;break;
}
lpRight->bf=EH;
lpTree->left=L_RotateTree(lpLeft);
IsLower=TRUE;
break;
}
*Tree=R_RotateTree(lpTree);
return IsLower;
}

int DeleteAVLTree(LPNODE *Tree,int Key)
{
static int IsLower;
LPNODE lpTree,lpSch;
if(!(lpTree=*Tree)) return FALSE;
if(lpTree->Key==Key)
{
if(lpTree->left&&lpTree->right)
{
for(lpSch=lpTree->right;lpSch->left;lpSch=lpSch->left);
lpTree->Key=lpSch->Key;
Key=lpSch->Key;
}
else
{
*Tree=(lpTree->left)?lpTree->left:lpTree->right;
IsLower=TRUE;
free(lpTree);
return TRUE;
}
}
if(lpTree->Key>Key)
{
if(!DeleteAVLTree(&lpTree->left,Key))return FALSE;
if(IsLower)
switch(lpTree->bf)
{
case LH:lpTree->bf=EH;break;
case EH:lpTree->bf=RH;IsLower=FALSE;break;
case RH:if(!DelRightBalance(Tree))IsLower=FALSE;break;
}
}
else
{
if(!DeleteAVLTree(&lpTree->right,Key))return FALSE;
if(IsLower)
switch(lpTree->bf)
{
case LH:if(!DelLeftBalance(Tree))IsLower=FALSE;break;
case EH:lpTree->bf=LH;IsLower=FALSE;break;
case RH:lpTree->bf=EH;break;
}
}
return TRUE;
}
2006-03-04 13:35
doitnow
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-7-11
收藏
得分:0 
2006-07-13 19:55
快速回复:[讨论]平衡二叉排序树的建立和增加.删除
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044307 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved