花了蛮长时间实现的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. Altium_Designer-各种布线总结

    1.常规布线:不详细说了,是个人就知道怎么弄.需要说明的是在布线过程中,可按小键盘的*键或大键盘的数字2键添加一个过孔:按L键可以切换布线层:按数字3可设定最小线宽.典型线宽.最大线宽的值进行切换. ...

  2. 一款带有CSS的单选框以及选中事件

    html <div class="radio radio-success"> <input type=" name="radioSingle1 ...

  3. 【转】Android 组件系列-----Activity保存状态

    本篇随笔将详细的讲解Activity保存状态的概念,也就是saving activity state. 一.Activity状态保持概念 保存Activity的状态是非常重要的,例如我们在玩一个游戏的 ...

  4. javascript中parseInt(),08,09,返回0

    javascript中在使用parseInt(08).parseInt(09),进行整数转换的时候,返回值是0 工具/原料   浏览器 文本编辑器 方法/步骤     javascript中在使用pa ...

  5. CentOS下用rinetd做端口转发

    windows下的端口转发一般用的是自带的nat和porttunnel.portmap linux下端口转发映射的程序叫rinetd,启动方法rinetd -c /etc/rinetd.conf  , ...

  6. HTTP Method 详细解读(`GET` `HEAD` `POST` `OPTIONS` `PUT` `DELETE` `TRACE` `CONNECT`)--转

    前言 HTTP Method的历史: HTTP 0.9 这个版本只有GET方法 HTTP 1.0 这个版本有GET HEAD POST这三个方法 HTTP 1.1 这个版本是当前版本,包含GET HE ...

  7. 在 publicId 和 systemId 之间需要有空格。

    spring  applicationContext_datasource.xml中约束错误 org.springframework.beans.factory.xml.XmlBeanDefiniti ...

  8. linux下 利用 rz 命令上传文件

    1. 如何安装? 1)编译安装  root 账号登陆后,依次执行以下命令: # cd /tmp # wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20 ...

  9. docker-compose 构建mongodb并导入基础数据示例

    使用docker-compose构建mongodb服务并导入基础数据示例. 1.文件目录结构 ——mongo/ |——docker-compose.yml |——mongo-Dockerfile |— ...

  10. MySQL 5.7基于GTID的主从复制环境搭建(一主一从)

      Preface       As wel all know,replication base on GTID with row format of binary log is comprehens ...