//根据先序序列和中序序列生成树
//后序遍历的递归算法
//中序遍历的非递归算法
//前序遍历的非递归算法
//后序遍历的非递归算法
//层次遍历
//求深度
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MAXSIZE 25
typedef struct BiTNode {
char data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
typedef struct Stack {
BiTNode* top;
BiTNode* base;
int stacksize;
}Stack;
/************以下是栈的基本操作*************/
Stack* InitStack(int size) {
Stack* S = (Stack*)malloc(sizeof(Stack));
S->base = (BiTNode*)malloc(size * sizeof(BiTNode));
S->top = S->base;
S->stacksize = size;
}
BiTree InitBiTNode() {
BiTree BiT = (BiTree)malloc(sizeof(BiTNode));
BiT->data = 0;
BiT->lchild = NULL;
BiT->rchild = NULL;
return BiT;
}
void Push(Stack* S, BiTNode* B) {
*(S->top) = *B;
S->top++;
}
BiTNode* Pop(Stack* S) {
BiTNode* B = InitBiTNode();
*B = *(--S->top);
return B;
}
BiTNode* GetTop(Stack* S) {
BiTNode* B = InitBiTNode();
*B = *(S->top - 1);
return B;
}
int Empty(Stack* S) {
int flag = 0;
if (S->top == S->base) { flag = 1; }
else { flag = 0; }
return flag;
}
/*********以上是栈的基本操作*********/
int Search(char* arr, char c) {
int index = -1, i = 0;
for (i = 0; i < strlen(arr); i++) {
if (*(arr + i) == c) {
index = i;
break;
}
}
return index;
}
//根据先序序列和中序序列生成树
void CreateBiTree(BiTree* B, char* preo, char* ino, int pref, int inf, int n) {
//char* preo 先序遍历序列数组; char* ino 中序遍历序列数组
//int pref 先序序列的开始位置 int inf 中序序列的开始位置
//int n 序列长度
if (n == 0) { *B = NULL; }
else {
int k = Search(ino, preo[pref]);
if (k == -1) { *B = NULL; }
else {
*B = InitBiTNode();
(*B)->data = preo[pref];
if (k == inf) { (*B)->lchild = NULL; }
else {
CreateBiTree(&((*B)->lchild), preo, ino, pref + 1, inf, k - inf);
}
if (k == inf + n - 1) { (*B)->rchild = NULL; }
else {
CreateBiTree(&((*B)->rchild), preo, ino, pref+1+k-inf, k+1, n-k+inf-1);
}
}
}
}
//后序遍历的递归算法
int PostOrder(BiTree B) {
if (B) {
PostOrder(B->lchild);
PostOrder(B->rchild);
printf("%c ", B->data);
}
return 1;
}
BiTree GoFarLeft( BiTree B, Stack* S) {
if(!B) { return NULL; }
while (B->lchild) {
Push(S, B);
B = B->lchild;
}
return B;
}
//中序遍历的非递归算法
void InOrder2(BiTree B) {
Stack* S = InitStack(MAXSIZE);
BiTree t = GoFarLeft(B, S);
while (t) {
printf("%c ", t->data);
if (t->rchild) {
t = GoFarLeft(t->rchild, S);
}
else if (!Empty(S)) {
t = Pop(S);
}
else
t = NULL;
}
}
//前序遍历的非递归算法
void PreOrder2(BiTree B) {
Stack* S = InitStack(MAXSIZE);
if (B) {
Push(S, B);
while (!Empty(S)) {
BiTree t = Pop(S);
printf("%c ", t->data);
if (t->rchild) {
Push(S, t->rchild);
}
if (t->lchild) {
Push(S, t->lchild);
}
}
}
}
//后序遍历的非递归算法
void PostOrder2(BiTree B) {
BiTree stack[MAXSIZE], t = B;
int tag[MAXSIZE];
int top = 0;
stack[0] = 0; //别忘了将栈底赋初值0,最后判断栈空
do {
while (t) {
top++;
stack[top] = t;
tag[top] = 0;
t = t->lchild;
}
while (top > 0 && tag[top] == 1) {
printf("%c ", stack[top]->data);
top--;
}
t = stack[top];
if (top > 0 && tag[top] == 0) {
t = t->rchild;
tag[top] = 1;
}
} while (t!=NULL || top != 0);
}
//层次遍历
void TransLevel(BiTree B) {
BiTree queue[MAXSIZE];
int front = 0, rear = 0;
if (B) { printf("%c ", B->data); }
queue[rear] = B;
rear++;
while (front < rear) {
B = queue[front];
front++;
if (B->lchild) {
printf("%c ", B->lchild->data);
queue[rear] = B->lchild;
rear++;
}
if (B->rchild) {
printf("%c ", B->rchild->data);
queue[rear] = B->rchild;
rear++;
}
}
}
//求深度
int Depth(BiTree BiT) {
int depthval, depthLeft, depthRight;
if (!BiT)
depthval = 0;
else {
depthLeft = Depth(BiT->lchild);
depthRight = Depth(BiT->rchild);
depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight);
}
return depthval;
}
int main() {
BiTree B;
char preo[30],ino[30];
printf("接下来请输入树的前序序列\n");
scanf("%s", preo);
printf("接下来请输入树的中序序列\n");
scanf("%s", ino);
CreateBiTree(&B, preo, ino, 0, 0, strlen(preo));
printf("接下来请收看:用递归方法进行的后序遍历\n");
PostOrder(B);
printf("\n\n");
printf("接下来请收看:用非递归方法进行的中序遍历\n");
InOrder2(B);
printf("\n\n");
printf("接下来请收看:用非递归方法进行的前序遍历\n");
PreOrder2(B);
printf("\n\n");
printf("接下来请收看:用非递归方法进行的后序遍历\n");
PostOrder2(B);
printf("\n\n");
printf("接下来请收看:层次遍历\n");
TransLevel(B);
printf("\n\n");
printf("接下来请收看树的深度:%d\n",Depth(B));
return 0;
}