#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//函数原型:
int CreateTree(BiTree &T);
void PreOrderTraverse(BiTree &T);
void InOrderTraverse(BiTree &T);
void EndOrderTraverse(BiTree &T);
void tree_num(BiTree &T,int &n);
void yezi_num(BiTree &T,int &m);
int tree_deep(BiTree &T);
//主函数
int main()
{
BiTree T;
int m=0,n=0;
CreateTree(T);
printf("先序遍历:");
PreOrderTraverse(T);
printf("\n");
printf("中序遍历:");
InOrderTraverse(T);
printf("\n");
printf("后序遍历:");
EndOrderTraverse(T);
printf("\n");
tree_num(T,n);
printf("树的总结点个数: %d\n",n);
yezi_num(T,m);
printf("树的叶子结点个数: %d\n",m);
printf("树的深度: %d\n",tree_deep(T));
return 0;
}
//创建二叉树
int CreateTree(BiTree &T)
{
char a;
printf("请输入该节点的信息:");
scanf(" %c",&a);
if(a=='0')
{
T=NULL;
return 0;
}
else
{
if(!(T=(BiTNode*)malloc(sizeof(BiTNode)))) exit(0);
T->data=a;
CreateTree(T->lchild);
CreateTree(T->rchild);
}
return 1;
}
//先序遍历
void PreOrderTraverse(BiTree &T)
{
if(T)
{
printf("%c",T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
//中序遍历
void InOrderTraverse(BiTree &T)
{
if(T)
{
InOrderTraverse(T->lchild);
printf("%c",T->data);
InOrderTraverse(T->rchild);
}
}
//后序遍历
void EndOrderTraverse(BiTree &T)
{
if(T)
{
EndOrderTraverse(T->lchild);
EndOrderTraverse(T->rchild);
printf("%c",T->data);
}
}
//树的总结点个数
void tree_num(BiTree &T,int &n)
{
if(T)
{
n++;
tree_num(T->lchild,n);
tree_num(T->rchild,n);
}
}
//叶子节点个数;
void yezi_num(BiTree &T,int &m)
{
if(T)
{
if(T->lchild==NULL && T->rchild==NULL) m++;
yezi_num(T->lchild,m);
yezi_num(T->rchild,m);
}
}
//求深度
int tree_deep(BiTree &T)
{
int r=0,l=0;
if(T!=NULL)
{
l=tree_deep(T->lchild);
r=tree_deep(T->rchild);
return l>r?l+1:r+1;
}
return 0;
}
//ABC00DEG00F000
//-+a00*b00-c00d00/e00f00
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/69358.html