Пример #1
0
 /*
  * Returns the parent tree
  */
 public AVLNode<E> get_parent_helper(AVLNode<E> currentNode, AVLNode<E> root, AVLNode<E> x) {
   if (currentNode == null) return null;
   // System.out.println("current " + currentNode.getElement() + " and x " + x.getElement() );
   // System.out.println(".equls is " + currentNode.getElement().equals(x.getElement()));
   if (currentNode.getElement().equals(x.getElement())) {
     return root;
   }
   AVLNode<E> value = get_parent_helper(currentNode.getLeft(), currentNode, x);
   if (value == null) return get_parent_helper(currentNode.getRight(), currentNode, x);
   return value;
 }
Пример #2
0
  public AVLNode<E> find(E element) {

    AVLNode<E> temp = rootAbove.getLeft();

    while (temp != null) {
      if (temp.getElement().equals(element)) return temp;

      int balance = element.compareTo(temp.getElement());
      temp = (balance < 0) ? temp.getLeft() : temp.getRight();
    }

    return null;
  }
Пример #3
0
  /**
   * @param element: The element to search for in the AVLTree
   * @return boolean: true if the element is found, false otherwise
   *     <p>The contains method simply traverses the binary search tree based on element's relation
   *     to the AVLNodes in the Tree until a match is found or it hits the bottom of the Tree.
   */
  public boolean contains(E element) {

    AVLNode<E> temp = rootAbove.getLeft();

    while (temp != null) {
      if (temp.getElement().equals(element)) return true;

      int balance = element.compareTo(temp.getElement());
      temp = (balance < 0) ? temp.getLeft() : temp.getRight();
    }

    return false;
  }
Пример #4
0
 /*
  * Return the parent tree
  */
 public AVLNode<E> get_parent(AVLNode<E> x) {
   if (rootAbove.getLeft() == null) {
     return null;
   } else if (x == null) {
     return null;
   } else if (rootAbove.getLeft().getElement().equals(x.getElement())) {
     return null;
   }
   AVLNode<E> value = get_parent_helper(rootAbove.getLeft(), rootAbove.getLeft(), x);
   if (value == null) return get_parent_helper(rootAbove.getRight(), rootAbove.getLeft(), x);
   return value;
 }
Пример #5
0
  /**
   * @param element: The element to insert into the Tree
   * @param temp: The AVLNode to evaluate for recursive insertion
   *     <p>This method recursively traverses the Tree, inserting the element at the appropriate
   *     spot and incrementing the balance factors for the subtrees as it evaluates. The Tree will
   *     then recursively rebalance as necessary.
   */
  private void insert(E element, AVLNode<E> temp) {
    if (this.rootAbove.getLeft() == null) {
      this.rootAbove.setLeftNode(new AVLNode<E>(element));
      return;
    }

    // travel left or right based on the
    // comparison of element to temp.element
    // remember that left means that element <= temp.element
    // and right means element > temp.element
    int compare = element.compareTo(temp.getElement());

    // travel to the left of the Tree, inserting
    // if the bottom has been reached
    if (compare <= 0) {

      // System.out.println(temp.getLeft());
      if (temp.getLeft() == null) {
        temp.setLeft(element);
        return;
      }

      insert(element, temp.getLeft());
    }

    // otherwise, travelling to the right of the Tree,
    // inserting if the bottom has been reached
    else {

      if (temp.getRight() == null) {
        temp.setRight(element);
        return;
      }

      insert(element, temp.getRight());
    }

    // if the root is being evaluated it, rotate if necessary
    if (temp == rootAbove.getLeft()) {
      rotate(rootAbove.getLeft(), rootAbove);
    }

    // otherwise, rotate the left and right subtrees
    // as necessary
    if (temp.getLeft() != null) {
      rotate(temp.getLeft(), temp);
    }

    if (temp.getRight() != null) {
      rotate(temp.getRight(), temp);
    }
  } // end insert
Пример #6
0
  /**
   * @param element: The element to remove from the AVLTree
   * @param temp: The root node of the subtree
   *     <p>This method recursively traverses the AVLTree based on the ordering of the element with
   *     respect to the Tree's elements. If the element is not found, then nothing happens.
   *     Otherwise, the element is removed, and either the far-right element on its left child or
   *     the far left element on its right child replaces it. *
   */
  private void remove(E element, AVLNode<E> temp) {
    if (temp == null) return;

    int compare = 0;

    if (temp != rootAbove) compare = element.compareTo(temp.getElement());

    boolean direction = (compare > 0 && temp != rootAbove);

    AVLNode<E> child = direction ? temp.getRight() : temp.getLeft();

    if (child == null) return;

    // if the root is perfectly balanced, slide the left Node up
    // and reinsert the left.right element if necessary
    if (temp == rootAbove && child.getBalance() == 0 && child.getElement().equals(element)) {

      AVLNode<E> newRoot = child.getLeft();

      if (newRoot == null) {
        rootAbove.setLeftNode(null);
        return;
      } else {
        enactRemoval(temp, child, false);
        return;
      }
    }

    // if the element is found and the root is not
    // perfectly balanced, remove it using enactRemoval()
    else if (element.compareTo(child.getElement()) == 0) {
      enactRemoval(temp, child, direction);
    }

    // otherwise, recursively traverse the tree
    else {
      remove(element, child);
    }
  }
Пример #7
0
  /**
   * Update the path from node down to the node containing element. Since it is guaranteed there is
   * a path between these two arguments, node should never become null.
   *
   * @param node the root of some subtree, originally the root of the subtree whose node was
   *     removed.
   * @param element the element stored at the end of the path to be updated. This element was the
   *     parent to the value t hat supplied the replacement value for a node during a remove()
   *     operation.
   */
  private AVLNode<E> updatePath(AVLNode<E> node, E element) {
    int compareResult = element.compareTo(node.getElement());

    if (compareResult == 0) { // reached the end of the path
      node = fixSubtreeRootedAt(node);
    } else if (compareResult < 0) {
      node.leftChild = updatePath((AVLNode<E>) node.leftChild, element);
      node = fixSubtreeRootedAt(node);
    } else if (compareResult > 0) {
      node.rightChild = updatePath((AVLNode<E>) node.rightChild, element);
      node = fixSubtreeRootedAt(node);
    }
    return node;
  }
Пример #8
0
  public AVLNode<E> get_left_neighbor(AVLNode<E> x) {
    AVLNode<E> left_child = x.getLeft();
    AVLNode<E> ret = left_child;
    if (left_child == null) {
      if (get_parent(x) != null && get_parent(x).getElement().compareTo(x.getElement()) == -1) {
        return get_parent(x);
      } else {
        return null;
      }
    }
    AVLNode<E> right = left_child.getRight();

    while (right != null) {
      ret = right;
      right = right.getRight();
    }

    return ret;
  }