数据结构作业11复习

在这里插入图片描述

分析: 假设没有孩子的结点(叶结点)个数为n₀,只有一个孩子的结点(度为1的结点)个数为n₁,有两个孩子的结点(度为2的结点)个数为n₂。
则n₀+n₁+n₂=2016
∵n₀=n₂+1(二叉树的性质:叶结点个数等于度为2的结点个数加1) ∴n₀+n₁+n₂=2016
⇨n₂+1+16+n₂=2016 ⇨2n₂=1999 n₂除不尽,所以答案错误。

在这里插入图片描述

等比数列求和,a0=1

在这里插入图片描述

从第二层开始,每增加一层,结点数加k
总结点数:K(h-1)+1(根结点)

在这里插入图片描述

总结点数mk+1

非叶子结点数m

叶子结点数 mk+1-m=m(k-1)+1

在这里插入图片描述
四叉树度为4,
法一:按照边的关系有e=n(总结点数)-1
得n1+2n2+3n3+4n4=n0+n1+n2+n3+n4-1
n1+2x2+3x3+4x4=n0+n1+2+3+4-1;
n0=21

法二:总结点数=总度数+1(根结点)
得n0+n1+n2+n3+n4=n1+2n2+3n3+4n4+1;
n0+n1+2+3+4=n1+2x2+3x3+4x4+1
n0=21.
在这里插入图片描述
同上题,将三叉树看作度为3的树即可

2-11
在一棵度为4的树T中,若有20个度为4的结点,10个度为3的结点,1个度为2的结点,10个度为1的结点,则树T的叶结点个数是:(3分)
A.41
B.82
C.113
D.122

点击查看解析

树中的总结点数=总度数+1
设叶结点数为n
则20x4+10x3+1x2+10x1=20+10+1+10+n
n=82

6-4 二叉树求深度和叶子数 (20分)

编写函数计算二叉树的深度以及叶子节点数。二叉树采用二叉链表存储结构

函数接口定义:

int GetDepthOfBiTree ( BiTree T);
int LeafCount(BiTree T);

其中 T是用户传入的参数,表示二叉树根节点的地址。函数须返回二叉树的深度(也称为高度)。

裁判测试程序样例:


//头文件包含
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>

//函数状态码定义
#define TRUE       1
#define FALSE      0
#define OK         1
#define ERROR      0
#define OVERFLOW   -1
#define INFEASIBLE -2
#define NULL  0
typedef int Status;

//二叉链表存储结构定义
typedef int TElemType;
typedef struct BiTNode{
    TElemType data;
    struct BiTNode  *lchild, *rchild; 
} BiTNode, *BiTree;

//创建二叉树各结点,输入零代表创建空树
//采用递归的思想创建
//递归边界:空树如何创建呢:直接输入0;
//递归关系:非空树的创建问题,可以归结为先创建根节点,输入其数据域值;再创建左子树;最后创建右子树。左右子树递归即可完成创建!

Status CreateBiTree(BiTree &T){
   TElemType e;
   scanf("%d",&e);
   if(e==0)T=NULL;
   else{
     T=(BiTree)malloc(sizeof(BiTNode));
     if(!T)exit(OVERFLOW);
     T->data=e;
     CreateBiTree(T->lchild);
     CreateBiTree(T->rchild);
   }
   return OK;  
}

//下面是需要实现的函数的声明
int GetDepthOfBiTree ( BiTree T);
int LeafCount(BiTree T);
//下面是主函数
int main()
{
   BiTree T;
   int depth, numberOfLeaves;
   CreateBiTree(T);
   depth= GetDepthOfBiTree(T);
     numberOfLeaves=LeafCount(T);
   printf("%d %d\n",depth,numberOfLeaves);
}

/* 请在这里填写答案 */

输入样例:

1 3 0 0 5 7 0 0 0
3 2

答案:

//思路:一求树深:用递归;递归边界:空树深度为0;递归关系:不空时,整颗树的深度为各子树深度最大值加1
     //二求叶子数:递归边界:空树叶子数为0;单节点二叉树叶子数为1;递归关系:其他情况下,二叉树的叶子数为左右子树叶子数之和
//注意:空树为最基本的情况
int GetDepthOfBiTree ( BiTree T)
{   
    int d;
	if(!T)
	{
		d=0;
	}
	else
	{
		if(GetDepthOfBiTree(T->lchild)>=GetDepthOfBiTree(T->rchild))
		{
		  d=GetDepthOfBiTree(T->lchild) +1; 	
		}
		else d=GetDepthOfBiTree(T->rchild)+1;
	}
	return d;
}
int LeafCount(BiTree T)
{
	if(!T)
	{
		return 0;
	}
	else if(!T->lchild&&!T->rchild)
	{
		return 1;
	}
	else
	{
		return LeafCount(T->lchild)+LeafCount(T->rchild);
	}
}

6-2 二叉树求结点数 (15分)

编写函数计算二叉树中的节点个数。二叉树采用二叉链表存储结构。

函数接口定义:

int NodeCountOfBiTree ( BiTree T);

其中 T是二叉树根节点的地址。

裁判测试程序样例:


//头文件包含
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>

//函数状态码定义
#define TRUE       1
#define FALSE      0
#define OK         1
#define ERROR      0
#define OVERFLOW   -1
#define INFEASIBLE -2

typedef int Status;

//二叉链表存储结构定义
typedef int TElemType;
typedef struct BiTNode{
    TElemType data;
    struct BiTNode  *lchild, *rchild; 
} BiTNode, *BiTree;

//创建二叉树各结点,输入0代表创建空树。
//采用递归的思想创建
//递归边界:空树如何创建呢:直接输入0;
//递归关系:非空树的创建问题,可以归结为先创建根节点,输入其数据域值;再创建左子树;最后创建右子树。左右子树递归即可完成创建!
Status CreateBiTree(BiTree &T){
   TElemType e;
   scanf("%d",&e);
   if(e==0)T=NULL;
   else{
     T=(BiTree)malloc(sizeof(BiTNode));
     if(!T)exit(OVERFLOW);
     T->data=e;
     CreateBiTree(T->lchild);
     CreateBiTree(T->rchild);
   }
   return OK;  
}

//下面是需要实现的函数的声明
int NodeCountOfBiTree ( BiTree T);
//下面是主函数
int main()
{
   BiTree T;
   int n;     
   CreateBiTree(T);  //先序递归创建二叉树     
   n= NodeCountOfBiTree(T);     
   printf("%d",n);
     return 0;
}

/* 请在这里填写答案 */

输入样例(注意输入0代表空子树):

1 3 0 0 5 3 0 0 0

输出样例:

4
//思路:递归边界:空树节点数为0
//递归关系:非空时,二叉树节点数为  左子树节点数+右子树节点数+ 1. 子树节点数递归可得
int NodeCountOfBiTree ( BiTree T)
{
    int NodeNumber;
	if(!T)
	{
		NodeNumber= 0;
	}
	else
	{
		NodeNumber= NodeCountOfBiTree(T->lchild)+ NodeCountOfBiTree(T->rchild)+1;
	}
    return NodeNumber;
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值