@Override public String toString() { if (root == null) return "NULL"; StringBuilder sb = new StringBuilder(); LinkedList<BTNode> queue = new LinkedList<BTNode>(); queue.push(root); BTNode tem = null; while ((tem = queue.poll()) != null) { for (BTNode node : tem.children) { if (!node.isNull()) queue.offer(node); } sb.append(tem.toString() + "\n"); } return sb.toString(); }
public void insertElem(E elem) { if (root == null) { // The first node constructRoot(elem); root.isLeaf = true; root.AddChildren(0, NullBTNode); root.AddChildren(1, NullBTNode); return; } BTNode curNode = root; if (root.isFull()) { // Extend the root constructRoot(curNode.getKey(t - 1)); // Get new node BTNode newNode = getExtendedNode(curNode); // Process old full node processFullNode(curNode); // Process root root.AddChildren(0, curNode); root.AddChildren(1, newNode); return; } int i = 0; BTNode childNode = null; // Find the node to insert while (true) { while ((i < curNode.getSize()) && (elem.compareTo(curNode.getKey(i)) > 0)) { i++; } childNode = curNode.getChildren(i); if (childNode.isFull()) { // Split the node // Add the element to parent curNode.number++; curNode.AddKey(i, childNode.getKey(t - 1)); // New node for extension BTNode newNode = getExtendedNode(childNode); // Process old full node processFullNode(childNode); // Add the new node for parent reference curNode.AddChildren(i + 1, newNode); // Down to low layer if (elem.compareTo(curNode.getKey(i)) < 0) { curNode = childNode; } else { curNode = newNode; } i = 0; continue; } // Down to child node if (!childNode.isNull()) { curNode = childNode; i = 0; continue; } // Insert the element in current node addElemToNode(curNode, elem, i); return; } }