二叉树遍历的应用
来源:互联网 发布:Linux ls相关命令 编辑:程序博客网 时间:2024/05/19 16:47
建立二叉链表存储结构,创建二叉树,求二叉树的叶子节点数,求二叉树的高度,翻转二叉树。
输入序列:1 2 0 4 0 0 3 5 0 0 0
#include<malloc.h>#include<iostream>#include<stdio.h>#define TRUE 1#define FALSE 0#define ok 1#define maxsize 100using namespace std;typedef struct binode{int data;struct binode *lchild,*rchild;}binode,*bitree;int treecreated=FALSE;int leafcount=0;int creatbitree(bitree *T);int preordertraverse(bitree T);int height(bitree T);void swap(bitree *T);void Leafcount(bitree T);void main(){int choice=0;int leave=FALSE,flag;binode *BT;cout<<"======二叉树演示程序======="<<endl;do{cout<<"1.创建一个二叉树,按先序遍历结果输入,空用0表示"<<endl;cout<<"2.线序遍历二叉树,递归方式遍历二叉树"<<endl;cout<<"3.求叶子树"<<endl;cout<<"4.计算二叉树的高度"<<endl;cout<<"0.quit"<<endl;cout<<"----input your selection"<<endl;cin>>choice;switch(choice){case 1:if(treecreated){cout<<"sorry,the tree has been already created!"<<endl;break;}cout<<"put in numbers!"<<endl;flag=creatbitree(&BT);if(flag==ok){cout<<"okey,the tree named BT is created.."<<endl;treecreated=TRUE;}break;case 2:if(!treecreated){cout<<"you need creat a tree for further steps!"<<endl;break;}cout<<"pre order:";preordertraverse(BT);cout<<endl;break;case 3:if(!treecreated){cout<<"you need creat a tree for further steps"<<endl;break;}Leafcount(BT);cout<<"leaf count is"<<leafcount<<endl;break;case 4:int h;h=height(BT);cout<<"the height is"<<h<<endl;break;case 5:swap(&BT);cout<<"okey,tree was swaped"<<endl;break;case 0:leave=TRUE;break;}}while(!leave);cout<<"thanks for using ,bye~"<<endl;}int creatbitree(bitree *T){int ch=0;cin>>ch;if(ch==0)(*T)=NULL;else{(*T)=(bitree)malloc(sizeof(binode));(*T)->data=ch;creatbitree(&(*T)->lchild);creatbitree(&(*T)->rchild);}return ok;}int preordertraverse(bitree T){if(T){cout<<T->data<<" ";preordertraverse(T->lchild);preordertraverse(T->rchild);return ok;}elsereturn ok;}int height(bitree T){int hl,hr;if(T==NULL)return 0;hl=height(T->lchild)+1;hr=height(T->rchild)+1;return(hl>hr?hl:hr);}void swap(bitree *T){bitree p;if(*T!=NULL){p=(*T)->lchild;(*T)->lchild=(*T)->rchild;(*T)->rchild=p;swap(&(*T)->lchild);swap(&(*T)->rchild);}}void Leafcount(bitree T){if(T){leafcount++;if(T->lchild!=NULL)Leafcount(T->lchild);if(T->rchild!=NULL)Leafcount(T->rchild);}}
- 二叉树的遍历应用
- 二叉树遍历的应用
- 遍历二叉树的应用
- 二叉树遍历的应用
- 二叉树的应用—二叉树遍历的应用
- 遍历二叉树应用
- 二叉树的遍历的应用
- 二叉树遍历算法的应用
- 线索二叉树的遍历应用
- 二叉树的遍历及其应用
- 二叉树遍历算法的应用
- 二叉树的遍历及应用
- 二叉树遍历算法的应用
- 【笔记】遍历二叉树的应用
- 二叉树的基本应用:二叉树的遍历
- 二叉树层次遍历的应用--改造二叉链表
- 遍历二叉树及其应用
- 数据结构-->二叉树遍历应用
- Java 调用底层接口
- MQ JMS发送消息并接收客户端回复
- UNIX系统的IO模型
- Java中没有传引用!!!
- 矩阵元素求和的实现步骤
- 二叉树遍历的应用
- 函数指针应用
- 从OLAP的角度看mysql和pgsql
- 呻吟两句
- EL JSTL表达式的简单应用
- java线程同步原理及wait,notify的用法及与sleep的区别
- wiki上IPC解释
- c/vc字符编码转换解决方案
- 设计模式之python版(一)--工厂模式