/*数据结构 树的基本操作

 //根据先序序列和中序序列生成树

//后序遍历的递归算法

//中序遍历的非递归算法

//前序遍历的非递归算法

//后序遍历的非递归算法

//层次遍历

//求深度

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李 董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值