Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:47:09+00:00 2026-06-17T15:47:09+00:00

I have a BST of three elements {1, 2, 3}. Its structure looks like

  • 0

I have a BST of three elements {1, 2, 3}. Its structure looks like

  2
 / \
1   3

Now I try to calculate the height for each node using BSTHeight() defined below and have some problem with calculating the height of ‘2’, which value is supposed to be 1 as the heights of ‘1’ and ‘3’ are defined as 0. My problem is that with direct use of heights from ‘2’s two children (see part 2 highlighted below), its height is ALWAYS 0. However, its value is correct if I use two temporary integer variables (see part 1 highlighted below). I couldn’t see any difference between the two approaches in terms of functionality. Can anyone help explain why?

void BSTHeight(bst_node *p_node)
{
    if (!p_node) 
        return;

    if (!p_node->p_lchild && !p_node->p_rchild) {
        p_node->height = 0;
    } else if (p_node->p_lchild && p_node->p_rchild) {
        BSTHeight(p_node->p_lchild);
        BSTHeight(p_node->p_rchild);
#if 0   // part 1
        int lchild_height = p_node->p_lchild->height;
        int rchild_height = p_node->p_rchild->height;
        p_node->height = 1 + ((lchild_height > rchild_height) ? lchild_height : rchild_height);
#else   // part 2
        p_node->height = 1 + ((p_node->p_lchild->height) > (p_node->p_rchild->height)) ? (p_node->p_lchild->height) : (p_node->p_rchild->height);
#endif
    } else if (!p_node->p_lchild) {
        BSTHeight(p_node->p_rchild);
        p_node->height = 1 + p_node->p_rchild->height;
    } else {
        BSTHeight(p_node->p_lchild);
        p_node->height = 1 + p_node->p_lchild->height;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:47:10+00:00Added an answer on June 17, 2026 at 3:47 pm

    Problem lies in operator precedence. Addition binds stronger than ternary operator, hence you must surround ternary operator (?:) with brackets.

    Below is the corrected version. Note that all brackets you used were superflous and I’ve removed them. I’ve added the only needed pair instead:

    1 + (p_node->p_lchild->height > p_node->p_rchild->height ?
         p_node->p_lchild->height : p_node->p_rchild->height);
    

    Even better would be to use std::max (from <algorithm>) instead:

    1 + std::max(p_node->p_lchild->height, p_node->p_rchild->height)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a BST in python, with each node holding 3 pieces of data.
Suppose I have a balanced BST (binary search tree). Each tree node contains a
I have been passed a date string that looks like this: Thu%20Mar%2011%202010%2015%3A09%3A11%20GMT%2B0000%20(BST) I want
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I'm trying to delete the node in BST which have two child nodes. For
I have a template class ( Node is an inner class within a BST).
I wanted to make a dictionary using BST but I did not have any
I have a Find function in order to find an element from a BST
i wana create a bst tree in c++ but i have a syntax error
I have the BST class same as in this thread BST.hpp template<class T> class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.