西电数据结构上机题——统计二叉树结点
【摘要】
网上查了查,发现类似统计二叉树结点用的函数都是void打头,意味着这些算法都是不断递归,然后靠着全局计数器。 然而西电这道题函数返回限定类型为int,意味着要处理递归时候的变量增加问题,着实耗费了我不少头...
网上查了查,发现类似统计二叉树结点用的函数都是void打头,意味着这些算法都是不断递归,然后靠着全局计数器。
然而西电这道题函数返回限定类型为int,意味着要处理递归时候的变量增加问题,着实耗费了我不少头发,哈哈!
//统计结点总数及叶子结点总数的程序代码
#include<stdio.h>
#include<malloc.h>
//二叉链表的结构类型定义
const int maxsize=1024;
typedef char datatype;
typedef struct node
{
datatype data;
struct node *lchild,*rchild;
}bitree;
bitree*creattree();
void preorder(bitree*);
int countnode(bitree*);
int countleaf(bitree*);
int main()
{
bitree*root;
int leafnum,nodenum;
root=creattree();
printf("删除子树之前的二叉树:");
preorder(root);
printf("\n");
nodenum=countnode(root);
printf("结点总数是:%d\n",nodenum);
leafnum=countleaf(root);
printf("叶子结点总数是:%d\n",leafnum);
}
//建立二叉树
bitree*creattree()
{
datatype ch;
bitree*Q[maxsize];
int front,rear;
bitree*root,*s;
root=NULL;
front=1;rear=0;
printf("按层次输入结点值,虚结点输入'@',以换行符结束:");
while((ch=getchar())!='\n')
{
s=NULL;
if(ch!='@')
{
s=(bitree*)malloc(sizeof(bitree));
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
Q[rear]=s;
if(rear==1)root=s;
else
{
if(s&&Q[front])
if(rear%2==0)Q[front]->lchild=s;
else Q[front]->rchild=s;
if(rear%2==1)front++;
}
}
return root;
}
//先序遍历输出二叉树
void preorder(bitree*p)
{
if(p!=NULL)
{
printf("%c",p->data);
if(p->lchild!=NULL||p->rchild!=NULL)
{
printf("(");
preorder(p->lchild);
if(p->rchild!=NULL) printf(",");
preorder(p->rchild);
printf(")");
}
}
}
//添加统计结点个数算法
int countnode(bitree*p)
{
int nodenum=0;
if(p==NULL)return 0;
if(p->lchild!=NULL||p->rchild!=NULL)nodenum++;
if(p->lchild==NULL&&p->rchild==NULL)nodenum++;
nodenum+=countnode(p->lchild);
nodenum+=countnode(p->rchild);
return nodenum;
}
//添加统计叶子结点个数算法
int countleaf(bitree*p)
{
int leafnum=0;
if(p==NULL)return 0;
if(p->lchild==NULL&&p->rchild==NULL)return 1;
leafnum+=countleaf(p->lchild);
leafnum+=countleaf(p->rchild);
return leafnum;
}
文章来源: zstar.blog.csdn.net,作者:zstar-_,版权归原作者所有,如需转载,请联系作者。
原文链接:zstar.blog.csdn.net/article/details/109634272
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)