二叉查找树

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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;i<n;i++)
		printf("   ");
	printf("%d\n",T->data.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;
		free(q);
	}
	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){
	case 1: 
		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");
		}
		else 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");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值