二叉树操作
来源:互联网 发布:统计网络直报系统 编辑:程序博客网 时间:2024/06/05 05:58
#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); }}
0 0
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- 二叉树常用操作
- 二叉树的操作
- 二叉树 常见操作
- 二叉树基本操作
- 二叉树相关操作
- 二叉树 基本操作
- 二叉树的操作
- 二叉树基本操作
- 二叉树操作
- 二叉树 一些操作
- 二叉树操作
- 数据结构-二叉树操作
- 二叉树的操作
- 二叉树操作
- 二叉树的操作
- 004
- Monkeyrunner学习之三(如何使用坐标去点击的方法)
- 打印螺旋矩阵
- Oracle(1)数据库
- hdu 1011 Starship Troopers 树形DP
- 二叉树操作
- oracle基础
- NYOJ-117 求逆序数(树状数组或归并排序)
- Android visibility中VISIBLE、INVISIBLE、GONE的区别
- ubuntu 13.10安装oracle sdk和PyCharm教程详解
- ps -eo uname,comm|grep `whoami`失败 用户名长度限制
- linux suse iP config
- cocos2d-x 动画
- kettle插入更新操作