let rec inter s1 s2 =
if is_empty s1 then empty else
if is_empty s2 then empty 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 n1 in
let r2 = after s2 n2 in
let m = until (from s2 n1) n2 in
concat (concat (inter l1 l2) m) (inter r1 r2)