1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <iostream>
#include <ostream>
#include "bptree.h"
#include "bptree2.h"
using namespace std;
void bptree::print_data(internal_container *d)
{
cout<<" print_data ";
// it will print the internal_container data (d->in_node[].st, d->in_node[].et)
};
void bptree::print_data(leaf_container *d)
{
cout<<" print_data leaf ";
// it will print the leaf_container data (d->leaf_node[].st, d->leaf_node[].et and d->leaf_node[].mob_id)
};
void bptree::newintcont()
{
cur_key=0;
cur_in_slot=0;
incont = new internal_container(max_key, max_slot);
internal_container *temp;
temp=NULL;
temp=(internal_container *) head;
incont->keys[cur_key] =(long) temp;
head=incont;
cout<<"\n insert new internal container at "<<head<<" "<<(long) head<<endl;
};
void bptree::newleafcont()
{
leafcont= new leaf_container(max_slot);
cout<<"\n newleafcontainer at "<<(long )leafcont<<endl;
incont->keys[cur_key] = (long )leafcont;
cur_leaf_slot=0;
cur_key++;
cout<< " cur_key "<<cur_key<<" ";
};
void bptree::insert_internal(int st, int et)
{cout<<"\n insert slot at "<< st <<"sec"<<endl;
incont->in_node[cur_in_slot].st=st;
incont->in_node[cur_in_slot].et=et;
cur_in_slot++;
};
void bptree::insert_leaf(string mob_id, int st, int et)
{cout<<endl<<" insert at "<<st<<" second ";
leafcont->leaf_node[cur_leaf_slot].st=st;
leafcont->leaf_node[cur_leaf_slot].et=et;
leafcont->leaf_node[cur_leaf_slot].mob_id=mob_id;
cur_leaf_slot++;
cout<< " cur_key after insert leaf "<<cur_key<<" ";
};
void bptree::insert(string mob_id, int st, int et)
{
if(head==NULL)
{cout<<endl<<"NULL head"<<endl;
insert_internal(st, et);
insert_leaf(mob_id, st, et);
head=incont;
incont->keys[cur_key] = (long )leafcont;
cur_key++;
cout<<" head "<<head<<" " <<(long)head<<endl;
}//end if
else//head!=NULL
{
if(cur_leaf_slot<max_slot) // LEAF
{
insert_leaf(mob_id, st, et);
if (incont->in_node[cur_in_slot-1].et<et)
incont->in_node[cur_in_slot-1].et=et;
}//end if(cur_leaf_slot<max_slot)
else
{
cout<< " \n cur_key "<<cur_key<<" maxkey"<<max_key<<endl;
if(cur_key<max_key)
{
cout<<" ora ";
newleafcont();
insert_leaf(mob_id, st, et);
if(cur_in_slot<max_slot)
insert_internal(st, et);
}//end (cur_key<max_key)
else
{
newintcont();
newleafcont();
insert_internal(st, et);
insert_leaf(mob_id, st, et);
}//end last else
}
}//end head !=null
}; //end insert
void bptree::print()
{
cout<<"\n print"<<endl;
internal_container *icp;
leaf_container *lcp;
icp=head;
print_data((icp->keys[0]));
}//end print
int main()
{
bptree tree(3,2);
tree.insert("m4", 1, 7);
tree.insert("m2", 2, 4);
tree.insert("m1", 3, 12);
tree.insert("m6", 4, 12);
tree.insert("m0", 5, 12);
tree.insert("m6", 6, 3);
tree.insert("m9", 7, 20);
tree.insert("m6", 8, 12);
tree.insert("m0", 9, 12);
tree.insert("m6", 10, 3);
tree.insert("m9",11, 20);
tree.insert("m6", 12, 12);
tree.insert("m0", 13, 12);
tree.insert("m6", 14, 3);
tree.insert("m9",15, 20);
tree.print();
return 0;
}///end main
|