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
    22,265
    Well, could it be that you got the names wrong? left_child and right_sibling seem rather inconsistent to me.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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, 06: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

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