数据结构树练习
题目
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;
}