二叉树的存储与遍历

来源:互联网 发布:java string 转json 编辑:程序博客网 时间:2024/05/05 17:38
typedef char status;typedef char Telemtype;#define NULL 0#define OK   1typedef struct bitnode{Telemtype data;struct bitnode *lchild,*rchild;}bitnode,*bitree;Creatbitree(bitree &t){//先序创建二叉树char ch;scanf("%c",&ch);if(ch=='*') t=NULL;else{t=(bitnode *)malloc(sizeof(bitnode));if(!t) exit(0);t->data=ch;Creatbitree(t->lchild);Creatbitree(t->rchild);}return OK;}status Leafcount(bitree t){//统计二叉树中叶子结点的个数if(!t)return 0;else if(t->lchild==0&&t->rchild==0)     return 1;     else  return Leafcount(t->lchild)+Leafcount(t->rchild);}status Depth(bitree t){//计算该二叉树的深度int dl,dr,d;if(!t) return 0;else {///////////#TODO7   dl=Depth(t->lchild);   dr=Depth(t->rchild);   if(dl>=dr)  return d=1+dl;   if(dl<dr)   return d=1+dr;}}status Nodecount(bitree t){//统计二叉树的总的结点数int n,nl,nr;if(!t) return 0;else{///////////#TODO8nl=Nodecount(t->lchild);nr=Nodecount(t->rchild);n=1+nl+nr;}return n;}void Preordertree(bitree t){//先序序列遍历二叉树if(t){printf("%c",t->data);Preordertree(t->lchild);Preordertree(t->rchild);}}void Inordertree(bitree t){//中序序列遍历二叉树if(t){Inordertree(t->lchild);printf("%c",t->data);Inordertree(t->rchild);}}void Pasttree(bitree t){   //后序序列遍历二叉树if(t){Pasttree(t->lchild);Pasttree(t->rchild);printf("%c",t->data);}}
编译运行结果如下:
<img src="http://img.blog.csdn.net/20140530230219765" alt="" />

2 0