-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet5b.hs
224 lines (198 loc) · 7.57 KB
/
Set5b.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
-- Exercise set 5b: playing with binary trees
module Set5b where
import Mooc.Todo
-- The next exercises use the binary tree type defined like this:
data Tree a = Empty | Node a (Tree a) (Tree a)
deriving (Show, Eq)
------------------------------------------------------------------------------
-- Ex 1: implement the function valAtRoot which returns the value at
-- the root (top-most node) of the tree. The return value is Maybe a
-- because the tree might be empty (i.e. just a Empty)
valAtRoot :: Tree a -> Maybe a
valAtRoot Empty = Nothing
valAtRoot (Node a _ _) = Just a
------------------------------------------------------------------------------
-- Ex 2: compute the size of a tree, that is, the number of Node
-- constructors in it
--
-- Examples:
-- treeSize (Node 3 (Node 7 Empty Empty) Empty) ==> 2
-- treeSize (Node 3 (Node 7 Empty Empty) (Node 1 Empty Empty)) ==> 3
treeSize :: Tree a -> Int
treeSize Empty = 0
treeSize (Node _ l r) = 1 + treeSize l + treeSize r
------------------------------------------------------------------------------
-- Ex 3: get the largest value in a tree of positive Ints. The
-- largest value of an empty tree should be 0.
--
-- Examples:
-- treeMax Empty ==> 0
-- treeMax (Node 3 (Node 5 Empty Empty) (Node 4 Empty Empty)) ==> 5
treeMax :: Tree Int -> Int
treeMax Empty = 0
treeMax (Node a l r) = max a (max (treeMax l) (treeMax r))
------------------------------------------------------------------------------
-- Ex 4: implement a function that checks if all tree values satisfy a
-- condition.
--
-- Examples:
-- allValues (>0) Empty ==> True
-- allValues (>0) (Node 1 Empty (Node 2 Empty Empty)) ==> True
-- allValues (>0) (Node 1 Empty (Node 0 Empty Empty)) ==> False
allValues :: (a -> Bool) -> Tree a -> Bool
allValues condition Empty = True
allValues condition (Node a l r) = condition a && allValues condition l && allValues condition r
------------------------------------------------------------------------------
-- Ex 5: implement map for trees.
--
-- Examples:
--
-- mapTree (+1) Empty ==> Empty
-- mapTree (+2) (Node 0 (Node 1 Empty Empty) (Node 2 Empty Empty))
-- ==> (Node 2 (Node 3 Empty Empty) (Node 4 Empty Empty))
mapTree :: (a -> b) -> Tree a -> Tree b
mapTree _ Empty = Empty
mapTree f (Node a l r) = Node (f a) (mapTree f l) (mapTree f r)
------------------------------------------------------------------------------
-- Ex 6: given a value and a tree, build a new tree that is the same,
-- except all nodes that contain the value have been removed. Also
-- remove the subnodes of the removed nodes.
--
-- Examples:
--
-- 1 1
-- / \ ==> \
-- 2 0 0
--
-- cull 2 (Node 1 (Node 2 Empty Empty)
-- (Node 0 Empty Empty))
-- ==> (Node 1 Empty
-- (Node 0 Empty Empty))
--
-- 1 1
-- / \ \
-- 2 0 ==> 0
-- / \
-- 3 4
--
-- cull 2 (Node 1 (Node 2 (Node 3 Empty Empty)
-- (Node 4 Empty Empty))
-- (Node 0 Empty Empty))
-- ==> (Node 1 Empty
-- (Node 0 Empty Empty)
--
-- 1 1
-- / \ \
-- 0 3 ==> 3
-- \ \
-- 2 0
--
-- cull 0 (Node 1 (Node 0 Empty
-- (Node 2 Empty Empty))
-- (Node 3 Empty
-- (Node 0 Empty Empty)))
-- ==> (Node 1 Empty
-- (Node 3 Empty Empty))
cull :: Eq a => a -> Tree a -> Tree a
cull _ Empty = Empty
cull val (Node a l r)
| a == val = Empty
| otherwise = Node a (cull val l) (cull val r)
------------------------------------------------------------------------------
-- Ex 7: check if a tree is ordered. A tree is ordered if:
-- * all values to the left of the root are smaller than the root value
-- * all of the values to the right of the root are larger than the root value
-- * and the left and right subtrees are ordered.
--
-- Hint: allValues will help you here!
--
-- Examples:
-- 1
-- / \ is ordered:
-- 0 2
-- isOrdered (Node 1 (Node 0 Empty Empty)
-- (Node 2 Empty Empty)) ==> True
--
-- 1
-- / \ is not ordered:
-- 2 3
-- isOrdered (Node 1 (Node 2 Empty Empty)
-- (Node 3 Empty Empty)) ==> False
--
-- 2
-- / \
-- 1 3 is not ordered:
-- \
-- 0
-- isOrdered (Node 2 (Node 1 Empty
-- (Node 0 Empty Empty))
-- (Node 3 Empty Empty)) ==> False
--
-- 2
-- / \
-- 0 3 is ordered:
-- \
-- 1
-- isOrdered (Node 2 (Node 0 Empty
-- (Node 1 Empty Empty))
-- (Node 3 Empty Empty)) ==> True
isOrdered :: Ord a => Tree a -> Bool
isOrdered Empty = True
isOrdered (Node a l r) = allValues (<a) l && allValues (>a) r && isOrdered l && isOrdered r
------------------------------------------------------------------------------
-- Ex 8: a path in a tree can be represented as a list of steps that
-- go either left or right.
data Step = StepL | StepR
deriving (Show, Eq)
-- Define a function walk that takes a tree and a list of steps, and
-- returns the value at that point. Return Nothing if you fall of the
-- tree (i.e. hit a Empty).
--
-- Examples:
-- walk [] (Node 1 (Node 2 Empty Empty) Empty) ==> Just 1
-- walk [StepL] (Node 1 (Node 2 Empty Empty) Empty) ==> Just 2
-- walk [StepL,StepL] (Node 1 (Node 2 Empty Empty) Empty) ==> Nothing
walk :: [Step] -> Tree a -> Maybe a
walk _ Empty = Nothing
walk [] (Node a _ _) = Just a
walk (StepL:steps) (Node _ l _) = walk steps l
walk (StepR:steps) (Node _ _ r) = walk steps r
------------------------------------------------------------------------------
-- Ex 9: given a tree, a path and a value, set the value at the end of
-- the path to the given value. Since Haskell datastructures are
-- immutable, you'll need to build a new tree.
--
-- If the path falls off the tree, do nothing.
--
-- Examples:
-- set [] 1 (Node 0 Empty Empty) ==> (Node 1 Empty Empty)
-- set [StepL,StepL] 1 (Node 0 (Node 0 (Node 0 Empty Empty)
-- (Node 0 Empty Empty))
-- (Node 0 Empty Empty))
-- ==> (Node 0 (Node 0 (Node 1 Empty Empty)
-- (Node 0 Empty Empty))
-- (Node 0 Empty Empty))
--
-- set [StepL,StepR] 1 (Node 0 Empty Empty) ==> (Node 0 Empty Empty)
set :: [Step] -> a -> Tree a -> Tree a
set _ _ Empty = Empty
set [] val (Node _ l r) = Node val l r
set (StepL:steps) val (Node a l r) = Node a (set steps val l) r
set (StepR:steps) val (Node a l r) = Node a l (set steps val r)
------------------------------------------------------------------------------
-- Ex 10: given a value and a tree, return a path that goes from the
-- root to the value. If the value doesn't exist in the tree, return Nothing.
--
-- You may assume the value occurs in the tree at most once.
--
-- Examples:
-- search 1 (Node 2 (Node 1 Empty Empty) (Node 3 Empty Empty)) ==> Just [StepL]
-- search 1 (Node 2 (Node 4 Empty Empty) (Node 3 Empty Empty)) ==> Nothing
-- search 1 (Node 2 (Node 3 (Node 4 Empty Empty)
-- (Node 1 Empty Empty))
-- (Node 5 Empty Empty)) ==> Just [StepL,StepR]
search :: Eq a => a -> Tree a -> Maybe [Step]
search _ Empty = Nothing
search val (Node a l r)
| val == a = Just []
| otherwise = if search val l == Nothing then fmap (StepR:) (search val r) else fmap (StepL:) (search val l)