统计二叉树的结点个数
2012-09-09
展开全部
#include <alloc.h>
#include <stdio.h>
typedef struct node
{ char data;
struct node *lchild,*rchild;
}JD;
void countleaf(JD *bt,int *count)
{ if(bt!=NULL)
{ if((bt->lchild==NULL)&&(bt->rchild==NULL))
{ (*count)++;
return;
}
countleaf(bt->lchild,count);
countleaf(bt->rchild,count);
}
}
JD *crt_bt_pre(JD *bt)
{ char ch;
printf("ch=");
scanf("%c",&ch);
getchar();
if(ch==' ') bt=NULL;
else
{ bt=(JD *)malloc(sizeof(JD));
bt->data=ch;
bt->lchild=crt_bt_pre(bt->lchild);
bt->rchild=crt_bt_pre(bt->rchild);
}
return(bt);
}
void main()
{ /* ABC00DE0G00F000 */
JD *head=NULL;
int count=0;
head=crt_bt_pre(head);
countleaf(head,&count);
printf("count of leaf node is %d\n",count);
}
#include <stdio.h>
typedef struct node
{ char data;
struct node *lchild,*rchild;
}JD;
void countleaf(JD *bt,int *count)
{ if(bt!=NULL)
{ if((bt->lchild==NULL)&&(bt->rchild==NULL))
{ (*count)++;
return;
}
countleaf(bt->lchild,count);
countleaf(bt->rchild,count);
}
}
JD *crt_bt_pre(JD *bt)
{ char ch;
printf("ch=");
scanf("%c",&ch);
getchar();
if(ch==' ') bt=NULL;
else
{ bt=(JD *)malloc(sizeof(JD));
bt->data=ch;
bt->lchild=crt_bt_pre(bt->lchild);
bt->rchild=crt_bt_pre(bt->rchild);
}
return(bt);
}
void main()
{ /* ABC00DE0G00F000 */
JD *head=NULL;
int count=0;
head=crt_bt_pre(head);
countleaf(head,&count);
printf("count of leaf node is %d\n",count);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
void size(BONode *T)
{
if(T!=NULL)
return 1+size(T-lchild)+size(T-rchild);
else
return 0;
{
if(T!=NULL)
return 1+size(T-lchild)+size(T-rchild);
else
return 0;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |