private void compactBranches(Branch node, int index, int nodeOffset) { if (index + 1 <= size) { Branch right = (Branch) getChild(nodeOffset + 2); Object rightKey = getChild(nodeOffset + 1); if (right.size() > capacity / 2) { node.append(rightKey, right.firstValue()); Object poppedKey = right.popKey(); setChild(nodeOffset + 1, poppedKey); } else { node.mergeFrom(rightKey, right); removeMergedNode(nodeOffset); } } else { Branch left = (Branch) getChild(nodeOffset - 2); Object nodeKey = getChild(nodeOffset - 1); if (left.size() > capacity / 2) { node.push(left.lastValue(), nodeKey); setChild(nodeOffset - 1, left.lastKey()); left.clearLast(); } else { left.mergeFrom(nodeKey, node); removeMergedNode(nodeOffset); } } }