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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started