Esempio n. 1
0
 // Should be called after calling getExtendedNode()
 private void processFullNode(BTNode fullNode) {
   fullNode.number = t - 1;
   for (int i = t - 1; i >= 0; i--) {
     fullNode.removeKey(t + i - 1);
     fullNode.removeChildren(t + i);
   }
 }
Esempio n. 2
0
 public BinaryTree(BTNode root, BinaryTree left, BinaryTree right) {
   this.root = root;
   root.setLeft(left.root);
   this.left = left;
   root.setRight(right.root);
   this.right = right;
 }
Esempio n. 3
0
  /** Prints the tree in order from left to right. */
  public void inOrderPrint() {
    if (left != null) {
      left.inOrderPrint();
    } // end left

    System.out.println(data);

    if (right != null) {
      right.inOrderPrint();
    } // end right
  } // end inOrderPrint
Esempio n. 4
0
 public E getRightMostData() {
   if (right == null) {
     return data;
   } else {
     return right.getRightMostData();
   } // end right == null if-else
 } // end getRightMostData
Esempio n. 5
0
 public BTNode<E> removeRightmost() {
   if (right == null) {
     return left;
   } else {
     right = right.removeRightmost();
     return this;
   } // end if right == null if=-else
 } // end removeRightMost
Esempio n. 6
0
  @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 BTNode getHead() {
    BTNode node1 = new BTNode(1);
    BTNode node2 = new BTNode(2);
    BTNode node3 = new BTNode(3);
    BTNode node4 = new BTNode(4);
    BTNode node5 = new BTNode(5);
    BTNode node6 = new BTNode(6);
    BTNode node7 = new BTNode(7);

    node1.leftNode = node2;
    node1.rightNode = node3;
    node2.leftNode = node4;
    node2.rightNode = node5;
    node3.leftNode = node6;
    node3.rightNode = node7;

    return node1;
  }
Esempio n. 8
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;
 }
 // Function to execute this node.
 public void step() throws IncorrectNodeException {
   super.step();
 }
Esempio n. 10
0
 public void setRight(BinaryTree right) {
   root.setRight(right.root);
   this.right = right;
 }
Esempio n. 11
0
 public void setLeft(BinaryTree left) {
   root.setLeft(left.root);
   this.left = left;
 }
Esempio n. 12
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;
    }
  }
Esempio n. 13
0
 private void addElemToNode(BTNode node, E element, int i) {
   node.AddKey(i, element);
   node.number++;
   node.AddChildren(i, NullBTNode);
 }
Esempio n. 14
0
 // Generate the root node
 private void constructRoot(E elem) {
   root = new BTNode();
   root.number = 1;
   root.AddKey(0, elem);
   root.isLeaf = false;
 }