二叉树的非递归遍历

二叉树的建立与遍历(非递归遍历)

二叉树建立的过程总会出现读入回车出错,现在终于找到了解决办法,用 fflush(stdin)  函数,就可以没输入一个字符建立节点,回车

 

 

#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT  10
#define OK 1
#define OVERFLOW 0
#define ERROR 0
typedef char TElemType;
typedef int Status;
typedef struct BiTNode{
	TElemType data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef BiTree SElemType;
typedef struct
{
	SElemType *base;
	SElemType *top;
	int stacksize;
}SqStack;
typedef struct
{
	int *base;
	int *top;
	int stacksize;
}flag;
Status InitStackflag(flag &S)
{  
	S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
	if(!S.base) return OVERFLOW;
	else
	{
		S.top=S.base;
		S.stacksize=STACK_INIT_SIZE;
	}
}
Status Pushflag(flag &S,int e)
{
	if(S.top-S.base>=S.stacksize)
	{
		S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
		if(!S.base) return OVERFLOW;
		else	
		{ 
			S.stacksize+=STACKINCREMENT;	
			*(S.top)=e;
			S.top++;		
		}
	}
	else 
	{
		*(S.top)=e;
		S.top++;					
	}
}
int Popflag(flag &S,int &e)
{
	if(S.top==S.base) return ERROR;
 	e=*(--S.top);
 	return e;
}
bool EmptyflagStack(flag &S)
{
	if(S.base==S.top)return true;
	else return false;
}
/************************************************************/
Status InitStack(SqStack &S)
{  
	S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
	if(!S.base) return OVERFLOW;
	else
	{
		S.top=S.base;
		S.stacksize=STACK_INIT_SIZE;
	}
}
Status Push(SqStack &S,SElemType e)
{
	if(S.top-S.base>=S.stacksize)
	{
		S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
		if(!S.base) return OVERFLOW;
		else	
		{ 
			S.stacksize+=STACKINCREMENT;	
			*(S.top)=e;
			S.top++;		
		}
	}
	else 
	{
		*(S.top)=e;
		S.top++;					
	}
}
SElemType Pop(SqStack &S,SElemType &e)
{
	if(S.top==S.base) return ERROR;
 	e=*(--S.top);
 	return e;
}
bool EmptyStack(SqStack &S)
{
	if(S.base==S.top)return true;
	else return false;
}
/*************************************************************************/
Status CreateBiTree(BiTree &T)
{
	TElemType ch;
	fflush(stdin); 
	scanf("%c",&ch); 
	fflush(stdin); 
	if(ch==' ')T=NULL;
	else{		
		if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))return OVERFLOW;
		T->data=ch;
		printf("请输入 %c 的左节点:\n",T->data);
		CreateBiTree(T->lchild);
		printf("请输入 %c 的右节点:\n",T->data);
		CreateBiTree(T->rchild);
	}
}
Status PreOrderTraver(BiTree &T,Status(*Visit)(TElemType))//先序遍历 
{
	SqStack S;
	BiTree P;
	InitStack(S);P=T;
	while(P||!EmptyStack(S))
	{
		if(P)
		{
			Visit(P->data);
			Push(S,P);			
			P=P->lchild;
		}
		else
		{
			Pop(S,P);
			P=P->rchild;
		}
	}
}
Status InOrderTraverse(BiTree T,Status(*Visit)(TElemType))//中序遍历 
{
	SqStack S;
	BiTree P;
	InitStack(S);P=T;
	while(P||!EmptyStack(S))
	{
		if(P)
		{
			Push(S,P);			
			P=P->lchild;
		}
		else
		{
			Pop(S,P);
			Visit(P->data);
			P=P->rchild;
		}
	}
}
Status PostOrderTraverse(BiTree &T,Status(*Visit)(TElemType))//后序遍历 
{
	SqStack S1;BiTree P;
	flag S2;
	InitStack(S1);P=T;InitStackflag(S2);
	int sign,e;//记录结点从栈中弹出的次数 
	while(P||!EmptyStack(S1))
	{
		if(P)
		{
			Push(S1,P);//第一次遇到结点T时压入其指针 
			Pushflag(S2,1);//置标志为1 
			P=P->lchild;
		}
		else
		{
			while(!EmptyflagStack(S2))
			{
				sign=Popflag(S2,e);
				Pop(S1,P);
				if(1==sign)//表示走过T的左子树 
				{
					Push(S1,P);
					Pushflag(S2,2);
					P=P->rchild;
					break;
				}
				else
				{
					if(2==sign)//表示T的左右子树都已走过 
					{
						Visit(P->data);
						P=NULL;
					}
				}
			}
		}
	}
}
Status PrintElement(TElemType e)
{
	printf("%c ",e);
	return OK;
}
int main()
{	
	BiTree T;
	printf("请输入树根:\n"); 
	CreateBiTree(T);
	printf("先序遍历:\n");
	PreOrderTraver(T,PrintElement);
	printf("\n中序遍历:\n");
	InOrderTraverse(T,PrintElement);
	printf("\n后序遍历:\n");
	PostOrderTraverse(T,PrintElement);
	printf("\n");	
}


 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值