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)); } }