Reading — step 1 of 5
Read
~1 min readInsert & Search
Insert Algorithm
python
Two phases:
- Walk to find the insertion point at each level — same as search, but record the predecessor at each level in
update[]. - 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.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…