#include <stdio.h>
#include <stdlib.h>
#define MAX 100 //存储空间初始分配量
#define INCR 10 //存储空间分配增量
typedef struct BiTNode {
char data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;//将BiTree定义为指向二叉链表结点结构的指针类型 BiTNode *bt;
typedef struct {
BiTree *base;
int top;
int stacksize;
}SqStack;
typedef struct {
BiTree *base;
int front;
int rear;
}SqQueue;
void InitStack(SqStack &S) { // 构造一个空栈S
S.base=(BiTree*) malloc(MAX*sizeof(BiTree));
if (!S.base) exit (0); //存储分配失败
S.top=0; //空表长度为0
S.stacksize=MAX; //初始存储容量
}//InitStack
int StackEmpty(SqStack &S) { // 判断栈S是否是空栈,是返回1,否则返回0
if(S.top==0)
return 1;
else
return 0;
}//StcakEmpty
void Push(SqStack &S, BiTree e) { // 插入元素e为新的栈顶元素,
if (S.top>=S.stacksize) {//栈满追加空间
S.base=(BiTree*) realloc(S.base,(S.stacksize+INCR)*sizeof(BiTree));
if(!S.base)
exit(0);//存储分配失败
S.stacksize+=INCR;
}
S.base[S.top++]=e;
}
void Pop(SqStack &S,BiTree &e){
if(S.top==0)
exit(0);
e=S.base[–S.top];
}
int GetTop(SqStack S,BiTree &e){
if(S.top==0)
printf(“栈空”);
else
e=S.base[S.top-1];
return 1;
}
void InitQueue(SqQueue &Q){
Q.base=(BiTree*)malloc(MAX*sizeof(BiTree));
if(!Q.base)
exit(0);
Q.front=Q.rear=0;
}
int QueueEmpty(SqQueue Q){
if(Q.front==Q.rear)
return 1;
return 0;
}
void EnQueue(SqQueue &Q,BiTree e){
if((Q.rear+1)%MAX==Q.front)
exit (0);
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAX;
}
void DeQueue(SqQueue &Q,BiTree &e){
if(Q.front == Q.rear)
exit (0);
e=Q.base[Q.front];
Q.front=(Q.front+1) %MAX;
}
void PreCreatBiTree(BiTree &bt){
char ch;
ch=getchar();
if(ch==’#’)
bt=NULL;
else {
bt=(BiTree)malloc(sizeof(BiTree));
bt->data=ch;
PreCreatBiTree(bt->lchild);
PreCreatBiTree(bt->rchild);
}
}
//递归算法:
void PreOrderTraverse(BiTree bt){
if(bt){
printf(“%5c”,bt->data);
PreOrderTraverse(bt->lchild);
PreOrderTraverse(bt->rchild);
}
}
void InOrderTraverse(BiTree bt){
if(bt){
InOrderTraverse(bt->lchild);
printf(“%5c”,bt->data);
InOrderTraverse(bt->rchild);
}
}
void PostOrderTraverse(BiTree bt){
if(bt){
InOrderTraverse(bt->lchild);
InOrderTraverse(bt->rchild);
printf(“%5c”,bt->data);
}
}
//非递归算法 :
void PreOrderTraverse1(BiTree bt) { //非递归对bt进行先序遍历
SqStack S;
BiTree p;
if(bt){
InitStack(S);
Push(S, bt);//根指针进栈
while(!StackEmpty(S)) {
while(GetTop(S,p)&&p){
printf(“%5c”,p->data);
Push(S, p->lchild);
}
Pop(S,p);
if(!StackEmpty(S)){
Pop(S,p);
Push(S, p->rchild);
}
}
}
}
void InOrderTraverse1(BiTree bt){
SqStack S;
BiTree p;
InitStack(S);
Push(S,bt);
if(bt){
while(!StackEmpty(S)){
while(GetTop(S,p)&&p)
Push(S,p->lchild);
Pop(S, p);
if(!StackEmpty(S)){
Pop(S,p);
printf(“%5c”,p->data);
Push(S,p->rchild);
}//IF
}//while
}//if
}//void
void PostOrderTraverse1(BiTree bt){
SqStack S;
InitStack(S);
Push(S,bt);
BiTree p,q;
while(!StackEmpty(S)){
while(GetTop(S,p)&&p)
Push(S,p->lchild);
Pop(S,p);
if(!StackEmpty(S)){
GetTop(S,p);
if(p->rchild)
Push(S,p->rchild);
else{
q=p;
Pop(S,p);
printf(“%5c”,p->data);
while(!StackEmpty(S)&&GetTop(S,q)&&q->rchild==p){
Pop(S,p);
printf(“%5c”,p->data);
}//while
if(!StackEmpty(S)){
GetTop(S,p);
Push(S,p->rchild);
}//if
}//ELSE
}//IF
}//while
}//void
//层次遍历算法:
void LeverOrderTraverse(BiTree bt){
SqQueue Q;
BiTree p;
if(bt){
InitQueue(Q);
EnQueue(Q,bt);
while(!QueueEmpty(Q)){
DeQueue(Q,p);
printf(“%5c”,p->data);
if(p->lchild)
EnQueue(Q,p->lchild);
if(p->rchild)
EnQueue(Q,p->rchild);
}
}
}
void PutOutLeaf(BiTree bt){
if(bt){
if(bt->lchild==NULL&&bt->rchild==NULL){
printf(“%2c”,bt->data);
}
else {
if(bt->lchild)
PutOutLeaf(bt->lchild);
if(bt->rchild)
PutOutLeaf(bt->rchild);
}
}
}
void CountNodes(BiTree bt,int &n){
if(bt){
n++;
CountNodes(bt->lchild,n);
CountNodes(bt->rchild,n);
}
}
int main(){
int n;
BiTree bt;
printf(“递归算法:\n”);
printf(“请输入二叉表的数据:\n”);
PreCreatBiTree(bt);
printf(“输出先序遍历的结果:\n”);
PreOrderTraverse(bt);
printf(“\n”);
printf(“输出中序遍历的结果:\n”);
InOrderTraverse(bt);
printf(“\n”);
printf(“输出后序遍历的结果:\n”);
PostOrderTraverse(bt);
printf(“\n”);
printf(“非递归算法:\n”);
printf(“输出先序遍历的结果:\n”);
PreOrderTraverse1(bt);
printf(“\n”);
printf(“输出中序遍历的结果:\n”);
InOrderTraverse1(bt);
printf(“\n”);
printf(“输出后序遍历的结果:\n”);
PostOrderTraverse1(bt);
printf(“\n”);
printf(“层次遍历二叉树的结果:\n”);
LeverOrderTraverse(bt);
printf(“\n”);
printf(“输出叶子节点:”);
PutOutLeaf(bt);
printf(“\n”);
CountNodes(bt,n);
printf(“输出节点的个数:%d”,n);
printf(“\n”);
return 0;
}