class BinaryTreeNode {
private BinaryTreeNode left;
private BinaryTreeNode right;
private int data;
BinaryTreeNode(int d) {
data = d;
}
public void insertLeft(BinaryTreeNode n) {
this.left = n;
}
public void insertRight(BinaryTreeNode n) {
this.right = n;
}
public int height() {
// height = Max(Hl, Hr) + 1
int leftHeight = this.left != null ? left.height() : -1;
int rightHeight = this.right != null ? right.height() : -1;
return Math.max(leftHeight, rightHeight) + 1;
}
public String toString() {
String leftStr = this.left == null ? "" : this.left.toString();
String rightStr = this.right == null ? "" : this.right.toString();
return leftStr + " : " + data + " : " + rightStr;
}
public static void main(String[] s) {
BinaryTreeNode n = new BinaryTreeNode(0);// 0 l0
// / \
BinaryTreeNode l1 = new BinaryTreeNode(1);// 1 4 l1
// /
BinaryTreeNode l2 = new BinaryTreeNode(2);// 2 l2
// /
BinaryTreeNode l3 = new BinaryTreeNode(3);// 3 l3
l2.insertLeft(l3);
l1.insertLeft(l2);
n.insertLeft(l1);
n.insertRight(new BinaryTreeNode(4));
System.out.println(n.height());
System.out.println(isBalanced(n));
}
public static boolean isBalanced(BinaryTreeNode n) {
// if height = (|hl - hr|) <=1
int leftHeight = n.left != null ? n.left.height() : -1;
int rightHeight = n.right != null ? n.right.height() : -1;
return leftHeight - rightHeight <= 1;
}
}
|
||||
Instead of having a method to calculate the heights of the subtrees or a method to check whether the tree is balanced, I suggest just storing the depth of a node in that node. Then when a node is added or removed – which at most will change the subtree depth by 1 – just update the node depths of each parent node out to the root, short-circuiting when you've reached a point where the depth doesn't change. This should non-trivially reduce the runtime complexity of balancing the tree. This, of course, comes at the cost of a little more storage space. Then your algorithm for adding a node would be:
This should give you a complexity of Note: this solution also requires you to store a pointer for each node's parent node (for use in backing out of the tree), which you don't have in the code you originally posted. |
|||||||||||||||||
|
Your question is unclear, though I can tell that your code is broken. Your code here:
Should return true if the tree is balanced, but, it takes a node in, computes the height of the left and right sides, and compares the differences. This method is broken.... Just because the height of each side of the tree (left, and right) are similar, does not mean that each side is also balanced. Consider a tree like:
When you test the root, you will determine that both sides have the same height, yet, the tree is far from balanced. |
|||||||||||||||||
|
isBalanced
andheight
... although, I agree that it would be nice to see more about how the data is loaded. It is possible that this is a pre-cursor to creating an always-balanced tree. – rolfl Apr 28 at 13:25BinaryTreeNode
should also be added.... without it there's only a partial picture of what's happening. – rolfl Apr 28 at 13:27