2012-01-07 03:20:03 +04:00
|
|
|
module btree
|
2011-12-31 21:51:44 +04:00
|
|
|
|
|
|
|
data BTree a = Leaf
|
2012-01-07 03:20:03 +04:00
|
|
|
| Node (BTree a) a (BTree a)
|
2011-12-31 21:51:44 +04:00
|
|
|
|
2012-01-07 03:20:03 +04:00
|
|
|
insert : Ord a => a -> BTree a -> BTree a
|
|
|
|
insert x Leaf = Node Leaf x Leaf
|
2011-12-31 21:51:44 +04:00
|
|
|
insert x (Node l v r) = if (x < v) then (Node (insert x l) v r)
|
2012-01-07 03:20:03 +04:00
|
|
|
else (Node l v (insert x r))
|
2011-12-31 21:51:44 +04:00
|
|
|
|
2012-01-07 03:20:03 +04:00
|
|
|
toList : BTree a -> List a
|
|
|
|
toList Leaf = []
|
2013-09-10 17:54:36 +04:00
|
|
|
toList (Node l v r) = btree.toList l ++ (v :: btree.toList r)
|
2011-12-31 21:51:44 +04:00
|
|
|
|
2012-01-07 03:20:03 +04:00
|
|
|
toTree : Ord a => List a -> BTree a
|
|
|
|
toTree [] = Leaf
|
|
|
|
toTree (x :: xs) = insert x (toTree xs)
|
2011-12-31 21:51:44 +04:00
|
|
|
|