The structure

Level 0: all nodes linked. Level 1: ~half. Level 2: ~quarter. Level k: ~1/(2^k).

Advertisement

Insert with random level

int randomLevel() {
  int lvl = 0;
  while (Math.random() < 0.5 && lvl < MAX_LEVEL) lvl++;
  return lvl;
}
void insert(int val) {
  int lvl = randomLevel();
  Node newNode = new Node(val, lvl + 1);
  Node cur = head;
  for (int i = lvl; i >= 0; i--) {
    while (cur.next[i] != null && cur.next[i].val < val) cur = cur.next[i];
    newNode.next[i] = cur.next[i];
    cur.next[i] = newNode;
  }
}
Advertisement

Search

Start at highest level. Move right while next < target. Drop down. Continue. O(log N) expected.

vs Balanced BST

Same complexity, simpler code, no rebalancing. Downside: not deterministic O(log N) (probabilistic).