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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:47:31+00:00 2026-05-30T16:47:31+00:00

I implemented in Red-Black trees in Python according to the pseudo code in Cormen’s

  • 0

I implemented in Red-Black trees in Python according to the pseudo code in Cormen’s Introduction to Algorithms.

I wanted to see in my own eyes that my insert is really O(logn) so I plotted the time it takes to insert n=1, 10, 20, ..., 5000 nodes into the tree.

This is the result:

enter image description here

the x-axis is n and the y-axis is the time it took in milliseconds.

To me the graph looks more linear than logarithmic. What can explain that?

  • 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-30T16:47:33+00:00Added an answer on May 30, 2026 at 4:47 pm

    Ok, so the graph displays a measurement of the cost of inserting n elements into your tree, where the x axis is how many elements we’ve inserted, and the y axis is the total time.

    Let’s call the function that totals the time it takes to insert n elements into the tree f(n).

    Then we can get a rough idea of what f might look like:

    f(1) < k*log(1)                 for some constant k.
    
    f(2) < k*log(1) + k*log(2)      for some constant k
    
    ...
    
    f(n) < k * [log(1) + log(2) + ... + log(n)]   for some constant k.
    

    Due to how logs work, we can collapse log(1) + ... + log(n):

    f(n) < k * [log(1*2*3*...*n)]     for some constant k
    
    f(n) < k * log(n!)                for some constant k
    

    We can take a look at Wikipedia to see a graph of what log(n!) looks like. Take a look at the graph in the article. Should look pretty familiar to you. 🙂

    That is, I think you’ve done this by accident:

    for n in (5000, 50000, 500000):
        startTime = ...
        ## .. make a fresh tree
        ## insert n elements into the tree
        stopTime = ...
        ## record the tuple (n, stopTime - startTime) for plotting
    

    and plotted total time to construct the tree of size n, rather than the individual cost of inserting one element into a tree of size n:

    for n in range(50000):
        startTime = ...
        ## insert an element into the tree
        stopTime = ...
        ## record the tuple (n, stopTime - startTime) for plotting
    

    Chris Taylor notes in the comments that if you plot f(n)/n, you’ll see a log graph. That’s because a fairly tight approximation to log(n!) is n*log(n) (see the Wikipedia page). So we can go back to our bound:

    f(n) < k * log(n!)                for some constant k
    

    and get:

    f(n) < k * n * log(n)             for some constant k
    

    And now it’s should be easier to see that if you divide f(n) by n, your graph will be bounded above by the shape of a logarithm.

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

Sidebar

Related Questions

AFAIK, F# Map and set are implemented as red-black trees, so I guess that
Since C++ STL set/map are implemented as red-black trees, it should be possible to
I have a Red Black tree implemented in c++ . It supports the functionality
I am trying to implement a red-black tree in C#. I already created an
Guys I'm trying to implement deletion algorithm for a Red Black tree and I'm
I have been trying to create a red black tree that only implements an
I have just implemented these ToggleButtons using ControlTemplates that set the Image content according
In wikipedia: http://en.wikipedia.org/wiki/Red-black_tree#Applications_and_related_data_structures red-black tree is a type of self-balancing binary search tree, a
I have completed a red black tree in c and i find it hard
A friend of mine asked me to implement a blue and a red pointer

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.