Question: Haskell Programming: Simply answer the question (Code) For Problems 4-7, we'll use this definition of binary trees that include values at the leafs and interior
For Problems 4-7, we'll use this definition of binary trees that include values at the leafs and interior nodes. Tree is polymorphic, so you can have a Tree Int or Tree Float or Tree (Int, Int). etc. data Tree a = Leaf a | Branch a (Tree a) (Tree a) deriving (Eq, Show) 7. 5 points] Now write preorder using a tai 7. 5points) Now write preorder using a tail-recursive helper function: preorder treelist should calculate the preorder traversal of the tree and prepend it to the list. E.g.. if t-(Branch 0left right) where left- (Leaf 1) and right- (Branch 2 (Leaf 3) (Leaf 4)),then the call preorder' t [5, 6] should return You still need two recursive calls: The first recursive call prepends the preorder traversal of the right subtree onto the given list; we would get [2, 3, 4, 5, 6]. The second recursive call would prepend the left subtree to this list and get [1, 2, 3,4, 5, 6. Adding on the branch's value 0 gives us [0, 1,2,3,4, 5, 61 In addition to preorder', define a top-level traversal function preorder2 t preorder' t
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
