一、双亲表示法
#define MAX_TREE_SIZE 100
using TElemType = char;
using PTNode = struct PTNode1 //结点结构
{
TElemType data; //结点数据
int parent; //双亲位置
};
using PTree = struct PTree1 //树结构
{
PTNode nodes[MAX_TREE_SIZE]; //结点数组
int r; //根结点位置
int n; //结点个数
};
总结:可见双亲这种表示法非常灵活。
二、孩子表示法
#define MAX_TRUE_SIZE 100
typedef struct CTNode //孩子结点
{
int child;
struct CTNode* next;
} *ChildPtr;
typedef struct //表头结构,内含该结点存放的数据和孩子链表的头指针
{
TElemType data;
ChildPtr firstchild;
} CTBox;
typedef struct //树结构
{
CTBox nodes[MAX_TREE_SIZE]; //结点数组
int r;
int n; //根的位置和结点数
} CTree;
三、双亲孩子表示法
#define MAX_TRUE_SIZE 100
typedef struct CTNode //孩子结点
{
int child;
struct CTNode* next;
} *ChildPtr;
//表头结构,内含该结点存放的数据和孩子链表的头指针
typedef struct
{
TElemType data;
int parent; //存放双亲的下标
ChildPtr firstchild;
} CTBox;
//树结构
typedef struct
{
CTBox nodes[MAX_TREE_SIZE]; //结点数组
int r;
int n; //根的位置和结点数
} CTree;