C ++ B +树搜索和插入实现

时间:2018-11-26 16:36:16

标签: c++ data-structures struct implementation b-tree

我为C ++制作了一个B + Tree示例。我测试了很多。我没有收到任何错误或错误,但是我无法确定它是否确实有效(没有任何错误),或者我不知道它是否适合使用(内存问题,优化等)。我想要您的评论,测试结果和建议。感谢您的评论。

这是我的代码:

 /*
 * C++ Program to Implement B+ Tree
 */
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include <stdlib.h>
using namespace std;

struct Student{
    int number;
    int failCount;
    string name;
    string surname;
    string field;
};

struct less_than_key
{
    inline bool operator() (const Student& struct1, const Student& struct2)
    {
        return (struct1.number < struct2.number);
    }
};

const int numberofdatas = 6;
const int numberofkeys = numberofdatas+1;

struct BTreeNode
{
    BTreeNode* parent;
    vector<BTreeNode*> childLeafs;
    vector<Student> datas;
    BTreeNode(){
        parent = NULL;
    }
} * BTreeRoot = new BTreeNode;


// Search in B+Tree and return last leaf
BTreeNode* BTreeSearch(BTreeNode* tree, int key){
    while(!tree->childLeafs.empty()){
        if(key < tree->datas[0].number){
            tree = tree->childLeafs[0];
        }
        for(int i =0; i < tree->datas.size()-1; i++){
            if(key >= tree->datas[i].number && key < tree->datas[i+1].number){
                if(!tree->childLeafs.empty())
                tree = tree->childLeafs[i+1];
            }
        }
        if(key > tree->datas[tree->datas.size()-1].number){
            tree = tree->childLeafs[tree->datas.size()];
        }
    }
    return tree;
}


// Split B+Tree
void BTreeSplit(BTreeNode* tree){
    BTreeNode* Left = new BTreeNode;
    BTreeNode* Right = new BTreeNode;
    BTreeNode* Parent;
    if(tree->parent == NULL){
        Parent = new BTreeNode;
    }else{
        Parent = tree->parent;
    }
    int middleInt = tree->datas.size()/2;
    Student middle = tree->datas[middleInt];
    for(int i = 0; i< middleInt;i++){
        Left->datas.push_back(tree->datas[i]);
    }
    for(int i = middleInt; i< tree->datas.size();i++){
        Right->datas.push_back(tree->datas[i]);
    }
    if(tree->parent == NULL){
        Parent->datas.push_back(middle);
        Parent->childLeafs.push_back(Left);
        Parent->childLeafs.push_back(Right);
        Left->parent = Right->parent = Parent;
        BTreeRoot = Parent;
    }else{
        int n = 0;
        if(middle.number < Parent->datas[0].number){
            n = 0;
        }
        for(int i =0; i < Parent->datas.size()-1; i++){
            if(middle.number >= Parent->datas[i].number && middle.number < Parent->datas[i+1].number){
                n = i+1;
            }
        }
        if(middle.number > Parent->datas[Parent->datas.size()-1].number){
            n = Parent->datas.size();
        }
        if(n == Parent->datas.size()){
            Parent->childLeafs.pop_back();
            Parent->datas.push_back(middle);
            Parent->childLeafs.push_back(Left);
            Parent->childLeafs.push_back(Right);
            Left->parent = Right->parent = Parent;
        }else{
            Parent->datas.insert(Parent->datas.begin()+n, middle);
            Parent->childLeafs.insert(Parent->childLeafs.begin()+n+1, Right);
            Parent->childLeafs[n] = Left;
            Left->parent = Right->parent = Parent;
        }
        if(Parent->datas.size() > numberofdatas){
            BTreeSplit(Parent);
        }
    }
}

// Insert to BTreeRoot
void BTreeInsert(Student student){
    BTreeNode* temp = BTreeSearch(BTreeRoot, student.number);
    temp->datas.push_back(student);
    std::sort(temp->datas.begin(), temp->datas.end(), less_than_key());
    if(temp->datas.size() > numberofdatas){
        BTreeSplit(temp);
    }
}

// Print B+Tree datas
void BTreePrint(BTreeNode* tree){
    if(tree->childLeafs.size() != 0)
    for(int i = 0; i < tree->childLeafs.size();i++){
        BTreePrint(tree->childLeafs[i]);
    }
    if(tree->childLeafs.size() == 0)
    for(int i = 0; i < tree->datas.size();i++){
        cout << tree->datas[i].number << " -> ";
    }
}

// Print B+Tree visual
void BTreePrint2(BTreeNode* tree){
    for(int i = 0; i < tree->datas.size();i++){
        cout << tree->datas[i].number << " -> ";
    }
    cout << endl;
    if(tree->childLeafs.size() != 0)
    for(int i = 0; i < tree->childLeafs.size();i++){
        BTreePrint2(tree->childLeafs[i]);
    }
}

int main()
{
    Student ogr;
    int a;
    while(true){
        cin >> a;
        ogr.number = a;
        BTreeInsert(ogr);
        BTreePrint2(BTreeRoot);
        cout << "--------" << endl;
    }
    return 0;
}

0 个答案:

没有答案