B树和B+树(二):例程

B树和B+树的代码类似,只介绍B+树的生成代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUM 6
#define NODE  0
#define LEAF  1

struct node {
	int type;
	struct node *parent;
	int ppos;

	int n;
	int key[NUM];
	struct node *child[NUM];
};

struct leaf: public node {
	struct leaf *prev;
	struct leaf *next;
};


struct bxtree_rec {
	struct node *root;
	struct leaf *head;
};

插入函数:

int insert(struct bxtree_rec *bxt, int key, void *data, int size)
{
	struct node *p;
	struct leaf *l2;
	struct node *l2data;
	struct node *newroot;
	struct node *save=NULL;

	void *rec=0;
	int i, j;
	int gob=0;
	int dataflag=1;

	p = bxt->root;
	if (key < p->key[0]){
		int k;
		k = p->key[0];
		p->key[0]= key;
		while( p->type != LEAF) {
			p= p->child[0];
			p->key[0]= key;
		}
		rec = malloc(size);		
		memcpy(rec, data, size);
		key = k;
		data = (void *)p->child[0];
		p->child[0] = (struct node*)rec;
		rec = data;
		i = 1;
		goto insb;
	}
loc:
	for(i=0; i<p->n; i++) {
		if (p->key[i] == key) return 0;
		if (p->key[i] > key) break;
	}
	if (p->type !=LEAF) {
		p = p->child[i-1];
		goto loc;
	}
insb:
	if (p->n <NUM) {
		for(j= p->n; j>i; j--){
			p->key[j] = p->key[j-1];
			p->child[j] = p->child[j-1];
			if (p->type == NODE) {
				++p->child[j]->ppos;
			}
		}
		p->key[i] = key;
		if (dataflag) {
			if (!rec) {
				rec = malloc(size);		
				memcpy(rec, data, size);
			}
			p->child[i] = (struct node*)rec;
			dataflag=0;
		}
		else {
			p->child[i] = l2data;
			l2data->parent =p;
			l2data->ppos =i;
		}
		p->n++;
		if (!gob) return 1; 
		else { gob=0; goto cont;}
	}	
	
	if (dataflag) {
		l2 = (struct leaf *)malloc(sizeof(struct leaf));
		l2->type = LEAF;
		l2->parent = p->parent;
		l2->ppos = p->ppos+1;
		l2->prev = (struct leaf*)p;
		l2->next = ((struct leaf*)p)->next;
		((struct leaf*)p)->next->prev = l2;
		((struct leaf*)p)->next = l2;
	}
	else {
		l2 = (struct leaf*) (struct node *)malloc(sizeof(struct node));
		l2->type = NODE;
		l2->parent = p->parent;
		l2->ppos = p->ppos+1;
	}

	for(j=0; j<NUM/2; j++) {
		l2->key[j] = p->key[j+NUM/2];
		l2->child[j] = p->child[j+NUM/2];
		if (l2->type == NODE) {
			l2->child[j]->parent = l2;
			l2->child[j]->ppos -= NUM/2;
		}
	}
	p->n = NUM/2;
	l2->n = NUM/2;
	save = p;

	if (i>NUM/2) { 
		i -= NUM/2;
		p = l2;
	}
	gob = 1;
	goto insb;
cont:
	key = l2->key[0];
	i = l2->ppos;
	if (l2->parent !=NULL) {
		p = l2->parent;
		l2data = l2;
		gob=0;
		goto insb;
	}
	if (save) p= save;

	newroot = (struct node *)malloc(sizeof(struct node));
	newroot->type = NODE;
	newroot->parent = NULL;
	newroot->n = 2;
	newroot->key[0]= p->key[0];
	newroot->key[1] = l2->key[0];
	newroot->child[0] = p;
	newroot->child[1] = l2;
	p->parent = newroot;
	p->ppos=0;
	l2->parent = newroot;
	l2->ppos=1;
	bxt->root = newroot;
	return 1;
}

插入不允许重复键,函数返回1代表成功,0失败。这里稍微提示一下,gob这个变量的用法。因为插入遇到节点满的情况,产生分裂。分裂后的新节点暂时搁置,可以看作把它放在只有一个元素的栈上。gob代表栈顶。因为只有一个元素,入栈出栈的动作全部简化掉了。分裂后继续原来的插入操作,然后从栈上拿回新节点,再用同样的插入操作把它加到B+树里。

下面是测试程序:

struct data {
	int key;
	char info[20];
};

int keys[] = { 
	35, 36, 37, 38, 39, 40,
	8,9,10, 11,12, 13, 14,
	1, 2,3,4, 5, 6, 7,
	21, 22, 23, 24, 25, 26,27,
	15, 16, 17,18, 19, 20,
	28, 29, 30, 31, 32, 33, 34,
};

int level(struct node *p)
{
	int i=0;
	while (p){
		++i;
		p= p->parent;
	}
	return i-1;
}

void printbxt(struct bxtree_rec *bxt)
{
	struct node *p;
	int i;
	int curlevel;
	struct node *queue[200];
	int first, last;

	first=last=0;
	curlevel=0;


	p = bxt->root;
	if(p) {
		printf("%d: ", curlevel);
	print_node:
		printf("(%d",p->key[0]);
		if(p->type==NODE) queue[last++] = p->child[0];
		for(i=1;i<p->n; i++) {
			printf(" %d", p->key[i]);
			if(p->type==NODE) queue[last++] = p->child[i];
		}
		printf(")");
	}

	while(first<last) {
		p = queue[first++];
		if (level(p)>curlevel) {
			printf("\n");
			curlevel++;
			printf("%d: ", curlevel);
		}
		goto print_node;
	}
	printf("\n");	
}

int main()
{
	struct data  d;
	struct data  *pd;
	struct bxtree_rec r;
	int i;

	d.key =keys[0];
	sprintf(d.info, "val:%d", d.key);

	r.head = (struct leaf*) malloc( sizeof(struct leaf) );
	r.head->type = LEAF;
	r.head->parent = NULL;
	pd = (struct data *)malloc(sizeof(struct data));
	memcpy(pd, &d, sizeof(struct data));
	r.head->key[0] = d.key;
	r.head->child[0] = (node*) pd;
	r.head->n = 1;
	r.head->prev = r.head->next= r.head;
	r.root = r.head;

	printbxt(&r);
	for( i=1; i<40; i++) {
		d.key = keys[i];
		sprintf(d.info, "val:%d", d.key);
		insert(	&r, d.key, &d, sizeof(d));
		printbxt(&r);
	}
	printbxt(&r);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值