#ifndef NSTREE_H_INCLUDED
#define NSTREE_H_INCLUDED
typedef char elemType;
typedef struct BiTNode
{
elemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void visit(elemType x);
BiTree createBiTree();
void PreOrderTraverseNonRec(BiTree T);
void InOrderTraversNonRec(BiTree T);
void PostOrderTraverseNonRec(BiTree T);
void LevelOrderTraverse(BiTree T);
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct StackNode
{
BiTNode **top;
BiTNode **base;
int size;
}Stack;
Stack *InitStack();
int isEmpty(Stack *S);
BiTNode*GetTop(Stack *S);
int push(Stack *S,BiTNode *p);
BiTNode *pop(Stack *S);
#define MAXSIZE 100
typedef struct
{
BiTNode *data[MAXSIZE];
int front,rear;
}Queue;
#endif
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "NSTree.h"
BiTree createBiTree()
{
char ch;
BiTree T;
if((ch=getchar())=='#')T=NULL;
else
{
T=(BiTNode*)malloc(sizeof(BiTNode));
T->data=ch;
T->lchild=createBiTree();
T->rchild=createBiTree();
}
return T;
}
void PreOrderTraverseNonRec(BiTree T)
{
Stack *S=InitStack();
BiTNode *p;
push(S,T);
while(!isEmpty(S))
{
p=GetTop(S);
while(p!=NULL)
{
visit(p->data);
push(S,p->lchild);
p=GetTop(S);
}
p=pop(S);
if(!isEmpty(S))
{
p=pop(S);
push(S,p->rchild);
}
}
}
void InOrderTraversNonRec(BiTree T)
{
Stack *S=InitStack();
BiTNode*p;
push(S,T);
while(!isEmpty(S))
{
p=GetTop(S);
while(p!=NULL)
{
push(S,p->lchild);
p=GetTop(S);
}
pop(S);
if(!isEmpty(S))
{
p=pop(S);
visit(p->data);
push(S,p->rchild);
}
}
}
void PostOrderTraverseNonRec(BiTree T)
{
Stack *S=InitStack();
BiTNode *p=T,*q;
do{
while(p)
{
push(S,p);
p=p->lchild;
}
q=NULL;
while(!isEmpty(S))
{
p=GetTop(S);
if((p->rchild==NULL)||q==p->rchild)
{
visit(p->data);
q=p;
pop(S);
}
else
{
p=p->rchild;
break;
}
}
}while(!isEmpty(S));
}
void LevelOrderTraverse(BiTree T)
{
Queue Q;
BiTNode *temp=NULL;
Q.front=Q.rear=0;
if(T)
{
Q.data[Q.rear]=T;
Q.rear=(Q.rear+1)%MAXSIZE;
while(Q.front!=Q.rear)
{
temp=Q.data[Q.front];
visit(temp->data);
Q.front=(Q.front+1)%MAXSIZE;
if(temp->lchild)
{
Q.data[Q.rear]=temp->lchild;
Q.rear=(Q.rear+1)%MAXSIZE;
}
if(temp->rchild)
{
Q.data[Q.rear]=temp->rchild;
Q.rear=(Q.rear+1)%MAXSIZE;
}
}
}
}
Stack *InitStack()
{
Stack *S;
S=(Stack*)malloc(sizeof(Stack));
S->base=(BiTNode**)calloc(STACK_INIT_SIZE,sizeof(BiTNode*));
S->top=S->base;
S->size=STACK_INIT_SIZE;
return S;
}
BiTNode*GetTop(Stack *S)
{
BiTNode *p=NULL;
if(S->top==S->base)return NULL;
p=*(S->top-1);
return p;
}
int push(Stack *S,BiTNode *p)
{
if(S->top-S->base>=S->size)
{
S->base=(BiTNode**)realloc(S->base,(STACKINCREMENT+STACK_INIT_SIZE)*sizeof(BiTNode*));
if(!S->base)
{
printf("入栈失败!\n");
return 0;
}
S->top=S->base+S->size;
S->size+=STACKINCREMENT;
}
*S->top++=p;
return 1;
}
BiTNode *pop(Stack *S)
{
if(S->top==S->base)
{
printf("出战失败!\n");
return NULL;
}
return *--S->top;
}
void visit(elemType x)
{
printf("%c,",x);
}
int isEmpty(Stack *S)
{
return S->top==S->base;
}
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "NSTree.h"
int main()
{
BiTree root;
printf("请按先序顺序输入节点值,输入'#'代表节点为空\n");
root=createBiTree();
printf("先序非递归\n");
PreOrderTraverseNonRec(root);
printf("\n");
printf("中序非递归\n");
InOrderTraversNonRec(root);
printf("\n");
printf("后序非递归\n");
PostOrderTraverseNonRec(root);
printf("\n");
printf("按层次遍历\n");
LevelOrderTraverse(root);
printf("\n");
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210521001706637.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzUyNzY1NTU0,size_16,color_FFFFFF,t_70)