题目来源:严蔚敏《数据结构》C语言版本习题册 6.67
【题目】6.67
假设以二元组(F,C)的形式输入一棵树的诸边(其中F表示双亲结点的标识,C表示孩子结点标识),且在输入的二元组序列C中,C是按层次顺序出现的。F='^'时C为根结点标识,若C也为‘^’,则表示输入结束。例如,如下所示树的输入序列为:
试编写算法,由输入的二元组序列建立该树的孩子-兄弟链表。
【答案】
/*---------------------------------|6.67 二元组(F,C)创建CSTree |---------------------------------*/#define maxSize 50Status CreateCSTreeByDuplet(CSTree *pT) {char input[5];CSNode *queue[maxSize];int front,rear;CSNode *p, *q;front=rear=0; //对队列初始化for (scanf("%s", input); input[1]!='^'; scanf("%s", input)) {//创建结点p = (CSNode *)malloc(sizeof(CSNode)); if (!p) exit(OVERFLOW);p->data=input[1];p->firstchild=p->nextsibling=NULL;//入队列queue[rear]=p;rear=(rear+1)%maxSize;//找爸爸if (input[0]=='^') { //根结点-->不需要找爸爸*pT = p; //传出去} else {for (q=queue[front]; q->data!=input[0]; front=(front+1)%maxSize,q=queue[front]) ; //找爸爸//找哥哥if (!q->firstchild) q->firstchild=p; //它是最大的else { //它不是最大的for(q=q->firstchild; q->nextsibling; q=q->nextsibling) ; //找最近的哥哥q->nextsibling = p; //和哥哥牵手}}}return OK;}
【完整代码】
/*-------------------|树-孩子兄弟表达法 |-------------------*/#include<stdio.h>#include<stdlib.h>#include<string.h>#ifndef BASE#define BASE#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef int Status;typedef int bool;#endif#define TElemType chartypedef struct CSNode{TElemType data;struct CSNode *firstchild, *nextsibling;}CSNode, *CSTree;/*-------------------|6.59 输出T的所有边 |-------------------*/void TreePrintEdge(CSTree T) {CSNode *p;for (p=T->firstchild; p; p=p->nextsibling) {printf("(%c,%c)\n", T->data, p->data); //输出T的孩子TreePrintEdge(p); //输出p的孩子}}/*-------------------------|6.60 统计叶子结点的个数 |-------------------------*/int TreeLeafCnt(CSTree T) {// 树的叶子结点-->没有孩子int ret=0;CSNode *p;if (!T) return 0;else if (!T->firstchild) return 1;else {for (p=T->firstchild; p; p=p->nextsibling) ret += TreeLeafCnt(p);return ret;}}/*-------------------------|6.61 求树的度 |-------------------------*/int TreeDegree(CSTree T) {// 最大的孩子数int max=-1;int cnt=0;CSNode *child;if (!T) return -1; //空树else if (!T->firstchild) return 0; //只有一个根结点,度为0else {for (cnt=0,child=T->firstchild; child; child=child->nextsibling) cnt++; //求自己的度max = cnt; //当前的最大值for (child=T->firstchild; child; child=child->nextsibling) {cnt = TreeDegree(child);if (cnt>max) max=cnt;}return max;}}/*-------------------------|6.62 求树的深度 |-------------------------*/int TreeDepth(CSTree T) {int h1,h2;if (!T) return 0;else {h1 = TreeDepth(T->firstchild)+1; //T孩子的深度+1h2 = TreeDepth(T->nextsibling); //T兄弟的深度return h1>h2 ? h1 : h2;}}/*---------------------------------|6.66 双亲表示法-->孩子兄弟表达式|---------------------------------*/#define MAX_TREE_SIZE 50typedef struct PTNode{TElemType data;int parent; //双亲的位置域}PTNode;typedef struct{PTNode nodes[MAX_TREE_SIZE];int r,n;}PTree;CSTree CreateCSTreeByPTree(PTree T) {CSNode *tmp[MAX_TREE_SIZE]; //创建一个辅助的数组,仿照PTree结点的位置存放CSNode *p, *q;int i,parent;if (T.n<=0) return NULL;for (i=0; i<T.n; i++) { //双亲表按层序存储//创建新结点p = (CSNode *)malloc(sizeof(CSNode)); if(!p) exit(OVERFLOW);//赋值p->data = T.nodes[i].data;p->firstchild=p->nextsibling=NULL;//连接parent=T.nodes[i].parent; //父亲if (parent!=-1) { //不是根结点if (tmp[parent]->firstchild==NULL) tmp[parent]->firstchild=p; //第一个孩子else { //不是第一个孩子for (q=tmp[parent]->firstchild; q->nextsibling; q=q->nextsibling) ; //找到最后一个孩子q->nextsibling = p; //连接}}tmp[i]=p;}return tmp[0];}/*---------------------------------|6.67 二元组(F,C)创建CSTree |---------------------------------*/#define maxSize 50Status CreateCSTreeByDuplet(CSTree *pT) {char input[5];CSNode *queue[maxSize];int front,rear;CSNode *p, *q;front=rear=0; //对队列初始化for (scanf("%s", input); input[1]!='^'; scanf("%s", input)) {//创建结点p = (CSNode *)malloc(sizeof(CSNode)); if (!p) exit(OVERFLOW);p->data=input[1];p->firstchild=p->nextsibling=NULL;//入队列queue[rear]=p;rear=(rear+1)%maxSize;//找爸爸if (input[0]=='^') { //根结点-->不需要找爸爸*pT = p; //传出去} else {for (q=queue[front]; q->data!=input[0]; front=(front+1)%maxSize,q=queue[front]) ; //找爸爸//找哥哥if (!q->firstchild) q->firstchild=p; //它是最大的else { //它不是最大的for(q=q->firstchild; q->nextsibling; q=q->nextsibling) ; //找最近的哥哥q->nextsibling = p; //和哥哥牵手}}}return OK;}int main() {/*6.57测试数据一^RRARBRCADAECFFGFHFI^^测试数据二:^AABACADCECF^^*/CSTree CST;int cnt;CreateCSTreeByDuplet(&CST);TreePrintEdge(CST);cnt = TreeLeafCnt(CST); //6.60 叶子结点个数printf("TreeLeafCnt:%d\n", cnt);cnt = TreeDegree(CST); //6.61 树的度printf("TreeDegree:%d\n", cnt);cnt = TreeDepth(CST); //6.62 树的深度printf("TreeDepth:%d\n", cnt);return 0;}
