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/ 一. ...
随机推荐
- 基于FPGA的VGA显示设计(二)
上一篇:基于FPGA的VGA显示设计(一) 参照 CrazyBingo 的 基于FPGA的VGA可移植模块终极设计代码 的工程代码风格,模块化处理了上一篇的代码,并增加了一点其它图形. 顶层 ...
- 随便讲讲自己了解的ajax在JQ中的应用
首先jQuery 库拥有完整的 Ajax 兼容套件.其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据. 定义和用法 ajax() 方法通过 HTTP 请求加载远程数据. 该方法是 jQu ...
- 使用命令创建jenkins的job,解决jenkinsapi.custom_exceptions.JenkinsAPIException错误
如果你使用 Python 2.7.12,Jenkins版本为Jenkins ver. 2.22,你使用我上面一种提到的修改的以下代码可以进行Jenkins的job复制 http://www.cnblo ...
- 2019.03.15 ZJOI2019模拟赛 解题报告
得分: \(20+45+15=80\)(三题暴力全写挂...) \(T1\):Lyk Love painting 首先,不难想到二分答案然后\(DP\)验证. 设当前需验证的答案为\(x\),则一个暴 ...
- 1.4 配置备份策略(Policy)
1.1 配置备份策略(Policy) 一个备份策略由四部分组成. Attributes(属性) Policy是否Active Policy类型 由此Policy产生的任务的优先级 使用的Storage ...
- 【转】Activity生命周期详解
三个循环 提供两个关于Activity的生命周期模型图示帮助理解: 图1 图2 从图2所示的Activity生命周期 ...
- 的NodeJS异步数据库函数需要同步的答案 +
我是新来的NodeJS和我写,需要从我去过的所有的函数应该是在这种情况下,读QUERY我的MySQL数据库,并返回代码,我非常希望服务器能够对其他事件作出回应而这个地方是轨迹查询请求.然而,它并不特别 ...
- 探索性数据分析EDA综述
目录 1. 数据探索的步骤和准备 2. 缺失值处理 为什么需要处理缺失值 Why data has missing values? 缺失值处理的技术 3. 异常值检测和处理 What is an ou ...
- office2010
MS office2010 360网盘:http://yunpan.cn/QajXaRWbnpTzF (提取码:cf72) 如何激活参见我下面的博客: http://www.cnblogs.com/l ...
- Linux笔记(开机自动将kerne log保存到SD卡中)
有时候为了测试机器的稳定性,需要煲机测试几天的情况,这个时候机器已经封装好,不能再接串口线出来. 为了追溯问题,就需要将log信息保存下来. 于是就需要这样一个功能:系统启动后,自动将kernel的l ...