/** * Split function. Find the middle element 'mid'. And split the NodeArray using 'mid'. Copy the * split array into a new Node 'newNode' of type 'Btree'. Insert corresponding child elements with * it Update keycount and childcount * * @param i */ private void split(int i) { if ((childArray[i].keycount) % 2 != 0) { int x1 = childArray[i].keycount / 2; int x2 = childArray[i].childcount / 2; int mid = childArray[i].keyArray[childArray[i].keycount / 2]; // Split the node at mid int midv = childArray[i].valueArray[childArray[i].keycount / 2]; BTree splitNode = new BTree(max); // New node created to store split data for (int j = x1 + 1; j <= 2 * x1; j++) splitNode.keyArray[j - x1 - 1] = childArray[i].keyArray[j]; for (int j = x1 + 1; j <= 2 * x1; j++) splitNode.valueArray[j - x1 - 1] = childArray[i].valueArray[j]; for (int j = x2; j < 2 * x2; j++) splitNode.childArray[j - x2] = childArray[i].childArray[j]; // Copy it's respective childArray childArray[i].keycount = x1; // Update Values' count of original child childArray[i].childcount = x2; // Update it's child count splitNode.keycount = x1; // Set new node's value count splitNode.childcount = x2; // Set new node's child count int temp = scanKeyArray(mid); // Insert the 'mid' node into it's parent insertKey(temp, mid, midv); insertChild(i + 1, splitNode); } }
/** * Add the new key 'n' directly in the node without caring about the maximum limit. First scan the * NodeArray to find the position of the new node, then add the node in that place If the * NodeArray has children, then scan the child array to insert the new Node. * * @param n */ public void preInsert(int n, int v) { int in = scanKeyArray(n); // Scan array to find position to insert 'n' using scanArray function if (childcount > 0) // If node has a child, then insert in it's child node { childArray[in].preInsert(n, v); if (childArray[in].keycount > max) // Child is full so split split(in); } else insertKey(in, n, v); // No children so insert in node itself }