I have the class Bintree that defines a binary tree. I need to implement a method that returns the maximum displacement of the nodes of the tree. So returns a pair of integers; the first of which is the height of t, the second is the maximum imbalance of the subtrees of t. With 'imbalance of a tree' I mean:
imbalance of a tree = absolute value of the difference between the height of the left subtree and the height of the right subtree.
I created the private inner class IntPair to contain the two integers. I know that the method is a recursive method, so I wrote the base case and I think it's correct. Instead I miss the recursive step... What I wrote is wrong. How do I find the maximum value?
public class BinTreeUtil {
protected static class IntPair {
int height;
int maxUnbal;
IntPair(int h, int u) {
height = h;
maxUnbal = u;
}
}
public static int maxUnbalance(BinTree t) {
return heightUnbalance(t).maxUnbal;
}
private static IntPair heightUnbalance(BinTree t) {
if(t == null)
return new IntPair(-1, 0);
else {
int sbil = Math.abs(height(t.left) + height(t.right));
return new IntPair(height(t), ???);
}
}
}
Thanks.