type 'a tree
= | Node
of 'a
* int * 'a tree
* 'a tree
| Leaf
let avlInsert x tree =
let rotate_right (Node (x, _, Node (y, _, yleft, yright), xright)) =
Node (y, 0, yleft, Node (x, 0, yright, xright))
in
let rotate_left (Node (x, _, xleft, Node (y, _, yleft, yright))) =
Node (y, 0, Node (x, 0, xleft, yleft), yright)
in
let rotate_leftright (Node (x, _,
Node (z, _, zleft,
Node (y, ybal, yleft, yright)),
xright)) =
Node (y, 0, Node (z, (if ybal > 0 then -1 else 0), zleft, yleft),
Node (x, (if ybal < 0 then 1 else 0), yright, xright))
in
let rotate_rightleft (Node (x, _, xleft,
Node (z, _,
Node (y, ybal, yleft, yright),
zright))) =
Node (y, 0, Node (x, (if ybal > 0 then -1 else 0), xleft, yleft),
Node (z, (if ybal < 0 then 1 else 0), yright, zright))
in
let rec avlInsertAux x tree =
match tree with
| Leaf -> (Node (x, 0, Leaf, Leaf), 1)
| Node (y, bal, left, right) ->
if x < y then
let (left_bal, delta_height) = avlInsertAux x left
in
if delta_height = 0 then
(Node (y, bal, left_bal, right), 0)
else
let new_bal = bal - delta_height
in
match new_bal with
| 0 -> (Node (y, new_bal, left_bal, right), 0)
| -1 -> (Node (y, new_bal, left_bal, right), 1)
| _ ->
let Node (_, bal_l, _, _) = left_bal
in
if bal_l > 0 then
(rotate_leftright (Node (y, new_bal, left_bal, right)), 0)
else
(rotate_right (Node (y, new_bal, left_bal, right)), 0)
else
let (right_bal, delta_height) = avlInsertAux x right
in
if delta_height = 0 then
(Node (y, bal, left, right_bal), 0)
else
let new_bal = bal + delta_height
in
match new_bal with
| 0 -> (Node (y, new_bal, left, right_bal), 0)
| 1 -> (Node (y, new_bal, left, right_bal), 1)
| _ ->
let Node (_, bal_r, _, _) = right_bal
in
if bal_r < 0 then
(rotate_rightleft (Node (y, new_bal, left, right_bal)), 0)
else
(rotate_left (Node (y, new_bal, left, right_bal)), 0)
in
fst (avlInsertAux x tree
)
let printTree tree =
let rec printTreeAux tree level =
match tree with
| Leaf -> ()
| Node (x, bal, left, right) ->
printTreeAux right (level + 1);
printTreeAux left (level + 1)
in
printTreeAux tree 0
let () =
Leaf
|> avlInsert 10 |> avlInsert 20 |> avlInsert 30 |> avlInsert 40
|> avlInsert 27 |> avlInsert 26 |> avlInsert 25 |> avlInsert 28
|> avlInsert 33 |> avlInsert 34 |> avlInsert 35 |> avlInsert 31
|> avlInsert 32 |> printTree
dHlwZSAnYSB0cmVlID0gfCBOb2RlIG9mICdhICogaW50ICogJ2EgdHJlZSAqICdhIHRyZWUgfCBMZWFmCgpsZXQgYXZsSW5zZXJ0IHggdHJlZSA9CiAgbGV0IHJvdGF0ZV9yaWdodCAoTm9kZSAoeCwgXywgTm9kZSAoeSwgXywgeWxlZnQsIHlyaWdodCksIHhyaWdodCkpID0KICAgIE5vZGUgKHksIDAsIHlsZWZ0LCBOb2RlICh4LCAwLCB5cmlnaHQsIHhyaWdodCkpCiAgaW4KICBsZXQgcm90YXRlX2xlZnQgKE5vZGUgKHgsIF8sIHhsZWZ0LCBOb2RlICh5LCBfLCB5bGVmdCwgeXJpZ2h0KSkpID0KICAgIE5vZGUgKHksIDAsIE5vZGUgKHgsIDAsIHhsZWZ0LCB5bGVmdCksIHlyaWdodCkKICBpbgogIGxldCByb3RhdGVfbGVmdHJpZ2h0IChOb2RlICh4LCBfLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBOb2RlICh6LCBfLCB6bGVmdCwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgTm9kZSAoeSwgeWJhbCwgeWxlZnQsIHlyaWdodCkpLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB4cmlnaHQpKSA9CiAgICBOb2RlICh5LCAwLCBOb2RlICh6LCAoaWYgeWJhbCA+IDAgdGhlbiAtMSBlbHNlIDApLCB6bGVmdCwgeWxlZnQpLAogICAgICAgICAgICAgICAgTm9kZSAoeCwgKGlmIHliYWwgPCAwIHRoZW4gMSBlbHNlIDApLCB5cmlnaHQsIHh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hbCwgcmlnaHQpLCAwKQogICAgICAgICAgICAgICAgfCAtMSAtPiAoTm9kZSAoeSwgbmV3X2JhbCwgbGVmdF9iYWwsIHJpZ2h0KSwgMSkKICAgICAgICAgICAgICAgIHwgXyAtPgogICAgICAgICAgICAgICAgICBsZXQgTm9kZSAoXywgYmFsX2wsIF8sIF8pID0gbGVmdF9iYWwKICAgICAgICAgICAgICAgICAgaW4KICAgICAgICAgICAgICAgICAgICBpZiBiYWxfbCA+IDAgdGhlbgogICAgICAgICAgICAgICAgICAgICAgKHJvdGF0ZV9sZWZ0cmlnaHQgKE5vZGUgKHksIG5ld19iYWwsIGxlZnRfYmFsLCByaWdodCkpLCAwKQogICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgIChyb3RhdGVfcmlnaHQgKE5vZGUgKHksIG5ld19iYWwsIGxlZnRfYmFsLCByaWdodCkpLCAwKQogICAgICAgIGVsc2UKICAgICAgICAgIGxldCAocmlnaHRfYmFsLCBkZWx0YV9oZWlnaHQpID0gYXZsSW5zZXJ0QXV4IHggcmlnaHQKICAgICAgICAgIGluCiAgICAgICAgICAgIGlmIGRlbHRhX2hlaWdodCA9IDAgdGhlbgogICAgICAgICAgICAgIChOb2RlICh5LCBiYWwsIGxlZnQsIHJpZ2h0X2JhbCksIDApCiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICBsZXQgbmV3X2JhbCA9IGJhbCArIGRlbHRhX2hlaWdodAogICAgICAgICAgICAgIGluCiAgICAgICAgICAgICAgICBtYXRjaCBuZXdfYmFsIHdpdGgKICAgICAgICAgICAgICAgIHwgMCAtPiAoTm9kZSAoeSwgbmV3X2JhbCwgbGVmdCwgcmlnaHRfYmFsKSwgMCkKICAgICAgICAgICAgICAgIHwgMSAtPiAoTm9kZSAoeSwgbmV3X2JhbCwgbGVmdCwgcmlnaHRfYmFsKSwgMSkKICAgICAgICAgICAgICAgIHwgXyAtPgogICAgICAgICAgICAgICAgICBsZXQgTm9k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