二叉树和广义表

This commit is contained in:
lzy
2024-04-18 18:11:46 +08:00
parent f80d052277
commit 71fdab3f9f
6 changed files with 688 additions and 2 deletions

View File

@@ -10,7 +10,7 @@ int queue_en(QUEUE *ptr, const void *data)
llist_insert(ptr, data, LLIST_BACKWARD);
}
static always_match(const void *p1, const void *p2)
static int always_match(const void *p1, const void *p2)
{
return 0;
}

View File

@@ -0,0 +1,35 @@
# 方便起见一般都会先定义编译器链接器
CC = gcc
LD = gcc
# 正则表达式表示目录下所有.c文件相当于SRCS = main.c a.c b.c
SRCS = $(wildcard *.c)
# OBJS表示SRCS中把列表中的.c全部替换为.o相当于OBJS = main.o a.o b.o
OBJS = $(patsubst %c, %o, $(SRCS))
# 可执行文件的名字
TARGET = main
# .PHONE伪目标具体含义百度一下一大堆介绍
.PHONY:all clean
# 要生成的目标文件
all: $(TARGET)
LDFLAGS=-L/usr/local/lib
LDLIBS=-lqueue -lllist
# 第一行依赖关系冒号后面为依赖的文件相当于Hello: main.o a.o b.o
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
$(TARGET): $(OBJS)
$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS)
# 上一句目标文件依赖一大堆.o文件这句表示所有.o都由相应名字的.c文件自动生成
%.o:%.c
$(CC) -c $^
# make clean删除所有.o和目标文件
clean:
rm -f $(OBJS) $(TARGET)

View File

@@ -0,0 +1,362 @@
#include <stdio.h>
#include <stdlib.h>
#include <queue.h>
#define NAMESIZE 32
struct score_st
{
int id;
char name[NAMESIZE];
int math;
int chinese;
};
struct node_st
{
struct score_st data;
struct node_st *l, *r;
};
struct node_st *tree = NULL;
/**
* @brief 插入节点
* @details
* 小左大右
*
* !!! 为什么要传入二级指针
* !!! 因为你可能改变的是根节点自身,不仅仅是改变根节点指向的值。
* !!! 要改变一级指针本身,就需要传入二级指针。
*
* @param root
* @param data
* @return int
*/
static int insert(struct node_st **root, struct score_st *data)
{
struct node_st *node;
if (NULL == *root)
{
node = malloc(sizeof(*node));
if (NULL == node)
return -1;
node->data = *data;
node->l = NULL;
node->r = NULL;
*root = node;
return 0;
}
if (data->id <= (*root)->data.id)
return insert(&(*root)->l, data);
return insert(&(*root)->r, data);
}
struct score_st *find(struct node_st *root, int id)
{
if (NULL == root)
return NULL;
if (id == root->data.id)
return &root->data;
if (id < root->data.id)
return find(root->l, id);
return find(root->r, id);
}
static void print_s(struct score_st *d)
{
printf("%d %s %d %d\n", d->id, d->name, d->math, d->chinese);
}
static void draw_(struct node_st *root, int level)
{
if (NULL == root)
return;
draw_(root->r, level + 1);
for (int i = 0; i < level; i++)
printf(" ");
print_s(&root->data);
draw_(root->l, level + 1);
}
/**
* @brief 绘制树
* @details
* 躺下来,根在左,子节点在右。
* 这样子,深度即制表符个数。
*D
* @param root
*/
static void draw(struct node_st *root)
{
draw_(root, 0);
printf("\n\n");
// getchar();
}
/**
* @brief 计算某个节点的节点总个数
* @details
*
* @param root
* @return int
*/
static int get_num(struct node_st *root)
{
if (NULL == root)
return 0;
return get_num(root->l) + get_num(root->r) + 1;
}
static struct node_st *find_min(struct node_st *root)
{
if (NULL == root->l)
return root;
return find_min(root->l);
}
/**
* @brief 左旋
* @details
*
* @param root
*/
static void turn_left(struct node_st **root)
{
struct node_st *cur = *root;
*root = cur->r;
cur->r = NULL;
find_min(*root)->l = cur;
// draw(tree);
}
static struct node_st *find_max(struct node_st *root)
{
if (NULL == root->r)
return root;
return find_min(root->r);
}
/**
* @brief 右旋
* @details
*
* @param root
*/
static void turn_right(struct node_st **root)
{
struct node_st *cur = *root;
*root = cur->l;
cur->l = NULL;
find_max(*root)->r = cur;
// draw(tree);
}
/**
* @brief 平衡
* @details
*
* @param root
*/
static void balance(struct node_st **root)
{
if (NULL == *root)
return;
int sub;
while (1)
{
sub = get_num((*root)->l) - get_num((*root)->r);
if (sub >= -1 && sub <= 1)
break;
if (sub < -1)
turn_left(root);
if (sub > 1)
turn_right(root);
}
balance(&(*root)->l);
balance(&(*root)->r);
}
/**
* @brief 删除某个节点
* @details
* 左子树顶上来
*
* @param root
* @param tmpid
*/
static void delete(struct node_st **root, int id)
{
struct node_st **node = root;
struct node_st *cur;
while (*node != NULL && (*node)->data.id != id)
{
if (id < (*node)->data.id)
node = &(*node)->l;
else
node = &(*node)->r;
}
if (NULL == *node)
return;
cur = *node;
if (NULL == cur->l)
*node = cur->r;
else
{
*node = cur->l;
find_max(cur->l)->r = cur->r;
}
free(cur);
}
#if 0
/**
* @brief 前序遍历
* @details
* 中序和后序改一改输出顺序就行
*
* @param root
*/
static void travel(struct node_st *root)
{
if (NULL == root)
return;
print_s(&root->data);
travel(root->l);
travel(root->r);
}
#endif
/**
* @brief 按层遍历
* @details
* 使用一个队列存储同一层
*
* @param root
*/
static void travel(struct node_st *root)
{
if (NULL == root)
return;
int ret;
QUEUE *qu;
struct node_st *cur;
qu = queue_create(sizeof(struct node_st *));
if (NULL == qu)
return;
queue_en(qu, &root);
/* if error */
while (1)
{
ret = queue_de(qu, &cur);
if (-1 == ret)
break;
print_s(&cur->data);
if (NULL != cur->l)
queue_en(qu, &cur->l);
if (NULL != cur->r)
queue_en(qu, &cur->r);
}
queue_destroy(qu);
}
int main()
{
int arr[] = {1, 2, 3, 7, 6, 5, 9, 8, 4};
int i;
struct score_st tmp, *datap;
for (i = 0; i < sizeof(arr) / sizeof(*arr); i++)
{
tmp.id = arr[i];
snprintf(tmp.name, NAMESIZE, "stu%d", arr[i]);
tmp.math = rand() % 100;
tmp.chinese = rand() % 100;
/* 小往左,大往右 */
insert(&tree, &tmp);
}
draw(tree);
printf("\n\n");
balance(&tree);
draw(tree);
printf("\n\n");
travel(tree);
#if 0
int tmpid = 5;
delete (&tree, tmpid);
draw(tree);
#endif
#if 0
int tmpid = 2;
datap = find(tree, tmpid);
if (NULL == datap)
printf("Can not find the id %d\n", tmpid);
else
print_s(datap);
#endif
exit(0);
}

View File

@@ -0,0 +1,106 @@
#include <stdio.h>
#include <stdlib.h>
#define FNAME "/tmp/out"
struct node_st
{
char data;
struct node_st *l, *r;
};
struct node_st *tree = NULL;
static void draw_(struct node_st *root, int level)
{
if (NULL == root)
return;
draw_(root->r, level + 1);
for (int i = 0; i < level; i++)
printf(" ");
printf("%c\n", root->data);
draw_(root->l, level + 1);
}
/**
* @brief 绘制树
* @details
* 躺下来,根在左,子节点在右。
* 这样子,深度即制表符个数。
*D
* @param root
*/
static void draw(struct node_st *root)
{
draw_(root, 0);
printf("\n\n");
}
static struct node_st *load_(FILE *fp)
{
int c;
struct node_st *root;
c = fgetc(fp);
/* if error */
if ('(' != c)
{
fprintf(stderr, "fgetc():error.\n");
exit(1);
}
c = fgetc(fp);
if (')' == c)
return 0;
root = malloc(sizeof(*root));
if (NULL == root)
exit(1);
root->data = c;
root->l = load_(fp);
root->r = load_(fp);
fgetc(fp);
/* if error */
return root;
}
static struct node_st *load(const char *path)
{
FILE *fp;
struct node_st *root;
fp = fopen(path, "r");
if (NULL == fp)
return NULL;
root = load_(fp);
fclose(fp);
return root;
}
int main()
{
struct node_st *root;
root = load(FNAME);
draw(root);
exit(0);
}

View File

@@ -0,0 +1,138 @@
#include <stdio.h>
#include <stdlib.h>
#define FNAME "/tmp/out"
struct node_st
{
char data;
struct node_st *l, *r;
};
struct node_st *tree = NULL;
/**
* @brief 插入节点
* @details
* 小左大右
*
* !!! 为什么要传入二级指针
* !!! 因为你可能改变的是根节点自身,不仅仅是改变根节点指向的值。
* !!! 要改变一级指针本身,就需要传入二级指针。
*
* @param root
* @param data
* @return int
*/
static int insert(struct node_st **root, int data)
{
struct node_st *node;
if (NULL == *root)
{
node = malloc(sizeof(*node));
if (NULL == node)
return -1;
node->data = data;
node->l = NULL;
node->r = NULL;
*root = node;
return 0;
}
if (data <= (*root)->data)
return insert(&(*root)->l, data);
return insert(&(*root)->r, data);
}
static void draw_(struct node_st *root, int level)
{
if (NULL == root)
return;
draw_(root->r, level + 1);
for (int i = 0; i < level; i++)
printf(" ");
printf("%c\n", root->data);
draw_(root->l, level + 1);
}
/**
* @brief 绘制树
* @details
* 躺下来,根在左,子节点在右。
* 这样子,深度即制表符个数。
*D
* @param root
*/
static void draw(struct node_st *root)
{
draw_(root, 0);
printf("\n\n");
}
static int save_(struct node_st *root, FILE *fp)
{
fputc('(', fp);
/* if error */
if (NULL == root)
{
fputc(')', fp);
return 0;
}
fputc(root->data, fp);
/* if error */
save_(root->l, fp);
save_(root->r, fp);
fputc(')', fp);
/* if error */
}
static int save(struct node_st *root, const char *path)
{
FILE *fp;
fp = fopen(path, "w");
if (NULL == fp)
return -1;
save_(tree, fp);
fputc('\n', fp);
fclose(fp);
return 0;
}
int main()
{
char arr[] = "cefadjbh";
int i;
struct score_st *datap;
for (i = 0; i < sizeof(arr) / sizeof(*arr) - 1; i++)
{
/* 小往左,大往右 */
insert(&tree, arr[i]);
}
draw(tree);
save(tree, FNAME);
exit(0);
}

View File

@@ -46,7 +46,52 @@
**递归**。**递归**转**非递归**。
- 深度:层数
- 度:子树的个数
- 叶子:边缘节点
- 孩子:与父节点对应
- 兄弟:相同父节点
- 堂兄弟:相同爷节点
- 二叉树:
- 满二叉树:深度为`k`且节点为`2^k-1`的二叉树
- 完全二叉树:一颗二叉树,只有倒数两层可以存在不满两个孩子的节点,且单个孩子时只能是左孩子
- 存储:
- 顺序:直观,但是浪费空间
满二叉树:父节点`n`,左孩子`2n`,右孩子`2n+1`
- 链式:灵活,空间利用率高
- 遍历
- 按行
- 先序(根,左,右)
- 中序(左,根,右)
- 后序(左,右,根)
先加中,或者,中加后,都可以逆推出树。先加后不行。
- 平衡:
有很多种条件判定。
这棵树的左右子树个数差值为jj1。
- 广义表
`( root ( left ) ( right) )`,进行嵌套。
-N:M