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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:16:36+00:00 2026-05-20T16:16:36+00:00

What is the time complexity of the implementation of a binary tree traversal below?

  • 0

What is the time complexity of the implementation of a binary tree traversal below?

void Tree::nonRecInOrder()
{
    // nonrecursive inOrder Traversal using Stack
    Stack< TreeNode* > s ; // declare and initialize stack
    TreeNode* currentNode = root ;

    while( true )
    {
        while( currentNode )
        {
            // move down leftChild fields
            s.add( currentNode ) ;
            currentNode = currentNode->leftChild ;
        }

         if( ! s.isEmpty() ) // stack is not empty
         {
             currentNode = *s.del( currentNode ) ; // delete from stack
             cout << currentNode->data ;
             currentNode = currentNode->rightChild ;
         }
         else
         {
            break ;
         }
    }    
}

Could you also explain how to calculate the complexity?

  • 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-20T16:16:36+00:00Added an answer on May 20, 2026 at 4:16 pm

    One way to characterize the big-O complexity of a difficult function is to think about some resource that it accesses and then to bound the number of times that that resource is accessed. In this case, when you’re doing a tree traversal, you might think about the number of times each node is pushed or popped from the stack. The reason this is a good bound is that all the hard work of this function – the inner loop descending down through a chain of nodes and the outer loop processing the topmost on the stack – can be bounded by the number of times a node is pushed onto the stack or popped from the stack. This is because the outermost loop terminates when the stack is empty, so it can’t run more times than the stack has something pushed onto it, and the innermost loop does work proportional to the number of times something is pushed onto the stack over the course of the loop.

    So let’s see how we can bound these quantities. The first question is how many times each node can be added to the stack. Well, a node is only added to the stack if it or one of its ancestors along a left-only path is the current node when the loop starts executing. How many times can this happen? My claim is that this occurs at most once. The proof of this is an induction based on the depth of the node in the tree. We use the observation that a node is only chosen again as the current node if it is the direct right child of a node in the stack. As a base case of the induction, if the node is the root (it’s at depth zero), then it can’t be chosen a second time because it has no parent. For the inductive step, if it’s true that no node at depth d can be chosen as the current node twice, then no node at depth d + 1 can be chosen twice because nodes at depth d + 1 are chosen only if their parents are chosen again, but by the inductive assumption we know this not to be true. Consequently, we have that no node is ever chosen as the current node twice. We follow this up with a simple observation that no node that’s a left child can ever be the current node at the start of the loop, since the current node is either the root (not a left child) or was the right child of some node. This claim, compounded with the fact that no node is visited twice, means that a node is only added to the queue at most once, which happens when its highest left ancestor becomes the current node.

    This also gives us a bound on the number of dequeues that are possible, since the number of dequeues can’t exceed the number of enqueues. Since each node is enqueued at most once, it’s also dequeued at most once. To finish things off, we know that the complexity of the whole function is bounded by the number of enqueues and dequeues performed, and so the complexity is O(n), where n is the number of nodes.

    Whew! That was not fun to analyze. I like the recursive version a lot more. 🙂

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

Sidebar

Related Questions

Can someone tell me the time complexity for the following code? #include<iostream> #include<string.h> using
What is the time complexity of the following operations in java.util.TreeSet ? first() last()
What is the time complexity of iterating a hash map in average case? I
EDIT: What time complexity has algorithm implemented in this assembly ? .file a.c .section
Is there any way to know exact time complexity for .NET predefined methods. Like
I'm currently developing a game engine in C++ using various pieces of middleware including
How can I merge 2 given Skip lists (each with n keys) into a
I have a simple problem: to collect objects into a list and traverse this
An interesting question I found, asked that an NxN matrix be rotated, in-place by

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.