手机版
你好,游客 登录 注册
背景:
阅读新闻

B-Tree的C++实现

[日期:2012-11-20] 来源:Linux社区  作者:yanlinwang [字体:大 中 小]

/*
B树,简要说明下B树的性质。M表示B树的阶数,L表示叶子节点的最大元素个


(性质说明来自于《数据结构与问题求解(C++版)》第19章)
1、数据项保存在叶子中
2、非叶子节点具有M-1个键指导查找的进行;键i代表子树i+1中最小的键
3、根要么是叶子,要么就有2~M个孩子
4、所有非叶子节点,(根除外)都具有[M/2]~M个孩子
5、所有叶子都在同一深度,并且对某一叶子,具有[L/2]~L个数据项
*/
#include"btree.h"
#include<vector>
#include<ctime>
using std::ctime;
using std::vector;


/*
接口功能函数的实现
*/
BTree::BTree()
{
is_leaf=true;
elem_size=0;
parent=NULL;
root=NULL;
child_size=0;
}
BTree::~BTree()
{
if(!is_leaf)
{
for(int i=0;i<child_size;i++)
delete child_list[i];
delete root;
}
}
BTree* BTree::getRoot(){ return root;}
void BTree::insert(int* array,int num)
{
for(int i=0;i<num;i++)
insert(*(array+i));
}
void BTree::insert(int data)
{
/*
如果树为空,或者树的根节点为叶子节点,就直接把这个元素插入到元素列表中
这个时候,要考虑到可能会引起分裂
*/
if(!root) //root为空
{
root=new BTree;
root->is_leaf=true;
root->parent=NULL;
root->elem_data[root->elem_size]=data;
root->elem_size=1;
}
else
{
/*首先找到要插入的叶子节点*/
BTree* p=root;
while(!(p->is_leaf))
{
int i=0;
for(;i<p->child_size-1;i++)
{
if(data<p->index_key[i])
break;
}
p=p->child_list[i];
}
/*检查此叶子节点是否已满*/
if(p->is_leaf)
{
/*如果元素已满,就需要将叶子分裂。否则就直接插入元素即可*/
if(p->elem_size==L)
{
/*用一个临时数组将L+1个数都有序保存起来*/
int tempArray[L+1];
for(int i=0;i<L;i++)
tempArray[i]=p->elem_data[i];
array_insert(tempArray,data,L);
/*
cout<<"[test] ";
for(int i=0;i<=L;i++)
cout<<tempArray[i]<<" ";
cout<<"[test ends]"<<" ";
*/
int num=(L+1)/2;
p->elem_size=num;
for(int i=0;i<num;i++){
p->elem_data[i]=tempArray[i];
//cout<<tempArray[i]<<" ";
}
BTree* newLeaf=new BTree;
newLeaf->is_leaf=true;
newLeaf->elem_size=L+1-num;
for(int i=0;i<newLeaf->elem_size;i++){
newLeaf->elem_data[i]=tempArray[num+i];
//cout<<tempArray[num+i]<<" ";
}
//cout<<endl;
/*
在调用node_break进行分裂的时候
并没有改变第一个参数代表的节点与其父节点的关系
*/
node_break(*p,*newLeaf);
}
else
{
array_insert(p->elem_data,data,p->elem_size);
p->elem_size=p->elem_size+1;
}
}
else //find p, but not a leaf
{
cout<<"something is wrong..."<<endl;
}
}
}
/*
* 辅助功能函数的实现
* 使用插入排序算法思想来实现插入数据
* 这里不考虑数组的溢出问题,这个由调用者保证
*/
void BTree::array_insert(int* array,int data,int size)
{
int index=0;
for(;index<size;index++)
if(data<*(array+index))
break;
for(int i=size;i>index;i--)
array[i]=array[i-1];
array[index]=data;


}
/*
第一个参数表示旧节点被分裂之后的第一个节点(含有旧节点的父节点信息)
第二个参数表示分裂之后新增加的节点(无旧节点的父节点信息)
*/
void BTree::node_break(BTree& lNode, BTree& rNode)
{
if(lNode.parent) //如果原节点的父节点不为空
{
BTree* parent=lNode.parent;
/*判断子节点的分裂是否会引起父节点的分裂*/
if((parent->child_size)==M) //子节点的分裂引起了父节点的分裂
{
int insert_index=0; //找出插入的点
for(;insert_index<M;insert_index++)
if(parent->child_list[insert_index]==&lNode)
break;


//将所有要分配的子树用一个统一的数组保存起来
BTree* temp_child_list[M+1];
for(int i=0;i<=insert_index;i++)
temp_child_list[i]=parent->child_list[i];
temp_child_list[insert_index+1]=&rNode;
for(int i=insert_index+2;i<M+1;i++)
temp_child_list[i]=parent->child_list[i-1];
/*
cout<<"[test] "
for(int i=0;i<M+1;i++)
cout<<get_min_value(*(temp_child_list[i]))<<" ";
cout<<"[test...end]"<<endl;
/*
现在开始进行子树分裂,将重新分配这些指向子树的指针
同时要改变这些子树的父节点指针
*/
int num=(M+1)/2;
for(int i=0;i<num;i++)
{
parent->child_list[i]=temp_child_list[i];
parent->child_list[i]->parent=parent;
}
parent->child_size=num;
//创建新的父节点
BTree* new_parent=new BTree;
new_parent->is_leaf=false;
for(int i=0;i<M+1-num;i++)
{
new_parent->child_list[i]=temp_child_list[num+i];
new_parent->child_list[i]->parent=new_parent;
}
new_parent->child_size=M+1-num;
/*
到此,全部的子树已经分割完毕
剩下的就是为两个父节点设置索引值
*/
for(int i=0;i<parent->child_size-1;i++)
parent->index_key[i]= \
get_min_value(*(parent->child_list[i+1]));
for(int i=0;i<new_parent->child_size-1;i++)
new_parent->index_key[i]= \
get_min_value(*(new_parent->child_list[i+1]));
/*
然后继续调整,解决父节点的分裂对更高层节点造成的影响
*/
node_break(*parent,*new_parent);


}
else //子节点的分裂没有引起父节点的分裂
{
/*
找到插入点之后,直接把后面的部分往后移一位
然后把新加入的子树加入进去
*/
rNode.parent=parent;
int index=0;
for(;index<parent->child_size;index++)
if((parent->child_list)[index]==&lNode)
break;
parent->child_size=parent->child_size+1;
for(int i=parent->child_size-1;i>index+1;i--)
{
parent->child_list[i]=parent->child_list[i-1];
}
parent->child_list[index+1]=&rNode;
//下面更新父节点的索引数组
/*其实不必把所有的索引值重新计算一遍
只需要计算加入的新节点的索引值即可
for(int i=parent->child_size-1;i>0;i--)
parent->index_key[i-1]= \
get_min_value(*(parent->child_list[i]));*/
for(int i=parent->child_size-2;i>index;i--)
parent->index_key[i]=parent->index_key[i-1];
parent->index_key[index]=get_min_value(rNode);
}
}
else //如果原节点的父节点为空,那么说明要分裂的是根节点
{
BTree* newRoot=new BTree;
newRoot->parent=NULL;
newRoot->child_size=2;
newRoot->is_leaf=false;
newRoot->child_list[0]=&lNode;
newRoot->child_list[1]=&rNode;
int index_key_value=get_min_value(rNode);
newRoot->index_key[0]=index_key_value;
lNode.parent=newRoot;
rNode.parent=newRoot;
root=newRoot;
}
}
/*
从叶子删除某一个元素
当数据项所在的叶子已经具有最低数量的数据项,那么就要进行数据项的合并
*/
void BTree::remove(int data)
{
if(!root)
return;
/*首先找到待删除元素所在的叶子*/
BTree* p=root;
while(!p->is_leaf)
{
int i=0;
for(;i<p->child_size-1;i++)
if(data<p->index_key[i])
break;
p=p->child_list[i];
}
if(p->is_leaf)
{
int remove=array_remove(p->elem_data,data,p->elem_size);
if(remove==-1) //删除失败,元素不在树中,直接返回即可
return;
p->elem_size=p->elem_size-1;
/*
如果删除之后,树叶中元素的个数太少,就需要进行合并
如果被删除的叶子节点是根节点,并且删除之后没有元素
那么就需要将树删除
*/
if(!(p->parent))
{
if(p->elem_size==0)
{
delete root;
root=NULL;
return;
}
}
else if(p->elem_size<(L/2+1))
{
node_merge(*p);
}
else /*更新父节点的索引信息*/
{
int index=0;
for(;index<p->parent->child_size;index++)
if(p==p->parent->child_list[index])
break;
if(index>0)
p->parent->index_key[index-1]=
get_min_value(*p);
}

}
else //find p, but not a leaf
{
cout<<"something is wrong..."<<endl;
}
}

linux
相关资讯       C++  B树  B-Tree 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款