Question: -- Description: This Haskell script contains the definition of a function -- named balance that converts a non-empty list into a balanced -- binary tree

-- Description: This Haskell script contains the definition of a function -- named balance that converts a non-empty list into a balanced -- binary tree in which data are stored within external nodes, -- but not within internal nodes. -- ----------------------------------------------------------------------------

import Test.HUnit

data Tree t = Leaf t | Node (Tree t) (Tree t) deriving (Eq,Show)

-- ---------------------------------------------------------------------------- -- Below, write your definition of the balance function. -- You may write auxiliary functions as desired.

-- YOUR DEFINITION OF THE balance FUNCTION -- AND ANY AUXILIARY FUNCTIONS GO HERE.

balance list = if length list == 1 then Leaf (head list) else (1) split list in half (2) recursively balance first half & second half (3) return a Node having those balanced first & second halves as the Node's left & right subtrees

------------------------------------------------------------------------------

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!