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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:00:43+00:00 2026-05-25T22:00:43+00:00

Assuming the tree is balanced, how much stack space will the routine use for

  • 0

Assuming the tree is balanced, how much stack space will the routine use for a tree of 1,000,000 elements?

void printTree(const Node *node) {
  char buffer[1000];
  if(node) {
    printTree(node->left);
    getNodeAsString(node, buffer);
    puts(buffer);
    printTree(node->right);
  }
}

This was one of the algo questions in “The Pragmatic Programmer” where the answer was 21 buffers needed (lg(1m) ~= 20 and with the additional 1 at very top)

But I am thinking that it requires more than 1 buffer at levels lower than top level, due to the 2 calls to itself for left and right node. Is there something I missed?

*Sorry, but this is really not a homework. Don’t see this on the booksite’s errata.

  • 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-25T22:00:43+00:00Added an answer on May 25, 2026 at 10:00 pm

    First the left node call is made, then that call returns (and so its stack is available for re-use), then there’s a bit of work, then the right node call is made.

    So it’s true that there are two buffers at the next level down, but those two buffers are required consecutively, not concurrently. So you only need to count one buffer in the high-water-mark stack usage. What matters is how deep the function recurses, not how many times in total the function is called.

    This assuming of course that the code is written in a language similar to C, and that the C implementation uses a stack for automatic variables (I’ve yet to see one that doesn’t), blah blah.

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

Sidebar

Related Questions

Assuming a complete binary tree, each node can be adressed with the position it
Assuming I have a tree structure UL --LI ---INPUT (checkbox) And I want to
Assuming you can't use LINQ for whatever reason, is it a better practice to
lets say i have a tree of the following structure: <div>node level1 <div>node level2
Assuming you have a DOM tree with nested tags, I would like to clean
Assuming a binary search tree, I would like to return an error in case
I have a N-Ary non sorted in any way tree and each node can
Say I have a B-Tree with nodes in a 3-4 configuration (3 elements and
Given a tree, how to find the centre node in the tree so that
I have a node tree built out of Node objects. They are more complex

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.