CS 70

Binary Heap INSERT

  • LHS Cow speaking

    Who wants to try inserting into a binary heap?

  • Sheep speaking

    I will!

  • Dog speaking

    A binary sheep for a binary heap! I love it!

  • Sheep speaking

    Uh… It hasn't come up before, but actually I'm non-binary. My name is Mead (from meadow), and most sheep use they/them pronouns, because we prefer to see ourselves as a representative of our flock.

  • Dog speaking

    All good with me. A non-binary sheep for a binary heap is just as awesome!

  • Robot speaking

    Nothing wrong with being binary, though. Beep boop.

  • LHS Cow speaking

    Great! Take it away, Mead!

  • RHS Cow speaking

    Here's our heap so far:

INSERT — Step by Step

  • LHS Cow speaking

    Okay, now say we need to insert a new item with key 10. Where can we put it in order to maintain the structure property? (i.e., that it's a complete tree)

  • Sheep speaking

    I guess right at the end of the bottom layer?

  • LHS Cow speaking

    Sure, let's try that!

  • LHS Cow speaking

    Now, of course, we need to fix the order property! What should we do?

  • Sheep speaking

    Maybe swap with its parent?

  • LHS Cow speaking

    Makes sense!

  • Sheep speaking

    …and then keep swapping until it's high enough.

  • LHS Cow speaking

    Makes sense to me!

  • LHS Cow speaking

    Is the tree complete?

  • Sheep speaking

    Yup!

  • LHS Cow speaking

    Does it satisfy the order property?

  • Sheep speaking

    Yup!

  • LHS Cow speaking

    Then it worked! Good job!

INSERT Algorithm

So here's the INSERT algorithm we just developed:

  • Insert at the end of the last layer of the tree (which is the next available spot in the array).
  • Swap with the parent until the parent has a smaller priority.

Exercise

Start with the heap pictured above (after we inserted 10) and, on paper, insert 20. (You can just draw the tree alone without the array representation; the array representation is easiest for a computer in its memory but the tree is easier for us to think about.)

Then write down the level-order traversal of the resulting tree after inserting 20 (this corresponds to the contents of the array).

List the keys (separated by a single space) in level-order after inserting 20.

INSERT Complexity

Step 1: Insert the Item at the End of the Last Layer

What is the complexity of adding the new item to the bottom of the tree?

Remember that we encode our heap as an array, which makes it easy to know where the new node will go.

If there are currently \( n \) items in the heap, then the index for the new item is just \( n \).

We can add something to an array in \( \Theta(1) \) time!

If we need a dynamically resizing array that can grow and shrink, it would be amortized \( \Theta(1) \) but worst-case \( \Theta(n) \).

Step 2: Swap with Parent Until the Parent Has Smaller Priority

We know we can do a single swap in\( \Theta(1) \) time. So the real question is how many swaps do we need to do?

What is the best-case complexity of bubbling the node up the tree?

What is the worst-case complexity of bubbling the node up the tree?

In the best case, we don't need to do any swaps (just compare to the parent once): \( \Theta(1) \) time.

In the worst case, we need to bubble all the way to the root! So how tall is the tree?

We figured out a long time ago that a balanced tree has \( \Theta( \log{n} ) \) height.

So in the worst case, we swap all the way to the top: \( \Theta( \log{n} ) \) time.

Overall complexity of insert

So, what is the most accurate and precise characterization of the complexity of INSERT?

In the best case, insert takes \( \Theta(1) + \Theta(1) = \Theta(1) \) time.

In the worst case, insert takes \( \Theta(1) + \Theta( \log{n} ) = \Theta( \log{n} ) \) time.

So, the best we can say is that insert takes \( \mathrm{O}( \log{n} ) \) time—no worse than logarithmic time, but it could be faster (depending on the value we're inserting and the heap we're inserting into).

(When logged in, completion status appears here.)