2021-11-14

在这里插入代码片
```#include "stdio.h"
#include "malloc.h"
#define max 20
//创建二叉树结构体 
typedef struct node{
	char data;
	struct node *lchild,*rchild;
}Tree,*btree;
//创建栈 
typedef struct{
	Tree elem[max];
	int top;
}stack;
void init(stack *s){
	s->top=0;
}
void push(stack *s,Tree e){
	if(s->top==20){
		printf("栈满\n");
		return;
	}else{
		s->elem[s->top++]=e;
	}
}
void pop(stack *s,Tree *e){
	if(s->top==0){
		printf("栈空\n");
		return;
	}else{
		s->top--;
		*e=s->elem[s->top];
	}
}
int empty(stack *s){
	if(s->top==0){
		return 1;
	}else{
		return 0;
	}
}
//创建二叉树 
void creat(btree *t){
	char a;
	scanf("%c",&a);
	if(' '==a){
		*t=NULL;
	}else{
		*t=(btree)malloc(sizeof(btree));
		(*t)->data=a;
		creat(&((*t)->lchild));
		creat(&((*t)->rchild));
	}
}
//中序非递归遍历输出 
void inorder(btree t){
	if(!t){
		return;
	}
	stack s;
	init(&s);
	Tree e;
	btree p=t;
	while(p||!empty(&s)){
		if(p){
			push(&s,*p);
			p=p->lchild;
		}else{
			pop(&s,&e);
			printf("%c\n",e.data);
			p=e.rchild;
		}
	}
}
int main(){
	btree t=NULL;
	printf("输入:\n");
	creat(&t);
	printf("中序遍历输出:\n");
	inorder(t);
}![在这里插入图片描述](https://img-blog.csdnimg.cn/b763237b96674df9b989ce8e82ea1182.png#pic_center)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值