树的相关术语
- 节点:数的分支或者边缘的结合部
- 叶子节点:树的最边缘部分,没有分支的节点
- 分之结点:数的分支结合部
- 结点的度:从结点出发的分支个数
- 树的宽度/度:所有树中结点的度的最大值
- 树的高度/深度:距离根节点最远的叶子节点与根节点之间间隔的节点个数。
树的性质
- 有且仅有一个称为root的跟结点
- 其他结点都是不相交的结点子集
- 跟结点没有前驱
- 叶子结点没有直接后续
2.树的简化形式 ==》二叉树
性质:除过必须有树本身的性质外,还有如下特性。
二叉树必须有左右子树之分:
定义:
1、有限集合
2、互不相交
3、左右之分
五种性质
- 二叉树第i层最多有2^(i-1)个结点
- 二叉树的高度为k,则最多有2^k -1 个节点
- 满二叉树 高度为k的树有2^k -1 个节点。除过叶子节点以外,其他节点都有左右孩子
- 完全二叉树将一棵二叉树按照满二叉树方式从根节点依次向下编码,如果最后的编码连续,则称为完全二叉树,否则不是完全二叉树
树的基本结构
typedef int datatype;
typedef struct _node_
{
struct _node_ * lchild;
datatype data;
struct _node_ * rchild;
}bitree;
//创建二叉树 ===>递归算法
bitree *create_node(datatype va)
{
bitree *n = malloc(sizeof(bitree));
n->data = va;
n->lchild = n->rchild = NULL;
return n;
}
///递归方式建立满二叉树
bitree * bitree_create(int size , datatype value)
{
bitree * t = create_node(value);
if(2* value <= size)
{
t->lchild = bitree_create(size,2*value );
}
if(2 * value +1 <= size)
{
t->rchild = bitree_create(size,2*value+1);
}
retrun t;
}
树的前中后遍历
//先序遍历
int pro_order(bitree * root)
{
printf("%d ",root->data); //中
if(root->lchild)
{
pro_order(root->lchild); //左
}
if(root->rchild)
{
pro_order(root->rchild); //右
}
return 0;
}
```c
//中序序遍历
int pro_order(bitree * root)
{
if(root->lchild)
{
pro_order(root->lchild); //左
}
printf("%d ",root->data); //中
if(root->rchild)
{
pro_order(root->rchild); //右
}
return 0;
}
```//后序遍历
int pro_order(bitree * root)
{
if(root->lchild)
{
pro_order(root->lchild); //左
}
if(root->rchild)
{
pro_order(root->rchild); //右
}
printf("%d ",root->data); //中
return 0;
}