In this problem you have to write several functions for generic binary trees. The definition of the trees is given by:
data Tree a = Node a (Tree a) (Tree a) | Empty deriving (Show)
That is, a tree with elements of type
is, either an empty tree, either a node with an element (of type
)
and two other trees of the same type. The deriving (Show)
statement simply enables an visualization of trees.
Write a function size :: Tree a -> Int that,
given a tree, returns its size, that is, the number of node it
contains.
Write a function height :: Tree a -> Int that,
given a tree, returns its height, assuming that empty trees have zero
height.
Write a function
equal :: Eq a => Tree a -> Tree a -> Bool that,
given two trees, tells whether they are the same.
Write a function
isomorphic :: Eq a => Tree a -> Tree a -> Bool
that, given two trees, tells whether they are isomorphic, that is, if
one can obtain one from the other flipping some of its
descendants.
Write a function preOrder :: Tree a -> [a] that,
given a tree, return its pre-order traversal.
Write a function postOrder :: Tree a -> [a] that,
given a tree, return its post-order traversal.
Write a function inOrder :: Tree a -> [a] that,
given a tree, return its in-order traversal.
Write a function breadthFirst :: Tree a -> [a]
that, given a tree, return its traversal by levels.
Write a function
build :: Eq a => [a] -> [a] -> Tree a that, given
a pre-order traversal of a tree and an in-order traversal of the same
tree, returns the original tree. You can assume that the three has no
repeated elements.
Write a function
overlap :: (a -> a -> a) -> Tree a -> Tree a -> Tree a
that, given two trees, returns its overlapping using a function.
Overlapping two trees with a function consists in placing the two trees
one on the other and combine the double nodes using the given
function.
Each function scores 10 points.
Author: Unknown
Translator: Jordi Petit
Generation: 2026-02-03T17:09:41.885Z
© Jutge.org, 2006–2026.
https://jutge.org