Binary Tree Problem

This is a discussion on Binary Tree Problem within the C++ Programming forums, part of the General Programming Boards category; Can anyone see what is wrong with this function the returns the height of a binary tree Code: int op::tree_height(const ...

  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
    23,820
    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,354
    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, 01: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, 02:20 PM
  4. Tree Problem
    By recluse in forum C Programming
    Replies: 36
    Last Post: 12-04-2004, 02:06 PM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 09:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21