花了蛮长时间实现的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. Python基础学习之集合

    集合是一个无序.不重复的数据集合,它的主要作用如下: 去重:把一个列表变成集合,就可以去除重复的内容 关系测试:测试两组数据之间的交集.并集.差集等关系 集合常用的操作: #创建一个数值集合 s=se ...

  2. 关于ASP.NET页面事件的知识点

    ASP是动态服务器页面(ActiveServerPage)的英文缩写,是微软公司开发的代替CGI脚本程序的一种应用,它可以与数据库和其它程序进行交互,是一种简单.方便的编程工具.那么关于ASP.NET ...

  3. navicat for mysql注册码:NAVN-LNXG-XHHX-5NOO

    名.组织可以为空或任意填写. 摘自: navicat for mysql10.0.0.0注册码中“名”.“组织”...._百度知道

  4. Selenium入门12 鼠标和键盘事件

    1 鼠标 集成在webdriver.ActionChains.单击.双击.右击.拖放等等.   2 键盘 引入包from selenium.webdriver.common.keys import K ...

  5. UESTC 761 LoveZx与期末考试

    被卡的一道题,其他情况都想出来了,主要是没想好A[i] == B[j]时候的处理,取最后面最大的可能不是最优解,相等的时候我暴力比较后缀的(为此还要维护一个链),这个操作是O(len) 所以T了.(也 ...

  6. RHEL/CentOS 6.x使用EPEL6与remi的yum源安装MySQL 5.5.x

    PS:如果既想获得 RHEL 的高质量.高性能.高可靠性,又需要方便易用(关键是免费)的软件包更新功能,那么 FedoraProject 推出的 EPEL(Extra Packages for Ent ...

  7. 2017.11.8 面向对象分析与设计(UML)---UML的作用及分类

    用到的工具 startUML 一些界面操作的说明 蓝色框是用来选择形状的,特别是接口的时候 UML有什么用? `` 有很多人认为,UML的主要用途就是软件设计!也有人认为,如果你不是开发人员,是难以理 ...

  8. 第3章 如何用DAP仿真器下载程序—零死角玩转STM32-F429系列

    第3章     如何用DAP仿真器下载程序 集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/firege ...

  9. pycharm中常用设置

    当安装时检查版本过低 首先 pip --help 进入帮助,找到 复制,然后 pip install --disable-pip-version-check 要安装的包 这样就会跳过版本检测. 在py ...

  10. BZOJ1965: [Ahoi2005]SHUFFLE 洗牌(exgcd 找规律)

    Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 989  Solved: 660[Submit][Status][Discuss] Description ...