Skip to content

Instantly share code, notes, and snippets.

@gtke
Created February 23, 2013 07:41
Show Gist options
  • Select an option

  • Save gtke/5018863 to your computer and use it in GitHub Desktop.

Select an option

Save gtke/5018863 to your computer and use it in GitHub Desktop.
AVL Tree Rotations
/*
* if BF is less than -1
* if right child - bf <=0
do left rotation
* if right child - bf > 0
do right-left rotation
* if BF is more than 1
* if left child - bf >=0
do right rotation
* if left child - bf < 0
do left-right rotation
*/
private Node<T> rotate(Node<T> n) {
updateHeightAndBF(n);
if(n.getBf() < -1){
if(n.getRight().getBf() <= 0){
return left(n);
}
if(n.getRight().getBf() > 0){
return rightLeft(n);
}
}
if(n.getBf() > 1){
if(n.getLeft().getBf() >= 0){
return right(n);
}
if(n.getLeft().getBf() < 0){
return leftRight(n);
}
}
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment