private void rebalance(Node<K,V> unbalanced,boolean insert){
  for (Node<K,V> node=unbalanced; node != null; node=node.parent) {
    Node<K,V> left=node.left;
    Node<K,V> right=node.right;
    int leftHeight=left != null ? left.height : 0;
    int rightHeight=right != null ? right.height : 0;
    int delta=leftHeight - rightHeight;
    if (delta == -2) {
      Node<K,V> rightLeft=right.left;
      Node<K,V> rightRight=right.right;
      int rightRightHeight=rightRight != null ? rightRight.height : 0;
      int rightLeftHeight=rightLeft != null ? rightLeft.height : 0;
      int rightDelta=rightLeftHeight - rightRightHeight;
      if (rightDelta == -1 || (rightDelta == 0 && !insert)) {
        rotateLeft(node);
      }
 else {
        rotateRight(right);
        rotateLeft(node);
      }
      if (insert) {
        break;
      }
    }
 else     if (delta == 2) {
      Node<K,V> leftLeft=left.left;
      Node<K,V> leftRight=left.right;
      int leftRightHeight=leftRight != null ? leftRight.height : 0;
      int leftLeftHeight=leftLeft != null ? leftLeft.height : 0;
      int leftDelta=leftLeftHeight - leftRightHeight;
      if (leftDelta == 1 || (leftDelta == 0 && !insert)) {
        rotateRight(node);
      }
 else {
        rotateLeft(left);
        rotateRight(node);
      }
      if (insert) {
        break;
      }
    }
 else     if (delta == 0) {
      node.height=leftHeight + 1;
      if (insert) {
        break;
      }
    }
 else {
      node.height=Math.max(leftHeight,rightHeight) + 1;
      if (!insert) {
        break;
      }
    }
  }
}

private void rebalance ( Node < K , V > unbalanced , boolean insert ) { for ( Node < K , V > node = unbalanced ; node != null ; node = node . parent ) { Node < K , V > left = node . left ; Node < K , V > right = node . right ; int leftHeight = left != null ? left . height : 0 ; int rightHeight = right != null ? right . height : 0 ; int delta = leftHeight - rightHeight ; if ( delta == - 2 ) { Node < K , V > rightLeft = right . left ; Node < K , V > rightRight = right . right ; int rightRightHeight = rightRight != null ? rightRight . height : 0 ; int rightLeftHeight = rightLeft != null ? rightLeft . height : 0 ; int rightDelta = rightLeftHeight - rightRightHeight ; if ( rightDelta == - 1 || ( rightDelta == 0 && ! insert ) ) { rotateLeft ( node ) ; } else { rotateRight ( right ) ; rotateLeft ( node ) ; } if ( insert ) { break ; } } else if ( delta == 2 ) { Node < K , V > leftLeft = left . left ; Node < K , V > leftRight = left . right ; int leftRightHeight = leftRight != null ? leftRight . height : 0 ; int leftLeftHeight = leftLeft != null ? leftLeft . height : 0 ; int leftDelta = leftLeftHeight - leftRightHeight ; if ( leftDelta == 1 || ( leftDelta == 0 && ! insert ) ) { rotateRight ( node ) ; } else { rotateLeft ( left ) ; rotateRight ( node ) ; } if ( insert ) { break ; } } else if ( delta == 0 ) { node . height = leftHeight + 1 ; if ( insert ) { break ; } } else { node . height = Math . max ( leftHeight , rightHeight ) + 1 ; if ( ! insert ) { break ; } } } }
rebalances the tree by making any avl rotations necessary between the newly - unbalanced node and the tree ' s root .
