知识准备
b+树线程代码很少,主要用于数据库底层管理,如下两份资请自行阅读。
https://www.bilibili.com/video/BV1n7411A7x3?from=search&seid=7003654934148976244
https://blog.youkuaiyun.com/xiaohusaier/article/details/77101640
需求

重点代码
B+树相关代码出自另一篇博客,引用如上。
如下是本人写的,readdata执行数据的录入和导出。
//readdata.h
#ifndef READDATA_H_INCLUDED
#define READDATA_H_INCLUDED
#include <stdio.h>
#include "bplusTree.h"
struct student{
int id,grade;
char gender[10];
char name[30];
};
extern struct student st[5000000];
BPlusTree get_info(BPlusTree T);
void write_data(BPlusTree T);
#endif // READDATA_H_INCLUDED
//readdata.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "readdata.h"
#include "bplusTree.h"
#define FILENAME "info.csv"
#define OUTFILENAME "out.txt"
struct student st[5000000];
BPlusTree get_info(BPlusTree T){
FILE *fp = NULL;
char *line,*record;
char buffer[1024];
if ((fp = fopen(FILENAME, "at+")) != NULL)
{
int j,id;
fgets(buffer, sizeof(buffer), fp);
while ((line = fgets(buffer, sizeof(buffer), fp))!=NULL)//当没有读取到文件末尾时循环继续
{
record = strtok(line, ",");
j=0;
while (record != NULL)//读取每一行的数据
{
switch(j)
{
case 0:
id=atoi(record);
T = Insert(T, id);
st[id].id=id;
break;
case 1:
strcpy(st[id].gender,record);
break;
case 2:
strcpy(st[id].name,record);
break;
case 3:
st[id].grade=atoi(record);
break;
}
//printf("%s ", record);//将读取到的每一个数据打印出来
record = strtok(NULL, ",");
j++;
}
printf("\n");
}
fclose(fp);
fp = NULL;
}
return T;
}
void write_data(BPlusTree T){
FILE* fp1 = fopen(OUTFILENAME , "w");
Position Tmp=FindFirst(T);
int id,i;
/* 第一片树叶 */
while (Tmp != NULL){
i = 0;
while (i < Tmp->KeyNum){
id=Tmp->Key[i++];
fprintf(fp1, "%d,%s,%s,%d\n",\
st[id].id,st[id].name,st[id].gender,st[id].grade);
}
Tmp = Tmp->Next;
}
fclose(fp1);//关闭文件
printf("Data saved.\n");
}
查询该节点是否存在,模拟磁盘的思路,磁盘中的文件如果删除,知识索引删除,文件实体暂时不会更新。
extern Position FindFirst(BPlusTree T){
Position Tmp;
if (T == NULL)
return T;
Tmp = T;
while (Tmp->Children[0] != NULL)
Tmp = Tmp->Children[0];
return Tmp;
}
extern int IsExist(BPlusTree T,KeyType Key){
int j = 0,i;
while(j<T->KeyNum){
//printf("%d\r\n",T->Key[j]);
if(j==0 && Key<T->Key[j]){
return -1;
}else if(Key == T->Key[j]){
return 1;
}else if((j<T->KeyNum-1 && Key>=T->Key[j] && Key<T->Key[j+1])\
||(j==T->KeyNum-1 && Key>=T->Key[j])){
if(T->Children[j]==NULL) {
// printf("%d\r\n",T->Key[j]);
for(i=0;i<T->KeyNum;i++){
if(T->Key[i]==Key) return 1;
}
return -1;
}
else return IsExist(T->Children[j],Key);
}
j++;
}
return 0;
}
完整工程
https://download.youkuaiyun.com/download/renzemingcsdn/19688648
效果
数据导入

查找

删除后 查无此人


这篇博客介绍了B+树的数据结构及其在数据库底层管理中的应用。通过提供的代码展示了如何使用B+树进行数据的读取、插入和导出。`get_info`函数用于从CSV文件中读取学生信息并构建B+树,而`write_data`函数则将数据写入到文本文件。同时,还提供了一个`IsExist`函数用于查询B+树中节点是否存在。完整工程可在链接中下载。
1万+

被折叠的 条评论
为什么被折叠?



