Skip to content

Commit

Permalink
second
Browse files Browse the repository at this point in the history
  • Loading branch information
ksshen0000 committed May 12, 2021
1 parent 96e4609 commit 109a5eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions B+ Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void instr() {
struct node {
int sz=0, szp=0;
int val[MAXN+3];
int leaf[MAXN+3];
node* ptr[MAXN+3];
node* par;
node* sibling;
Expand All @@ -49,6 +50,12 @@ struct node {
val[in] = x;
sz++;
}
void insertLeaf(int in,int x)
{
for(int i=sz-1; i>=in; i--) leaf[i+1] = leaf[i];
leaf[in] = x;
sz++;
}
void insertPtr(int in, node* p) {
for(int i=szp-1; i>=in; i--) {
ptr[i]->parPtrID++;
Expand All @@ -65,11 +72,18 @@ struct node {
void pushBackPtr(node* p) {
ptr[szp++] = p;
}
void pushBackLeaf(int x) {
leaf[sz++] = x;
}

void eraseVal(int in) {
for(int i=in+1; i<sz; i++) val[i-1] = val[i];
sz--;
}
void eraseLeaf(int in) {
for(int i=in+1; i<sz; i++) leaf[i-1] = leaf[i];
sz--;
}
void erasePtr(int in) {
for(int i=in+1; i<szp; i++) ptr[i-1] = ptr[i];
szp--;
Expand All @@ -81,18 +95,25 @@ struct node {
void reversePtr() {
reverse(ptr, ptr+szp);
}
void reverseLeaf()
{
reverse(leaf, leaf+sz);
}

void insert(int x) {
void insert(int x,int y) {
int iny=0;
while(iny < sz) {
if(x < val[iny]) break;
iny++;
}

if(isLeaf) insertVal(iny, x);
if(isLeaf)
{
insertVal(iny, x);
insertLeaf(iny,y);
}
else {
// cout << "INS " << iny << endl;
ptr[iny]->insert(x);
ptr[iny]->insert(x,y);
}

if(sz == MAXN) split();
Expand All @@ -104,6 +125,7 @@ struct node {
sibling = splitedNode;

while(sz > MAXN/2) {
splitedNode->pushBackLeaf(leaf[sz-1]);
splitedNode->pushBackVal(val[sz-1]);
sz--; ///val.pp();
}
Expand All @@ -112,6 +134,7 @@ struct node {
szp--; ///ptr.pp();
}
splitedNode->reverseVal();
splitedNode->reverseLeaf();
splitedNode->reversePtr();
for(int i=0; i<splitedNode->szp; i++) {
splitedNode->ptr[i]->parPtrID = i;
Expand All @@ -133,11 +156,15 @@ struct node {

///handle parent er majhkhane insert
par->insertVal(parPtrID, splitedNode->val[0]);
// par->insertLeaf(parPtrID, splitedNode->leaf[0]);
par->insertPtr(parPtrID+1, splitedNode);

///handle leaf na hoile split thk 0 er ta delete
if(!isLeaf) splitedNode->eraseVal(0);

if(!isLeaf)
{
splitedNode->eraseVal(0);
// splitedNode->eraseLeaf(0);
}
// cout << "AFTER SPLIT" << endl;
// for(int i=0; i<sz; i++) cout << val[i] << ' ' ; cout << endl;
// for(int i=0; i<splitedNode->sz; i++) cout << splitedNode->val[i] << ' ' ; cout << endl;
Expand Down Expand Up @@ -274,7 +301,7 @@ int main() {
// printf("%d\n", x);
// getchar();

tree->insert(x);
tree->insert(x,1);
while(tree->par != NULL) {
tree = tree->par;
}
Expand Down
Binary file added B+ Tree.exe
Binary file not shown.

0 comments on commit 109a5eb

Please sign in to comment.