Skip to content

Instantly share code, notes, and snippets.

@y-yu
Created November 19, 2012 11:47
Show Gist options
  • Save y-yu/4110254 to your computer and use it in GitHub Desktop.
Save y-yu/4110254 to your computer and use it in GitHub Desktop.
3.ml
type 'a tree = Lf | Br of 'a * 'a tree * 'a tree;;
let rec height t =
match t with
Lf -> 1
| Br (_, t1, t2) ->
if (height t1) > (height t2) then
1 + (height t1)
else
1 + (height t2);;
let rec balance t =
match t with
Lf -> Lf
| Br (_, t1, t2) ->
Br ((height t1) - (height t2), balance t1, balance t2);;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment