树:
空树:
圆圈和直线来表示
圆圈表示数据
直线表示数据之间关系
根节点 :无父节点节点就是根节点
父节点:
子节点:
祖先节点:
子孙节点:
兄弟节点:父节点是同一个节点子节点就是兄弟节点
表兄节点:
叶子节点:没有子节点节点就是叶子节点
森林:n个树就组成森林
二叉树: 最多只有两个节点的树就是二叉树
左子树:
右子树:
树深度: depth
树节点个数 count
满二叉树:如果树深度是depth
count = 2^depth - 1
完全二叉树:
1 : count = 2^(depth - 1) - 1
2 : 第depth层,节点必须是从右到左的依次减少
平衡二叉树:
1 : 每一个节点左子树深度和右子树深度的绝对值差值应该小于等于1
数据操作方法:
所有右子树的数据最小值也比左子树最大值都要大
=============================================================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 10
//实现树结构
//定义树节点信息
struct tree_t{
int data; //表示数据
struct tree_t *right;//右孩子 右子树
struct tree_t *left;//左孩子 左子树
};
//实现插入函数
int insert(int num, struct tree_t **root)
{
struct tree_t *new = NULL;
struct tree_t *tail = *root;
new = (struct tree_t *)malloc(sizeof(struct tree_t));
if (NULL == new)
{
printf("malloc new false!\n");
return -1;
}
//树节点信息
new->data = num;
new->right = NULL;
new->left = NULL;
if (*root == NULL)
{
*root = new;
}
else
{
while(1)
{
if (new->data >= tail->data)
{
if (tail->right == NULL)
{
tail->right = new;
return 0;
}
tail = tail->right;
}
else if (new->data < tail->data)
{
if (tail->left == NULL)
{
tail->left = new;
return 0;
}
tail = tail->left;
}
}
}
return 0;
}
void fir(struct tree_t *root)
{
if (root == NULL)
{
return ;
}
printf("%d ", root->data);
fir(root->left);//左孩子
fir(root->right);//右孩子
}
//中序
void mid(struct tree_t *root)
{
if (root == NULL)
{
return ;
}
mid(root->left);
printf("%d ", root->data);
mid(root->right);
}
//后序
void end(struct tree_t *root)
{
if (root == NULL)
{
return ;
}
end(root->left);
end(root->right);
printf("%d ", root->data);
}
//层次变量
void lev(struct tree_t *root)
{
//定义队列
struct tree_t *queue[MAX];
int front = 0;
int end = 0;
queue[end++] = root;
while(front != end)
{
root = queue[front++];
printf("%d ", root->data);
if (root->left != NULL)
queue[end++] = root->left;
if (root->right != NULL)
queue[end++] = root->right;
}
}
//结构打印
void show(struct tree_t *root)
{
int i;
static int count = 0;
if (root == NULL)
{
return ;
}
count++;
show(root->right);
count--;
for (i = 0; i < count; i++)
{
printf("\t");
}
printf("%d\n", root->data);
count++;
show(root->left);
count--;
}
struct tree_t *max(struct tree_t *root)
{
if (root == NULL)
{
return NULL;
}
while (root->right != NULL)
{
root = root->right;
}
return root;
}
struct tree_t *min(struct tree_t *root)
{
if (root == NULL)
{
return NULL;
}
while (root->left != NULL)
{
root = root->left;
}
return root;
}
//求最大数 最小数 深度 个数 查找 删除
int main(void)
{
int i;
int num;
struct tree_t *root = NULL;//定义一个空树
int ret;
srand(time(NULL));
for (i = 0; i < MAX; i++)
{
num = rand() % 100;
printf("%d ", num);
//把数据保存到树中
ret = insert(num, &root);
if (ret == -1)
{
printf("insert false!\n");
return -1;
}
}
printf("\n");
printf("fir : ");
fir(root);
printf("\n");
printf("mid : ");
mid(root);
printf("\n");
printf("end : ");
end(root);
printf("\n");
printf("lev : ");
lev(root);
printf("\n");
printf("max : %d\n", max(root)->data);
printf("\n");
printf("min : %d\n", min(root)->data);
printf("==================\n");
show(root);
printf("==================\n");
return 0;
}