0
votes

How would I go about writing a contains, balanced function given this definition using folds.

data Tree a = Tree a [Tree a]

treeFold :: (a -> [b] -> b) -> Tree a -> b
treeContains :: Eq a => a -> Tree a -> Bool
treeBalanced :: Tree a -> Bool

Note that in the above definition, empty trees are not allowed, and that a leaf is a tree with an empty list of subtrees.

A contains function determines whether the tree contains a given label.

A balanced function determine whether a tree is balanced.

A tree is balanced if the heights of its subtrees different by at most one, and the subtrees are balanced

The contains function that I have got so far is given below.

treeContains :: Eq a => a -> Tree a -> Bool
treeContains a = treeFold (\x y -> if a == x then True else ...)

So how do you do the else part indicate by ...

Any help would be greatly appreciated.

1
Look at functions and, or and any in the prelude. Especially figure out what type of an term you should write in place of ...aleator

1 Answers

5
votes

As you are learning, I shall answer your question by asking you more questions.


Re treeContains:

  1. What value do you want to compute in the ...? What should it mean? What is its type?

    In the ..., you want to look for the next node in the tree and check if that node is what you are after.

    1. Which node is the next node? If the current node has four subtrees, why would we want to check only one of them? What if there are no subtrees?

      Check if the node that you are after is in the root. If it is return true otherwise check if it is in any of the sub trees. If it is return true otherwise check if there are any sub sub trees. If there are then check the node in those sub sub tree otherwise return false.

      1. You are saying you should check the nodes in breadth-first order. Is that the best order to check them in? What other orders might you check them? Might they be easier?
  2. What does y mean? What is its type?

    y is the list containing elements of type b.

    1. Can you be more specific? (I am asking in the specific context of treeContains, not the more general context of treeFold.) What is b?

      y is a list containing elements of type Tree a (so b is Tree a)

      Are you sure?

      1. You have been told that treeContains :: Eq a => a -> Tree a -> Bool, so what is the type of treeContains a?

      2. You have decided that treeContains a = treeFold (\x y -> if a == x then True else ...). Given that and your answer to the previous question, what is the type of treeFold (\x y -> if a == x then True else ...)?

      3. Given your answer to the previous question, and given that treeFold :: (a -> [b] -> b) -> Tree a -> b, what is the type of (\x y -> if a == x then True else ...)?

      4. Given your answer to the previous question, what is the type of y?

    2. What does this value mean? Does it tell you something about other nodes in the tree? Entire subtrees? Which ones? What does it tell you about them? Their size? Their height? Whether they're balanced? Something about their contents?

  3. Could you compute the ... from y? What function(s) would you need? What would it/their type(s) be?


Re treeBalanced:

  1. If you wanted to write a function treeHeight :: Tree a -> Int, how would you do that? Would you use treeFold?

  2. If you wanted to write a function treeHeightAndBalanced :: Tree a -> (Int, Bool), how would you do that?

  3. If you already had treeHeightAndBalanced :: Tree a -> (Int, Bool), how could you write treeBalanced :: Tree a -> Bool?