let rec union s1 s2 =
if is_empty s1 then s2 else
if is_empty s2 then s1 else
let s1, s2 = if height s1 > height s2 then s1, s2 else s2, s1 in
let n1, n2 = root s1 in
let l1 = left_branch s1 in
let r1 = right_branch s1 in
let l2 = before s2 ~n:n1 in
let r2 = after s2 n2 in
let n1, l =
if n1 = min_int then n1, empty else
let l = union l1 l2 in
if is_empty l then n1, l else
let (v1, v2), l' = split_rightmost l in
if v2 + 1 = n1 then v1, l' else n1, l in
let n2, r =
if n1 = max_int then n2, empty else
let r = union r1 r2 in
if is_empty r then n2, r else
let (v1, v2), r' = split_leftmost r in
if n2 + 1 = v1 then v2, r' else n2, r in
make_tree l (n1, n2) r