要求:
1.编写函数,输入字符序列,建立二叉树的二叉链表
2.编写函数,实现二叉树的中序递归遍历算法。
3.编写函数,实现二叉树的中序非递归遍历算法
4.编写函数,借助队列实现二叉树的层次遍历算法
5.编写函数,求二叉树的高度
6.编写函数,求二叉树的结点个数
7.编写函数,求二叉树的叶子个数
8.编写函数,交换二叉树每个结点的左子树和右子树
9.编写一个主函数,在主函数中设计一个简单的菜单,分别调试上述算法
代码:
#include <stdio.h>
#include<windows.h>
#include <malloc.h>
#define MAXSIZE 80
typedef char DataType;
#pragma warning (disable:4996)
typedef struct BiTNode //二叉链表存储结构
{
DataType data;
struct BiTNode *lchild, *rchild;
}BiTree;
typedef BiTree* ElemType; //栈中数据元素类型,栈中保存结点指针
typedef struct
{
ElemType data[MAXSIZE];
int top;
}SeqStack; //栈的类型定义,顺序栈
typedef struct
{
ElemType queue[MAXSIZE];
int front, rear;
}SP;
SeqStack *initSeqStack() //初始化栈
{
SeqStack *s; //首先建立栈空间,然后初始化栈顶指针
s = (SeqStack*)malloc(sizeof(SeqStack));
s->top = -1;
return s;
}
int push(SeqStack *s, ElemType x)
{
if (s->top == MAXSIZE - 1) { //栈满不能入栈
printf("栈满");
return 0;
}
s->top++;
s->data[s->top] = x;
return 1;
}
void pop(SeqStack *s) //出栈,假设栈不空
{
s->top--;
}
int empty(SeqStack *s) //判栈空
{
if (s->top == -1) return 1;
else return 0;
}
ElemType top(SeqStack *s) //取栈顶元素
{
return (s->data[s->top]);
}
BiTree *createBiTree()
{
DataType ch;
BiTree *T;
ch = getchar();
if (ch == '*')
return NULL;
else {
if (!(T = (BiTree*)malloc(sizeof(BiTree)))) {
perror("malloc\n");
}
T->data = ch;
T->lchild = createBiTree();
T->rchild = createBiTree();
}
return T;
}
void InOrder(BiTree *T) //中序遍历二叉树的递归算法
{
if (T)
{
InOrder(T->lchild);
printf("%c", T->data);
InOrder(T->rchild);
}
} //中序遍历二叉树的非递归算法
void InOrderFei(BiTree *p)
{
SeqStack *s; s = initSeqStack();
while (1)
{
while (p) { push(s, p); p = p->lchild; } //先将结点指针压栈,待出栈时再访问
if (empty(s))
break;
p = top(s); pop(s);
printf("%c", p->data); p = p->rchild;
}
}
void LevelOrder(BiTree *T) {
SP *p;
p = (SP*)malloc(sizeof(SP));
p->front = 0;
p->rear = 0;
if (T != NULL) {
p->queue[p->front] = T;
p->front = p->front + 1;
}
while (p->front != p->rear) {
T = p->queue[p->rear];
p->rear = p->rear + 1;
printf("%c", T->data);
if (T->lchild != NULL) {
p->queue[p->front] = T->lchild; //左孩子进队列
p->front = p->front + 1;
}
if (T->rchild != NULL) {
p->queue[p->front] = T->rchild; //右孩子进队列
p->front = p->front + 1;
}
}
} //求二叉树的高度
int height(BiTree *T)
{
int i, j;
if (!T)
return 0;
i = height(T->lchild); //求左子树的高度
j = height(T->rchild); // 求右子树的高度
return i > j ? i + 1 : j + 1; //二叉树的高度为左右子树中较高的高度加1
}
int Nodes(BiTree *T) { //求二叉树的所有结点个数
int n1, n2;
if (T == NULL)
return 0;
else if (T->lchild == NULL && T->rchild == NULL)return 1;
else {
n1 = Nodes(T->lchild);
n2 = Nodes(T->rchild);
return n1 + n2 + 1;
}
} //求二叉树的叶子结点个数
int leafs(BiTree *T)
{
int num1, num2;
if (T == NULL) return 0;
else {
if (T->lchild == NULL && T->rchild == NULL) return 1;
num1 = leafs(T->lchild); //求左子树中叶子结点数
num2 = leafs(T->rchild); //求右子树中叶子结点数
return num1 + num2;
}
}
void exchange(BiTree *T)
{
BiTree *temp = NULL;
if (T->lchild == NULL && T->rchild == NULL)
return ;
else {
temp = T->lchild;
T->lchild = T->rchild;
T->rchild = temp;
}
if (T->lchild)
exchange(T->lchild);
if (T->rchild)
exchange(T->rchild);
}
int main()
{
BiTree*bt;
bt = NULL;
int n;
while (1) {
printf("############################################################\n");
printf("############################################################\n");
printf("~~~~~~~~~~~~~~~~~ 1.递归-创建二叉链表 ~~~~~~~~~~~~~~~~\n");
printf("~~~~~~~~~~~~~~~~~ 2.递归-中序遍历二叉树 ~~~~~~~~~~~~~~~~\n");
printf("~~~~~~~~~~~~~~~~~ 3.非递归-中序遍历二叉树 ~~~~~~~~~~~~~~~\n");
printf("~~~~~~~~~~~~~~~~~ 4.层次-遍历二叉树 ~~~~~~~~~~~~~~~\n");
printf("~~~~~~~~~~~~~~~~~ 5.二叉树的高度 ~~~~~~~~~~~~~~~\n");
printf("~~~~~~~~~~~~~~~~~ 6.二叉树的结点个数 ~~~~~~~~~~~~~~~\n");
printf("~~~~~~~~~~~~~~~~~ 7.二叉树的叶子结点个数 ~~~~~~~~~~~~~~~\n");
printf("~~~~~~~~~~~~~~~~~ 8.交换二叉树的所有左右子树 ~~~~~~~~~~~~~~~\n");
printf("~~~~~~~~~~~~~~~~~ 0.退出系统 ~~~~~~~~~~~~~~~\n");
printf("############################################################\n");
printf("############################################################\n");
printf("please enter your select:>");
scanf("%d", &n);
getchar();
switch (n) {
case 1:
printf("请输入结点的先序序列创建二叉树,其中*表示空: \n");
bt = createBiTree();
break;
case 2:printf("递归-中序遍历二叉树:\n");
InOrder(bt);
printf("\n"); break;
case 3:printf("非递归-中序遍历二叉树\n");
inorderfei(bt);
printf("\n"); break;
case 4:printf("按层次遍历二叉树:");
levelorder(bt);
printf("\n"); break;
case 5:printf("二叉树的高度为:%d\n", height(bt));
printf("\n"); break;
case 6:printf("二叉树的结点数为:%d\n", nodes(bt));
printf("\n"); break;
case 7:printf("二叉树中叶子结点数为:%d\n", leafs(bt)); break;
case 8:printf("交换二叉树的所有左右子树\n");
printf("\n");
exchange(bt);
printf("交换后以中序遍历二叉树\n");
inorder(bt);
printf("\n\n");
break;
case 0:
printf("即将退出系统!\n");break;
default:
printf("输入错误!"); break;
}
}
system("pause");
return 0;
}