Ejemplo n.º 1
0
    boolean delete(Object x, int parentIndex) {
      int i = childToInsertAt(x, true);
      int priorParentIndex = parentIndex;
      BTreeNode temp = this;
      if (i != -1) {
        do {
          if (temp.entries[i] == null || temp.entries[i].child == null) return false;
          temp = temp.entries[i].child;
          priorParentIndex = parentIndex;
          parentIndex = i;
          i = temp.childToInsertAt(x, true);
        } while (i != -1);
      } // Now temp contains element to delete and temp's parentIndex is parentIndex

      if (temp.isLeaf()) { // If leaf and have more than MIN elements, simply delete
        if (temp.nrElements > MIN) {
          temp.deleteElement(x);
          BTreeSet.this.size--;
          return true;
        }

        // else - If leaf and have less than MIN elements, than prepare the BTreeSet for deletion
        temp.prepareForDeletion(parentIndex);
        temp.deleteElement(x);
        BTreeSet.this.size--;
        temp.fixAfterDeletion(priorParentIndex);
        return true;
      }

      // else - Only delete at leaf so first switch with successor than delete
      temp.switchWithSuccessor(x);
      parentIndex = temp.childToInsertAt(x, false) + 1;
      return temp.entries[parentIndex].child.delete(x, parentIndex);
    }
Ejemplo n.º 2
0
    private Object nextElement() {
      if (currentNode.isLeaf()) {
        if (index < currentNode.nrElements) return currentNode.entries[index++].element;
        else if (!parentIndex
            .empty()) { // All elements have been returned, return successor of lastReturned if it
          // exists
          currentNode = currentNode.parent;
          index = ((Integer) parentIndex.pop()).intValue();

          while (index == currentNode.nrElements) {
            if (parentIndex.empty()) break;
            currentNode = currentNode.parent;
            index = ((Integer) parentIndex.pop()).intValue();
          }

          if (index == currentNode.nrElements)
            return null; // Reached root and he has no more children
          return currentNode.entries[index++].element;
        } else { // Your a leaf and the root
          if (index == currentNode.nrElements) return null;
          return currentNode.entries[index++].element;
        }
      }

      // else - You're not a leaf so simply find and return the successor of lastReturned
      currentNode = currentNode.entries[index].child;
      parentIndex.push(Integer.valueOf(index));

      while (currentNode.entries[0].child != null) {
        currentNode = currentNode.entries[0].child;
        parentIndex.push(Integer.valueOf(0));
      }

      index = 1;
      return currentNode.entries[0].element;
    }