统计二叉树中特定点的个数

6-1 统计二叉树度为2的结点个数


#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

BiTree Create();/* 细节在此不表 */

int NodeCount ( BiTree T);

int main()
{
    BiTree T = Create();
    
    printf("%d\n", NodeCount(T));
    return 0;
}
/* 你的代码将被嵌在这里 */

代码:
 

int NodeCount ( BiTree T)
{
   if(!T)
       return 0;
    else if(T->lchild&&T->rchild)
        return 1+NodeCount(T->lchild)+NodeCount(T->rchild);
    else
        return NodeCount(T->lchild)+NodeCount(T->rchild);
}

6-4 二叉树求深度和叶子数


//头文件包含
#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);
}

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

函数:

int GetDepthOfBiTree ( BiTree T)
{
    if(!T)
        return 0;
    else
    {
        int d1,d2;
        d1=1+GetDepthOfBiTree(T->lchild);
        d2=1+GetDepthOfBiTree(T->rchild);
        return d1>d2?d1:d2;
    }
}
int LeafCount(BiTree T)
{
    if(!T)
        return 0;
    if(!T->lchild&&!T->rchild)
        return 1;
    else
        return LeafCount(T->lchild)+LeafCount(T->rchild);
    
}

6-1 二叉树统计指定取值元素节点的个数


//头文件包含
#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 XNodeCountOfBiTree ( BiTree T, TElemType x);
//下面是主函数
int main()
{
   BiTree T;
   int n;
     TElemType x;
   CreateBiTree(T);  //先序递归创建二叉树
     scanf("%d", &x);
   n= XNodeCountOfBiTree(T,x);     
   printf("%d",n);
     return 0;
}

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

//头文件包含
#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 XNodeCountOfBiTree ( BiTree T, TElemType x);
//下面是主函数
int main()
{
   BiTree T;
   int n;
     TElemType x;
   CreateBiTree(T);  //先序递归创建二叉树
     scanf("%d", &x);
   n= XNodeCountOfBiTree(T,x);     
   printf("%d",n);
     return 0;
}

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

函数:

int XNodeCountOfBiTree ( BiTree T, TElemType x)
{
    if(T==NULL)
        return 0;
    else
    {
        if(T->data==x)
         return 1+XNodeCountOfBiTree(T->lchild,x)+XNodeCountOfBiTree(T->rchild,x);
        else
            return XNodeCountOfBiTree(T->lchild,x)+XNodeCountOfBiTree(T->rchild,x);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值