Functional Programming Lecture 7 Trees Binary Trees of
Functional Programming Lecture 7 - Trees
Binary Trees of Numbers 42 13 19 12 data NTree = Nil. T | Node Int NTree Node 42 (Node 13 Nil. T) (Node 19 Nil. T (Node 12 Nil. T))
Operations on Binary Trees of Numbers Most operations can be defined using primitive recursion and pattern matching. depth : : Ntree -> Int -- calculate the depth of a tree depth Nil. T = 0 depth (Node n t 1 t 2) = 1 + max (depth t 1) (depth t 2) sumtree : : Ntree -> Int -- sum all the nodes of a tree sumtree Nil. T = 0 sumtree (Node n t 1 t 2) = n + (sumtree t 1) + (sumtree t 2) Evaluate on tree 42 13 19 12
Operations on Binary Trees of Numbers occurs : : Int -> Ntree -> Int -- occurrences of a number in a tree occurs x Nil. T = 0 occurs x (Node n t 1 t 2) | (x == n) = 1 + (occurs x t 1) + (occurs x t 2) | otherwise = (occurs x t 1) + (occurs x t 2) 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 : : Ntree -> Ntree -- left subtree left Nil. T = Nil. T left (Node n t 1 t 2) = t 1 right : : Ntree -> Ntree -- right subtree right Nil. T = Nil. T right (Node n t 1 t 2) = t 2
Operations on Binary Trees of Numbers Conjoin two trees 6 2 4 3 6 1 => 5 2 4 3 6 conjoin : : Int -> Ntree -- conjoin two trees conjoin x t 1 t 2 = Node x t 1 t 2 1 5
Operations on Binary Trees of Numbers maxt : : Ntree -> Int -- find max value in a tree maxt Nil. T = ? ? maxt(Node n Nil. T) = n maxt(Node n Nil. T t 2) = max n (maxt t 2) maxt(Node n t 1 Nil. T) = max n (maxt t 1) maxt (Node n t 1 t 2) = max 3 n (maxt t 1) (maxt t 2) or maxt t = maxlist preorder t where maxlist [x] = x maxlist x: xs = max x (maxlist xs)
Operations on Binary Trees of Numbers or maxt : : Ntree -> Maybe Int maxt Nil. T = Nothing maxt(Node n Nil. T) = Just n maxt(Node n Nil. T t 2) = max n y maxt(Node n t 1 Nil. T) = max n x maxt (Node n t 1 t 2) = Just max 3 n x y where Just x = maxt t 1 Just y = maxt t 2
Traversing Binary Trees of Numbers preorder : : Ntree -> [Int] preorder Nil. T = [] preorder (Node n t 1 t 2) = n: (preorder t 1 ++ preorder t 2) Example: 42 3 8 16 5 preorder (Node 42 (Node 3 Nil) (Node 8 (Node 16 Nil) (Node 5 Nil)))
Traversing Binary Trees of Numbers inorder : : Ntree -> [Int] inorder Nil. T = [] inorder (Node n t 1 t 2) = inorder t 1 ++ [n] ++ inorder t 2 Example: 42 3 8 16 5 inorder (Node 42 (Node 3 Nil) (Node 8 (Node 16 Nil) (Node 5 Nil)))
Traversing Binary Trees of Numbers postorder : : Ntree -> [Int] postorder Nil. T = [] postorder (Node n t 1 t 2) = postorder t 1 ++ postorder t 2 ++ [n] Example: 42 3 8 16 5
Sorting Binary Trees of Numbers sorttree : : Ntree -> [Int] sorttree t = sort (preorder t) where sort is your favourite sorting algorithm.
Insertion sort xs = inssort(xs, []) a common programming paradigm 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]
Map maptree : : (Int -> Int) -> Ntree maptree f Nil. T = Nil. T maptree f (Node n t 1 t 2) = Node (f n) (maptree f t 1) (maptree f t 2) Example: t= 42 3 8 16 maptree times 2 t 5
Polymorphic Binary Trees We can define trees with an arbitrary type at the nodes: data Tree a = Nil | Node a (Tree a) deriving Show So define: maptree : : (a -> b) -> (Tree a) -> (Tree b) maptree Nil = Nil maptree f (Node n t 1 t 2) = Node f n (maptree f t 1) (maptree f t 2) preorder : : Tree a -> [a] Etc. What is type of maptree times 2?
- Slides: 15