树的三种常用链表结构
1.双亲表示法
#define MAX_TREE_SIZE 100
typedef struct PTNode{
TElemType dat;
int parent; //双亲结点
}PTNode;
typedef struct{
PTNode nodes[MAX_TREE_SIZE];
int n; //结点个数
}PTree;
Parent(T,x)寻找双亲结点在常量时间内完成。
2.孩子表示法
typedef struct CTNode{
int child;
Struct CTnode *next;
}CTNode,*ChildPtr;
typedef struct CTBox{
TElemType data;
ChildPtr *firstchild;
}CTBox;
typedef struct{
CTBox nodes[MAX_TREE_SIZE];
int n,root;
}CTree;
可以结合双亲表示法和孩子表示法一起使用,在孩子表示法中,增加双亲域。
3.孩子兄弟表示法
typedef struct CSNode{
ElemType data;
struct CSNode *firstchild , *nextsibling;
}CSNode,*CSTree;
同样也可以为其增加parent域。
森林与二叉树的转换:
转换原则,把森林中第二棵树的根节点看作是第一棵树的根结点的兄弟,放置在其右子树上。