Idris-dev/examples/btree.idr
Jan de Muijnck-Hughes 17c7240825 Tutorial changes.
+ Tutorial is now developed in a separate github repo.
+ Moved examples to top-level
+ Added binary of tutorial for v0.9.10
2013-11-28 11:40:27 +00:00

19 lines
482 B
Idris

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