数据结构树练习

数据结构树练习

题目

1、请写出二叉树的二叉链表存储结构,基于此结构,设计算法:统计二叉树中结点个数。
2、请写出树的带双亲的孩子链表法存储结构,基于此结构,设计算法:计算树的度。

代码:

1、

#include<stdio.h>
#include<stdlib.h>
#define int TElemType
typedef struct tree
{
    TElemType data;
    struct tree *lchild,*rchild;
}Tree,*Bitree;
int temp=0;
int CountNode(Tree *T);
{
    if(T!=NULL)
    {
        temp++;
        CountNode(T->lchild);
        CountNode(T->rchild);
    }
    return temp;
}


int main(void)
{
    return 0;
}

2、

#include<stdio.h>
#include<stdlib.h>
#define Max 100

typedef struct CTNode 
{  
     int child;
     struct CTNode *next;  
} *ChildPtr;

typedef struct 
{
     TElemType    data;
     ChildPtr  firstchild;
     int parent; 
} CTBox;

typedef struct {
     CTBox  nodes[Max];  
     int n, r;    
} CTree;

int CountDu(CTree *T)
{
    int MAX=0,MIN=0;
    int i=0;
    for(i=0;i<T->n;i++)
    {
        ChildPtr newTemp=T->nodes[i]->firstchild;
        while(newTemp!=NULL)
        {
            MIN++;
            newTemp=newTemp->next;
        }
        if(MAX<MIN)
        {
            MAX=MIN;
        }
        
    }
    return MAX;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值