Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java B-Tree Insert (no libraries)

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java B-Tree Insert (no libraries)

    Hi,

    My task is to implement a B-Tree data structure in Java using no libraries.

    My current problem is inserting when the root is full, thus the middle key goes to root.keys[0] and then it get the left and right side which are new Nodes separating and inserting their keys. My problem is that for some reason there is a random second number when in the root and secondly my boolean leaf is always false meaning it never detects that the Tree is deeper than root.

    The coding makes sense to me and I have tried printing everywhere but still can't seem to find the problem.

    Please any help would be appreciated.
    public class BTree {
        /*
         1. You may not modify the public interface of this class.  You may however
         add any additional methods and/or field which you may require to aid you 
         in the completion of this assignment.
     
         2. You will have to design and implement a Node class.  The BTree should
         house Integer objects.
     
         3. You will notice that there are some overloaded methods, some of which work for Integer objects and some with primitive type int.  You have to find a way to implement the methods to work with both types.
         */
     
        class BTreeNode {
     
            boolean leaf = true;
            int numKeys = 1;
            int mOrder;
            Integer keys[];
            BTreeNode refs[];
     
            BTreeNode(int m) {
                mOrder = m;
                root = this;
                keys = new Integer[m - 1];
                refs = new BTreeNode[m];
                for (int i = 0; i < m; i++) {
                    refs[i] = null;
                }
                for (int i = 0; i < m - 1; i++) {
                    keys[i] = 0;
                }
     
            }
            public void setLeaf(){
                this.leaf = true;      
            }
    // Done
            public BTreeNode BTreeSearch(Integer key) {
                return BTreeSearch(key, root);
            }
    // Done
            protected BTreeNode BTreeSearch(Integer key, BTreeNode node) {
                if (node != null) {
                    int i = 1;
                    for (; i <= node.numKeys && node.keys[i - 1].compareTo(key) < 0; i++) {
                        for(int p = 0; p < node.numKeys - 1; p++)
                            if(node.keys[p] != 0)
                                search += "[" + node.keys[p] + "]";
                            else
                                search += "[]";
                        if (i > node.numKeys || node.keys[i - 1].compareTo(key) > 0) {
                            return BTreeSearch(key, node.refs[i - 1]);
                        } else {
                            return node;
                        }
                    }
     
                }
                return null;
            }
        }
     
        // BTree Vars
        BTreeNode root;
        String search;
    // Done
        public BTree(int m) {
            root = new BTreeNode(m);
            search = "";
        }
    // Done
        public boolean insertElement(int element) {
            Integer el = new Integer(element);
            return insertElement(el);
        }
    // Needs completion
        public boolean insertElement(Integer element) {
            BTreeNode temp = root;
     
            for(int i = 0; i < temp.mOrder-2; i++)
                if(temp.refs[i] != null)
                {
                    temp.leaf = true;
                    break;
                }
                else
                {
                    temp.leaf = false;
                    break;
                }
            System.out.println(temp.leaf);
     
            if (temp.numKeys == temp.mOrder) {
                // First insert elem into fake arr
                int tArr[] = new int[temp.mOrder]; 
                for(int i = 0; i < temp.numKeys-1; i++)
                    tArr[i] = temp.keys[i];
                    tArr[temp.mOrder-1] = element;
     
                // Sort the fake array    
                for(int i = 0; i < temp.mOrder-1; i++)
                    for(int j = i+1; j < temp.mOrder; j++)
                        if(tArr[i] > tArr[j])
                        {
                            int t = tArr[i];
                            tArr[i] = tArr[j];
                            tArr[j] = t;
                        }
                // Split into halfies
                int midKey = temp.mOrder / 2;
                // Root gets 1 elem - 2 leaf
                if(temp == root && temp.keys[3] != 0)
                {
                    for(int i = 0; i < temp.mOrder-1; i++)
                        temp.keys[i] = 0;
                    temp.keys[0] = tArr[midKey];
                    temp.numKeys = 2;
                    // HIER STEL EK LEAF TRUE EN AS JY DIT PRINT IS DIT TRUE
                    temp.setLeaf();
     
                    // Create new nodes vir root
                    BTreeNode left = new BTreeNode(temp.mOrder);
                    BTreeNode right = new BTreeNode(temp.mOrder);
                    temp.refs[0] = left;
                    temp.refs[1] = right;
     
                    left = new BTreeNode(temp.mOrder);
                    right = new BTreeNode(temp.mOrder);
                    left.leaf = false;
                    right.leaf = false;
                    for(int j = 0; j < midKey; j++)
                    {
                        left.keys[j] = tArr[j];
                        right.keys[j] = tArr[midKey+j];
                    }
                        return true;
                }
                // Create an extra leaf
                else
                {
     
                }
     
     
     
     
            //  
            } else {
                // Kyk eers of kan afgan
                // MICHELLE HIER MOET DIT KYK OF leaf TRUE IS OM AF TE GAN MAAR LEAF IS ALTYD FALSE
     
                while(temp.leaf)
                {
                    if(element < temp.keys[0])
                        temp = temp.refs[0];
                    else for(int i = 0; i < temp.numKeys-3; i++)
                        if(element > temp.keys[i] && element < temp.keys[i+1] || temp.keys[i+1] == 0)
                            temp = temp.refs[i+1];
                        else
                            temp = temp.refs[temp.mOrder-1];
     
     
                }
                // Dan plek insert
                temp.keys[temp.numKeys-1] = element;
                temp.numKeys++;
                for(int i = 0; i < temp.numKeys-2; i++)
                    for(int j = i+1; j < temp.numKeys-1; j++)
                        if(temp.keys[i] > temp.keys[j])
                        {
                            int t = temp.keys[i];
                            temp.keys[i] = temp.keys[j];
                            temp.keys[j] = t;
                        }                                                             
                return true; 
            }
            return false;
        }
    // Done
        public boolean deleteElement(int element) {
            Integer el = new Integer(element);
            return deleteElement(el);
        }
     
        public boolean deleteElement(Integer element) {
            /*
             The Integer object passed as a parameter should be deleted from
             your B-Tree.  The method should return true after a 
             successful delete, and false otherwise.
             */
     
            return false;
        }
    // Done
        public String search(int element) {
            BTreeNode temp = root;
            Integer t = new Integer(element);
            temp.BTreeSearch(t);
            return search;
        }
    // Done
        public String search(Integer element) {
            BTreeNode temp = root;
            temp.BTreeSearch(element);
            return search;
        }
     
        public int height() {
            return 0;
     
    	}
     
        public int fullness() {
            if(root == null)
                return 0;
            else
            {
     
            }
            return 0;
        }
     
        public String breadthFirst() {
            if(root == null)
                return "";
            else
            {
     
            }
            return "";
        }
    }
     
    public class Main
    {
    	/*Use this class to test your implementation.  This file will be overwritten for marking purposes.*/
     
    	public static void main(String[] args)
    	{
                BTree awe = new BTree(5);
               // Integer ins = new Integer(6);
               // awe.insertElement(ins);
                awe.insertElement(7);
                awe.insertElement(9);
                awe.insertElement(12);		
                awe.insertElement(8);
                awe.insertElement(2);
             //   awe.insertElement(15); 
             //   awe.insertElement(20);
             //   awe.insertElement(6); 
     
     
     
     
               for(int i = 0; i < 4; i++)
                    System.out.println(awe.root.keys[i]);
     
                //System.out.println(awe.search(9));
    	}
     
    }
    Last edited by a1mz; April 11th, 2014 at 09:35 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Java B-Tree Insert (no libraries)

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Binary Tree insert(int value) method
    By geeksutopia in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 28th, 2014, 10:37 AM
  2. Referencing libraries in java
    By pyler in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 30th, 2013, 03:08 PM
  3. java libraries
    By pettiville in forum The Cafe
    Replies: 1
    Last Post: June 28th, 2013, 06:35 PM
  4. Java Crypto Libraries
    By bidemiokus in forum Java Theory & Questions
    Replies: 1
    Last Post: April 14th, 2012, 06:37 AM
  5. The difference between two Java libraries
    By ice in forum Java Theory & Questions
    Replies: 1
    Last Post: January 11th, 2011, 10:34 AM

Tags for this Thread