花了蛮长时间实现的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树的实现的更多相关文章

  1. B树——算法导论(25)

    B树 1. 简介 在之前我们学习了红黑树,今天再学习一种树--B树.它与红黑树有许多类似的地方,比如都是平衡搜索树,但它们在功能和结构上却有较大的差别. 从功能上看,B树是为磁盘或其他存储设备设计的, ...

  2. ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单

    前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...

  3. 再讲IQueryable<T>,揭开表达式树的神秘面纱

    接上篇<先说IEnumerable,我们每天用的foreach你真的懂它吗?> 最近园子里定制自己的orm那是一个风生水起,感觉不整个自己的orm都不好意思继续混博客园了(开个玩笑).那么 ...

  4. HDU1671——前缀树的一点感触

    题目http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目本身不难,一棵前缀树OK,但是前两次提交都没有成功. 第一次Memory Limit Exceed ...

  5. 算法与数据结构(十一) 平衡二叉树(AVL树)

    今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...

  6. [C#] C# 知识回顾 - 表达式树 Expression Trees

    C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...

  7. bzoj3207--Hash+主席树

    题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...

  8. bzoj1901--树状数组套主席树

    树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...

  9. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  10. jquery-treegrid树状表格的使用(.Net平台)

    上一篇介绍了DataTable,这一篇在DT的基础之上再使用jquery的一款插件:treegrid,官网地址:http://maxazan.github.io/jquery-treegrid/ 一. ...

随机推荐

  1. php程序员发展建议

    2017PHP程序员的进阶之路 又是一年毕业季,可能会有好多毕业生即将进入开发这个圈子,踏上码农这个不归路.根据这些年在开发圈子总结的LNMP程序猿发展轨迹,结合个人经验体会,总结出很多程序员对未来的 ...

  2. 用iSee图片专家制作淘宝店标教程

    普通的淘宝店铺都会有店标.店标都显示在店铺首页的显现位置,买家在逛淘宝店的时候,一眼都会瞄到店标.因此,如果可以制作一个专属于自己店铺的店标,可以吸引买家的眼光,也更好地宣传了店铺. 下面就用iSee ...

  3. mybatis怎样自动生成java类,配置文件?

    其实没有什么东西是可以自动生成的,只不过是别人已经写好了,你调用罢了. 所以想要mybatis自动生成java类,配置文件等,就必须要一些配置和一些jar包.当然这些配置也很简单. 为了有个初步的认识 ...

  4. HDU 1532

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题意: 三叶草是这个人的最喜欢的植物,结果下雨淹没了他家里,要排水,一个点到一个点的排水速度已知 ...

  5. 【[ZJOI2015]诸神眷顾的幻想乡】

    题目 听说这是广义\(SAM\)的板子 看来对于广义\(SAM\)我也就只会板子了 叶子数很少,所以可以枚举每一个叶子节点作为根建一遍\(Trie\)树 只需要对\(Trie\)树建出\(SAM\)就 ...

  6. jQuery 效率提升建议

    jQuery简洁通用的方法集把编码者从繁重的工作中解脱出来,也拉低了进入javascript的门槛,初学者对浏览器兼容性一无所知的情况下,几行代码就可以写出超炫的特效.网上有一篇文章转载比较泛滥,已经 ...

  7. CentOS 5 - 安装PHP MongoDB扩展

    For driver developers and people interested in the latest bugfixes, you can compile the driver from ...

  8. Spring boot 集成三种拦截方式

    三种拦截方式分别为: javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.aspectj.lang. ...

  9. numpy中生成随机矩阵并打印出矩阵的shape

    from numpy import * c=zeros((4,5)) print c.shape print numpy.random.random((2,3))

  10. Windows平台下源码分析工具

    最近这段时间在阅读 RTKLIB的源代码,目前是将 pntpos.c文件的部分看完了,准备写一份文档记录下这些代码的用处.处理过程.理论公式来源.注意事项,自己还没有弄明白的地方.目前的想法是把每一个 ...