数据结构——二叉树
来源:互联网 发布:淘宝刹那数码靠谱不 编辑:程序博客网 时间:2024/05/10 08:38
//功能:创建二叉树,实现先序,中序,后序,叶子节点个数,求深度功能#include<iostream>#include<stdio.h>#include<stdlib.h>#include<malloc.h>using namespace std;//存储结构typedef struct BiTNode{ char data; struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;int leafCount=0;int Creat(BiTree &T);int PreOrder(BiTree T);int InOrder(BiTree T);int PostOrder(BiTree T);int Leaf(BiTree T);int Deepth(BiTree T);void Visit(char e);int main(){ BiTree T; Creat(T); while(1){ cout<<"***************二叉树****************\n"; cout<<"1:先序遍历\n"; cout<<"2:中序遍历\n"; cout<<"3:后序遍历\n"; cout<<"4:求叶子节点\n"; cout<<"5:求树的深度\n"; cout<<"其他任意键退出!\n"; cout<<"*************************************\n"; int in; cout<<"choice:"; cin>>in; if(in==1){PreOrder(T);cout<<endl;} else if(in==2){InOrder(T);cout<<endl;} else if(in==3){PostOrder(T);cout<<endl;} else if(in==4){Leaf(T);cout<<leafCount<<endl;} else if(in==5){cout<<"deep:"<<Deepth(T);cout<<endl;} else if(in!=1||in!=2||in!=3||in!=4||in!=5)break; } return 0;}int Creat(BiTree &T){//按先序建树 char data; cout<<"Input Node data:"; cin>>data; if(data=='*')T=NULL; else{ if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))exit(0); T->data=data; Creat(T->lchild); Creat(T->rchild); } return 1;}int PreOrder(BiTree T){//先序遍历 if(T){ Visit(T->data); PreOrder(T->lchild); PreOrder(T->rchild); } return 1;}int InOrder(BiTree T){//中序遍历 if(T){ InOrder(T->lchild); Visit(T->data); InOrder(T->rchild); } return 1;}int PostOrder(BiTree T){//后序遍历 if(T){ PostOrder(T->lchild); PostOrder(T->rchild); Visit(T->data); } return 1;}int Leaf(BiTree T){//求叶子节点 if(T){ if(T->lchild==NULL&&T->rchild==NULL)leafCount++; Leaf(T->lchild); Leaf(T->rchild); } return 1;}int Deepth(BiTree T){//求树的深度 int ld,rd; if(T==NULL)return 0; ld=1+Deepth(T->lchild); rd=1+Deepth(T->rchild); return ld>rd?ld:rd;}void Visit(char e){//访问节点 cout<<e<<" ";}
0 0
- 数据结构—二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构——二叉树
- 数据结构学习—二叉树
- 数据结构—平衡二叉树
- 数据结构—平衡二叉树
- LeetCode Wildcard Matching DP/贪心
- Android 单元测试
- arm-linux-ld: ERROR: Source object init.o has EABI version 0, but target lcd_elf has EABI version 5
- POJ 2488 A Knight's Journey【dfs】
- 转stack around the variable “XX” was corrupted
- 数据结构——二叉树
- 线段树系列-hdu-1698-Just a Hook-区间修改求总和
- 从大数据菜鸟走上大师的历程
- tomcat7w.exe ,提示 指定的服务未安装 unable to open the service 'tomcat6'
- 学期总结--数据结构学而有感
- 第十六周项目一----基数排序
- jQuery插件的理解 $.extend()与$.fn.extend()
- 礼拜天签到能够累积2次签到次数哦
- 版本控制工具笔记-Git by STP