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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:34:48+00:00 2026-06-13T00:34:48+00:00

Related question: Time Complexity of InOrder Tree Traversal of Binary Tree O(N)? , however

  • 0

Related question: Time Complexity of InOrder Tree Traversal of Binary Tree O(N)?, however it is based on a traversal via recursion (so in O(log N) space) while iterators allow a consumption of only O(1) space.

In C++, there normally is a requirement that incrementing an iterator of a standard container be a O(1) operation. With most containers it’s trivially proved, however with map and such, it seems a little more difficult.

  • If a map were implemented as a skip-list, then the result would be obvious
  • However they are often implemented as red-black trees (or at least as binary search trees)

So, during an in-order traversal there are moments where the “next” value is not so easily reached. For example should you be pointing at the bottom-right leaf of the left subtree, then the next node to traverse is the root, which is depth steps away.

I have tried “proving” that the algorithmic complexity (in terms of “steps”) was amortized O(1), which seems alright. However I don’t have the demonstration down yet.

Here is a small diagram I traced for a tree with a depth of 4, the numbers (in the place of the nodes) represent the number of steps to go from that node to the next one during an in-order traversal:

       3
   2       2
 1   1   1   1
1 2 1 3 1 2 1 4

Note: the right-most leaf has a cost of 4 in case this would be a sub-tree of a larger tree.

The sum is 28, for a total number of nodes of 15: thus a cost less than 2 per node, in average, which (if it holds up) would be a nice amortized cost. So:

  • During in-order traversal, is incrementing the iterator really O(1) for a balanced (and full) binary search tree ?
  • May the result be extended to cover non-full binary search trees ?
  • 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-13T00:34:50+00:00Added an answer on June 13, 2026 at 12:34 am

    Yes, the amortized cost is indeed O(1) per iteration, for a any tree.

    The proof is based on the number of times you “visit” each node.

    Leaves are visited only once.
    None leaves are visited at most 3 times:

    1. when going from the parent to the node itself.
    2. when coming back from the left subtree
    3. when coming back from the right subtree

    There are no more visits to any nodes, thus if we sum the number of visits of each node, we get a number that is smaller then 3n, so the total number of visits of all nodes combined is O(n), which gives us O(1) per step amortized.

    (Note since in a full tree there are n/2 leaves, we are getting the 2n you were encountering, I believe one can show that the sum of visits will be smaller then 2n for any tree, but this “optimization” is out of scope here IMO).


    The worst case per step is O(h), which is O(logn) in a balanced tree, but might be O(n) in some cases.


    P.S. I have no idea how Red-Black trees are implemented in C++, but if your tree data structure contains a parent field from each node, it can replace the recursive stack and allow O(1) space consumption. (This is of course “cheating” because storing n such fields is O(n) itself).

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

Sidebar

Related Questions

I asked another SOAP related question some time ago, which asks for a working
Some time ago i posted a question related to a WriteableBitmap memory leak, and
My question is related to the design-time support of WPF. From MSDN I read,
this time i have a simple question: I have one table, related with himself
Couple very basic time complexity related questions here: What is the time complexity of
There's a very related question: Create List<CustomObject> from List<string> however it doesn't deal with
Related question: why does Git send whole repository each time push origin master The
I have java related question... Website www.stationv3.com gets updated daily (most of the time
Related question [stackoverflow] here . I'm trying to do the above, but I want
Related question, about assignment-initialization-declaration. $ javac MatchTest.java MatchTest.java:7: ')' expected for((int i=-1 && String

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.