树的双亲孩子表示法

来源:互联网 发布:沈阳软件外包公司 编辑:程序博客网 时间:2024/05/02 11:11

完成了图的邻接表表示法,发现它的存储结构与树的孩子链表表示法相似,都是由一个数组加若干邻接链表形成的结构。只是在构造和遍历时有所区别。
在孩子链表中查找兄弟结点比较困难,查找孩子和双亲很方便,故适用于对孩子操作较多的应用。

结构图示为:

这里写图片描述

下面是c语言代码实现:

#include<stdio.h>#include<stdlib.h>#define MAXNODE 20typedef char ELemType;typedef struct SCNode{  //孩子结点     int childnode;    struct SCNode *nextchild;}CNode;typedef struct{                      //表头结构     ELemType data;    CNode *firstchid;    int r;                           //根的位置}CTBox;typedef struct{              CTBox tree[MAXNODE];    int n;                           //结点数目 }CTree;void InitCtree(CTree &t){//初始化树     int i;    printf("请输入树的结点个数:\n");    scanf("\n%d",&t.n);    printf("依次输入各个结点:\n");     for(i=0; i<t.n; i++)    {      fflush(stdin);      t.tree[i].data = getchar();      t.tree[i].r = 0;      t.tree[i].firstchid = NULL;     }}void AddChild(CTree &t){//添加孩子    int i,j,k;    printf("添加孩子\n");               for(k=0; k<t.n-1; k++)    {      fflush(stdin);       printf("请输入孩子结点及其双亲结点的序号:\n");      scanf("%d,%d",&i,&j);       fflush(stdin);      CNode *p = (CNode *)malloc(sizeof(CNode));      p->childnode = i;      p->nextchild = NULL;      t.tree[i].r = j;                   //找到双亲       if(!t.tree[j].firstchid)          t.tree[j].firstchid = p;      else       {         CNode *temp = t.tree[j].firstchid;         while(temp->nextchild)          temp = temp->nextchild;        temp->nextchild = p;       }    }}void FindChild(CTree &t){//查找孩子结点   int i,n;  printf("\n请输入要查询的结点的序号\n");  scanf("%d",&n);   if(!t.tree[n].firstchid)   printf("结点 %c 无孩子结点\n",t.tree[n].data);  else  {    CNode *p = t.tree[n].firstchid;    printf("%c 结点的孩子序号为: \n",t.tree[i].data);    while(p)    {        printf("%d ",p->childnode);         p = p->nextchild;    }  }}void FindParent(CTree &t){//查找双亲结点   int i,n;  printf("\n请输入要查询的结点的序号\n");  scanf("%d",&n);   if(!n)                //根结点无双亲    printf("结点 %c 无双亲结\n",t.tree[n].data);  else   printf("结点 %c 的双亲结点为 %c\n",t.tree[n].data,t.tree[t.tree[n].r].data); }int main(){    CTree t;    InitCtree(t);    AddChild(t);    FindChild(t);    //FindParent(t);    return 0;}
0 0
原创粉丝点击