Last active
December 14, 2015 13:49
-
-
Save gtke/5096520 to your computer and use it in GitHub Desktop.
AVL Tree 4 rotation methods
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
| /** | |
| * Performs a left rotation on a node | |
| * | |
| * @param n The node to have the left rotation performed on | |
| * @return The new root of the subtree that is now balanced due to the rotation | |
| */ | |
| private Node<T> left(Node<T> n) { | |
| if(n != null){ | |
| Node<T> temp = n.getRight(); | |
| n.setRight(temp.getLeft()); | |
| temp.setLeft(n); | |
| return temp; | |
| } | |
| return n; | |
| } | |
| /** | |
| * Performs a right rotation on a node | |
| * | |
| * @param n The node to have the right rotation performed on | |
| * @return The new root of the subtree that is now balanced due to the rotation | |
| */ | |
| private Node<T> right(Node<T> n) { | |
| if(n != null){ | |
| Node<T> temp = n.getLeft(); | |
| n.setLeft(temp.getRight()); | |
| temp.setRight(n); | |
| return temp; | |
| } | |
| return n; | |
| } | |
| /** | |
| * Performs a left right rotation on a node | |
| * | |
| * @param n The node to have the left right rotation performed on | |
| * @return The new root of the subtree that is now balanced due to the rotation | |
| */ | |
| private Node<T> leftRight(Node<T> n) { | |
| n.setLeft(left(n.getLeft())); | |
| Node<T> temp = right(n); | |
| return temp; | |
| } | |
| /** | |
| * Performs a right left rotation on a node | |
| * | |
| * @param n The node to have the right left rotation performed on | |
| * @return The new root of the subtree that is now balanced due to the rotation | |
| */ | |
| private Node<T> rightLeft(Node<T> n) { | |
| n.setRight(right(n.getRight())); | |
| Node<T> temp = left(n); | |
| return temp; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment