Question: please help me with exercise using haskell Programming Component Please write Haskell code to solve the following problems and submit your code following the submission
please help me with exercise using haskell Programming Component Please write Haskell code to solve the following problems and submit your code following the submission guidelines. 1. Tree Operations During the lecture, we introduced a datatype definition for a binary tree that stores integers only at the leaves. data IntTree - Leaf Integer | Interior (IntTree, IntTree) deriving Show Please define the following functions that operate on IntTree. (a) Function treeSum takes an Int Tree as input, and returns the sum of all the integers associated with its leaf nodes. Examples: treeSum (Leaf 3) = 3 treeSum (Interior (Leaf 2, Leaf 3)) - 5 treeSum (Interior (Leaf 2, Interior (Leaf 1, Leaf 1))) - 4 (b) Function treeHeight takes an Int Tree as input, and returns the height of the tree. Examples: treeHeight (Leaf 3) - O treeHeight (Interior (Leaf 2, Leaf 3)) - 1 treeHeight (Interior (Leaf 2, Interior (Leaf 1, Leaf 1))) - 2 (c) Function balanced takes an Int Tree as input, and returns true if it is balanced and false otherwise. We say a binary tree rooted at a node is balanced if both subtrees are balanced and differ in height by at most one. Examples: balanced (Leaf 3) balanced (Interior (Leaf 2, Leaf 3)) = true balanced (Interior (Leaf 2, Interior (Leaf 1, Leaf 1))) = true balanced (Interior (Leaf 3, Interior (Leaf 2, Interior (Leaf 1, Leaf 1)))) = false true
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
