CS 70

Time Complexity

  • LHS Cow speaking

    Since lookup and insert are our most important operations, let's examine their complexities.

On the last page we saw that the height of the tree can vary a lot, depending on the order of insertion.

  • The shortest possible tree (perfectly balanced) has \( \Theta( \log{n} ) \) height. This is the best-case height.
  • The tallest possible tree (a stick) has \( \Theta( n ) \) height. This is the worst-case height.

Now think back to our lookup procedure.

Complexity of Lookup

Best-Case Height

For a perfectly balanced tree, what is the most accurate and precise way to characterize the complexity of lookup?

The best-case tree shape is perfectly balanced, with \( \Theta( \log{n} ) \) height.

That's still not quite enough information to count operations, though! It also depends on how far down the tree we have to search.

In the worst-case lookup, we have to search all the way to the bottom of tree. That would mean \( \Theta( \mathit{height} ) = \Theta( \log{n} ) \) operations.

On the other hand, in a best-case lookup, our procedure might find the requested key right away (it could be at the root!). That would only be \( \Theta(1) \) operations.

So, we see that the complexity of lookup for a perfectly balanced tree would be no worse than logarithmic, but it could be constant time.

So the most accurate and precise characterization overall would be \( \mathrm{O}( \log{n} ) \).

Worst-Case Height

For a stick, what is the most accurate and precise way to characterize the complexity of lookup?

The worst-case tree shape is a stick, with \( \Theta(n) \) height.

The height alone is not quite enough information to count operations, though! It also depends on how far down the tree we have to search.

In the worst-case lookup, we have to search all the way to the bottom of tree. That would mean \( \Theta(n) \) operations.

On the other hand, in a best case lookup, our procedure might find the requested key right away! That would only be \( \Theta(1) \) operations.

So, we see that the complexity of lookup for a perfectly balanced tree would be no worse than linear, but it could be better.

So the most accurate and precise characterization would be \( \mathrm{O}(n) \).

What Can We Say Overall?

So, if we had to characterize the complexity of BST lookup overall, what would the most accurate and precise way to characterize the complexity of lookup be?

We know that, in the worst of the worst cases, for a terrible tree height and an unlucky choice of key, lookup can take linear time.

We also know that for a lucky key or for a nice tree, it can take less time.

So, the best we could say is that lookup takes \( \mathrm{O}(n) \) time.

Complexity of Insert and Delete

How about insert? Remember that the first step of insert is to find a place for the new key!

So, if we had to characterize the complexity of BST insert overall, what would the most accurate and precise way to characterize the complexity of insert be?

The first step of insert is essentially the same steps as lookup to find the insertion position, which we know takes \( \mathrm{O}(n) \) time (i.e., at most linear time).

The second step is just some simple pointer manipulation, which can be done in \( \Theta(1) \) time.

So, overall, we'd have to say that insert takes \( \mathrm{O}(n) \) time.

Delete has essentially the same analysis. First we have to find the node to delete, then we have to do some pointer manipulation. So similarly we would say that delete has \( \mathrm{O}(n) \) time complexity.

  • Hedgehog speaking

    This is terrible! I thought we invented BSTs to get away from linear-time lookup/insert!

  • LHS Cow speaking

    Yes, we did. The linear-time worst case is definitely bad news. But all hope is not lost!

  • Hedgehog speaking

    How so?

  • LHS Cow speaking

    Well, we discovered that we can have log-time worst case if the tree is balanced.

  • RHS Cow speaking

    So, all we have to do is figure out how to keep the tree balanced!

  • LHS Cow speaking

    We'll spend the next few lessons discussing clever balancing methods for BSTs.

  • Dog speaking

    That sounds so cool! I can hardly wait!!!

  • LHS Cow speaking

    Okay, great! But first let's wrap up our discussion of basic BSTs.

  • Hedgehog speaking

    Can we stop with the complexity stuff now? I'm getting a headache!

  • LHS Cow speaking

    Yes, we're done with that for now. We'll move on to a slightly different topic.

  • RHS Cow speaking

    Let's head up to the parent page to see our progress and what's next!

(When logged in, completion status appears here.)