-
Notifications
You must be signed in to change notification settings - Fork 7
/
TreeFolds.hs
232 lines (178 loc) · 5.41 KB
/
TreeFolds.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
225
226
227
228
229
230
231
232
{-
---
fulltitle: "Extra practice: Tree folds"
date: September 20, 2023
---
-}
module TreeFolds where
{-
>
-}
import qualified Data.DList as DL
import Test.HUnit
{-
This exercise is about efficiently iterating over tree-structured data.
Recall the basic type of binary trees.
-}
-- | a basic tree data structure
data Tree a = Empty | Branch a (Tree a) (Tree a) deriving (Show, Eq)
{-
And also the `infixOrder` function from the Datatypes module.
-}
infixOrder :: Tree a -> [a]
infixOrder Empty = []
infixOrder (Branch x l r) = infixOrder l ++ [x] ++ infixOrder r
{-
For example, using this tree
5
/ \
2 9
/ \ \
1 4 7
-}
exTree :: Tree Int
exTree =
Branch
5
(Branch 2 (Branch 1 Empty Empty) (Branch 4 Empty Empty))
(Branch 9 Empty (Branch 7 Empty Empty))
{-
the infix order traversal produces this result.
-}
testInfixOrder :: Test
testInfixOrder = "infixOrder" ~: infixOrder exTree ~?= [1, 2, 4, 5, 9, 7]
{-
However, as in the DList exercise, the (++) in the definition of
`infixOrder` should bother you. What if the tree is terribly skewed?
1
/
2
/
3
/
4
/
5
/
...
-}
-- | A big "right-skewed" tree
bigRightTree :: Int -> Tree Int
bigRightTree m = go 0
where
go n = if n <= m then Branch n Empty (go (n + 1)) else Empty
-- | A big "left-skewed" tree
bigLeftTree :: Int -> Tree Int
bigLeftTree m = go 0
where
go n = if n <= m then Branch n (go (n + 1)) Empty else Empty
{-
If you turn on benchmarking, you can observe the difference between a left
skewed and right skewed tree in ghci. At this scale, the time taken to print
these trees dominates the computation, but take a look at the difference in
allocation!
ghci> :set +s
ghci> sum (infixOrder (bigRightTree 10000))
50005000
(0.02 secs, 7,102,016 bytes)
ghci> sum (infixOrder (bigLeftTree 10000))
50005000
(0.97 secs, 4,305,693,360 bytes)
We can improve things by using `DList`s while traversing the tree. Try to
complete this version so that the number of bytes used for traversing t1 and
t2 is more similar to the version above...
(NOTE: There is an implementation of `DList`s in the standard library, and we
have imported it above. So you can try this out even if you have not completed the
`DList` exercise.)
-}
infixOrder1 :: Tree a -> [a]
infixOrder1 = undefined
tinfixOrder1 :: Test
tinfixOrder1 = "infixOrder1a" ~: infixOrder1 exTree ~?= [1, 2, 4, 5, 9, 7]
{-
ghci> sum (infixOrder1 (bigRightTree 10000))
50005000
(0.02 secs, 9,016,880 bytes)
ghci> sum (infixOrder1 (bigLeftTree 10000))
50005000
(0.02 secs, 9,016,880 bytes)
Now, let's inline the `DList` definitions to get rid of the uses of `(.)` and `id`.
-}
infixOrder2 :: Tree Int -> [Int]
infixOrder2 = undefined
{-
On my microbenchmark, this also sped up the traversal!
ghci> sum (infixOrder2 (bigLeftTree 10000))
50005000
(0.01 secs, 6,696,624 bytes)
Foldable Trees
--------------
Does this idea generalize to other forms of tree recursion? You betcha.
Let's generalize the "base case" and "inductive step" of the definition above, separating
the recursion from the specific operation of traversal. First, we identify these operators
inside the definition of infixOrder.
-}
infixOrder3 :: Tree Int -> [Int]
infixOrder3 t = aux t b
where
b = []
f = (:)
aux = undefined
{-
ghci> sum (infixOrder3 (bigLeftTree 10000))
50005000
(0.01 secs, 6,856,744 bytes)
Then we abstract them to make a generic `foldr` function for trees.
-}
foldrTree :: (a -> b -> b) -> b -> Tree a -> b
foldrTree f b t = undefined
{-
>
-}
infixOrder4 :: Tree a -> [a]
infixOrder4 = foldrTree (:) []
{-
ghci> sum (infixOrder4 (bigLeftTree 10000))
50005000
(0.01 secs, 6,856,728 bytes)
This fold function is general. We can use it define *many* different tree operations.
-}
sizeTree :: Tree Int -> Int
sizeTree = foldrTree (const (1 +)) 0
sumTree :: Tree Int -> Int
sumTree = foldrTree (+) 0
anyTree :: (a -> Bool) -> Tree a -> Bool
anyTree f = foldrTree (\x b -> f x || b) False
allTree :: (a -> Bool) -> Tree a -> Bool
allTree f = foldrTree (\x b -> f x && b) True
{-
Extra challenge
---------------
Now use `foldrTree` as an inspiration to define a `foldlTree` function, which
folds over the tree in the opposite order.
-}
foldlTree :: (b -> a -> b) -> b -> Tree a -> b
foldlTree = undefined
revOrder :: Tree a -> [a]
revOrder = foldlTree (flip (:)) []
trevOrder :: Test
trevOrder = "revOrder" ~: revOrder exTree ~?= [7, 9, 5, 4, 2, 1]
{-
Note: Although they are efficient and useful, neither `foldlTree` nor `foldrTree` capture the general principle of tree recursion.
-}
foldTree :: (a -> b -> b -> b) -> b -> Tree a -> b
foldTree _ e Empty = e
foldTree f e (Branch a n1 n2) = f a (foldTree f e n1) (foldTree f e n2)
{-
Define `foldrTree` and `foldlTree` in terms of `foldTree`. (This is challenging!)
-}
foldrTree' :: (a -> b -> b) -> b -> Tree a -> b
foldrTree' = undefined
tree1 :: Tree Int
tree1 = Branch 1 (Branch 2 Empty Empty) (Branch 3 Empty Empty)
tfoldrTree' :: Test
tfoldrTree' = "foldrTree'" ~: foldrTree' (+) 0 tree1 ~?= 6
foldlTree' :: (b -> a -> b) -> b -> Tree a -> b
foldlTree' = undefined
tfoldlTree' :: Test
tfoldlTree' = "foldlTree'" ~: foldlTree' (+) 0 tree1 ~?= 6