-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.hs
45 lines (33 loc) · 1.09 KB
/
tree.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show)
intTree :: Tree Int
intTree =
Node 4
(Node 7
(Node 19 Nil Nil)
Nil
)
(Node (-3)
(Node 8 Nil Nil)
(Node 12 Nil Nil)
)
elemTree :: (Eq a) => a -> Tree a -> Bool
elemTree _ Nil = False
elemTree a (Node x left right) | a == x = True
| otherwise = elemTree a left || elemTree a right
inorder :: Tree a -> [a]
inorder Nil = []
inorder (Node x left right) = inorder left ++ [x] ++ inorder right
mapTree :: (a -> b) -> Tree a -> Tree b
mapTree _ Nil = Nil
mapTree f (Node x left right) = Node (f x) (mapTree f left) (mapTree f right)
foldTree :: (a -> b -> b -> b) -> b -> Tree a -> b
foldTree _ z Nil = z
foldTree f z (Node x left right) = f x (foldTree f z left) (foldTree f z right)
-- elemTree a = foldTree check False
-- where check x l r = x == a || l || r
-- inorder = foldTree combine []
-- where combine x l r = l ++ [x] ++ r
leaves :: Tree a -> [a]
leaves = foldTree count []
where count x [] [] = [x]
count _ left right = left ++ right