树
树的定义用到了递归的思想,即一颗非空的树,其每个子节点也是一棵树的根节点
基本概念
节点的子树个数为度
除叶子节点和根节点的节点是内部节点
- parent,双亲
- child,孩子
- sibling,兄弟
- 广义兄弟:同层
- 狭义兄弟:同前驱
- level:根节点为第一层,子节点为第二层,以此递推
- depth:树的最大层次,从上往下数
- height:与深度值相同,只是从下往上数
- forest:不相交的树的集合,某个节点的n颗子树就是一个forest
- 有序树:子树是有次序的
树的存储结构
双亲表示法
基于顺序存储
每个节点:一个data域,一个parent域
#define MaxSize 100
typedef int DataType
typedef struct treenode_{
DataType data;
int parent; //双亲下标
}TreeNode;
typedef struct tree{
TreeNode nodes[MaxSize];
int r; //根节点下标
int n; //节点个数
}Tree;
扩展:根据实际需要增加更多的指针域:firstchild,rightsibling
孩子表示法
把节点存在数组里面,每个节点含data域,firstchild域(指向第一个儿子节点的指针),firstchild域指向的是一个包含该节点所有儿子的链表,每个链表节点含一个child域(该儿子在数组中的下标),一个next域(下个儿子节点)
与图的邻接表表示法类似
#define MaxSize 100
typedef int DataType
typedef struct childnode_{
int Child;
struct childnode_* next;
}ChildNode;
typedef struct treenode_{
DataType data;
ChildNode* firstchild;
}TreeNode;
typedef struct tree_{
TreeNode* nodes[MaxSize];
int r; //根节点下标
int n; //节点数
}Tree;
孩子兄弟表示法
基于二叉链表存储
节点包含一个data域,一个firstchild域,一个rightsibling域
typedef int DataType
typedef struct treenode_{
DataType data;
struct treenode_* firstchild,rightsibling;
}TreeNode;

浙公网安备 33010602011771号