Skip to content
Lesson 4 of 8

Step 1 of 5 · Reading · ~1 min

Read

Insert & Search

Insert Algorithm

python

Two phases:

  1. Walk to find the insertion point at each level — same as search, but record the predecessor at each level in update[].
  2. Splice the new node in by swapping forward pointers, level by level.

The random_level() function flips coins to decide how tall the new node is. With p=0.5: ~50% are level 0, ~25% level 1, ~12% level 2, etc. Expected average pointers per node: 2.

If duplicate keys are allowed: append (with same key, after existing matches). For unique-key set, replace value if found.

Up nextDelete AlgorithmDelete & Iterate

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…