# 99 questions/54A to 60

This is part of Ninety-Nine Haskell Problems, based on Ninety-Nine Prolog Problems.

If you want to work on one of these, put your name in the block so we know someone's working on it. Then, change n in your block to the appropriate problem number, and fill in the <Problem description>,<example in lisp>,<example in Haskell>,<solution in haskell> and <description of implementation> fields.

## Binary trees

A binary tree is either empty or it is composed of a root element and two successors, which are binary trees themselves.

## Problem 54A

(*) Check whether a given term represents a binary tree

In Prolog or Lisp, one writes a predicate to do this.

Example in Lisp:

```* (istree (a (b nil nil) nil))
T
* (istree (a (b nil nil)))
NIL
```

In Haskell, we characterize binary trees with a datatype definition:

```data Tree a = Empty | Branch a (Tree a) (Tree a)
deriving (Show, Eq)
```

The above tree is represented as:

```tree1 = Branch 'a (Branch 'b' (Branch 'd' Empty Empty)
(Branch 'e' Empty Empty))
(Branch 'c' Empty
(Branch 'f' (Branch 'g' Empty Empty)
Empty)))
```

Other examples of binary trees:

```tree2 = Branch 'a' Empty Empty   -- a binary tree consisting of a root node only

tree3 = nil                      -- an empty binary tree

tree4 = Branch 1 (Branch 2 Empty (Branch 4 Empty Empty))
(Branch 2 Empty Empty)
```

The type system ensures that all terms of type `Tree a` are binary trees: it is just not possible to construct an invalid tree with this type. Hence, it is redundant to introduce a predicate to check this property: it would always return `True`.

## Problem 55

(**) Construct completely balanced binary trees

In a completely balanced binary tree, the following property holds for every node: The number of nodes in its left subtree and the number of nodes in its right subtree are almost equal, which means their difference is not greater than one.

Write a function cbal-tree to construct completely balanced binary trees for a given number of nodes. The predicate should generate all solutions via backtracking. Put the letter 'x' as information into all nodes of the tree.

Example:

```* cbal-tree(4,T).
T = t(x, t(x, nil, nil), t(x, nil, t(x, nil, nil))) ;
T = t(x, t(x, nil, nil), t(x, t(x, nil, nil), nil)) ;
etc......No
```

```*Main> cbalTree 4
[Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' Empty (Branch 'x' Empty Empty)),Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' (Branch 'x' Empty Empty) Empty),Branch 'x' (Branch 'x' Empty (Branch 'x' Empty Empty)) (Branch 'x' Empty Empty),Branch 'x' (Branch 'x' (Branch 'x' Empty Empty) Empty) (Branch 'x' Empty Empty)]
```

Solution:

```cbalTree 0 = [Empty]
cbalTree n = [Branch 'x' l r | i <- [q .. q + r], l <- cbalTree i, r <- cbalTree (n - i - 1)]
where (q, r) = quotRem (n-1) 2
```

Here we use the list monad to enumerate all the trees, in a style that is more natural than standard backtracking.

## Problem 56

(**) Symmetric binary trees

Let us call a binary tree symmetric if you can draw a vertical line through the root node and then the right subtree is the mirror image of the left subtree. Write a predicate symmetric/1 to check whether a given binary tree is symmetric. Hint: Write a predicate mirror/2 first to check whether one tree is the mirror image of another. We are only interested in the structure, not in the contents of the nodes.

```*Main> symmetric (Branch 'x' (Branch 'x' Empty Empty) Empty)
False
*Main> symmetric (Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' Empty Empty))
True
```

Solution:

```mirror Empty          Empty          = True
mirror (Branch _ a b) (Branch _ x y) = mirror a y && mirror b x
mirror _              _              = False

symmetric Empty          = True
symmetric (Branch _ l r) = mirror l r
```

## Problem 57

(**) Binary search trees (dictionaries)

Use the predicate add/3, developed in chapter 4 of the course, to write a predicate to construct a binary search tree from a list of integer numbers.

Example:

```* construct([3,2,5,7,1],T).
T = t(3, t(2, t(1, nil, nil), nil), t(5, nil, t(7, nil, nil)))
```

Then use this predicate to test the solution of the problem P56.

Example:

```* test-symmetric([5,3,18,1,4,12,21]).
Yes
* test-symmetric([3,2,5,7,1]).
No
```

```*Main> construct [3, 2, 5, 7, 1]
Branch 3 (Branch 2 (Branch 1 Empty Empty) Empty) (Branch 5 Empty (Branch 7 Empty Empty))
*Main> symmetric . construct \$ [5, 3, 18, 1, 4, 12, 21]
True
*Main> symmetric . construct \$ [3, 2, 5, 7, 1]
True
```

Solution:

```add :: Ord a => a -> Tree a -> Tree a
add x Empty            = Branch x Empty Empty
add x t@(Branch y l r) = case compare x y of
LT -> Branch y (add x l) r
GT -> Branch y l (add x r)
EQ -> t

construct xs = foldl (flip add) Empty xs
```

Here, the definition of construct is trivial, because the pattern of accumulating from the left is captured by the standard function foldl.

## Problem 58

Apply the generate-and-test paradigm to construct all symmetric, completely balanced binary trees with a given number of nodes.

Example:

```* sym-cbal-trees(5,Ts).
Ts = [t(x, t(x, nil, t(x, nil, nil)), t(x, t(x, nil, nil), nil)), t(x, t(x, t(x, nil, nil), nil), t(x, nil, t(x, nil, nil)))]
```

```*Main> symCbalTrees 5
[Branch 'x' (Branch 'x' Empty (Branch 'x' Empty Empty)) (Branch 'x' (Branch 'x' Empty Empty) Empty),Branch 'x' (Branch 'x' (Branch 'x' Empty Empty) Empty) (Branch 'x' Empty (Branch 'x' Empty Empty))]
```

Solution:

```symCbalTrees = filter symmetric . cbalTree
```

## Problem 59

(**) Construct height-balanced binary trees

In a height-balanced binary tree, the following property holds for every node: The height of its left subtree and the height of its right subtree are almost equal, which means their difference is not greater than one.

Example:

```?- hbal_tree(3,T).
T = t(x, t(x, t(x, nil, nil), t(x, nil, nil)), t(x, t(x, nil, nil), t(x, nil, nil))) ;
T = t(x, t(x, t(x, nil, nil), t(x, nil, nil)), t(x, t(x, nil, nil), nil)) ;
etc......No
```

```*Main> take 4 \$ hbalTree 'x' 3
[Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' Empty (Branch 'x' Empty Empty)),
Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' (Branch 'x' Empty Empty) Empty),
Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' Empty Empty)),
Branch 'x' (Branch 'x' Empty (Branch 'x' Empty Empty)) (Branch 'x' Empty Empty)]
```

Solution:

```hbalTree x = map fst . hbalTree'
where hbalTree' 0 = [(Empty, 0)]
hbalTree' 1 = [(Branch x Empty Empty, 1)]
hbalTree' n =
let t = hbalTree' (n-2) ++ hbalTree' (n-1)
in  [(Branch x lb rb, h) | (lb,lh) <- t, (rb,rh) <- t
, let h = 1 + max lh rh, h == n]
```

Alternative solution:

```hbaltree :: a -> Int -> [Tree a]
hbaltree x 0 = [Empty]
hbaltree x 1 = [Branch x Empty Empty]
hbaltree x h = [Branch x l r |
(hl, hr) <- [(h-2, h-1), (h-1, h-1), (h-1, h-2)],
l <- hbaltree x hl, r <- hbaltree x hr]
```

If we want to avoid recomputing lists of trees (at the cost of extra space), we can use a similar structure to the common method for computation of all the Fibonacci numbers:

```hbaltree :: a -> Int -> [Tree a]
hbaltree x h = trees !! h
where trees = [Empty] : [Branch x Empty Empty] :
zipWith combine (tail trees) trees
combine ts shortts = [Branch x l r |
(ls, rs) <- [(shortts, ts), (ts, ts), (ts, shortts)],
l <- ls, r <- rs]
```

## Problem 60

(**) Construct height-balanced binary trees with a given number of nodes

% hbal_tree_nodes(N,T) :- T is a height-balanced binary tree with N nodes.

Find out how many height-balanced trees exist for N = 15.

Example:

```?- count_hbal_trees(15,C).
C = 1553
```

```*Main> length \$ hbalTreeNodes 15
1553
*Main> map hbalTreeNodes [0,1,2,3]
[[Empty],
[Branch 'x' Empty Empty],
[Branch 'x' Empty (Branch 'x' Empty Empty),Branch 'x' (Branch 'x' Empty Empty) Empty],
[Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' Empty Empty)]]
```

Solution:

```hbalTreeNodes 0 = [Empty]
hbalTreeNodes n = concatMap toFilteredTrees [minHeight .. maxHeight]
where toFilteredTrees = filter ((n ==) . countNodes) . hbalTree 'x'

-- Similar to the Fibonacci sequence but adds 1 in each step.
minNodesSeq = 0:1:zipWith ((+).(1+)) minNodesSeq (tail minNodesSeq)
minNodes = (minNodesSeq !!)

minHeight = ceiling \$ logBase 2 \$ fromIntegral (n+1)
maxHeight = (fromJust \$ findIndex (>n) minNodesSeq) - 1

countNodes Empty = 0
countNodes (Branch _ l r) = countNodes l + countNodes r + 1
```