Idris-dev/examples/btree.idr

19 lines
482 B
Idris
Raw Normal View History

2012-01-07 03:20:03 +04:00
module btree
data BTree a = Leaf
2012-01-07 03:20:03 +04:00
| Node (BTree a) a (BTree a)
2012-01-07 03:20:03 +04:00
insert : Ord a => a -> BTree a -> BTree a
insert x Leaf = Node Leaf x Leaf
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))
2012-01-07 03:20:03 +04:00
toList : BTree a -> List a
toList Leaf = []
toList (Node l v r) = btree.toList l ++ (v :: btree.toList r)
2012-01-07 03:20:03 +04:00
toTree : Ord a => List a -> BTree a
toTree [] = Leaf
toTree (x :: xs) = insert x (toTree xs)