Skip to content

Instantly share code, notes, and snippets.

@lironsade
Created May 18, 2017 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lironsade/818caa7d57ca3b8bebb535ccbebf87ee to your computer and use it in GitHub Desktop.
Save lironsade/818caa7d57ca3b8bebb535ccbebf87ee to your computer and use it in GitHub Desktop.
/**
* Balances the tree according to the case
*
* @param pointer node to balance
*/
private void balanceTree(Node pointer) {
int balanceFactor = balanceFactor(pointer);
if (balanceFactor == HEAVY_LEFT) {
if (balanceFactor(pointer.getLeft()) == LEFT_THEN_RIGHT) {
// LR
rotateLeft(pointer.getLeft());
}
// LL
rotateRight(pointer);
}
if (balanceFactor == HEAVY_RIGHT) {
if (balanceFactor(pointer.getRight()) == RIGHT_THEN_LEFT) {
// RL
rotateRight(pointer.getRight());
}
// RR
rotateLeft(pointer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment