Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Tree balance(Tree a)
- {
- if (a.getEmpty())
- {
- return a;
- }
- else if (a.getLeft().getEmpty() && a.getRight().getEmpty())
- {
- return a;
- }
- Tree leftSubTree = balance(a.getLeft());
- Tree rightSubTree = balance(a.getRight());
- Tree b = new Tree(a.getValue(), leftSubTree, rightSubTree);
- if (b.getLeft().getHeight() - b.getRight().getHeight() == 0 || b.getLeft().getHeight() - b.getRight().getHeight() == 1)
- {
- return b;
- }
- else if (b.getLeft().getHeight() - b.getRight().getHeight() >= 2)
- {
- return rotateRight(b);
- }
- else
- {
- return rotateLeft(b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement