b树的实现
花了蛮长时间实现的b树插入操作。有时间再实现其他操作。
#include <stdio.h>
#include <stdlib.h>
#define M 5 enum KeyStatus { Duplicate,SearchFailure,Success,InsertIt,LessKeys };
struct node {
int n; /* n < M No. of keys in node will always less than order of B tree */
int keys[M-]; /*array of keys*/
struct node *p[M]; /* (n+1 pointers will be in use) */
}*root=NULL; int pushIn(struct node* currentNode, int key , struct node* toInsertNode){
int i=; for(i=currentNode->n -;i>=-;i--){
if(key < currentNode->keys[i]){
currentNode->keys[i+]=currentNode->keys[i];
currentNode->p[i+]=currentNode->p[i+];
}
else{
currentNode->keys[i+]=key;
currentNode->p[i+]= toInsertNode;
break;
} }
currentNode->n++;
return ;
} int searchNode(struct node* currentNode, int key, int *downNodeIndex){
int i=;
for(i=currentNode->n -;i>=;i--){
if(key== currentNode->keys[i]){
return Success;
}else if (key < currentNode->keys[i]){
if( i> ){
continue;
}else {
*downNodeIndex=;
}
}else{
*downNodeIndex=i+;
break;
}
}
return SearchFailure;
} int splitNode(struct node* currentNode, int toInsertkey, struct node* toInsertNode, int *toLiftKey, struct node** toLiftNode ){
struct node* rightSplittedNode = calloc(sizeof(struct node),);
int i=;
int leftMedian=((M-)/ - );
int rightMedian=(M-)/;
if(toInsertkey > currentNode->keys[rightMedian]){
for(i=M-;i>=rightMedian+;i--){
pushIn(rightSplittedNode,currentNode->keys[i-],currentNode->p[i]);
currentNode->n--;
}
pushIn(rightSplittedNode,toInsertkey, toInsertNode);
*toLiftKey=currentNode->keys[currentNode->n-];
rightSplittedNode->p[]=currentNode->p[currentNode->n];
*toLiftNode=rightSplittedNode;
currentNode->n--;
}
else {
for(i=M-;i>=rightMedian+;i--){
pushIn(rightSplittedNode,currentNode->keys[i-],currentNode->p[i]);
currentNode->n--;
}
pushIn(currentNode,toInsertkey, toInsertNode);
*toLiftKey=currentNode->keys[currentNode->n-];
rightSplittedNode->p[]=currentNode->p[currentNode->n];
*toLiftNode=rightSplittedNode;
currentNode->n--;
} return ; } int pushDown(struct node* currentNode, int toInsertKey, int* toLiftKey, struct node** toLiftNode){ int downNodeIndex;
struct node* toInsertNode;
int rc; if( NULL == currentNode){
*toLiftNode=NULL;
(*toLiftKey)=toInsertKey;
return InsertIt;
} if( Success == searchNode(currentNode,toInsertKey, &downNodeIndex) ){
return Duplicate;
} rc=pushDown(currentNode->p[downNodeIndex], toInsertKey, toLiftKey, toLiftNode); if(InsertIt !=rc){
return rc;
} if( currentNode->n < M- ){
pushIn(currentNode,*toLiftKey,*toLiftNode);
return Success;
}else {
toInsertKey=(*toLiftKey);
toInsertNode=*toLiftNode;
splitNode(currentNode,toInsertKey,toInsertNode,toLiftKey,toLiftNode);
printf("toLiftNode %d\n", toLiftNode);
} return rc; } int insert(struct node * currentNode, int key){
struct node* toLiftNode;
struct node* newRoot;
int toLiftKey;
int rc;
toLiftNode=NULL;
rc=pushDown(currentNode,key, &toLiftKey,&toLiftNode); if(InsertIt == rc ){
newRoot = calloc( sizeof(struct node),);
newRoot->n=;
newRoot->keys[]=toLiftKey;
newRoot->p[]=root;
newRoot->p[]=toLiftNode;
root=newRoot;
rc=Success;
}
return rc;
} void display(struct node *ptr, int blanks , int level)
{
if (ptr)
{
int i;
printf("level:%d nodeAddr:%ld ",level, ptr);
for(i=; i<=blanks; i++)
printf(" ");
for (i=; i < ptr->n; i++)
printf("%d ",ptr->keys[i]);
printf("\n");
for (i=; i <= ptr->n; i++)
display(ptr->p[i], blanks+,level+);
}/*End of if*/
}/*End of display()*/ int main(int argc, char const *argv[]) {
insert(root,*);
insert(root,*);
insert(root,*);
insert(root,*);
display(root,,);
insert(root,);
insert(root,*);
insert(root,*);
insert(root,*);
display(root,,);
insert(root,*);
insert(root,);
insert(root,);
insert(root,);
insert(root,);
insert(root,);
insert(root,);
insert(root,);
insert(root,);
insert(root,);
insert(root,);
insert(root,); display(root,,);
return ;
}
b树的实现的更多相关文章
- B树——算法导论(25)
B树 1. 简介 在之前我们学习了红黑树,今天再学习一种树--B树.它与红黑树有许多类似的地方,比如都是平衡搜索树,但它们在功能和结构上却有较大的差别. 从功能上看,B树是为磁盘或其他存储设备设计的, ...
- ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单
前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...
- 再讲IQueryable<T>,揭开表达式树的神秘面纱
接上篇<先说IEnumerable,我们每天用的foreach你真的懂它吗?> 最近园子里定制自己的orm那是一个风生水起,感觉不整个自己的orm都不好意思继续混博客园了(开个玩笑).那么 ...
- HDU1671——前缀树的一点感触
题目http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目本身不难,一棵前缀树OK,但是前两次提交都没有成功. 第一次Memory Limit Exceed ...
- 算法与数据结构(十一) 平衡二叉树(AVL树)
今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- bzoj3207--Hash+主席树
题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...
- bzoj1901--树状数组套主席树
树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...
- bzoj3932--可持久化线段树
题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...
- jquery-treegrid树状表格的使用(.Net平台)
上一篇介绍了DataTable,这一篇在DT的基础之上再使用jquery的一款插件:treegrid,官网地址:http://maxazan.github.io/jquery-treegrid/ 一. ...
随机推荐
- kk录像机怎么剪辑视频 kk录像机视频剪辑教程
很多朋友录制视频都是用KK录像机,录制视频过后我们需要对视频进行修改和调整,下面小编就教大家怎么来剪辑KK录像机录制的视频 1.首先我们打开软件点[添加一个视频],添加需要剪切的视频 2.将播放指针移 ...
- QT学习之QString的arg方法
在QT的QString中,arg方法类似于C中的printf中使用的格式输出符(只是有点类似). 在QT5的帮助文档中,可以看出以下几点: 使用arg(str1, str2, str3)这种方法进行替 ...
- MYSQL忘记超级用户密码修改
#service mysql stop #mysqld_safe --skip-grant-tables 另外开个SSH连接或终端 [root@localhost ~]# mysql mysql> ...
- 使用selenium启动火狐浏览器,解决Unable to create new remote session问题
今天用火狐浏览器来做自动化,才启动就报错,提示不能创建新的session,不能启动浏览器 问题原因: 火狐driver与火狐浏览器与selenium版本的不兼容 我使用的火狐driver是0.21.0 ...
- vue中动画的封装
<style> .v-enter,.v-leave-to{ opacity: 0; } .v-enter-active,.v-leave-active{ transition:opacit ...
- Java连接mysql中遇到的一些问题及解决方法
1.Java使用mysql-jdbc连接MySQL出现如下警告: Establishing SSL connection without server's identityverification i ...
- C#面向对象的基本概念
“面向对象=对象+类+继承+通信”.如果一个软件系统使用了这样四个概念进行设计和实现,我们就可以认为这个软件系统是面向对象的. 一.一切都是对象 1. 对象概述 对象可以表示几乎所有的实物和概念.比如 ...
- iclr2015
http://www.iclr.cc/doku.php?id=iclr2015:main#accepted_papers iclr2015的accept papers,有些看过,有些没看明白,看来还是 ...
- malloc动态分配字符串数组“ 一个月内的提醒”
//输出一个月提醒 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_R ...
- 使用免费公开的api接口示例(iOS)
做项目难免需要测试,要测试就需要一些接口,现在网上的很多接口都是需要收费的. 以下是目前找到的免费 JSON API免费接口 云聚数据 网吧数据 其中选取了一个百度百科的接口 百度接口 百度百科接口: ...