#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct Node{
ElemType data;
struct Node *lchild;
struct Node *rchild;
}Node;
int count;
void Manu(); //菜单
Node *Create_BT_Tree(); //先序建立二叉树
void Preorder(Node *bt); //二叉树先序递归遍历
void Inorder(Node* bt); //二叉树中序递归遍历
void Postorder(Node* bt); //二叉树后序递归遍历
int Count_Leaf(Node *bt); //二叉树叶子结点数的递归遍历
int Count_Node(Node *bt); //二叉树结点数的递归遍历
int Tree_Depth(Node *bt); //二叉树深度的递归遍历
int main() {
Node *bt;
int choice;
int num = 1;
int cnt;
while (num) {
system("cls");
Manu();
scanf("%d", &choice);
getchar();
switch(choice) {
case 1: {
printf("\n\t\t\t请输入按先序建立二叉树的结点序列: ");
printf("\n\t\t\t说明: 若输入空格代表后续结点为空.");
printf("\n\t\t\t请输入根结点: ");
bt = Create_BT_Tree();
printf("\n\t\t\t二叉树成功建立!\n");
getchar();
break;
}
case 2: {
printf("\n\t\t\t该二叉树的先序遍历序列为: ");
Preorder(bt);
getchar();
break;
}
case 3: {
printf("\n\t\t\t该二叉树的中序遍历序列为: ");
Inorder(bt);
getchar();
break;
}
case 4: {
printf("\n\t\t\t该二叉树的后序遍历序列为: ");
Postorder(bt);
getchar();
break;
}
case 5: {
count = 0;
Count_Leaf(bt);
printf("\n\t\t\t该二叉树有%d个叶子结点。\n", count);
getchar();
break;
}
case 6: {
count = 0;
cnt = Count_Node(bt);
printf("\n\t\t\t该二叉树共有%d个结点。\n", count);
getchar();
break;
}
case 7: {
cnt = Tree_Depth(bt);
printf("\n\t\t\t该二叉树的深度为%d", cnt);
getchar();
break;
}
case 0: {
num = 0;
printf("\n\t\t\t\t程序结束!\n");
getchar();
break;
}
default: {
printf("\n\t\t\t\t输入错误!");
getchar();
break;
}
}
}
return 0;
}
void Manu() {
printf("\n\n\n");
printf("\t\t\t -二叉树的基本运算--\n");
printf("\n\t\t ************************************");
printf("\n\t\t * 1-------建二叉树 *");
printf("\n\t\t * 2-------先序遍历 *");
printf("\n\t\t * 3-------中序遍历 *");
printf("\n\t\t * 4-------后序遍历 *");
printf("\n\t\t * 5-------统计叶子 *");
printf("\n\t\t * 6-------统计结点 *");
printf("\n\t\t * 7-------统计深度 *");
printf("\n\t\t * 0-------退 出 *");
printf("\n\t\t ************************************");
printf("\t\t\t请选择菜单号(0--7):");
return ;
}
Node *Create_BT_Tree() {
Node *bt;
char ch;
printf("\n\t\t\t");
scanf("%c", &ch);
getchar();
if (ch==' ') {
bt = NULL;
} else {
bt = (Node*)malloc(sizeof(Node));
bt->data = ch;
printf("\n\t\t\t请输入%c结点的左孩子:", bt->data);
bt->lchild = Create_BT_Tree();
printf("\n\t\t\t请输入%c结点的右孩子:", bt->data);
bt->rchild = Create_BT_Tree();
}
return bt;
}
void Preorder(Node *bt) {
if (bt != NULL) {
printf("\n\t\t\t %c",bt->data);
Preorder(bt->lchild);
Preorder(bt->rchild);
}
return ;
}
void Inorder(Node* bt) {
if (bt != NULL) {
Inorder(bt->lchild);
printf("\n\t\t\t %c", bt->data);
Inorder(bt->rchild);
}
return ;
}
void Postorder(Node* bt) {
if(bt != NULL) {
Postorder(bt->lchild);
Postorder(bt->rchild);
printf("\n\t\t\t %c", bt->data);
}
return ;
}
int Count_Leaf(Node *bt) {
if (bt == NULL) {
return 0;
}
if(bt->lchild == NULL && bt->rchild == NULL) {
count++;
}
Count_Leaf(bt->lchild);
Count_Leaf(bt->rchild);
return (count);
}
int Count_Node(Node *bt) {
if (bt == NULL) {
return 0;
} else {
count++;
}
Count_Node(bt->lchild);
Count_Node(bt->rchild);
return (count);
}
int Tree_Depth(Node *bt) {
int l, r;
if (bt == NULL) {
return 0;
} else {
l = Tree_Depth(bt->lchild);
}
r = Tree_Depth(bt->rchild);
if(l > r) {
return (l+1);
} else {
return (r+1);
}
}
二叉树操作
最新推荐文章于 2022-05-03 20:53:18 发布