Lean4
/-- O(log(m+n)). Build a tree from an element between two trees, without any
assumption on the relative sizes.
link {1, 2} 4 {5, 6} = {1, 2, 4, 5, 6}
link {1, 3} 2 {5} = precondition violation -/
def link (l : Ordnode α) (x : α) : Ordnode α → Ordnode α :=
match l with
| nil => insertMin x
| node ls ll lx lr => fun r ↦
match r with
| nil => insertMax l x
| node rs rl rx rr =>
if delta * ls < rs then balanceL (link ll x rl) rx rr
else if delta * rs < ls then balanceR ll lx (link lr x rr) else node' l x r