typedef struct node{
char data;
struct node *Lchild;
struct node *Rchild;
}BiTNode,*BiTree;
int cnt=0;//统计叶子节点个数
BiTNode *Create(){ //二叉树的先序建立
char ch;
BiTNode *s;
ch=getchar();
if(ch=='#')erchashu
return NULL;
s=(BiTNode *)malloc(sizeof(BiTNode));
s->data=ch;
s->Lchild=Create();
s->Rchild=Create();
return s;
}
void PreOrder(BiTree root){ //前序遍历
if(root){
printf("%c ",root->data);
PreOrder(root->Lchild);
PreOrder(root->Rchild);
}
}
void InOrder(BiTree root){ //中序遍历
if(root){
InOrder(root->Lchild);
printf("%c ",root->data);
InOrder(root->Rchild);
}
}
void PostOrder(BiTree root){ //后序遍历
if(root){
PostOrder(root->Lchild);
PostOrder(root->Rchild);
printf("%c ",root->data);
}
}
void LeafCountNode(BiTree root){ //统计叶子结点个数
if(root){
if(!root->Lchild && !root->Rchild)
cnt++;
LeafCountNode(root->Lchild);
LeafCountNode(root->Rchild);
}
}
void IInOrder(BiTree root){ //输出各个叶子结点值
if(root){
IInOrder(root->Lchild);
if(!root->Lchild && !root->Rchild)
printf("%c ",root->data);
IInOrder(root->Rchild);
}
}
int PostTreeDepth(BiTree root){ //求树的高度
int h1,h2,h;
if(root==NULL){
return 0;
}
else{
h1=PostTreeDepth(root->Lchild);
h2=PostTreeDepth(root->Rchild);
h=(h1>h2?h1:h2)+1;
return h;
}
}
void MirrorTree(BiTree root){ //二叉树镜像树
BiTree t;
if(root==NULL)
return;
else{
t=root->Lchild;
root->Lchild=root->Rchild;
root->Rchild=t;
MirrorTree(root->Lchild);
MirrorTree(root->Rchild);
}
}
void OutPutPath(BiTree root,char path[],int len){ //输出每个叶子结点到根节点的路径
if(root){
if(!root->Lchild && !root->Rchild){
printf("%c ",root->data);
for(int i=len-1;i>=0;i--)
printf("%c ",path[i]);
printf("\n");
}
path[len]=root->data;
OutPutPath(root->Lchild,path,len+1);
OutPutPath(root->Rchild,path,len+1);
}
}
void PrintPath(BiTree root,char path[],int l){ //输出根到每个叶子结点的路径
int len=l-1;
if(root){
if(root->Lchild==NULL && root->Rchild==NULL){
path[len]=root->data;
for(int i=9;i>=len;i--)
printf("%c ",path[i]);
printf("\n");
}
path[len]=root->data;
PrintPath(root->Lchild,path,len);
PrintPath(root->Rchild,path,len);
}
}
int main(void){
int h,len;
char path[20];
BiTree root;
root=Create();
// PreOrder(root);
// printf("\n");
// InOrder(root);
// printf("\n");
// PostOrder(root);
// printf("\n");
// LeafCountNode(root);
// printf("叶子结点个数为:%d\n",cnt);
// IInOrder(root);
h=PostTreeDepth(root);
printf("树的高度为:High=%d\n",h);
// PrintTree(root,0);
// MirrorTree(root);
// PrintTree(root,0);
// OutPutPath(root,path,0);
// PrintPath(root,path,10);
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有