数据结构实验二叉排序树C语言实现 十数据结构实验二叉排序树C语言实现十
#include
#include
#include
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)data.key=key;
s->lchild=s->rchild=NULL;
T=s;
}
else if LT(key,T->data.key)
InsertBST(T->lchild,key);
else InsertBST(T->rchild,key);
return TRUE;
}
/*创建二叉排序树...
数据结构实验二叉排序树C语言实现十
#include
#include
#include
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)< (b))
#define LQ(a,b) ((a)<=(b))
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int KeyType;
typedef int Status;
typedef struct{
KeyType key; /*关键字域*/
}SElemType;
typedef struct BitNode{
SElemType data; /*存储空间基址,建时按实际长度分配,0号单元留空*/ struct BitNode *lchild,*rchild;
}BitNode,* BiTree;
/*二叉排序树的插入*/
Status InsertBST(BiTree &T,KeyType key){
BiTree s;
if(!T){
s=(BiTree)malloc(sizeof(BitNode));
s->data.key=key;
s->lchild=s->rchild=NULL;
T=s;
}
else if LT(key,T->data.key)
InsertBST(T->lchild,key);
else InsertBST(T->rchild,key);
return TRUE;
}
/*创建二叉排序树*/
void CreateBST(BiTree &T){
KeyType key;
T=NULL;
scanf("%d",&key);
while(key!=0){
InsertBST(T,key);
scanf("%d",&key);
}
}
/*中序遍历*/
void InOrderTraverse(BiTree T){
if(T){
InOrderTraverse(T->lchild);
printf("%4d",T->data.key);
InOrderTraverse(T->rchild);
}
}
/*打印二叉树*/
Status PrintTree(BiTree T,int n){
if(T==NULL)return FALSE;
PrintTree(T->rchild,n+1);
for(int i=0;idata.key);
PrintTree(T->lchild,n+1);
return TRUE;
}
/*二叉排序树的查找*/
BiTree SearchBST(BiTree T,KeyType key){
if(!T){printf("Can not find!!\n");return T;}
else if EQ(key,T->data.key){return T;}
else if LT(key,T->data.key) return SearchBST(T->lchild,key); else return SearchBST(T->rchild,key);
}
/*二叉排序树的删除*/
Status Delete(BiTree &p){
BiTree q,s;
if(!p->rchild){
q=p;
p=p->lchild;
}
else if(!p->lchild){
q=p;
p=p->rchild;
free(q);
}
else{
q=p;
s=p->lchild;
while(s->rchild){q=s;s=s->rchild;}
p->data=s->data;
if(q!=p) q->rchild=s->lchild;
else q->lchild=s->lchild;
delete s;
}
return TRUE;
}
Status DeleteBST(BiTree &T,KeyType key){
if(!T)return FALSE;
else{
if (EQ(key,T->data.key))return Delete(T);
else
if(LT(key,T->data.key))return DeleteBST(T->lchild,key); else return DeleteBST(T->rchild,key);
}
}
void main() {
BiTree b1,b2;
KeyType key;
int t;
begin:
printf("1:创建二叉排序树\n");
printf("2:打印排序树\n");
printf("3:查找结点\n");
printf("4:中序遍历\n");
printf("5:插入结点\n");
printf("6:删除结点\n");
printf("0:退出\n");
printf("请选择要进行的操作:");
scanf("%d",&t);
switch(t){
printf("Input every key(0 to quit):");
CreateBST(b1);
PrintTree(b1,0);//排序树的结果打印出来
goto begin;
case 2:PrintTree(b1,0);goto begin;
case 3:
printf("Input the key to search:");
scanf("%d",&key);
if(key!=0){
b2=SearchBST(b1,key);//把key 为根的子树打印出来PrintTree(b2,0);
printf("\nThe root is the key to search!!\n\n");
}
el
se printf("Can not find!!\n"); goto begin;
case 4:InOrderTraverse(b1);goto begin;
case 5:
printf("输入要插入的数据:");
scanf("%d",&key);
if(InsertBST(b1, key))printf("\n插入完毕!\n");
else printf("插入失败\n");
goto begin;
case 6:
printf("输入要删除的数据:");
scanf("%d",&key);
if(DeleteBST(b1, key))printf("\n删除完毕!\n"); else printf("删除失败\n");
goto begin;
case 0:goto end;break;
default: printf("输入错误\n");
}
end:
printf("\n谢谢使用!\n");
}
本文档为【数据结构实验二叉排序树C语言实现 十】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。