Skip to content

Instantly share code, notes, and snippets.

@VienosNotes
Created November 19, 2012 12:08
Show Gist options
  • Select an option

  • Save VienosNotes/4110334 to your computer and use it in GitHub Desktop.

Select an option

Save VienosNotes/4110334 to your computer and use it in GitHub Desktop.
let rec balance (t:'a tree) : int tree =
let rec height t =
match t with
| Br(_,left,right) ->
let (hl, hr) = (height left, height right) in
if (hl > hr) then hl + 1 else hr + 1
| _ -> 0
in
match t with
| Br(value, left, right) -> Br(height left - height right, balance left, balance right)
| Lf -> Lf
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment