Thread: Binary Tree Problem

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    27

    Binary Tree Problem

    Can anyone see what is wrong with this function the returns the height of a binary tree

    Code:
    int op::tree_height(const BNODE *mt)
    {
    
    
    if(mt == NULL) return 0;
    int left = 0;
    int right = 0;
    
    if(mt -> left_child != NULL)
    {
      left = tree_height(mt -> left_child);
    }
    
    if(mt -> right_sibling != NULL)
    {
      right = tree_height(mt -> right_child);
    }
    
    if(left > right) return 1 + left;
    return 1 + right;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Well, could it be that you got the names wrong? left_child and right_sibling seem rather inconsistent to me.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    Well, could it be that you got the names wrong? left_child and right_sibling seem rather inconsistent to me.
    Maybe just a typo. Ignoring that, the function is correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. binary tree problem
    By spank in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:27 AM
  3. problem in storing data in a binary search tree
    By alavardi in forum C Programming
    Replies: 5
    Last Post: 02-13-2005, 03:20 PM
  4. Tree Problem
    By recluse in forum C Programming
    Replies: 36
    Last Post: 12-04-2004, 03:06 PM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM