0

I am trying to make a B+tree implementation in java and I tried to use help from the internet. There are no help almost about this subject, but I was able to find this piece of Code in C++ and I am trying to convert it to java. My question is this a good implementation of a b+tree??

    /*
 * C++ Program to Implement B+ Tree
 */
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
struct B+TreeNode
{
    int *data;
    B+TreeNode **child_ptr;
    bool leaf;
    int n;
}*root = NULL, *np = NULL, *x = NULL;
B+TreeNode * init()
{
    int i;
    np = new B+TreeNode;
    np->data = new int[5];
    np->child_ptr = new B+TreeNode *[6];
    np->leaf = true;
    np->n = 0;
    for (i = 0; i < 6; i++)
    {
        np->child_ptr[i] = NULL;
    }
    return np;
}
void traverse(B+TreeNode *p)
{
    cout<<endl;
    int i;
    for (i = 0; i < p->n; i++)
    {
        if (p->leaf == false)
        {
            traverse(p->child_ptr[i]);
        }
        cout << " " << p->data[i];
    } 
    if (p->leaf == false)
    {
        traverse(p->child_ptr[i]);
    }
    cout<<endl;
}
void sort(int *p, int n)
{
    int i, j, temp;
    for (i = 0; i < n; i++)
    {
        for (j = i; j <= n; j++)
        {
            if (p[i] > p[j])
            {
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }
}
int split_child(B+TreeNode *x, int i)
{
    int j, mid;
    B+TreeNode *np1, *np3, *y;
    np3 = init();
    np3->leaf = true;
    if (i == -1)
    {
        mid = x->data[2];
        x->data[2] = 0;
        x->n--;
        np1 = init();
        np1->leaf = false;
        x->leaf = true;
        for (j = 3; j < 5; j++)
        {
            np3->data[j - 3] = x->data[j];
            np3->child_ptr[j - 3] = x->child_ptr[j];
            np3->n++;
            x->data[j] = 0;
            x->n--;
        }
        for(j = 0; j < 6; j++)
        {
            x->child_ptr[j] = NULL;
        }
        np1->data[0] = mid;
        np1->child_ptr[np1->n] = x;
        np1->child_ptr[np1->n + 1] = np3;
        np1->n++;
        root = np1;
    }
    else
    {
        y = x->child_ptr[i];
        mid = y->data[2];
        y->data[2] = 0;
        y->n--;
        for (j = 3; j < 5; j++)
        {
            np3->data[j - 3] = y->data[j];
            np3->n++;
            y->data[j] = 0;
            y->n--;
        }
        x->child_ptr[i + 1] = y;
        x->child_ptr[i + 1] = np3; 
    }
    return mid;
}
void insert(int a)
{
    int i, temp;
    x = root;
    if (x == NULL)
    {
        root = init();
        x = root;
    }
    else
    {
        if (x->leaf == true && x->n == 5)
        {
            temp = split_child(x, -1);
            x = root;
            for (i = 0; i < (x->n); i++)
            {
                if ((a > x->data[i]) && (a < x->data[i + 1]))
                {
                    i++;
                    break;
                }
                else if (a < x->data[0])
                {
                    break;
                }
                else
                {
                    continue;
                }
            }
            x = x->child_ptr[i];
        }
        else
        {
            while (x->leaf == false)
            {
            for (i = 0; i < (x->n); i++)
            {
                if ((a > x->data[i]) && (a < x->data[i + 1]))
                {
                    i++;
                    break;
                }
                else if (a < x->data[0])
                {
                    break;
                }
                else
                {
                    continue;
                }
            }
                if ((x->child_ptr[i])->n == 5)
                {
                    temp = split_child(x, i);
                    x->data[x->n] = temp;
                    x->n++;
                    continue;
                }
                else
                {
                    x = x->child_ptr[i];
                }
            }
        }
    }
    x->data[x->n] = a;
    sort(x->data, x->n);
    x->n++;
}
int main()
{
    int i, n, t;
    cout<<"enter the no of elements to be inserted\n";
    cin>>n;
    for(i = 0; i < n; i++)
    {
        cout<<"enter the element\n";
        cin>>t;
        insert(t);
    }
    cout<<"traversal of constructed tree\n";
    traverse(root);
    getch();
}
David
  • 165
  • 12
  • *"My question is this a good implementation of a b+tree?"* - That is a subjective question. – Stephen C Mar 31 '15 at 07:44
  • 1
    1. That code won't compile (`B+TreeNode` at least will cause compilation failures). 2. It's quite a lot of code to go through and try to understand if it is "good" or not. 3. It is not written in C++, it is written in C with some C++ syntax thrown in. 4. Quick scan reveals redundant code (setting node `leaf` to true, when that has already been done, `continue` when it's not needed). So probably not the best C++ implementation of a B+tree out there. [And if you want to translate to Java, I'm not sure C or C++ is the best starting point] – Mats Petersson Mar 31 '15 at 07:44
  • @MatsPetersson thanks for the Help do have a better source in java or just some sort of B+tree pseudocode implementation in java?? – David Mar 31 '15 at 07:50
  • @StephenC I really couldnt find a better title. – David Mar 31 '15 at 07:51
  • 1
    I do not program in Java, and haven't written B+ tree code for many years. Searching in google for `Java B+ tree` gives two apparently good implementations in the first two hits - I did only spend about a minute or two on looking at them, so no guarantees, but looks OK to me. But if you want "help" in the form of "how do you write such code yourself", this is not the right place for that, and there are plenty of books on the subject of "Algorithms" or "Data Structures + Code" that you probably want to read to find out how the code actually works. – Mats Petersson Mar 31 '15 at 07:55
  • @MatsPetersson I kind of need it for today. – David Mar 31 '15 at 07:57
  • 1
    @David - it isn't a title. It is a question. And if you couldn't "find" a better (more appropriate) question to ask, perhaps you should not have asked any question! While I accept that your English skills may not be as good as a native speaker ... it is your responsibility to make sure that what you write expresses the real question that is in your head. – Stephen C Mar 31 '15 at 07:58
  • 1
    @David - *"I kind of need it for today."* - Google is working today :-) – Stephen C Mar 31 '15 at 07:59
  • 1
    Google is definitely working today. Book shops, at least here in the UK are open in about an hours time, if not earlier. I'm fairly sure that you weren't given this task as a surprise by your teacher yesterday evening, so your poor preparation is not our problem. – Mats Petersson Mar 31 '15 at 08:02
  • @MatsPetersson I had other practicals that I was working on them I kind of put this in a lower priority, and Now I now that it was a wrong decision. – David Mar 31 '15 at 08:11
  • 1
    Ok, so to learn from this: Read through and understand the relative size of different tasks before engaging in them. – Mats Petersson Mar 31 '15 at 08:13

0 Answers0