/**
  * Internal method to remove from a subtree.
  *
  * @param x the item to remove.
  * @param t the node that roots the tree.
  * @return the new root.
  * @throws ItemNotFoundException if x is not found.
  */
 protected BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t) {
   if (t == null) throw new ItemNotFoundException(x.toString());
   if (x.compareTo(t.element) < 0) t.left = remove(x, t.left);
   else if (x.compareTo(t.element) > 0) t.right = remove(x, t.right);
   else if (t.left != null && t.right != null) // Two children
   {
     t.element = findMin(t.right).element;
     t.right = removeMin(t.right);
   } else t = (t.left != null) ? t.left : t.right;
   return t;
 }
 /**
  * Internal method to insert into a subtree.
  *
  * @param x the item to insert.
  * @param t the node that roots the tree.
  * @return the new root.
  * @throws DuplicateItemException if x is already present.
  */
 protected BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) {
   if (t == null) t = new BinaryNode<AnyType>(x);
   else if (x.compareTo(t.element) < 0) t.left = insert(x, t.left);
   else if (x.compareTo(t.element) > 0) t.right = insert(x, t.right);
   else throw new DuplicateItemException(x.toString()); // Duplicate
   return t;
 }
 /**
  * Recursive helper method to remove a value from the tree
  *
  * @param x the value to be removed
  * @param t the node to remove value from
  * @return the node with removed value
  */
 protected BinaryNode<Integer> remove(Integer x, BinaryNode<Integer> t) {
   if (t == null) {
     return t;
   }
   int compareResult = x.compareTo(t.element);
   if (compareResult < 0) {
     t.left = remove(x, t.left);
   } else if (compareResult > 0) {
     t.right = remove(x, t.right);
   } else if (t.left != null && t.right != null) {
     t.element = findMin(t.right).element;
     t.right = remove(t.element, t.right);
   } else {
     t = (t.left != null) ? t.left : t.right;
   }
   return t;
 }
Beispiel #4
0
  /**
   * Return a reference to a node that is the root of a duplicate of the binary tree rooted at the
   * current node.
   */
  public BinaryNode<AnyType> duplicate() {
    BinaryNode<AnyType> root = new BinaryNode<AnyType>(element, null, null);

    if (left != null) // If there's a left subtree
    root.left = left.duplicate(); // Duplicate; attach
    if (right != null) // If there's a right subtree
    root.right = right.duplicate(); // Duplicate; attach
    return root; // Return resulting tree
  }
Beispiel #5
0
 private BinaryNode singleRightRotation() {
   BinaryNode temp = this.left;
   if (temp != null) {
     this.left = temp.right;
   }
   temp.right = this;
   rotationCount++;
   return temp;
 }
  public BinaryNode add(BinaryNode current, int data) {
    if (current == null) {
      current = new BinaryNode(data);
    } else {
      if (data >= (Integer) current.data) current.right = add(current.right, data);
      else current.left = add(current.left, data);
    }

    return current;
  }
 /**
  * Internal method to insert into a subtree.
  *
  * @param x the item to insert.
  * @param t the node that roots the tree.
  * @return the new root.
  * @throws DuplicateItemException if x is already present.
  */
 protected BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) {
   if (t == null) t = new BinaryNode<AnyType>(x);
   else if (x.compareTo(t.element) < 0) t.left = insert(x, t.left);
   else if (x.compareTo(t.element) > 0) t.right = insert(x, t.right);
   else {
     t.increaseDuplicateCount();
     t.hasDuplicate = true;
     t.duplicate = duplicate(x, t.duplicate);
   }
   return t;
 }
 /**
  * Recursive helper method to insert a value in the tree
  *
  * @param x the value to be inserted
  * @param t the node to insert value in
  * @return the node with the inserted value
  */
 protected BinaryNode<Integer> insert(Integer x, BinaryNode<Integer> t) {
   if (t == null) {
     return new BinaryNode<Integer>(x, null, null);
   }
   int compareResults = x.compareTo((Integer) t.element);
   if (compareResults < 0) {
     t.left = insert(x, t.left);
   } else if (compareResults > 0) {
     t.right = insert(x, t.right);
   }
   return t;
 }
  public BinaryNode toTree(BinaryNode root, int[] array, int length, int counter) {
    if (counter == length) {
      counter = 0;
      return null;
    } else {
      BinaryNode node = new BinaryNode(array[counter]);

      if (array[counter] <= (Integer) root.data) {
        root.right = node;
        counter++;
        toTree(root.right, array, length, counter);
      } else {
        root.left = node;
        counter++;
        toTree(root.left, array, length, counter);
      }
    }

    return root;
  }
 static BinaryNode WithRightChild(BinaryNode k1) {
   BinaryNode k2 = k1.right;
   k1.right = k2.left;
   k2.left = k1;
   return k2;
 }
 static BinaryNode doubleWithRightChild(BinaryNode k1) {
   k1.right = WithLeftChild(k1.right);
   return WithRightChild(k1);
 }
 static BinaryNode WithLeftChild(BinaryNode k2) {
   BinaryNode k1 = k2.left;
   k2.left = k1.right;
   k1.right = k2;
   return k1;
 }
 public void InsertRight(BinaryNode current, BinaryNode insert) {
   insert.right = current.right;
   insert.left = current;
   current.right = insert;
 }