树——存储结构

树——存储结构


双亲表示法(顺序存储)

双亲表示法:每个节点中保存指向双亲的“指针”

#define MAX_TREE_SIZE 100	//树中最多的结点数
typedef struct{	//树的结点定义
    ElemType data;	//数据元素
    int parent;	//双亲位置域
}PTNode;
typedef struct{	//树类型定义
    PTNode nodes[MAX_TREE_SIZE];	//双亲表示,由各个结点组成的数组
    int n;	//结点数
}PTree;

孩子表示法(顺序+链式存储)

顺序存储各个结点,每个节点中保存孩子链表头指针

struct CTNode{
    int child;//孩子结点在数组中的位置
    struct CTNode *next;//下一个孩子
};
typedef struct {
    ElemType data;
    struct CTNode *firstChild;//第一个孩子
}CTBox;
typedef struct{
    CTBox nodes[MAX_TREE_SIZE];
    int n,r;//结点数和根的位置
}CTree;

孩子兄弟表示法(链式存储)

//树的存储——孩子兄弟表示法
typedef struct CSNode{
    ElemType data;//数据域
    struct CSNode *firstchild,*nextsibling;//第一个孩子和右兄弟指针
}CSNode,*CSTree;

优点:利用我们熟悉的二叉树操作来处理树

森林二叉树相互转换

猜你喜欢

转载自www.cnblogs.com/jev-0987/p/13202186.html