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

  • SEARCH
  • Home
  • 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 6766005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:46:43+00:00 2026-05-26T14:46:43+00:00

here base->left is NULL , and base->right->height is 0 : ((((base->left ? base->left->height :

  • 0

here base->left is NULL, and base->right->height is 0:

((((base->left ? base->left->height : -1)) > ((base->right ? base->right->height : -1))) 
? 
((base->left ? base->left->height : -1) + 1) 
: 
((base->right ? base->right->height : -1) + 1))

IMO the result of the above expression should be 1,

but it turns out it’s 0 when I print it out.

Anyone knows the reason?

Is it a bug of gcc 4.3.2?

UPDATE

how the expression come?

#define MAX_PLUS_1(a, b) (((a) > (b)) ? (a + 1) : (b + 1))
#define BINARY_TREE_HEIGHT(node) (node ? node->height : -1)
#define BINARY_TREE_SYN_HEIGHT(left, right) \
    MAX_PLUS_1(\
        BINARY_TREE_HEIGHT(left),\
        BINARY_TREE_HEIGHT(right)\
    )

it’s really BINARY_TREE_SYN_HEIGHT(base->left, base->right)

And the problem is gone if I replace MAX_PLUS_1 with function:

int MAX_PLUS_1(int a, int b){
    return (((a) > (b)) ? (a + 1) : (b + 1));
}
  • 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-05-26T14:46:44+00:00Added an answer on May 26, 2026 at 2:46 pm

    From additional information in our Comments discussion, your height field is unsigned. That means that when you try to compare it to -1 you end up in trouble – (uint32_t)-1 == 0xFFFF_FFFF = Very Big Number, so the ‘left’ branch gets chosen over the ‘right’ branch at some point when you don’t expect it to.

    Writing MAX_PLUS_1() as a function solves, or perhaps rather hides, this problem because you use int for the parameters. That means the comparison is done between the height value and (int)-1, which is what you wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since Hex (base 16) uses 0-9A-F, and (I'm assuming here) Base 17 uses 0-9A-G
What do people here use C++ Abstract Base Class constructors for in the field?
here i need a batch file which can apply and create label or base
Here's a wierd one. I'm reusing a code base that unfortunately must not be
Which gets called first - the base constructor or other stuff here? public class
Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
I've heard people talking about base 64 encoding here and there. What is it
i need a Function Pointer from a base class. Here is the code: class
I have a question, here are two classes below: class Base{ public: virtual void
I can't seem to figure out what I am doing wrong here. I have

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.