Ejemplo n.º 1
0
 private BTNode getExtendedNode(BTNode fullNode) {
   BTNode newNode = new BTNode();
   newNode.number = t - 1;
   newNode.isLeaf = fullNode.isLeaf;
   for (int i = 0; i < t; i++) {
     if (i != t - 1) {
       newNode.AddKey(i, fullNode.getKey(t + i));
     }
     newNode.AddChildren(i, fullNode.getChildren(t + i));
   }
   return newNode;
 }
Ejemplo n.º 2
0
  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;
    }
  }