#include<stdio.h>
#include<stdlib.h>
#define MaxSize 255
typedef char ElemType;
typedef struct BiTNode {
ElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
typedef struct {
BiTree BNode[MaxSize];
int front, rear;
} SqQueue;
//根据输入结点初始化并建立二叉树
bool CreateBiTree(BiTree &T) {
ElemType ch;
scanf_s("%c", &ch);
getchar();
if (ch == ' ') {
T = NULL;
}
else {
if (!(T = (BiTree)malloc(sizeof(BiTNode)))) {
return false;
}
T->data = ch; //生成根结点
printf("请输入%c的左子树:", ch);
CreateBiTree(T->lchild); //构造左子树
printf("请输入%c的右子树:", ch);
CreateBiTree(T->rchild); //构造右子树
}
return true;
}
//初始化顺序队列
void InitQueue(SqQueue &Q) {
Q.front = Q.rear = 0;
}
//判断队列是否为空
bool IsEmpty(SqQueue Q) {
if (Q.front == Q.rear)
return true;
else
return false;
}
//顺序队列入队操作
bool EnQueue(SqQueue &Q, BiTree T) {
if ((Q.rear + 1) % MaxSize == Q.front)
return false;
Q.BNode[Q.rear] = T;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
//顺序队列出队操作
bool DeQueue(SqQueue &Q, BiTree &T) {
if (Q.front == Q.rear)
return false;
T = Q.BNode[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
//访问输出Visit函数
void Visit(BiTree T) {
printf("%c", T->data);
}
//先序遍历二叉树T
void PreOrder(BiTree T) {
//根左右
if (T != NULL) {
Visit(T);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
//中序遍历二叉树T
void InOrder(BiTree T) {
if (T != NULL) {
InOrder(T->lchild);
Visit(T);
InOrder(T->rchild);
}
}
//后序遍历二叉树T
void PosOrder(BiTree T) {
if (T) {
PosOrder(T->lchild);
PosOrder(T->rchild);
Visit(T);
}
}
//层序遍历二叉树T
void LevelOrder(BiTree T, SqQueue &Q) {
if (T != NULL) {
EnQueue(Q, T);
}
while (IsEmpty(Q) == false) {
BiTree T1;
DeQueue(Q, T1);
Visit(T1);
if (T1->lchild != NULL)
EnQueue(Q, T1->lchild);
if (T1->rchild != NULL)
EnQueue(Q, T1->rchild);
}
}
//求树的深度
int TreeDeep(BiTree T) {
int deep = 0;
if (T != NULL) {
int leftDeep = TreeDeep(T->lchild);
int rightDeep = TreeDeep(T->rchild);
deep = leftDeep >= rightDeep ? leftDeep + 1 : rightDeep + 1;
}
return deep;
}
//求树叶的个数
int LeafCount(BiTree T, int &num) {
if (T) {
if (T->lchild == NULL && T->rchild == NULL) {
num++;
}
LeafCount(T->lchild, num);
LeafCount(T->rchild, num);
}
return num;
}
//退出二叉树学习系统
void tree_outside()
{
system("cls");
printf("\n");
printf("\t\t\t\t*==============================*\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* 感谢使用 *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t*==============================*\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t* 广东海洋大学 *\n");
printf("\t\t\t\t* 物联网工程1191 黄伊权 *\n");
printf("\t\t\t\t* *\n");
printf("\t\t\t\t*==============================*\n");
printf("\n");
}
int main() {
BiTree T;
SqQueue Q;
InitQueue(Q);
int num = 0;
int c;
printf("请先输入树根结点(空格代表空结点):");
CreateBiTree(T);
printf("\t\t\t\t|====================================|\n");
printf("\t\t\t\t| 0--退出二叉树系统 |\n");
printf("\t\t\t\t| 1--二叉树前序遍历 |\n");
printf("\t\t\t\t| 2--二叉树中序遍历 |\n");
printf("\t\t\t\t| 3--二叉树后序遍历 |\n");
printf("\t\t\t\t| 4--二叉树层序遍历 |\n");
printf("\t\t\t\t| 5--计算二叉树的深度 |\n");
printf("\t\t\t\t| 6--计算二叉树的叶子节点个数 |\n");
printf("\t\t\t\t|====================================|\n");
while (1) {
printf("\n\n请输入选项:");
scanf_s("%d", &c);
switch (c)
{
case 0:
tree_outside(); //退出二叉树学习系统
return 0;
case 1:
printf("该二叉树的先序遍历结果为:");
PreOrder(T);
break;
case 2:
printf("该二叉树的中序遍历结果为:");
InOrder(T);
break;
case 3:
printf("该二叉树的后序遍历结果为:");
PosOrder(T);
break;
case 4:
printf("该二叉树的层次遍历结果为:");
LevelOrder(T, Q);
break;
case 5:
printf("该二叉树的深度为:");
printf("%d\n", TreeDeep(T));
break;
case 6:
printf("该二叉树的叶子结点个数为:");
printf("%d\n", LeafCount(T, num));
break;
}
}
}
用C实现二叉树遍历算法(数据结构作业)
最新推荐文章于 2022-11-12 23:48:39 发布