二叉树的基本操作

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

typedef char str[30];
typedef struct Node{
	str data;
	struct Node *lchild,*rchild;
}BiNode,*BiTree;

typedef struct queue{
	BiTree data;
    struct queue * next;
} QNode,* LinkQueue;

typedef struct {
  LinkQueue front;
  LinkQueue rear;
}LQueue;

typedef enum{visit,travel} tasktype;//非递归遍历
typedef struct{
	BiTree ptr;
	tasktype task;
}Node1;

typedef struct slink{//链栈
     Node1 data;
     struct slink *next;
}SNode, *SLinkList;

int precreat(BiTree &T);//前序创建
//void creat1(BiTree &T,char pre[], char zhong[],int ps, int is, int n );//前中创建
void aorubiao(BiTree T,int n,char c);//凹入表
void depth(BiTree T,int level,int &dval);//算树的深度
void levelcreat(BiTree &T);//层序创建
void initqueue(LinkQueue &Q);//初始化队列
void enqueue(LinkQueue &Q,BiTree e);//进队列
void outqueue(LinkQueue &Q,BiTree &e);//出队列
void getqueue(LinkQueue &Q,BiTree &e);//得到队列头
int empty(LinkQueue Q);//判断队空
void predisp(BiTree T);//前序遍历
void zhongdisp(BiTree T);//中序遍历
void reardisp(BiTree T);//后序遍历
void leveldisp(BiTree T);//层序遍历
void depth(BiTree T,int level,int &dval);//算树的深度
void num1(BiTree T,int &n);//统计结点数
void num2(BiTree T,int &n);//统计叶子结点数
int initlink(SLinkList &L);//初始栈
int pushlink(SLinkList &L,BiTree x);//进栈
int poplink(SLinkList &L);//出栈
int emptystack(SLinkList  L);//判断栈空
void predisp1(BiTree FT);//前序遍历非递归
void zhongdisp1(BiTree FT);//中序遍历非递归
void reardisp1(BiTree FT);//后序遍历非递归
void leveldisp1(BiTree FT);//层次遍历非递归
int menu();

void depth(BiTree T,int level,int &dval){//算树的深度
	if(T){
		if(level>dval) dval=level;
	depth(T->lchild,level+1,dval);
	depth(T->rchild,level+1,dval);
	}
}

int precreat(BiTree &T){//前序创建
	char  ch[30];
	scanf("%s",ch);
	if(strcmp(ch,"#")==0) T=NULL;
	else{
		if(!(T=new BiNode))exit(0);
		strcpy(T->data,ch);
		precreat(T->lchild);
		precreat(T->rchild);
	}
	return 1;
}
/*void creat1(BiTree &T,char pre[], char zhong[],int ps, int is, int n ){//前中创建
	int k;
	if(n==0)T=NULL;
	else{
		k=search(zhong,pre[ps]);
		if(k==-1)T=NULL;
	else{
			if(!(T=new BiTNode))exit(0);
			T->data=pre[ps];
			if(k==is) 
				T-lchild=NULL;
			else
		creat1(T->lchild,pre,zhong,ps+1, is, k-is);
			if(k==is+n-1)
				T->rchild=NULL;
			else
creat1(T->rchild,pre,zhong,ps+1-(k-is),k+1, n-1-(k-is));
		}
	}
}*/
void aorubiao(BiTree T,int n,char c){//凹入表
	int i;
	if(T){
		for(i=1;i<n+15;i++) putchar('-');
		putchar('+');
		printf("%s(%c)\n",T->data,c);
		n=n-1;
		aorubiao(T->lchild,n,'L');
		aorubiao(T->rchild,n,'R');
	}
}
void initqueue(LQueue &Q){//初始化队列
  Q.front=Q.rear=new QNode;
if(!Q.front) exit(0);
   Q.front->next=NULL;
}
void enqueue(LQueue &Q,BiTree e){//进队列
	LinkQueue p;
	 p=new QNode;
	 p->data=e;
	 p->next=NULL;
	 Q.rear->next=p;
	 Q.rear=p;
}
void outqueue(LQueue &Q,BiTree &e){//出队列
    LinkQueue p;
    if(Q.front!=Q.rear){
		p=Q.front->next;
		e=p->data;
		Q.front->next=p->next;
		if(Q.rear==p)Q.rear=Q.front;
		delete p;
	}
}
void getqueue(LQueue &Q,BiTree &e){//得到队列头
	if(Q.front->next!=NULL)
	e=Q.front->next->data;
}
int empty(LQueue Q){//判断队空
	if(Q.front->next==NULL)return 1;
	else return 0;
}
void levelcreat(BiTree &T){//层序创建
	LQueue Q;
	initqueue(Q);
	BiTree p,s;   str fa,ch;    int type;
	T=NULL;
	printf("输入创建的数据(父 孩子 编号):");
	scanf("%s%s%d",fa,ch,&type);
	while(strcmp(ch,"#")!=0){
		p=new BiNode;
		strcpy(p->data,ch);
		p->lchild=p->rchild=NULL;
		enqueue(Q,p);
		if(strcmp(fa,"#")==0) T=p;
		else{
			getqueue(Q,s);
			while(strcmp(s->data,fa)!=0){
				outqueue(Q,s);
				getqueue(Q,s);
			}
			if(type==0) s->lchild=p;
			else  s->rchild=p;
			}
printf("输入创建的数据(父 孩子 编号)(结尾的那个孩子用#表示):");
    	scanf("%s%s%d",fa,ch,&type);
	}
}
void predisp(BiTree T){//前序遍历
	if(T==NULL) return ;
	else{
		printf("%s",T->data);
		predisp(T->lchild);
		predisp(T->rchild);
	}
}
void zhongdisp(BiTree T){//中序遍历
	if(T==NULL) return ;
	else{
		zhongdisp(T->lchild);
		printf("%s",T->data);
		zhongdisp(T->rchild);
	}
}
void reardisp(BiTree T){//后序遍历
	if(T==NULL) return ;
	else{
		reardisp(T->lchild);
		reardisp(T->rchild);
		printf("%s",T->data);
	}
}
void leveldisp(BiTree T){//层序遍历
	LQueue Q;
	BiTree q;
	initqueue(Q);
	if(T) enqueue(Q,T);
	while(!empty(Q)){
		outqueue(Q,q);
		printf("%s",q->data);
		if(q->lchild!=NULL) enqueue(Q,q->lchild);
		if(q->rchild!=NULL) enqueue(Q,q->rchild);
	}
}
void num1(BiTree T,int &n){//统计结点数
	if(T){
		n++;
		num1(T->lchild,n);
		num1(T->rchild,n);
	}
}
void num2(BiTree T,int &n){//统计叶子结点数
	if(T){
		if(!(T->lchild)&&!(T->rchild))n++;
		num2(T->lchild,n);
		num2(T->rchild,n);
	}
}
int initlink(SLinkList &L){//初始栈
	L=NULL;   return 1;}
int pushlink(SLinkList &L,Node1 x){//进栈
	SLinkList s;
	s=new SNode;
	s->data=x;
	s->next=L;
	L=s;
	return 1;
}
int poplink(SLinkList &L,Node1 &x){//出栈
    SLinkList q;
    if(L==NULL)return 0;
    q=L;
	x=q->data;
	L=L->next;
	delete q;
	return 1;
}
void predisp1(BiTree FT){//前序遍历非递
	SLinkList s;
	initlink(s);
	Node1 e;
	BiTree p;
	e.ptr=FT;e.task=travel;
	if(FT) pushlink(s,e);
	while(!emptystack(s)){
		poplink(s,e);
	if(e.task==visit) printf("%s  ",e.ptr->data);
		else{
			p=e.ptr; e.ptr=p->rchild;
			if(e.ptr) pushlink(s,e);
			e.ptr=p->lchild;e.task=travel;
			if(e.ptr) pushlink(s,e);
			e.ptr=p; e.task=visit; pushlink(s,e);
		}
	}
}

int emptystack(SLinkList  L){//判断栈空
	if(L==NULL)return 1;
	else return 0;
}
void zhongdisp1(BiTree FT){//中序遍历非递归
	SLinkList s;
	initlink(s);
	Node1 e;
	BiTree p;
	e.ptr=FT;e.task=travel;
	if(FT) pushlink(s,e);
	while(!emptystack(s)){
		poplink(s,e);
	if(e.task==visit) printf("%s  ",e.ptr->data);
		else{
			p=e.ptr; e.ptr=p->rchild;
			if(e.ptr) pushlink(s,e);
			e.ptr=p;e.task=visit; pushlink(s,e);
			e.ptr=p->lchild;e.task=travel;
			if(e.ptr) pushlink(s,e);
		}
	}
}
void reardisp1(BiTree FT){//后序遍历非递归
	SLinkList s;
	initlink(s);
	Node1 e;BiTree p;
	e.ptr=FT;e.task=travel;
	if(FT) pushlink(s,e);
	while(!emptystack(s)){
		poplink(s,e);
	if(e.task==visit) printf("%s  ",e.ptr->data);
		else{
			e.task=visit; pushlink(s,e);
			p=e.ptr; e.ptr=p->rchild;
			if(e.ptr) pushlink(s,e);
			e.ptr=p->lchild;e.task=travel;
			if(e.ptr) pushlink(s,e);
		}
	}
}
void leveldisp1(BiTree FT){//层次遍历非递归
   LQueue Q;BiTree q;
	initqueue(Q);
   if(FT) enqueue(Q,FT);
   while(!empty(Q)){
       outqueue(Q,q); 
       printf("%s  ",q->data);
       if(q->lchild) enqueue(Q, q->lchild);  
       if(q->rchild) enqueue(Q, q->rchild); 
   }
 }
int menu(){
   int num;
   while(1){
   system("cls");
   printf("1.前序递归创建  2.非递归层次创建\n");
   printf("3.凹入表显示          4.前序遍历\n");
   printf("5.中序遍历            6.后序遍历\n");
   printf("7.退出                8.层次遍历\n");
   printf("9.树的深度           10.结点总数\n");
   printf("11.叶子结点数  12.前序遍历非递归\n"); 
   printf("13.中序遍历非14.后序遍历非递归\n");
   printf("15.层次遍历非递归\n");
   printf("请选择功能编号(1-7):");
   scanf("%d",&num);break;
   //if(num>=1 && num<=7)break;
   }
   return num;
}
void main(){
	BiTree T;
	int level=1,dval=0;
    int n=0,i=0,num,ps=0,is=0,s=0,s1=0;
	char *pre,*zhong;
	pre=new char[30];
	zhong=new char[30];
   while(1){
     num=menu();
     switch(num){
        case 1:
		     printf("请输入字符串组:\n");
	           precreat(T);break; 
        case 2:
			  levelcreat(T);break;
        case 3:  depth(T,level,dval);
               aorubiao(T,dval,'L');
	           getch();break;
        case 4:predisp(T);getch();break; 
        case 5: zhongdisp(T); getch();
				break;
		case 6: reardisp(T);getch();
				break;
        case 7: exit(0);
		case 8: leveldisp(T);getch();
			break;
			case 9: depth(T,level,dval);
			printf("深度为:%d\n",dval);getch();
			break;
			case 10: num1(T,s);
		printf("结点总数为:%d\n",s);getch();
			break;
			case 11: num2(T,s1);
	printf("叶子结点总数为:%d\n",s1);getch();
			break;
			case 12:predisp1(T);getch();break; 
        case 13: zhongdisp1(T); getch();
				break;
		case 14: reardisp1(T);getch();
				break;
		case 15:leveldisp1(T);getch();
			break;
	 }
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值