[数据结构]B树的基本定义和操作

B树

B树的定义

  • 多叉搜索树,每个节点最多有M个子树,M-1个关键字
  • 根节点若非唯一节点,则最少有2个子树,即最少有1个关键字
  • 非根节点也非叶子节点的,最少有ceil(M/2)个子树,ceil(M/2)-1个关键字
  • 叶子节点都在树的同一层
  • 每个节点的关键字从小到大排列,充当划分子树的边界
c 复制代码
// B树的结构体如下:
typedef struct _btree_node {
	int *keys;
	struct _btree_node **childrens;
	int num;  // 有多少个关键字

	int leaf; // 是否是叶子节点
} btree_node;

typedef struct _btree {
	struct _btree_node *root;
	int t;  // ceil(M/2)-1,方便B树的分裂和合并操作
} btree;

B树的节点生成和销毁

c 复制代码
btree_node *btree_node_create(int t, int leaf)
{
	btree_node *node = calloc(1, sizeof(btree_node ));
	node->keys= calloc(2*t-1, sizeof(int));
	node->childrens = calloc(2*t, sizeof(btree_node *));
	node->num = 0;
	node->leaf = leaf;
	return node;
}

void btree_node_destroy(btree_node *node)
{
	free(node->keys);
	free(node->childrens);
	free(node);
}

B树的分裂

c 复制代码
void btree_split_child(btree *T, btree_node *x, int idx) {
	btree_node *y = x->childrens[idx];
	int t = T->t;
	btree_node *z = btree_node_create(t, y->leaf);

	for (int i = 0; i < t - 1; i++) {
		z->keys[i] = y->keys[i+t];
	}
	if (y->leaf == 0) {
		for (int i = 0; i < t; i++) {
			z->childrens[i] = y->chiledrens[i+t];
		}
	}
	z->num = t - 1;
	y->num = t - 1;

	for (int i = x->num; i > idx; i--) {
		x->keys[i] = x->keys[i-1];
		x->childrens[i+1] = x->childrens[i];
	}
	x->keys[idx] = y->keys[t-1];
	x->childrens[idx+1] = z;
	x->num += 1;
}

B树的合并

c 复制代码
void btree_merge(btree *T, btree_node *x, int idx) {
	btree_node *y = x->childrens[idx];
	btree_node *z = x->childrens[idx+1];

	int t = T->t;
	for (int i = 0; i < t - 1; i++) {
		y->keys[i+t] = z->keys[i];
	}
	y->keys[t - 1] = x->keys[idx];

	if (z->leaf == 0) {
		for (int i = 0; i < t; i++) {
			y->childrens[i+t] = z->childrens[i];
		}
	}
	y->num += t;
	btree_node_destroy(z);

	for (int i = idx + 1; i < x->num; i++) {
		x->keys[i-1] = x->keys[i];
		x->childrens[i] = x->childrens[i+1];
	}
	x->num -= 1;
}
相关推荐
Lucky_ldy7 分钟前
数据结构从入门到精通:链表的分类
数据结构·链表
微风欲寻竹影17 分钟前
Java数据结构——二叉树相关OJ题目详解
java·数据结构
微风欲寻竹影18 分钟前
Java数据结构——二叉树(Binary Tree)详解
java·数据结构·算法
禅思院34 分钟前
大列表性能优化 · 工程实战·四
开发语言·前端·性能优化·前端框架·php·异步加载
悠仁さん38 分钟前
数据结构 排序
数据结构·算法·排序算法
代码中介商1 小时前
数据结构进阶(五):最短路径——Dijkstra 与 Floyd 算法
数据结构·算法
微扬嘴角11 小时前
React篇1--JSX语法规则、组件、组件实例的3大特性
前端·react.js·前端框架
Lsk_Smion12 小时前
力扣实训 _ [75].颜色分类 _ 杨辉三角
数据结构·算法·leetcode
jidaowansui12 小时前
P11375 [GESP202412 六级] 树上游走
数据结构·算法
一切皆是因缘际会15 小时前
AI智能新时代
数据结构·人工智能·ai·架构