Created
February 23, 2013 07:41
-
-
Save gtke/5018863 to your computer and use it in GitHub Desktop.
AVL Tree Rotations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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