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 663579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:30:18+00:00 2026-05-13T23:30:18+00:00

assume this following function: int binaryTree::findHeight(node *n) { if (n == NULL) { return

  • 0

assume this following function:

int binaryTree::findHeight(node *n) {
    if (n == NULL) {
        return 0;
    } else {
        return 1 + max(findHeight(n->left), findHeight(n->right));
    }
}

Pretty standard recursive treeHeight function for a given binary search tree binaryTree. Now, I was helping a friend (he’s taking an algorithms course), and I ran into some weird issue with this function that I couldn’t 100% explain to him.

With max being defined as max(a,b) ((a)>(b)?(a):(b)) (which happens to be the max definition in windef.h), the recursive function freaks out (it runs something like n^n times where n is the tree height). This obviously makes checking the height of a tree with 3000 elements take very, very long.

However, if max is defined via templating, like std does it, everything is okay. So using std::max fixed his problem. I just want to know why.

Also, why does the countLeaves function work fine, using the same programmatic recursion?

int binaryTree::countLeaves(node *n) {
    if (n == NULL) {
        return 0;
    } else if (n->left == NULL && n->right == NULL) {
        return 1;
    } else {
        return countLeaves(n->left) + countLeaves(n->right);
    }
}

Is it because in returning the ternary function, the values a => countLeaves(n->left) and b => countLeaves(n->right) were recursively double called simply because they were the resultants?

Thank you!

The question was answered below

I just wanted to link some literature on the subject for future reference:
http://www.boostpro.com/tmpbook/preprocessor.html
http://msdn.microsoft.com/en-us/library/z3f89ch8.aspx

The main difference between the two implementations being:

#define max(i, j) (((i) > (j)) ? (i) : (j))

vs

template<class T> T max (T i, T j) { return ((i > j) ? i : j) }

Thank you all!

  • 1 1 Answer
  • 1 View
  • 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-13T23:30:18+00:00Added an answer on May 13, 2026 at 11:30 pm

    Macros are expanded by the preprocessor, before the compiler gets to see the code. This means that, for example, macro parameters might be evaluated more than once.

    With your macro, you’re going to end up with something akin to:

    int binaryTree::findHeight(node *n) {
        if (n == NULL) {
            return 0;
        } else {
            return 1 + (findHeight(n->left) > findHeight(n->right)) ? // call once...
                        findHeight(n->left) : findHeight(n->right); // and ouch
        }
    }
    

    As you can see, it’s going to evaluate both functions, then one more an additional time. This is why macros can be evil.

    You can disable the macro by defining NOMINMAX prior to including the Windows headers. Then use the function in <algorithm> instead.

    If he must use the macro, he’ll have to store the calculations in a variable:

    int binaryTree::findHeight(node *n) {
        if (n == NULL) {
            return 0;
        } else {
            const int leftHeight = findHeight(n->left);
            const int rightHeight = findHeight(n->right);
            return 1 + max(leftHeight, rightHeight);
        }
    }
    

    With a function, each call will be evaluated prior to calling the function. That is, it’s somewhat like the previous code block. It evaluates the function’s arguments, gets the results, then passes those into the std::max function. There are no repeated evaluations.

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

Sidebar

Related Questions

I assume this just returns an int. Is there anything else going on I
I have a static library. This library have the following function defined int WriteData(LPTSTR
Assume this code works: left '*' left '+' expr: expr '+' expr | expr
Let's assume this class in C#: public class LimitedList<T> : List<T> { private int
Assume I have the following function: // Precondition: foo is '0' or 'MAGIC_NUMBER_4711' //
Assume I have the following exemplary function: template <typename Fn> auto call(Fn fn) ->
Assume the following: private static boolean A() { int parsedUntil = 0; ... ...
I've been wondering about the following issue: assume I have a C style function
Consider the following simple function void foo_rt(int n) { for(int i=0; i<n; ++i) {
Assume I have the following string: Hellotoevryone<img height=115 width=150 alt= src=/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg />Iamsogladtoseeall. This

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.