title: Java B+tree implementation
body: <p>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??</p>
  
  <pre><code>    /*
   * C++ Program to Implement B+ Tree
   */
  #include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  #include&lt;iostream&gt;
  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-&gt;data = new int[5];
      np-&gt;child_ptr = new B+TreeNode *[6];
      np-&gt;leaf = true;
      np-&gt;n = 0;
      for (i = 0; i &lt; 6; i++)
      {
          np-&gt;child_ptr[i] = NULL;
      }
      return np;
  }
  void traverse(B+TreeNode *p)
  {
      cout&lt;&lt;endl;
      int i;
      for (i = 0; i &lt; p-&gt;n; i++)
      {
          if (p-&gt;leaf == false)
          {
              traverse(p-&gt;child_ptr[i]);
          }
          cout &lt;&lt; " " &lt;&lt; p-&gt;data[i];
      } 
      if (p-&gt;leaf == false)
      {
          traverse(p-&gt;child_ptr[i]);
      }
      cout&lt;&lt;endl;
  }
  void sort(int *p, int n)
  {
      int i, j, temp;
      for (i = 0; i &lt; n; i++)
      {
          for (j = i; j &lt;= n; j++)
          {
              if (p[i] &gt; 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-&gt;leaf = true;
      if (i == -1)
      {
          mid = x-&gt;data[2];
          x-&gt;data[2] = 0;
          x-&gt;n--;
          np1 = init();
          np1-&gt;leaf = false;
          x-&gt;leaf = true;
          for (j = 3; j &lt; 5; j++)
          {
              np3-&gt;data[j - 3] = x-&gt;data[j];
              np3-&gt;child_ptr[j - 3] = x-&gt;child_ptr[j];
              np3-&gt;n++;
              x-&gt;data[j] = 0;
              x-&gt;n--;
          }
          for(j = 0; j &lt; 6; j++)
          {
              x-&gt;child_ptr[j] = NULL;
          }
          np1-&gt;data[0] = mid;
          np1-&gt;child_ptr[np1-&gt;n] = x;
          np1-&gt;child_ptr[np1-&gt;n + 1] = np3;
          np1-&gt;n++;
          root = np1;
      }
      else
      {
          y = x-&gt;child_ptr[i];
          mid = y-&gt;data[2];
          y-&gt;data[2] = 0;
          y-&gt;n--;
          for (j = 3; j &lt; 5; j++)
          {
              np3-&gt;data[j - 3] = y-&gt;data[j];
              np3-&gt;n++;
              y-&gt;data[j] = 0;
              y-&gt;n--;
          }
          x-&gt;child_ptr[i + 1] = y;
          x-&gt;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-&gt;leaf == true &amp;&amp; x-&gt;n == 5)
          {
              temp = split_child(x, -1);
              x = root;
              for (i = 0; i &lt; (x-&gt;n); i++)
              {
                  if ((a &gt; x-&gt;data[i]) &amp;&amp; (a &lt; x-&gt;data[i + 1]))
                  {
                      i++;
                      break;
                  }
                  else if (a &lt; x-&gt;data[0])
                  {
                      break;
                  }
                  else
                  {
                      continue;
                  }
              }
              x = x-&gt;child_ptr[i];
          }
          else
          {
              while (x-&gt;leaf == false)
              {
              for (i = 0; i &lt; (x-&gt;n); i++)
              {
                  if ((a &gt; x-&gt;data[i]) &amp;&amp; (a &lt; x-&gt;data[i + 1]))
                  {
                      i++;
                      break;
                  }
                  else if (a &lt; x-&gt;data[0])
                  {
                      break;
                  }
                  else
                  {
                      continue;
                  }
              }
                  if ((x-&gt;child_ptr[i])-&gt;n == 5)
                  {
                      temp = split_child(x, i);
                      x-&gt;data[x-&gt;n] = temp;
                      x-&gt;n++;
                      continue;
                  }
                  else
                  {
                      x = x-&gt;child_ptr[i];
                  }
              }
          }
      }
      x-&gt;data[x-&gt;n] = a;
      sort(x-&gt;data, x-&gt;n);
      x-&gt;n++;
  }
  int main()
  {
      int i, n, t;
      cout&lt;&lt;"enter the no of elements to be inserted\n";
      cin&gt;&gt;n;
      for(i = 0; i &lt; n; i++)
      {
          cout&lt;&lt;"enter the element\n";
          cin&gt;&gt;t;
          insert(t);
      }
      cout&lt;&lt;"traversal of constructed tree\n";
      traverse(root);
      getch();
  }
  </code></pre>