Created
May 18, 2017 17:10
-
-
Save lironsade/818caa7d57ca3b8bebb535ccbebf87ee to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* 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