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:

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?
Ok, so the graph displays a measurement of the cost of inserting
nelements 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
fmight look like:Due to how logs work, we can collapse
log(1) + ... + log(n):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:
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:
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 tolog(n!)isn*log(n)(see the Wikipedia page). So we can go back to our bound:and get:
And now it’s should be easier to see that if you divide
f(n)byn, your graph will be bounded above by the shape of a logarithm.