/* B 树结构体 */
typedef struct BTNode {
int keyNum; // 结点中关键字个数,即结点的大小
KeyType key[M + 1]; // 关键字数组,0 号单元未用
Record rcd[M + 1]; // 记录指针数组,0 号单元未用
struct BTNode* parent; // 指向双亲结点
struct BTNode* child[M + 1];// 子树指针数组,0 号有使用
} BTNode, *BTree;
/* 结果类型 */
typedef struct Result {
int i; // 在结点中的关键字位序
int tag; // 是否找到了
BTree data; // 找到的数据
} Result;
然后是所有的接口
/* 通用辅助接口 */
Record getRecord(int key, int data); // 获得一个记录值
Result getResult(int i, int tag, BTree data); // 获得一个结果值
/* B 树接口 */
Status InitBTree(BTree& tree, Record data); // 初始化 B 树
BTree MakeBTree(const int treeData[][2], int num); // 构建 B 树
void TraverseBTree(BTree tree); // 打印 B 树
Result SearchBTree(BTree tree, KeyType key); // 对 B 树执行查找操作
Status InsertBTree(BTree& tree, Record data); // 对 B 树执行插入操作
Status DeleteBTree(BTree tree, KeyType key); // 对 B 树执行删除操作
Status UpdateBTree(BTree tree, KeyType key, Record data); // 将 B 树中关键字为 key 的结点
记录更换成新的记录
/* B 树辅助接口 */
int Search(BTree node, KeyType key); // 寻找 key 在 node 所在结点中的位置
void InsertBTNode(BTree& node, int i, Record rcd, BTree newNode); // 将新记 录 rcd 插入到
结点 node 的第 i 个位置,同时将新子节点 newNode 作为后继孩子
Status SplitBTNode(BTree& node, BTree& newNode); // 将 node 结点从中间分裂成两部分,
前半部分留在原位,后半部分进入 newNode 并指向原结点的双亲
Status newRoot(BTree& root, Record rcd, BTree child1, BTree child2); // 生成一个新根
int CountKeyNum(BTree tree); // 计算出整棵树上记录条数的总和
void Successor(BTree& node, int& i); // 找到前驱结点,并进行关键字的替换
Status InsertRecord(BTree& node, int i, Record rcd); // 将 rcd 插入到指定结点的第 i 个位
置
Status RemoveRecord(BTree& node, int i); // 将指定结点中第 i 个记录移除
void RestoreBTree(BTree& node, int pi); // 针对某个结点调整一颗 B 树
评论