森林:若干树的集合;
将一棵二叉树去掉根节点,就成为了树;因此树和森林是可以相互转换的;
双亲表示法:
直接用一个结构数组,记录每个结点的双亲,
这样找到父节点是很简单的;但找到孩子节点是很难的;
树的数据结构就包括:一个数组,一个值记录根节点在数组里的位置,数组内元素的个数;
也就是图的邻接矩阵表示法;
#ifndef MULTITREE_H_
#define MULTITREE_H_
#define MAX_TREE_SIZE 100
typedef char TElemType;
//双亲结点
typedef struct PTNode
{
TElemType data;
int parent;
}PTNode,PTNList[MAX_TREE_SIZE];
typedef struct //数组内节点个数
{
PTNList nodes; //数组
int r; //根节点在数组里的索引
int n; //数组内节点个数;
}PTree;
//孩子链表
typedef struct CTNode//孩子节点;
{
int child_index;
struct CTNode* next; //下一个孩子节点;
}ChilePtr;
typedef struct CTBox
{
TElemType data;
CTNode* firstchild; //第一个孩子节点;
}CTBox,CTList[MAX_TREE_SIZE];
typedef struct
{
CTList nodes;
int nodes_num; //节点数
int root_index; //根节点位置
}CTree;
//带双亲的孩子节点表示法:
typedef struct Child_ptr
{
int child_index;
struct Child_ptr* next;
}Child_ptr;
typedef struct Parent_Node
{
TElemType data;
int parent_index;
Child_ptr* first_child;
}Parent_Node,ParentList[MAX_TREE_SIZE];
typedef struct
{
ParentList nodes;
int root_index; //根节点索引;
int node_num; //节点数
} CT_Tree;
#endif
孩子链表:邻接表的形式
孩子兄弟表示法:
通过一个bor_ptr访问兄弟节点;
通过child_ptr访问孩子节点;
缺点:找父亲困难,可以再增加一个父亲节点来解决;
//孩子兄弟表示法
typedef struct P_CNode //节点
{
TElemType data;
struct P_Node* child_ptr, * bro_ptr;
}P_CNode;