예제 #1
0
 private void updateHeight(AVLTreeNode<E> node) {
   if (node.left == null && node.right == null) node.height = 0;
   else if (node.left == null) node.height = 1 + ((AVLTreeNode<E>) (node.right)).height;
   else if (node.right == null) node.height = 1 + ((AVLTreeNode<E>) (node.left)).height;
   else
     node.height =
         1
             + Math.max(
                 ((AVLTreeNode<E>) (node.right)).height, ((AVLTreeNode<E>) (node.left)).height);
 }
예제 #2
0
  // RR rotation
  private void rotateRR() {
    AVLTreeNode Me = this;
    AVLTreeNode Left = this.LeftNode;

    // switch 'Me' and 'Left' and attach 'Me' to the right connector of 'Left'
    if (Me.Parent != null) Me.Parent.updateMyConnector(Me, Left);

    Left.Parent = Me.Parent;
    if (Left.Parent == null) this.tree.root = Left;
    Me.Parent = Left;
    Me.LeftNode = null;

    if (Left.RightNode != null) {
      // we need to drop a node attached to the place where 'Me' is going to be attached
      // so we don't lose it. We're going to attach it to 'Me' instead
      //
      // Before: Element attached to 'Left'
      // Going to be: Element attached to 'Me' which is attached to 'Left'
      // print("[DEBUG] Dropping right node " + Left.RightNode.Name + " caught by " + Me.Name)
      Left.RightNode.Parent = Me;
      Me.LeftNode = Left.RightNode;
    }
    // finally attach 'Me'
    Left.RightNode = Me;
  }
예제 #3
0
 /** @param value */
 @Override
 public void add(V value) {
   if (root_node == null) {
     root_node = new AVLTreeNode(null, value);
   } else {
     ((AVLTreeNode) root_node).add(value);
     // make sure its still root
     while (root_node.getParentNode() != null) {
       root_node = root_node.getParentNode();
     }
   }
 }
예제 #4
0
  // LL rotation (same as for RR but mirrored)
  private void rotateLL() {
    AVLTreeNode Me = this;
    AVLTreeNode Right = this.RightNode;

    if (Me.Parent != null) Me.Parent.updateMyConnector(Me, Right);

    Right.Parent = Me.Parent;
    if (Right.Parent == null) this.tree.root = Right;
    Me.Parent = Right;
    Me.RightNode = null;

    if (Right.LeftNode != null) {
      // print("[DEBUG] Dropping left node " + Right.LeftNode.Name + " caught by " + Me.Name)
      Right.LeftNode.Parent = Me;
      Me.RightNode = Right.LeftNode;
    }
    Right.LeftNode = Me;
  }
예제 #5
0
    private AVLTreeNode singleRotateRight(AVLTreeNode node) {
      AVLTreeNode parent_node = (AVLTreeNode) node.getParentNode();
      AVLTreeNode swap_node = (AVLTreeNode) node.getRightChild();
      node.setRightChild(swap_node.getLeftChild());
      swap_node.setLeftChild(node);
      node.setDepth(max(node.getLeftChildDepth(), (node.getRightChildDepth() + 1)));
      swap_node.setDepth(max(swap_node.getRightChildDepth(), (node.getDepth() + 1)));

      if (parent_node == null) {
        swap_node.setParentNode(parent_node);
      } else {
        if (parent_node.getLeftChild() == node) {
          parent_node.setLeftChild(swap_node);
        } else {
          parent_node.setRightChild(swap_node);
        }
      }

      return swap_node;
    }
예제 #6
0
 private AVLTreeNode doubleRotateLeft(AVLTreeNode node) {
   node.setLeftChild(singleRotateRight((AVLTreeNode) node.getLeftChild()));
   return singleRotateLeft(node);
 }