二叉树系统
来源:互联网 发布:数控车u型槽的编程 编辑:程序博客网 时间:2024/04/28 19:03
传到网上以后自己备一个优盘,专门存自己的代码库/************************2015/6/25 21:06二叉树的构造(层次输入),输出,结点数量,叶子结点数量,树的高度************************/#include <stdio.h>struct tree{ int lchild; int data; int rchild;}tree[1000005];int ok;int count;void createtree(int root){//创建一颗二叉树 层次遍历输入树的结点,输入0时结束 int r=root; for(int i=1;i<=r;i++) { int number; scanf("%d",&number); if(number==0) { break; } tree[i].data=number; tree[i].lchild=2*i; tree[2*i].data=0; tree[i].rchild=2*i+1; tree[2*i+1].data=0; r=2*i+1; }}void pre(int root){ //先序遍历 输出树 if(tree[root].data==0) return ; printf("%d ",tree[root].data); pre(tree[root].lchild); pre(tree[root].rchild);}void in(int root){//中序遍历 if(tree[root].data==0) return ; pre(tree[root].lchild); printf("%d ",tree[root].data); pre(tree[root].rchild);}void last(int root){//后序遍历 if(tree[root].data==0) return ; pre(tree[root].lchild); pre(tree[root].rchild); printf("%d ",tree[root].data);}int treehigh(int root){//输出树的高度 int lh,rh,h; if(tree[root].data==0) h=0; else { lh=treehigh(tree[root].lchild); rh=treehigh(tree[root].rchild); h=(lh>rh?lh:rh)+1; } return h;}void searchh(int root,int x){ if(ok==1) return ; if(tree[root].data==x) { printf("有这个值哦,地址是:"); ok=1; printf("%d\n",tree+root); return ; } else { searchh(tree[root].lchild,x); searchh(tree[root].rchild,x); }}void countr(int root){ if(tree[root].data==0) return ; count++; countr(tree[root].lchild); countr(tree[root].rchild);}int countleaf(int root){ if(tree[root].data==0) return 0; else if(tree[tree[root].lchild].data==0&&tree[tree[root].rchild].data==0) { return 1; } else return countleaf(tree[root].lchild)+countleaf(tree[root].rchild);}int main(void){ ok=1; while(1) { printf("你要做什么:1(我要创建一棵树)\n2(我想要知道树高)\n3(我要查找一个值)\n4(有几个结点呢)\n5(有几个叶子结点呢)\n6(俺要看看树中都有什么)"); int operation; printf("退出(-1)"); scanf("%d",&operation); if(operation==-1) break; switch(operation) { case 1: { printf("请输入值:(我是层次遍历输入的哦!0位结束标志。)\n"); createtree(1); break; } case 2: { printf("亲,你的树高是:"); int high=treehigh(1); printf("%d\n",high); break; } case 3: { printf("你要查找什么值呢?"); int x; scanf("%d",&x); searchh(1,x); break; } case 4: { count=0; printf("结点的个数为:"); countr(1); printf("%d个\n",count); break; } case 5: { printf("结点的个数为:"); int count=countleaf(1); printf("%d\n",count); break; } case 6: { printf("你想要怎么输出树中的数呢?\n1(先序遍历)\n2(中序遍历)\n3(后序遍历)"); int number; scanf("%d",&number); switch(number) { case 1: printf("我是先序遍历哦!"); pre(1); break; case 2: printf("我是中序遍历哦!"); in(1); break; case 3: printf("我是后序遍历哦!"); last(1); break; } } } } //pre(1); //printf("¸ß¶ÈΪ£º"); return 0;}//数据结构 二叉树的输入,查找,输出
0 0
- 二叉树系统
- 用二叉树写的仓库系统
- 嵌入式 二叉树实现通讯录管理系统
- 利用二叉树设计同学录管理系统
- 二叉查找树系统操作------tsearch tfind tdelete
- 二叉树、二叉堆
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 用Python和Pygame写游戏-从入门到精通(13)
- Android 2.3.3 SDK 源代码关联Eclipse ?
- Python使用__future__
- 回溯算法
- 用Python和Pygame写游戏-从入门到精通(14)
- 二叉树系统
- poj 1416 Shredding Company 二进制枚举
- Invert Binary Tree
- JVM -XX:NewRatio、-XX:SurvivorRatio参数含义
- 用Python和Pygame写游戏-从入门到精通(15)
- Tomcat启动时卡在“INFO: Deploying web application directory ......”的解决方法
- 用Python和Pygame写游戏-从入门到精通(16)
- BNUOJ 12887 isumi (计算几何+最小割)
- jquery 的datatable的属性说明