Question: Haskell Programming (Not any other Language): Simply answer the question (Code) For Problems 4 - 7, we'll use this definition of binary trees that include

Haskell Programming (Not any other Language): Simply answer the question (Code)

Haskell Programming (Not any other Language): Simply answer the question (Code) For

Problems 4 - 7, we'll use this definition of binary trees thatinclude values at the leafs and interior nodes. Tree is polymorphic, so

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) 6. [5 points] Write a preorder traversal function for trees. E.g., if t is the tree (Branch 0 (Leaf 1) (Branch 2 (Leaf 3) (Leaf 4))) then preordert [0, 1, 2, 3, 4]. Write your function so that it uses recursion in the usual way: For a branch, recursively calculate the preorder traversals of the left and right subtrees and join them together (prepending the node value). 7. 5 points] Now write preorder using a tail-recursive helper function: preorder tree list should calculate the preorder traversal of the tree and prepend it to the list. E.g., if t(Branch 0 left right) where left (Leaf 1) and right- (Branch 2 (Leaf 3) (Leaf 4)), then the call preorder' t [5, 6] should return [0, 1, 2, 3,4,5,6] 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,6] 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!