I defined my own Data Type BinTree
, which describes my binary trees:
data BinTree a = Empty | Node a (BinTree a) (BinTree a) deriving (Show,Eq)
After that I implemented three sort-functions for the binary trees: preorder
, inorder
and postorder
:
preorder :: BinTree a -> [a]
preorder Empty = []
preorder (Node x lt rt) = [x] ++ preorder lt ++ preorder rt
inorder :: BinTree a -> [a]
inorder Empty = []
inorder (Node x lt rt) = inorder lt ++ [x] ++ inorder rt
postorder :: BinTree a -> [a]
postorder Empty = []
postorder (Node x lt rt) = postorder lt ++ postorder rt ++ [x]
To improve my order-functions, I implemented the foldTree
function (which works as a normal foldr
function, but with binary trees):
foldTree :: (a -> b -> b -> b) -> b -> BinTree -> b
foldTree f e Empty = e
foldTree f e (Node x lt rt) = f x (foldTree f e lt) (foldTree f e rt)
And now I got stuck, because I cant't figure out how to combine the order-functions with the foldTree
.
Can someone give me a hint please?