1 / 15

# Functional Programming Lecture 7 - Trees - PowerPoint PPT Presentation

Functional Programming Lecture 7 - Trees. Binary Trees of Numbers. 42. data NTree = NilT | Node Int NTree NTree Node 42 (Node 13 NilT NilT) (Node 19 NilT (Node 12 NilT NilT)). 13. 19. 12. Operations on Binary Trees of Numbers.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Functional Programming Lecture 7 - Trees' - brie

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Functional ProgrammingLecture 7 - Trees

42

data NTree = NilT

| Node Int NTree NTree

Node 42

(Node 13 NilT NilT)

(Node 19 NilT (Node 12 NilT NilT))

13

19

12

Most operations can be defined using primitive recursion and pattern matching.

depth :: Ntree -> Int

-- calculate the depth of a tree

depth NilT = 0

depth (Node n t1 t2) = 1 + max (depth t1) (depth t2)

sumtree :: Ntree -> Int

-- sum all the nodes of a tree

sumtree NilT = 0

sumtree (Node n t1 t2)

= n + (sumtree t1) + (sumtree t2)

Evaluate on tree 42

13 19

12

occurs :: Int -> Ntree -> Int

-- occurrences of a number in a tree

occurs x NilT = 0

occurs x (Node n t1 t2)

| (x == n) = 1 + (occurs x t1) + (occurs x t2)

| otherwise = (occurs x t1) + (occurs x t2)

occurs on lists?

occurs :: Int -> [Int] -> Int

occurs x [] = 0

occurs x (y:ys)

| (x == y) = 1+(occurs x ys)

| otherwise = occurs x ys

-- left subtree

left NilT = NilT

left (Node n t1 t2) = t1

right :: Ntree -> Ntree

-- right subtree

right NilT = NilT

right (Node n t1 t2) = t2

Conjoin two trees

6

2 3 => 2 3

4 6 1 5 4 6 1 5

conjoin :: Int -> Ntree -> Ntree -> Ntree

-- conjoin two trees

conjoin x t1 t2 = Node x t1 t2

maxt :: Ntree -> Int

-- find max value in a tree

maxt NilT = ??

maxt(Node n NilT NilT) = n

maxt(Node n NilT t2) = max n (maxt t2)

maxt(Node n t1 NilT) = max n (maxt t1)

maxt (Node n t1 t2) = max3 n (maxt t1) (maxt t2)

or

maxt t = maxlist preorder t

where maxlist [x] = x

maxlist (x:xs) = max x (maxlist xs)

or

maxt :: Ntree -> Maybe Int

maxt NilT = Nothing

maxt(Node n NilT NilT) = Just n

maxt(Node n NilT t2) = max n y

maxt(Node n t1 NilT) = max n x

maxt (Node n t1 t2) = Just max3 n x y

where Just x = maxt t1

Just y = maxt t2

preorder :: Ntree -> [Int]

preorder NilT = []

preorder (Node n t1 t2) = n:(preorder t1 ++ preorder t2)

Example: 42

3 8

16 5

preorder (Node 42 (Node 3 Nil Nil) (Node 8 (Node 16 Nil Nil) (Node 5 Nil Nil)))

inorder :: Ntree -> [Int]

inorder NilT = []

inorder (Node n t1 t2)

= inorder t1 ++ [n] ++ inorder t2

Example: 42

3 8

16 5

inorder (Node 42 (Node 3 Nil Nil) (Node 8 (Node 16 Nil Nil) (Node 5 Nil Nil)))

postorder :: Ntree -> [Int]

postorder NilT = []

postorder (Node n t1 t2)

= postorder t1 ++ postorder t2 ++ [n]

Example: 42

3 8

16 5

sorttree :: Ntree -> [Int]

sorttree t = sort (preorder t)

where sort is your favourite sorting algorithm.

sort xs = inssort(xs,[])

inssort :: ([Int],[Int]) -> [Int]

-- insertion sort

-- first component is list to be sorted

-- second component is sorted list

inssort ([],ys) = ys

inssort (x:xs, ys) = inssort(xs, ins x ys)

ins :: Int ->[Int] -> [Int]

-- insert integer at correct position in sorted list

ins x [] = [x]

ins x (y:ys) = if (x<=y) then (x:y:ys) else y: (ins x ys)

sort( [3,2,1]

maptree :: (Int -> Int) -> Ntree -> Ntree

maptree f NilT = NilT

maptree f (Node n t1 t2) =

Node (f n) (maptree f t1) (maptree f t2)

Example: t= 42

3 8

16 5

maptree times2 t

We can define trees with an arbitrary type at the nodes:

data Tree a = Nil

| Node a (Tree a) (Tree a)

deriving Show

So define:

maptree :: (a -> b) -> (Tree a) -> (Tree b)

maptree Nil = Nil

maptree f (Node n t1 t2) =

Node f n (maptree f t1) (maptree f t2)

preorder :: Tree a -> [a]

Etc.

What is type of maptree times2?