Hello, I am building a B-Tree with data from a txt file, extracting a numerical record (as an int) and storing that in my B-Tree. I followed the debugger and added a cout statement to see that it is actually inserting, and it seems to work fine, but upon closer inspection, the splitChild and splitParent are not occurring nearly as much as expected (with branching factor of 3), and once I return to my initial insert statement and use the Locals window, I cannot navigate past Root and it's three children (all the 'next's are NULL.
constint PAGE_SIZE = 3; //Branching Factor
class BTreeNode;
struct keys
{
int value = 0;
BTreeNode* next;
};
class BTreeNode
{
public:
BTreeNode()
{
root = true;
leaf = false;
}
bool root;
bool leaf;
int n; // how many keys
keys page[PAGE_SIZE];
};
class BTree
{
BTreeNode* Root;
bool audit;
public:
BTree();
BTreeNode* indexData(BTreeNode*);
int find(BTreeNode*, int);
int insert(BTreeNode*&, int);
int splitRoot(BTreeNode*&);
int splitChild(BTreeNode*&, int);
int remove(BTreeNode*, int);
void printTreeBOM(BTreeNode*);
void OnOffAuditTrail(); //prompts user if they want a message for each time a node is split;
void setAudit(bool);
bool getAudit();
Here is the function that reads the file and sends the key to the insert function:
BTreeNode* BTree::indexData(BTreeNode* root)
{
int key; // to store the extracted unique identifier from the file
size_t eofMarker;
string line;
fstream inOutFile;
longint offset = 3;
longint begin = 0;
longint end = 0;
int i = 0;
inOutFile.open("censusdata.txt", fstream::binary | fstream::ate | fstream::in | fstream::out);
if (!inOutFile) {
cout << "File open error" << endl;
return root;
}
inOutFile.seekg(0, ios::end); // position 0 bytes from the end of the file
eofMarker = inOutFile.tellg(); // sets eofMarker to the end of the file returned from tellg()
inOutFile << "0 -> "; // so it can print 0 -> at the end of the document
inOutFile.seekg(0, ios::beg); // position 0 bytes from the beginning
begin = inOutFile.tellg();
while (begin + offset < eofMarker)
{
inOutFile.seekg((begin + offset), ios::beg);
inOutFile >> key;
inOutFile.ignore(300, '\n');
offset = inOutFile.tellg();
insert(root, key);
}
inOutFile.seekp(0, fstream::end);
inOutFile << " EOF" << endl;
inOutFile.close();
return root;
}