C语言 2-3树

2-3树简介

2-3树是最简单的B-树(或-树)结构,其每个非叶节点都有两个或三个子女,而且所有叶都在统一层上。

2-3树的模拟过程可以看链接:InsertSortion

实现过程

在2-3树中,一个结点只有一个值的我们称为2-结点,有两个值的称为3-结点,在2-3树中,一个结点最多只有两个值,即3-结点
这里写图片描述

什么时候我们认为2-3树不平衡呢?就是任意一个结点到最后每个结点的距离都相等。
当结点变成4-结点的时候,总共会有6种情况:
这里写图片描述
所以针对每一种情况,我们一一实现就可以实现2-3树了。

代码实现

ADT :

typedef struct node {
    int a[3];  //a[2],作为4-结点时的缓存

    int num; //数组a的长度:1,2,3

    struct node *left_child;
    struct node *mid_child;
    struct node *right_child;
    struct node *tmp_child;  //作为4-结点时的缓存

    struct node *parent;
} Btree, *BtreePtr;

void exchange(int *a, int *b);
BtreePtr _node(const int key);  //create a node
void _sort(BtreePtr b); //sort a[]
BtreePtr _checkNum(BtreePtr p);  //balance
BtreePtr _insertBTree(BtreePtr b, const int key, const int pos);  //insert key
BtreePtr insertBTree(BtreePtr root, const int key, const int pos);  //initial
void freeTree(BtreePtr p);
int BTreeSearch(const int *a, const int length, const int key) ;

完整代码:

#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#define NUM(p)  ((p==NULL)?0:p->num)

typedef struct node {
    int a[3<
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值