data Tree a = Empty | Tree a (Tree a) (Tree a)
deriving (Show, Eq, Read)
I have this data Tree in my programm and i need a foldr funtion in instance Foldable Tree where. The Function should go first through pivot element then smaller tree and then bigger tree.
But i couldn't figure out how to go first with pivot element. I mean, how can i find the pivot Element in a tree in Haskell?
I used quicksort before, which use a pivot element to sort a list.(thats actually what i understood from quicksort) But i don't know how to search(or find) the pivot in a list or in a tree.
here is a example:
foldr (++) [] (Tree "m" (Tree "b" (Tree "a" Empty Empty) (Tree "g" Empty Empty)) (Tree "z" Empty Empty))
and the output should seem like this
"mbagz"
instance Foldable Tree
where
foldr _ b Empty = b
foldr op b (Tree val left right) = foldr op (foldr op (op val b) left) right
I did already implement a foldr function but it works not exactly what i want.
It depends on how you want to process the elements. This can be done in preorder, inorder or postorder.
For preorder we first process the element, and then recurse on the left and right subtree, so:
The type signatures here are however
(a -> a -> a) -> a -> Tree a -> a, it thus can only work if the result and the item of theTreehave the same type.Foldablerequires that the result can have a different type. We can however work with this with:For the same tree:
these then produce: