树的3种常用链表结构
- 双亲表示法
typedef struct{
elemType data;
int parent;
}TNode;
typedef struct{
TNode T[100];
int r,n; //根的位置和结点数
}Tree;
对应结构如下图:(显然很容易进行Parent操作,缺点是不容易进行Child操作)
- 孩子表示法
typedef struct CNode{ //孩子结点的结点结构
int child;
struct CNode* next;
}*childNode;
typedef struct{
elemType child;
childNode firstChild; //孩子链表的头指针
}TNode;
typedef struct{
TNode T[100];
int r,n;
}Tree;
①.对应结构如下图:(显然很容易进行Child操作,缺点是不容易进行Parent操作)
②.可以将其与双亲表示法结合,构成带双亲的孩子链表,如下图:
- 孩子兄弟表示法
typedef struct TNode{
elemType data;
struct TNode* firstChild;
struct TNode* nextBrother;
};
树的结点结构中的两个链域分别指向它的第一个孩子和下一个兄弟结点
对应结构如下图:(Child和Parent操作都很容易实现)
根据孩子兄弟表示法可知,树可以转换为一棵与之唯一对应的二叉树,同样如果我们加一些条件,森林也可以转换为二叉树,具体内容略。