西电数据结构上机题——统计二叉树结点

举报
zstar 发表于 2022/08/06 02:43:54 2022/08/06
【摘要】 网上查了查,发现类似统计二叉树结点用的函数都是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;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103

文章来源: zstar.blog.csdn.net,作者:zstar-_,版权归原作者所有,如需转载,请联系作者。

原文链接:zstar.blog.csdn.net/article/details/109634272

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。