views:

383

answers:

6

Hey I was wondering if anybody could help me rework this method to find the height of a binary search tree. So far my code looks like this however the answer im getting is larger than the actual height by 1, but when I remove the +1 from my return statements its less than the actual height by 1? I'm still trying to wrap my head around recursion with these BST any help would be much appreciated.

public int findHeight(){
    if(this.isEmpty()){
        return 0;
    }
    else{
        TreeNode<T> node = root;
        return findHeight(node);
    }
}
private int findHeight(TreeNode<T> aNode){
    int heightLeft = 0;
    int heightRight = 0;
    if(aNode.left!=null)
        heightLeft = findHeight(aNode.left);
    if(aNode.right!=null)
        heightRight = findHeight(aNode.right);
    if(heightLeft > heightRight){
        return heightLeft+1;
    }
    else{
        return heightRight+1;
    }
}
A: 

Here's a concise and hopefully correct way to express it:

  private int findHeight(TreeNode<T> aNode){
    if(aNode == null || (aNode.left == null && aNode.right == null))
      return 0;
    return Math.max(findHeight(aNode.left), findHeight(aNode.right)) + 1;
  }

If the current node is null, there's no tree. If both children are, there's a single layer, which means 0 height. This uses the definition of height (mentioned by Stephen) as # of layers - 1

Matthew Flaschen
A: 

the code looks fine, it's probably an error in the test harness.

BlessedKey
+1  A: 

IMO, you code would benefit from being simplified a bit. Rather than attempting to end the recursion when a child pointer is null, only end it when the current pointer is null. That makes the code a lot simpler to write. In pseudo-code it looks something like this:

if (node = null)
    return 0;
else
    left = height(node->left);
    right = height(node->right);
    return 1 + max(left, right);
Jerry Coffin
Can't call a method on a null object, though :)
jemfinch
@jemfinch, where is he calling it on a null object, isn't that what the base case is for?
Bruce
@jemfinch:I guess it's a good thing I didn't suggest doing such a thing!
Jerry Coffin
+1 for giving some extra information. @jemfinch: Huh?
Dykam
A: 

The height of a binary search tree is equal to number of layers - 1.

See the diagram at http://en.wikipedia.org/wiki/Binary_tree

Your recursion is good, so just subtract one at the root level.

Also note, you can clean up the function a bit by handling null nodes:

int findHeight(node) {
  if (node == null) return 0;
  return 1 + max(findHeight(node.left), findHeight(node.right));
}
Stephen
My first attempt at this method I used something along these lines however I kept getting a StackOverFlow exception for some reason when I ran my code? Im assuming because I check for pointers pointing to null?
mike
(Removed comment about c++, which doesn't apply). It's likely that your "node == null" wasn't terminating properly.
Stephen
Stephen, shouldn't it be 0 for a single-node tree? This returns 1.
Matthew Flaschen
@Matthew: You're right, but I had suggested that his public function subtract one from the result. Instead, you could "fix" the recursive function by returning -1 in the base case.
Stephen
A: 
jemfinch
This code works, and is clearer than what I had however, it is still returning the height +1. In my book, height is defined as the length of the path from the root to its deepest leaf. So from my understanding a BST containing 15, 25, 30, 45 (in this order) would have a height of only 3 correct?
mike
Actually, a tree with only the root node has a height of 0 not 1.
Bruce
jemfinch
+1  A: 

The problem lies in your base case. "The height of a tree is the length of the path from the root to the deepest node in the tree. A (rooted) tree with only a node (the root) has a height of zero." - Wikipedia If there is no node you want to return -1 not 0 because you are adding 1 at the end so if there isn't a node you return -1 which cancels out the +1.

int findHeight(TreeNode<T> aNode)
{
    if(aNode == 0)
        return -1;

    left = findHeight(aNode.left);
    right = findHeight(aNode.right);

    if(left > right)
        return left + 1;
    else
        return right +1
}
Bruce
Yes this works correctly without having to subtract 1 in my public method. I am still confused as to how this method works with recursion. I declared ints left and right after the if statement, but I dont understand how they are incremented through this method
mike
This method works by subtracting 1 at the base case, they are incremented like every other method given, when you go deeper into the tree you add 1 to the height.
Bruce