150 likes | 253 Views
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.
E N D
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 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
Operations on Binary Trees of Numbers 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 :: Ntree -> Ntree -- 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
Operations on Binary Trees of Numbers 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
Operations on Binary Trees of Numbers 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)
Operations on Binary Trees of Numbers 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
Traversing Binary Trees of Numbers 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)))
Traversing Binary Trees of Numbers 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)))
Traversing Binary Trees of Numbers postorder :: Ntree -> [Int] postorder NilT = [] postorder (Node n t1 t2) = postorder t1 ++ postorder t2 ++ [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 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 -> 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
Polymorphic 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 -> 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?