Question: How do I insert the new Node in a B-Tree? Below is the code I have, what is the code to insert the new node
How do I insert the new Node in a B-Tree?
Below is the code I have, what is the code to insert the new node
#ifndef BTree_h
#define BTree_h
#include
#include
#include
using namespace std;
#define M 4
struct record
{
int idNo;
string name;
string surname;
};
typedef struct record Record;
//====================================================
//B-Tree Node Class code
//====================================================
class BNode
{
private:
vector
Record data[M];
BNode* parent;
public:
// Constructor
BNode();
//Accessors
string getData();
BNode* getPtr(int x){return pointer[x];}
//Mutators
bool setData(int, string, string);
void setPtr(int x, BNode* ptr){pointer[x] = ptr;}
};
BNode::BNode()
{
pointer.resize(M+1);
for (int x = 0; x < M; x++)
{
pointer[x] = NULL;
data[x].idNo = 0;
data[x].name = "";
data[x].surname = "";
}
pointer[M] = NULL;
parent = NULL;
}
string BNode::getData()
{
string str = "";
for (int x = 0; x < M; x++)
{
str.append("[");
str.append(to_string(data[x].idNo));
str.append(" ");
str.append(data[x].name);
str.append(" ");
str.append(data[x].surname);
str.append("] ");
}
str.append(" ");
return str;
}
bool BNode::setData(int num, string first, string last)
{
if (data[M-1].name != "")//Node is already full
return false;
int x = 0;
for (; x < M && data[x].name != ""; x++);
if (x > 0)
{
while (data[x-1].idNo > num)
{
data[x] = data[x-1];
x--;
}
}
data[x].idNo = num;
data[x].name = first;
data[x].surname = last;
return true;
}
//====================================================
//B-Tree Class code
//====================================================
class BTree
{
private:
BNode* root;
BNode* insertHelper(int, string, string);
public:
BTree(){root = NULL;}
//Mutator
void insert(int, string, string);
void remove(int);
//Accessor
string display();
};
void BTree::insert(int num, string first, string last)
{
root = insertHelper(num, first, last);
}
BNode* BTree::insertHelper(int num, string first, string last)
{
BNode* ptr = NULL;
if (root == NULL)
{
ptr = new BNode();
ptr->setData(num, first, last);
return ptr;
}
//ELSE
//
// Where should the new node be inserted??????
//
return ptr;
}
#endif /* BTree_h */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
