new to Haskell here. Trying to figure out how to make preorder traversal given various tree definitions. I`ve seen preorder for a tree defined as
data BB a = L | K a (BB a) (BB a) deriving Show
prefixCollect L = []
prefixCollect(K w l r) = w : prefixCollect l ++ prefixCollect r
However, I am not sure how to perform same operation if the tree is defined differently:
data BB a = L | K (BB a) a (BB a) deriving Show
?
Could please somebody help?
There is virtually no difference in how you would define the function; the difference in type definitions only changes how you would pattern-match against the argument.
prefixCollect L = []
-- K l w r instead of K w l r
prefixCollect (K l w r) = w : prefixCollect l ++ prefixCollect r
Both definitions define a tree in exactly the same way: as a node consisting of a value and two subtrees. The order in which the data constructor K
lists those three values doesn't really matter.