孩子兄弟表示法求树的度c语言
时间: 2023-06-22 22:31:15 浏览: 57
下面是使用孩子兄弟表示法来求树的度的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct CSNode {
char data;
struct CSNode *firstchild, *nextsibling;
} CSNode, *CSTree;
void CreateCSTree(CSTree *T) {
char c;
scanf("%c", &c);
if (c == '#') {
*T = NULL;
} else {
*T = (CSTree)malloc(sizeof(CSNode));
(*T)->data = c;
CreateCSTree(&(*T)->firstchild);
CreateCSTree(&(*T)->nextsibling);
}
}
int CountNodes(CSTree T) {
if (T == NULL) {
return 0;
} else {
return CountNodes(T->firstchild) + CountNodes(T->nextsibling) + 1;
}
}
int main() {
CSTree T;
printf("Please input a tree with child-sibling representation: ");
CreateCSTree(&T);
printf("The degree of the tree is: %d\n", CountNodes(T) - 1);
return 0;
}
```
这个程序首先使用孩子兄弟表示法来创建一棵树。然后使用一个递归函数 CountNodes 来计算树的结点数,最后树的度等于结点数减一。