B树

摘自:《算法:C语言实现 (第1~4部分)


//B_tree.h
typedef char Item;
typedef Item Key;
#define  key(A)  (A)
#define  M  6
#define  NULLitem (-1)
typedef struct STnode *link;

void STinit();
Item STsearch(Key v);
void STinsert(Item item);

//B_tree.cpp
#include <iostream>
#include <malloc.h>
#include "B_tree.h"
using namespace std;


typedef struct 
{
	Key key; 
	union {link next; Item item;} ref;
} entry;
struct STnode
{
	entry b[M];
	int m;
};

static link head;
static int H, N;

link NEW()
{
	link x = (link)malloc(sizeof *x);
	x->m = 0;
	return x;
}

void STinit()
{
	head = NEW(); H = 0; N = 0;
}


//搜索
Item searchR(link h, Key v, int H)
{
	int j;
	if(H == 0)
		for (j = 0; j < h->m; j++)
			if(v == h->b[j].key)
				return h->b[j].ref.item;
	if(H != 0)
		for (j = 0; j < h->m; j++)
			if((j+1 == h->m) || (v < h->b[j+1].key))
				return searchR(h->b[j].ref.next, v, H-1);
	return NULLitem;
}
Item STsearch(Key v)
{
	return searchR(head, v, H);
}


/************************************************************************/
/* M为偶数,树中的每个节点至多只有M-1个数据项,
   对一个节点分裂之前可以插入第M个数据项                                                                     */
/************************************************************************/
link split(link h)                 
{
	int j;
	link t = NEW();
	for(j = 0; j < M/2; j++)
		t->b[j] = h->b[j + M/2];
	t->m = M/2; 
	h->m = M/2;
	return t;
}


//插入操作
link insertR(link h, Item item, int H)
{
	int i, j; 
	Key v = key(item);
	entry x; 
	link t, u;
	x.key = v;
	x.ref.item = item;

	if (H == 0)
		for(j = 0; j < h->m; j++)
			if(v < h->b[j].key)
				break;
	
	if(H != 0)
		for(j = 0; j < h->m; j++)
			if((j+1 == h->m) || (v < h->b[j+1].key))
			{
				t = h->b[j++].ref.next;
				u = insertR(t, item, H-1);
				if(u == NULL)
					return NULL;
				x.key = u->b[0].key;
				x.ref.next = u;
				break;
			}

	for(i = (h->m)++; i > j; i--)
		h->b[i] = h->b[i-1];
	h->b[j] = x;
	
	if(h->m < M)         //数据项到M就会分裂,即最多保持M-1个,
		return NULL;	 //但第M个可以插入
	else
		return split(h);
}

void STinsert(Item item)
{
	link t, u = insertR(head, item, H);
	if(u == NULL) return;
	t = NEW();                //分裂根节点
	t->m = 2;
	t->b[0].key = head->b[0].key;
	t->b[0].ref.next = head;
	t->b[1].key = u->b[0].key;
	t->b[1].ref.next = u;
	head = t;
	H++;
}

//main.cpp 测试主函数

#include <iostream>
#include "B_tree.h"
using namespace std;

int main(void)
{
	STinit();
	char  c;
	STinit();
	while ((c = getchar()) != EOF)
	{
		if(c == ' ' || c == '\t' || c == '\n')
			continue;

		STinsert(c);
	}

	while ((c = getchar()) != EOF)
	{
		if(c == ' ' || c == '\t' || c == '\n')
			continue;

		if(c == STsearch(c))
			cout << "found " << c << endl;
		else
			cout << "not found " << c << endl;
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
B树是一种自平衡的搜索树,通常用于在数据库系统中进行索引操作。在Matlab中实现B树时,我们可以使用多叉树的数据结构进行实现。 首先,我们需要定义B树的节点结构。每个节点包含一个数组,用于存储关键字和指向子节点的指针。同时,还需要定义B树的阶数,即每个节点最多可以存储的关键字个数。 接下来,我们可以实现B树的插入操作。插入操作首先需要找到合适的叶节点,然后插入关键字到该节点的数组中,并且保持数组的有序性。如果插入后关键字个数超过了阶数,需要进行节点分裂操作,将一半的关键字移到新建的节点中,并更新父节点的指针。 类似地,我们可以实现B树的删除操作。删除操作首先需要找到要删除的关键字所在的节点,然后进行删除操作。如果删除后关键字个数低于阶数的一半,需要进行节点合并操作,合并相邻的兄弟节点,并更新父节点的指针。 此外,我们还可以实现B树的搜索操作。搜索操作从根节点开始,递归地进行比较,根据关键字在节点数组中的位置选择下一个节点并继续搜索,直到找到关键字或者搜索到叶节点。 总结起来,我们可以通过定义节点结构和实现插入、删除和搜索操作来实现B树。在Matlab中,可以使用多叉树的数据结构来表示B树,并使用递归算法来实现相关操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值