#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 0;
#define ERROR -1
#define OVERFLOW -2
#define YES 1
#define NO 0
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
typedef int Status;
typedef int TElemType;
//二叉树的顺序存储结构
#define MaxTreeSize 101
typedef TElemType BiTreeArr[MaxTreeSize];
BiTreeArr bt;
//二叉树的链式存储结构
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTreeLk;
typedef BiTreeLk SElemType;
//@@@@@@@@@@@@@@@@@@@
//栈的数据结构部分开始
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//初始化栈
Status InitStack(SqStack &Stack)
{
Stack.base = (SElemType *)malloc(sizeof(SElemType)*STACK_INIT_SIZE);
if(!Stack.base) exit(OVERFLOW);
Stack.top = Stack.base;
Stack.stacksize = STACK_INIT_SIZE;
return OK;
}
//得到栈顶元素
Status GetTop(SqStack &Stack,SElemType &e)
{
if(Stack.top==Stack.base)
return ERROR;
e = *(Stack.top-1);
return OK;
}
//压栈
Status Push(SqStack &Stack,SElemType e)
{
if(Stack.top-Stack.base>=Stack.stacksize)
{
Stack.base= (SElemType *)realloc(Stack.base, sizeof(SElemType)*(Stack.stacksize+STACK_INCREMENT));
if(!Stack.base) return ERROR;
Stack.top = Stack.base+Stack.stacksize;
Stack.stacksize += STACK_INCREMENT;
}
*Stack.top++ = e;
return OK;
}
//出栈
Status Pop(SqStack &Stack,SElemType &e)
{
if(Stack.top==Stack.base) return ERROR;
Stack.top--;
e = *Stack.top; return OK;
}
Status StackEmpty(SqStack &Stack)
{
if(Stack.base == Stack.top)
return YES;
return NO;
}
//栈的数据结构部分结束
//@@@@@@@@@@@@@@@@@@@@@@@@@@@
Status Visit(TElemType data)
{
if(&data==NULL)
{
return ERROR;
}
else
{
printf(" %d",data);
return OK;
}
}
//采用递归算法的先序、中序和后序遍历算法
//先序递归便利算法
Status PreOrderTraverse( BiTreeLk T, Status(*Visit)(TElemType) )
{
if(T)
{
if(Visit(T->data))
if(PreOrderTraverse(T->lchild, Visit))
if (PreOrderTraverse(T->rchild, Visit))
return OK;
return ERROR;
}
else
return OK;
}
//中序递归遍历算法
Status InOrderTraverse( BiTreeLk T, Status(*Visit)(TElemType) )
{
if(T)
{
if(PreOrderTraverse(T->lchild, Visit))
if(Visit(T->data))
if (PreOrderTraverse(T->rchild, Visit))
return OK;
return ERROR;
}
else
return OK;
}
//后序递归遍历算法
Status PostOrderTraverse( BiTreeLk T, Status(*Visit)(TElemType) )
{
if(T)
{
if(PreOrderTraverse(T->lchild, Visit))
if (PreOrderTraverse(T->rchild, Visit))
if(Visit(T->data))
return OK;
return ERROR;
}
else
return OK;
}
//非递归的二叉树中序遍历算法
//大体的思路是保存在遍历树时走过的路径,节点弹出的顺序即为节点访问的顺序
Status InOrderTraverse1(BiTreeLk T,Status(*Visit)(TElemType e))
{
SqStack Stack;
BiTreeLk p;
InitStack(Stack); //初始化一个栈
Push(Stack,T); //将根节点压入栈中
while(!StackEmpty(Stack)) //满足栈不空的条件
{
while(GetTop(Stack,p)&&p) //获取栈的最上面的指针
Push(Stack,p->lchild); //向左走到尽头,将这条线路上的点都压入栈中
Pop(Stack,p); //弹出入栈的空指针
if(!StackEmpty(Stack)) //如果栈不为空
{
Pop(Stack,p); //弹出一个节点指针
if(
!Visit(p->data)) //访问这个节点
return ERROR;
Push(Stack,p->rchild); //将其右节点入栈
}
}
return OK;
}
//非递归的二叉树中序遍历算法
Status InOrderTraverse2(BiTreeLk T, Status (*Visit)(TElemType e))
{
SqStack Stack;
InitStack(Stack);
BiTreeLk p = T;
while(p||!StackEmpty(Stack))
{
if(p)
{
Push(Stack,p);
p = p->lchild;
}
else
{
Pop(Stack,p);
if(!Visit(p->data))
return ERROR;
p = p->rchild;
}
}
return OK;
}
Status CreateBitreeLk(BiTreeLk &T)
{
char ch=' ';
scanf("%c",ch);
if(ch==' ')
T = NULL;
else
{
if(!(T=(BiTreeLk)malloc(sizeof(BiTNode))))
return ERROR;
T->data = ch;
CreateBitreeLk(T->lchild);
CreateBitreeLk(T->rchild);
}
return OK;
}
int main()
{
//Todo functions is here
//test the data structure above using the sampes of " A B C @ @ D E @ g @ @ f @ @ @ "
//'@' denote the empty character
BiTreeLk T;
CreateBitreeLk(T);
InOrderTraverse1(T,Visit);
return 0;
}
#include <stdlib.h>
#include <string.h>
#define OK 0;
#define ERROR -1
#define OVERFLOW -2
#define YES 1
#define NO 0
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
typedef int Status;
typedef int TElemType;
//二叉树的顺序存储结构
#define MaxTreeSize 101
typedef TElemType BiTreeArr[MaxTreeSize];
BiTreeArr bt;
//二叉树的链式存储结构
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTreeLk;
typedef BiTreeLk SElemType;
//@@@@@@@@@@@@@@@@@@@
//栈的数据结构部分开始
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//初始化栈
Status InitStack(SqStack &Stack)
{
Stack.base = (SElemType *)malloc(sizeof(SElemType)*STACK_INIT_SIZE);
if(!Stack.base) exit(OVERFLOW);
Stack.top = Stack.base;
Stack.stacksize = STACK_INIT_SIZE;
return OK;
}
//得到栈顶元素
Status GetTop(SqStack &Stack,SElemType &e)
{
if(Stack.top==Stack.base)
return ERROR;
e = *(Stack.top-1);
return OK;
}
//压栈
Status Push(SqStack &Stack,SElemType e)
{
if(Stack.top-Stack.base>=Stack.stacksize)
{
Stack.base= (SElemType *)realloc(Stack.base, sizeof(SElemType)*(Stack.stacksize+STACK_INCREMENT));
if(!Stack.base) return ERROR;
Stack.top = Stack.base+Stack.stacksize;
Stack.stacksize += STACK_INCREMENT;
}
*Stack.top++ = e;
return OK;
}
//出栈
Status Pop(SqStack &Stack,SElemType &e)
{
if(Stack.top==Stack.base) return ERROR;
Stack.top--;
e = *Stack.top; return OK;
}
Status StackEmpty(SqStack &Stack)
{
if(Stack.base == Stack.top)
return YES;
return NO;
}
//栈的数据结构部分结束
//@@@@@@@@@@@@@@@@@@@@@@@@@@@
Status Visit(TElemType data)
{
if(&data==NULL)
{
return ERROR;
}
else
{
printf(" %d",data);
return OK;
}
}
//采用递归算法的先序、中序和后序遍历算法
//先序递归便利算法
Status PreOrderTraverse( BiTreeLk T, Status(*Visit)(TElemType) )
{
if(T)
{
if(Visit(T->data))
if(PreOrderTraverse(T->lchild, Visit))
if (PreOrderTraverse(T->rchild, Visit))
return OK;
return ERROR;
}
else
return OK;
}
//中序递归遍历算法
Status InOrderTraverse( BiTreeLk T, Status(*Visit)(TElemType) )
{
if(T)
{
if(PreOrderTraverse(T->lchild, Visit))
if(Visit(T->data))
if (PreOrderTraverse(T->rchild, Visit))
return OK;
return ERROR;
}
else
return OK;
}
//后序递归遍历算法
Status PostOrderTraverse( BiTreeLk T, Status(*Visit)(TElemType) )
{
if(T)
{
if(PreOrderTraverse(T->lchild, Visit))
if (PreOrderTraverse(T->rchild, Visit))
if(Visit(T->data))
return OK;
return ERROR;
}
else
return OK;
}
//非递归的二叉树中序遍历算法
//大体的思路是保存在遍历树时走过的路径,节点弹出的顺序即为节点访问的顺序
Status InOrderTraverse1(BiTreeLk T,Status(*Visit)(TElemType e))
{
SqStack Stack;
BiTreeLk p;
InitStack(Stack); //初始化一个栈
Push(Stack,T); //将根节点压入栈中
while(!StackEmpty(Stack)) //满足栈不空的条件
{
while(GetTop(Stack,p)&&p) //获取栈的最上面的指针
Push(Stack,p->lchild); //向左走到尽头,将这条线路上的点都压入栈中
Pop(Stack,p); //弹出入栈的空指针
if(!StackEmpty(Stack)) //如果栈不为空
{
Pop(Stack,p); //弹出一个节点指针
if(
!Visit(p->data)) //访问这个节点
return ERROR;
Push(Stack,p->rchild); //将其右节点入栈
}
}
return OK;
}
//非递归的二叉树中序遍历算法
Status InOrderTraverse2(BiTreeLk T, Status (*Visit)(TElemType e))
{
SqStack Stack;
InitStack(Stack);
BiTreeLk p = T;
while(p||!StackEmpty(Stack))
{
if(p)
{
Push(Stack,p);
p = p->lchild;
}
else
{
Pop(Stack,p);
if(!Visit(p->data))
return ERROR;
p = p->rchild;
}
}
return OK;
}
Status CreateBitreeLk(BiTreeLk &T)
{
char ch=' ';
scanf("%c",ch);
if(ch==' ')
T = NULL;
else
{
if(!(T=(BiTreeLk)malloc(sizeof(BiTNode))))
return ERROR;
T->data = ch;
CreateBitreeLk(T->lchild);
CreateBitreeLk(T->rchild);
}
return OK;
}
int main()
{
//Todo functions is here
//test the data structure above using the sampes of " A B C @ @ D E @ g @ @ f @ @ @ "
//'@' denote the empty character
BiTreeLk T;
CreateBitreeLk(T);
InOrderTraverse1(T,Visit);
return 0;
}