数据结构:二叉排序树(C语言)

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <iostream>
#define ENDFLAG 999

//二叉排序树的二叉链表存储方式 
typedef struct
{
	int key;					//关键字项 

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


typedef BSTree ElementType;

typedef struct SNode{  
    ElementType Data;  
    struct SNode *Next;  
}SNode ,*Stack;

//堆栈的建立  
Stack CreateStack(){  
    Stack S;  
    S=(Stack)malloc(sizeof(struct SNode));  
    S->Next=NULL;  
    return S;  
} 
 
//判断是否空  
int IsEmpty(Stack S){  
    return(S->Next==NULL);  
} 
 
//Push操作  
void Push(ElementType item,Stack S){  
    struct SNode *TmpCell;  
    TmpCell=(struct SNode *)malloc(sizeof(struct SNode));  
    TmpCell->Data=item;  
    TmpCell->Next=S->Next;  
    S->Next=TmpCell;  
}  
 
//Pop操作
ElementType Pop(Stack S){  
    struct SNode *FirstCell;  
    ElementType TopElem;  
    if(IsEmpty(S)){  
        printf("堆栈空\n");  
        return NULL;  
    }else{  
        FirstCell=S->Next;  
        S->Next=FirstCell->Next;  
        TopElem=FirstCell->Data;  
        free(FirstCell);  
        return TopElem;  
    }  
}

//二叉排序树的递归查找 
BSTree SearchBST(BSTree T,int key)
{
	if((!T)||(key==T->data.key)) 
		return T;
	else if(key< T->data.key) 
		return SearchBST(T->lchild,key);
	else 
		return SearchBST(T->rchild,key);
 }

//二叉排序树的递归插入 
void InsertBST(BSTree &T,ElemType e) {
	if(!T){
		BSTree S;
		S=(BSTNode*)malloc(sizeof(BSTNode)); 
		S->data=e;
		S->lchild=S->rchild=NULL;
		T=S; 
	}
	else if(e.key<T->data.key)
	InsertBST(T->lchild,e);
	else if(e.key>T->data.key)
	InsertBST(T->rchild,e);
}

//二叉排序树的创建 
void CxeatBST(BSTree &T)
{
	T=NULL;
	ElemType c;
	scanf("%d",&c);
	while(c.key!=ENDFLAG)
	{
		InsertBST(T,c);
		scanf("%d",&c);
	}
}

//摧毁二叉树 
void DestroyBiTree(BSTree &T)
{
    if (T) return;
    else {
        DestroyBiTree(T->lchild);
        DestroyBiTree(T->rchild);
        free(T);
    }
}
//删除一个节点
void DeleteBST(BSTree &T,int key)
{
	BSTNode *p=T,*s=NULL,*o=T;
	while(p){
		if(p->data.key==key) 
			break;
		else if(p->data.key>key){
			o=p;
			p=p->lchild;
		}
		else
			{o=p;p=p->rchild;}
		
	}
	if(!p) return;
	BSTNode *q = p;
	BSTree F;F=p->rchild;
	if((p->lchild)&&(p->rchild)){
	   	s=p->lchild;
		while(s->rchild)
		{
		 q=s; s=s->rchild; 
		} 
		if(o!=p){
		s->rchild=p->rchild;
		p->rchild=NULL;
		DestroyBiTree(F);
		if(o->rchild==p)o->rchild=p->lchild;
		else  o->lchild=p->lchild;
		free(p);
		}
		else{
			s->rchild=p->rchild;
			p->rchild=NULL;
			DestroyBiTree(F);
			p=p->lchild;
			T=p;
			free(o);
		}
	}
	else if((p->lchild)&&(!p->rchild)){
		if(o->rchild==p)o->rchild=p->lchild;
		else  if(o->lchild==p)o->lchild=p->lchild;
		else T=p->lchild;
		free(p);
	}
	else if((!p->lchild)&&(p->rchild)){
		if(o->rchild==p)o->rchild=p->rchild;
		else if(o->lchild==p)  o->lchild=p->rchild;
		else T=p->rchild;
		free(p);
		}
	else {	
		if(o->rchild==p)o->rchild=NULL;
		else if(o->lchild==p)  o->lchild=NULL;
		else T=p->rchild;
		free(p);
	}
}
 
//利用栈中序遍历
void InOderTraversal(BSTree BT){
	 BSTree T=BT;
	 Stack S=CreateStack();
	 while(T||!IsEmpty(S)){
		 while(T){
			 Push(T,S);
			 T=T->lchild;
		 }
		T=Pop(S);
		printf(" %d  ",T->data);
		T=T->rchild;
	 }
	 printf("\n");
 }
main()
{
	printf("请输入您先建立的二叉排序树(数字之间用空格隔开"); 
	printf("以数字999为结束符)\n");
	BSTree T;
	CxeatBST(T);
	printf("创建成功");
	printf("\n----------------------------------------\n\n");
	int G;
	printf("请输入您想输入的操作\n1 插入一个元素\n2 ");
	printf("删除一个元素\n3 查找一个元素是否在树中\n4 中序遍历\n") ;
	printf("\n----------------------------------------\n\n");
	do{
		scanf("%d",&G);
		if(G==1){
			ElemType e;
			printf("请输入要插入的元素:\n");
			scanf("%d",&e);
			printf("%d插入成功后二叉排序树为:\n",e);
			InsertBST(T,e);
			InOderTraversal(T);
		}
	else if(G==2){
		int E;
		printf("请输入要删除的元素:\n");
		scanf("%d",&E);
		BSTree D=SearchBST(T,E);
		if(D)
		{
		printf("%d删除成功后二叉排序树为 \n",E);
		DeleteBST(T,E);
		InOderTraversal(T);
		}
		else printf("您输入的元素不在树中\n");
	}
	else if(G==3){
		ElemType L;
		printf("请输入您要查找的元素:\n");
		scanf("%d",&L);
		int  K;
		K=L.key ;
		BSTree D=SearchBST(T,K);
		if(D){
			printf("查找成功\n");
			printf("请输入操作\n 1将此元素从树中删除");
			printf("\n 2不删\n");
			int E;scanf("%d",&E);
			if(E==1) {
				DeleteBST(T,K);
				printf("%d删除成功后二叉排序树为:\n",K);
				InOderTraversal(T);
			} 
			else printf("%d未删除\n",K); 
		}
		else {
			printf("未查到此元素\n");
			printf("请输入操作\n 1将此元素插入到树中");
			printf("\n 2不插入\n");
			int E;scanf("%d",&E);
			if(E==1) {
				InsertBST(T,L);
				printf("%d插入成功后二叉排序树为:\n",K);
				InOderTraversal(T);
				} 
			else printf("%d未插入\n",K); 
		}	
	}
	else if(G==4){
		InOderTraversal(T);
	}
	printf("\n----------------------------------------\n\n");
}while(G);
}
posted @ 2020-01-31 17:32  lcyok  阅读(435)  评论(0)    收藏  举报