1

I'm trying to figure out how to calculate the depth of a general tree in Haskell. I can figure out the solution for simple binary trees, but not for general trees with any number of leaves.

Here's the code I have for binary trees.

--depth of a binary tree.

depth :: Tree a -> Int
depth Nil            = 0
depth (Node n x1 x2) = 1 + max (depth x1) (depth x2)

How can I modify this for general trees? General trees contain a list of trees, and that is where I am encountering difficulty.

Secondly, I want to turn the tree into a list (so I can do operations such as calculating the sum, etc.)

Again, I can figure it out for binary trees but not for general trees.

--tree into a list.

treeToList:: Tree a -> [a]
treeToList Nil = []
treeToList (Node n x1 x2)
         = collapse x1 ++ [n] ++ collapse x2
user3247171
  • 81
  • 3
  • 7

3 Answers3

5

Use Foldable to get single values out, use Functor to map functions

user2407038's good answer shows you how to write a Foldable instance by hand, which is very good advice, and you can use foldMap not just to make treeToList, but also to make handy other functions.

GHC lets you derive these instances automatically:

{-# LANGUAGE DeriveFunctor, DeriveFoldable #-}

import Data.Monoid
import Data.Foldable

data Tree a = Node a [Tree a] 
  deriving (Show,Functor,Foldable)

Let's use an example to test this out:

example :: Tree Int
example = Node 3 [Node 2 [], Node 5 [Node 2 [],Node 1 []],Node 10 []]

--   3
--   |
--   +--+-----+
--   2  5     10
--      |
--      +--+
--      2  1

Let's use fmap to multiply everything by 10:

> example
Node  3 [Node  2 [], Node 5 [Node  2 [], Node 1 []], Node 10 []]
> fmap (*10) example
Node 30 [Node 20 [],Node 50 [Node 60 [],Node 10 []],Node 100 []]

Use a Monoid to combine values

A Monoid lets you combine (mappend) values, and has a do-nothing/identity value called mempty. Lists are a Monoid, with mempty = [] and mappend = (++), numbers are moinoids in more than one way, for example, using (+) (the Sum monoid), using (*) (the Product monoid), using maximum (I had to hand-write the Max monoid).

We'll use foldMap to tag the Ints with what monoid we want to use:

> foldMap Sum example
Sum {getSum = 23}
> foldMap Product example
Product {getProduct = 600}
> foldMap Max example
Max {unMax = 10}

You can define your own monoid however you like - here's how to make the Max monoid:

instance (Ord a,Bounded a) => Monoid (Max a) where
    mempty = Max minBound
    mappend (Max a) (Max b) = Max $ if a >= b then a else b

The most general fold you can make

In this great question with great answers, Haskell's top asker, MathematicalOrchid asks how to generalise fold to other data types. The answers to the question are great and worth reading.

A generalised fold simply replaces each constructor of the data type with a function and evaluates to get a value.

The hand-rolled way is to look at the types of each of the constructors, and make a function that takes a function argument to match each constructor and an argument for the object itself, and returns a value.

Examples:

[] has two constructors, (:) :: a -> [a] -> [a] and [] :: [a] so

foldList :: (a -> l -> l) ->    l      -> [a] -> l
foldList    useCons         useEmpty      = f where 
       f (a:as) = useCons a (f as)
       f [] = useEmpty

Either a b has two constructors, Left :: a -> Either a and Right :: a -> Either so

foldEither :: (a -> e) -> (b -> e) -> Either a b -> e
foldEither    useLeft      useRight    = f where 
       f (Left a) = useLeft a
       f (Right b) = useRight b  

Generalised fold for your tree

generalFold :: (a -> [t] -> t) -> Tree a -> t
generalFold useNode = f where
     f (Node a ts) = useNode a (map f ts)

we can use that to do pretty much anything we want to to a tree:

-- maximum of a list, or zero for an empty list:
maxOr0 [] = 0
maxOr0 xs = maximum xs

height :: Tree a -> Int
height = generalFold maxPlus1 where
      maxPlus1 a as = 1 + maxOr0 as

sumTree = generalFold sumNode where
      sumNode a as = a + sum as

productTree = generalFold productNode where
      productNode a as = a * product as

longestPath = generalFold longest where
      longest a as = a + maxOr0 as

Let's test them:

ghci> example
Node 3 [Node 2 [],Node 5 [Node 2 [],Node 1 []],Node 10 []]
ghci> height example
3
ghci> sumTree example  -- 3 + sum[2, 5+sum[2,1], 10] = 3+2+5+2+1+10
23
ghci> productTree example  -- 3*(2*(5*(2*1))*10) = 3*2*5*2*1*10
600
ghci> longestPath example  -- 3 + maximum [2, 5+maximum[2,1], 10]
13
ghci> toList example -- 3 : concat [[2], 5:concat[[2],[1]], [10]]
[3,2,5,2,1,10]
Community
  • 1
  • 1
AndrewC
  • 32,300
  • 7
  • 79
  • 115
3

Think about generalizing the pattern to lists:

data Tree a = Node a [Tree a] | Nil

depth Nil = 0
depth (Node _ [a]) = 1 + depth a
depth (Node _ [a,b]) = 1 + max (depth a) (depth b)
depth (Node _ [a,b,c]) = 1 + max (max (depth a) (depth b)) (depth c)
etc...

Well, all you are doing is finding the depth of each subtree (map depth), then finding the maximum of those numbers (maximum):

depth Nil = 0
depth (Node _ a) = 1 + maximum (map depth a)

You can flatten the tree in the same way, just map over the subtrees:

treeToList (Node n a) = n : concat (map treeToList a)

You have to use concat because map collapse returns a list of lists and you just want a list. Alternatively, you can define an instance for the Foldable typeclass and you automatically get toList :: Foldable t => t a -> [a]

import Data.Foldable
import Data.Monoid

instance Foldable Tree where 
  foldMap f Nil = mempty
  foldMap f (Node a n) = f a `mappend` mconcat (map foldMap n)

If you scrutinize the definition of foldMap very carefully, you will see that it is just a more general treeToList, where : is replaced by mappend and [] by mempty. Then it is logical that you can write treeToList in terms of the monoid ([], ++):

data List a = List {getList :: [a]}
instance Monoid (List a) where 
  mempty = List []
  mappend (List a) (List b) = List (a ++ b)

treeToList = getList . foldMap (List . (:[]))
user2407038
  • 14,400
  • 3
  • 29
  • 42
0

A few pointers:

Take a look at the map function which allows you to apply a function to each element in a list. In your case you want to apply depth to each Tree a in the list of children.

After you get that part you have to find the max depth in the list. Do a google search for "haskell max of list" and you'll find what you need.

Daniel
  • 26,899
  • 12
  • 60
  • 88