Title: Functional%20Programming%20Lecture%207%20-%20Trees
1Functional ProgrammingLecture 7 - Trees
2Binary Trees of Numbers
42
13
19
12
- data NTree NilT
- Node Int NTree NTree
- Node 42
- (Node 13 NilT NilT)
- (Node 19 NilT (Node 12 NilT NilT))
3Operations on Binary Trees of Numbers
- Most operations can be defined using primitive
recursion and pattern matching. - depth Ntree -gt Int
- -- calculate the depth of a tree
- depth NilT 0
- depth (Node n t1 t2) 1 max (depth t1) (depth
t2) - sumtree Ntree -gt 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
4Operations on Binary Trees of Numbers
- occurs Int -gt Ntree -gt 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 -gt Int -gt Int
- occurs x 0
- occurs x (yys)
- (x y) 1(occurs x ys)
- otherwise occurs x ys
5- left Ntree -gt Ntree
- -- left subtree
- left NilT NilT
- left (Node n t1 t2) t1
- right Ntree -gt Ntree
- -- right subtree
- right NilT NilT
- right (Node n t1 t2) t2
6Operations on Binary Trees of Numbers
- Conjoin two trees
-
6 - 2 3 gt
2 3 - 4 6 1 5 4
6 1 5 - conjoin Int -gt Ntree -gt Ntree -gt Ntree
- -- conjoin two trees
- conjoin x t1 t2 Node x t1 t2
7Operations on Binary Trees of Numbers
- maxt Ntree -gt 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 (xxs) max x (maxlist xs)
8Operations on Binary Trees of Numbers
- or
- maxt Ntree -gt 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
9Traversing Binary Trees of Numbers
- preorder Ntree -gt 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)))
10Traversing Binary Trees of Numbers
- inorder Ntree -gt 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)))
11Traversing Binary Trees of Numbers
- postorder Ntree -gt Int
- postorder NilT
- postorder (Node n t1 t2)
- postorder t1 postorder t2 n
- Example 42
- 3 8
- 16 5
12Sorting Binary Trees of Numbers
- sorttree Ntree -gt Int
- sorttree t sort (preorder t)
- where sort is your favourite sorting algorithm.
13Insertion sort
sort xs inssort(xs,) a common programming
paradigm inssort (Int,Int) -gt Int --
insertion sort -- first component is list to be
sorted -- second component is sorted list inssort
(,ys) ys inssort (xxs, ys) inssort(xs, ins
x ys) ins Int -gtInt -gt Int -- insert
integer at correct position in sorted list ins x
x ins x (yys) if (xlty) then (xyys)
else y (ins x ys) sort( 3,2,1
14Map
- maptree (Int -gt Int) -gt Ntree -gt 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
15Polymorphic Binary Trees
- 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 -gt b) -gt (Tree a) -gt (Tree b)
- maptree Nil Nil
- maptree f (Node n t1 t2)
- Node f n (maptree f t1) (maptree f t2)
- preorder Tree a -gt a
- Etc.
- What is type of maptree times2?