The pseudocode + example given for the initial implementation of 'insert' doesn't seem right, no 'below' connection is made between the two '13' nodes.
The way to fix this would be to let 'insert' return the node inserted at the given level. There already are a few returns in the 'insert' pseudocode right now, but it doesn't seem like anything is being returned right now.
Something like this:
-- Recursive skip list insertion function.
define insert(elem, root, height, level):
if right of root < elem:
return insert(elem, right of root, height, level)
else:
if level = 0:
new ← makenode elem
old ← right of root
right of root ← new
right of elem ← old
return new
else:
if level ≤ height:
new ← makenode elem
old ← right of root
right of root ← new
right of elem ← old
below new ← insert(elem, below root, height, level - 1)
return new
else:
return insert(elem, below root, height, level - 1)
Or more simply:
define insert(elem, root, height, level):
if right of root < elem:
return insert(elem, right of root, height, level)
elif level > height:
return insert(elem, below root, height, level - 1)
else:
new ← makenode elem
old ← right of root
right of root ← new
right of elem ← old
if level > 0:
below new ← insert(elem, below root, height, level - 1)
return new
The way to fix this would be to let 'insert' return the node inserted at the given level. There already are a few returns in the 'insert' pseudocode right now, but it doesn't seem like anything is being returned right now.
Something like this:
Or more simply: