簡體   English   中英

B+ 樹插入和搜索

[英]B+ tree insertion and searching

我正在嘗試使用 b+tree 實現文件中信息的插入和搜索,同時這樣做我得到一個錯誤作為未定義的引用

我對原始代碼進行了更改,其鏈接僅在 main.cpp 和 header 文件已更改

主文件

#include <iostream>
#include <vector>
//#include <algorithm>
#include <fstream>
#include <string>
//#include <filesystem>
#include "BPTree.h"

void insertionMethod(BPTree** bPTree) {
    int rollNo;
    int age, marks;
    std::string name;

    std::cout << "Please provide the rollNo: ";
    std::cin >> rollNo;

    std::cout << "\nWhat's the Name, Age and Marks acquired?: ";
   std::cin >> name >> age >> marks;

    std::string fileName = "DBFiles/";
     fileName += std::to_string(rollNo) + ".txt";
    FILE* filePtr = fopen(fileName.c_str(), "w");
    std::string userTuple = name + " " + std::to_string(age) + " " + std::to_string(marks) + "\n";
    fprintf(filePtr, userTuple.c_str());
    //fclose(filePtr);

    (*bPTree)->insert(rollNo, filePtr);
    fclose(filePtr);
    std::cout << "Insertion of roll No: " << rollNo << " Successful"<<std::endl;
}

void searchMethod(BPTree* bPTree) {
    int rollNo;
    std::cout << "What's the RollNo to Search? ";
    std::cin >> rollNo;

    bPTree->search(rollNo);
}
int main() {
  
    bool flag = true;
    int option;

    int maxChildInt = 4, maxNodeLeaf = 3;
    std::cout << "Please provide the value to limit maximum child Internal Nodes can have: ";
    std::cin >> maxChildInt;
    std::cout << "\nAnd Now Limit the value to limit maximum Nodes Leaf Nodes can have: ";
    std::cin >> maxNodeLeaf;

    BPTree* bPTree = new BPTree(maxChildInt, maxNodeLeaf);
    std::cout<<bPTree->getMaxLeafNodeLimit()<<std::endl;
    do {
        std::cout << "\nPlease provide the queries with respective keys : " << std::endl;
        std::cout << "\tPress 1: Insertion \n\tPress 2: Search<< std::endl;
        std::cin >> option;

        switch (option) {
            case 1:
                insertionMethod(&bPTree);
                break;
            case 2:
                searchMethod(bPTree);
                break;
            default:
                flag = false;
                break;
        }
    }while (flag);

    return 0;
}

BPTree.h


#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <string>

#ifndef NODE_H
#define NODE_H
class Node {
   public:
    bool isLeaf;
    std::vector<int> keys;
  
    Node* ptr2next;              
    union ptr {                    
        std::vector<Node*> ptr2Tree;  //Array of pointers to Children sub-trees for intermediate Nodes
        std::vector<FILE*> dataPtr;   // Data-Pointer for the leaf node

        ptr();   // To remove the error !?
        ~ptr();  // To remove the error !?
    } ptr2TreeOrData;

    friend class BPTree;  // to access private members of the Node and hold the encapsulation concept
   public:
    Node(){
        this->isLeaf = false;
        this->ptr2next = NULL;
    }
};



class BPTree {
   private:
    int maxIntChildLimit;                                   //Limiting  #of children for internal Nodes!
    int maxLeafNodeLimit;                                   // Limiting #of nodes for leaf Nodes!!!
     Node* root;  Node* pparent = NULL;                                           //Pointer to the B+ Tree root
    void insertInternal(int x, Node** cursor, Node** child);  //Insert x from child in cursor(parent)
  Node** findParent(Node* cursor, Node* child){
       if (cursor->isLeaf || cursor->ptr2TreeOrData.ptr2Tree[0]->isLeaf)
        return NULL;

    for (int i = 0; i < cursor->ptr2TreeOrData.ptr2Tree.size(); i++) {
        if (cursor->ptr2TreeOrData.ptr2Tree[i] == child) {
            pparent = cursor;
        } else {
            Node* tmpCursor = cursor->ptr2TreeOrData.ptr2Tree[i];
            findParent(tmpCursor, child);
        }
    }

    return &pparent;
  }


   public:
    BPTree();
    BPTree(int degreeInternal, int degreeLeaf){
        this->maxIntChildLimit = degreeInternal;
    this->maxLeafNodeLimit = degreeLeaf;
    this->root = NULL;
    }

    int getMaxIntChildLimit();
    int getMaxLeafNodeLimit() 
    {
        return maxLeafNodeLimit;
    }
    void search(int key);
    void insert(int key, FILE* filePtr);
   
};
#endif

搜索插入代碼

我得到的錯誤是

C:\Users\admin\AppData\Local\Temp\cctqZ0YA.o:insert.cpp:(.text$_ZN4NodeC1Ev[__ZN4NodeC1Ev]+0x20): undefined reference to `Node::ptr::ptr()'
collect2.exe: error: ld returned 1 exit status

我不太確定 Node::ptr::ptr() 是什么,有人可以幫忙嗎

刪除那些已聲明但未定義的構造函數和析構函數后收到的錯誤消息指向實際問題。 為什么聯合可能需要構造函數和析構函數在別處有更好的解釋,例如這里這里

您可以通過提供一個簡單的空實現來編譯您的代碼。

ptr() {};
~ptr() {};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM