Skip to content

Commit 2781574

Browse files
jiegilletleios
authored andcommitted
Re-added tree traversal in Haskell, pre/post/in-order (#111)
1 parent 482b1fa commit 2781574

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
data Tree a = Node { node :: a
2+
, forest :: [Tree a]
3+
} deriving (Show)
4+
5+
dfs :: Tree a -> [a]
6+
dfs (Node x ts) = x : concatMap dfs ts
7+
8+
dfsPostOrder :: Tree a -> [a]
9+
dfsPostOrder (Node x ts) = concatMap dfsPostOrder ts ++ [x]
10+
11+
dfsInOrder :: Tree a -> [a] -- For binary trees only
12+
dfsInOrder (Node x []) = [x]
13+
dfsInOrder (Node x [l]) = dfsInOrder l ++ [x] -- Single branch assumed to be left
14+
dfsInOrder (Node x [l, r]) = dfsInOrder l ++ [x] ++ dfsInOrder r
15+
dfsInOrder _ = error "Not a binary tree"
16+
17+
bfs :: Tree a -> [a]
18+
bfs (Node x ts) = x : go ts
19+
where go [] = []
20+
go ts = map node ts ++ go (concatMap forest ts)
21+
22+
toBin :: Tree a -> Tree a
23+
toBin (Node x ts) = Node x (map toBin $ take 2 ts)
24+
25+
main = do
26+
print $ dfs testTree
27+
print $ dfsPostOrder testTree
28+
print $ dfsInOrder $ toBin testTree
29+
print $ bfs testTree
30+
31+
testTree :: Tree Int
32+
testTree = Node 1 [ Node 2 [ Node 3 []
33+
, Node 4 [ Node 5 []]
34+
]
35+
, Node 6 [ Node 7 []
36+
, Node 8 [ Node 9 [ Node 10 [ Node 11 []]
37+
, Node 12 []
38+
]
39+
]
40+
, Node 13 [ Node 14 []]
41+
]
42+
, Node 15 []
43+
]

chapters/tree_traversal/tree_traversal.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ This has not been implemented in your chosen language, so here is the Julia code
2323
[import:3-7, lang:"julia"](code/julia/Tree.jl)
2424
{% sample lang="rs"%}
2525
[import:4-7, lang:"rust"](code/rust/tree.rs)
26+
{% sample lang="hs"%}
27+
[import:1-3, lang:"haskell"](code/haskell/TreeTraversal.hs)
2628
{% endmethod %}
2729

2830
Because of this, the most straightforward way to traverse the tree might be recursive. This naturally leads us to the Depth-First Search (DFS) method:
@@ -48,6 +50,8 @@ This has not been implemented in your chosen language, so here is the Julia code
4850
[import:9-16, lang:"julia"](code/julia/Tree.jl)
4951
{% sample lang="rs"%}
5052
[import:9-15 lang:"rust"](code/rust/tree.rs)
53+
{% sample lang="hs"%}
54+
[import:5-6, lang:"haskell"](code/haskell/TreeTraversal.hs)
5155
{% endmethod %}
5256

5357
At least to me, this makes a lot of sense. We fight recursion with recursion! First, we first output the node we are on and then we call `DFS_recursive(...)` on each of its children nodes. This method of tree traversal does what its name implies: it goes to the depths of the tree first before going through the rest of the branches. In this case, the ordering looks like:
@@ -56,7 +60,7 @@ At least to me, this makes a lot of sense. We fight recursion with recursion! Fi
5660
<img src="res/DFS_pre.png" width="500" height="500" />
5761
</p>
5862

59-
Note that the in the code above, we are missing a crucial step: *checking to see if the node we are using actually exists!* Because we are using a vector to store all the nodes, we will be careful not to run into a case where we call `DFS_recursive(...)` on a node that has yet to be initialized; however, depending on the language we are using, we might need to be careful of this to avoid recursion errors!
63+
Note that the in the code above, we are missing a crucial step: *checking to see if the node we are using actually exists!* Because we are using a vector to store all the nodes, we will be careful not to run into a case where we call `DFS_recursive(...)` on a node that has yet to be initialized; however, depending on the language we are using, we might need to be careful of this to avoid recursion errors!
6064

6165
Now, in this case the first element searched through is still the root of the tree. This type of tree traversal is known as *pre-order* DFS. We perform an action (output the ID) *before* searching through the children. If we shift the function around and place the data output at the end of the function, we can modify the order in which we search through the tree to be *post-order* and look something like this:
6266

@@ -86,6 +90,8 @@ This has not been implemented in your chosen language, so here is the Julia code
8690
{% sample lang="rs"%}
8791
This has not been implemented in your chosen language, so here is the Julia code
8892
[import:18-26, lang:"julia"](code/julia/Tree.jl)
93+
{% sample lang="hs"%}
94+
[import:8-9, lang:"haskell"](code/haskell/TreeTraversal.hs)
8995
{% endmethod %}
9096

9197
<p align="center">
@@ -119,6 +125,8 @@ This has not been implemented in your chosen language, so here is the Julia code
119125
{% sample lang="rs"%}
120126
This has not been implemented in your chosen language, so here is the Julia code
121127
[import:28-43, lang:"julia"](code/julia/Tree.jl)
128+
{% sample lang="hs"%}
129+
[import:11-15, lang:"haskell"](code/haskell/TreeTraversal.hs)
122130
{% endmethod %}
123131

124132
<p align="center">
@@ -128,7 +136,7 @@ This has not been implemented in your chosen language, so here is the Julia code
128136

129137
The order here seems to be some mix of the other 2 methods and works through the binary tree from left to right.
130138

131-
Now, at this point, it might seem that the only way to search through a recursive data structure is with recusion, but this is not necessarily the case! Rather surprisingly, we can perform a DFS non-recursively by using a stack, which are data structures that hold multiple elements, but only allow you to interact with the very last element you put in. The idea here is simple:
139+
Now, at this point, it might seem that the only way to search through a recursive data structure is with recursion, but this is not necessarily the case! Rather surprisingly, we can perform a DFS non-recursively by using a stack, which are data structures that hold multiple elements, but only allow you to interact with the very last element you put in. The idea here is simple:
132140

133141
1. Put the root node in the stack
134142
2. Take it out and put in its children
@@ -157,6 +165,9 @@ This has not been implemented in your chosen language, so here is the Julia code
157165
[import:45-56, lang:"julia"](code/julia/Tree.jl)
158166
{% sample lang="rs"%}
159167
[import:17-24, lang:"rust"](code/rust/tree.rs)
168+
{% sample lang="hs"%}
169+
This has not been implemented in your chosen language, so here is the Julia code
170+
[import:45-56, lang:"julia"](code/julia/Tree.jl)
160171
{% endmethod %}
161172

162173
All this said, there are a few details about DFS that might not be idea, depending on the situation. For example, if we use DFS on an incredibly long tree, we will spend a lot of time going further and further down a single branch without searching the rest of the data structure. In addition, it is not the natural way humans would order a tree if asked to number all the nodes from top to bottom. I would argue a more natural traversal order would look something like this:
@@ -187,6 +198,8 @@ This has not been implemented in your chosen language, so here is the Julia code
187198
[import:58-69, lang:"julia"](code/julia/Tree.jl)
188199
{% sample lang="rs"%}
189200
[import:26-34, lang:"rust"](code/rust/tree.rs)
201+
{% sample lang="hs"%}
202+
[import:17-20, lang:"haskell"](code/haskell/TreeTraversal.hs)
190203
{% endmethod %}
191204

192205
# Example Code
@@ -224,13 +237,16 @@ tree_traversal.c
224237
{% sample lang="rs"%}
225238
### Rust
226239
[import, lang:"rust"](code/rust/tree.rs)
240+
### Haskell
241+
{% sample lang="hs"%}
242+
[import, lang:"haskell"](code/haskell/TreeTraversal.hs)
227243
{% endmethod %}
228244

229245

230246
<script>
231247
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
232248
</script>
233-
$$
249+
$$
234250
\newcommand{\d}{\mathrm{d}}
235251
\newcommand{\bff}{\boldsymbol{f}}
236252
\newcommand{\bfg}{\boldsymbol{g}}
@@ -249,4 +265,3 @@ $$
249265
\newcommand{\bfomega}{\boldsymbol{\omega}}
250266
\newcommand{\bftau}{\boldsymbol{\tau}}
251267
$$
252-

0 commit comments

Comments
 (0)