Skip to content

Commit b86286c

Browse files
jiegilletleios
authored andcommitted
Haskell: added DFS Stack to Tree Traversal (#395)
* Added DFS Stack * Fixed import lines after reformatting code
1 parent e347d67 commit b86286c

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
data Tree a = Node { node :: a
2-
, forest :: [Tree a]
3-
} deriving (Show)
1+
data Tree a = Node
2+
{ node :: a
3+
, forest :: [Tree a]
4+
} deriving (Show)
45

56
dfs :: Tree a -> [a]
67
dfs (Node x ts) = x : concatMap dfs ts
@@ -9,15 +10,22 @@ dfsPostOrder :: Tree a -> [a]
910
dfsPostOrder (Node x ts) = concatMap dfsPostOrder ts ++ [x]
1011

1112
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
13+
dfsInOrder (Node x []) = [x]
14+
dfsInOrder (Node x [l]) = dfsInOrder l ++ [x] -- Single branch assumed to be left
1415
dfsInOrder (Node x [l, r]) = dfsInOrder l ++ [x] ++ dfsInOrder r
15-
dfsInOrder _ = error "Not a binary tree"
16+
dfsInOrder _ = error "Not a binary tree"
17+
18+
dfsStack :: Tree a -> [a]
19+
dfsStack t = go [t]
20+
where
21+
go [] = []
22+
go ((Node x ts):stack) = x : go (ts ++ stack)
1623

1724
bfs :: Tree a -> [a]
1825
bfs (Node x ts) = x : go ts
19-
where go [] = []
20-
go ts = map node ts ++ go (concatMap forest ts)
26+
where
27+
go [] = []
28+
go ts = map node ts ++ go (concatMap forest ts)
2129

2230
toBin :: Tree a -> Tree a
2331
toBin (Node x ts) = Node x (map toBin $ take 2 ts)
@@ -26,18 +34,19 @@ main = do
2634
print $ dfs testTree
2735
print $ dfsPostOrder testTree
2836
print $ dfsInOrder $ toBin testTree
37+
print $ dfsStack testTree
2938
print $ bfs testTree
3039

3140
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-
]
41+
testTree =
42+
Node
43+
1
44+
[ Node 2 [Node 3 [], Node 4 [Node 5 []]]
45+
, Node
46+
6
47+
[ Node 7 []
48+
, Node 8 [Node 9 [Node 10 [Node 11 []], Node 12 []]]
49+
, Node 13 [Node 14 []]
50+
]
51+
, Node 15 []
52+
]

contents/tree_traversal/tree_traversal.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This has not been implemented in your chosen language, so here is the Julia code
2525
{% sample lang="rs"%}
2626
[import:4-7, lang:"rust"](code/rust/tree.rs)
2727
{% sample lang="hs"%}
28-
[import:1-3, lang:"haskell"](code/haskell/TreeTraversal.hs)
28+
[import:1-4, lang:"haskell"](code/haskell/TreeTraversal.hs)
2929
{% sample lang="swift"%}
3030
[import:1-9, lang:"swift"](code/swift/tree.swift)
3131
{% sample lang="php"%}
@@ -59,7 +59,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
5959
{% sample lang="rs"%}
6060
[import:9-15 lang:"rust"](code/rust/tree.rs)
6161
{% sample lang="hs"%}
62-
[import:5-6, lang:"haskell"](code/haskell/TreeTraversal.hs)
62+
[import:6-7, lang:"haskell"](code/haskell/TreeTraversal.hs)
6363
{% sample lang="swift"%}
6464
[import:24-30, lang:"swift"](code/swift/tree.swift)
6565
{% sample lang="php"%}
@@ -101,7 +101,7 @@ Now, in this case the first element searched through is still the root of the tr
101101
{% sample lang="rs"%}
102102
[import:17-23, lang:"rust"](code/rust/tree.rs)
103103
{% sample lang="hs"%}
104-
[import:8-9, lang:"haskell"](code/haskell/TreeTraversal.hs)
104+
[import:9-10, lang:"haskell"](code/haskell/TreeTraversal.hs)
105105
{% sample lang="swift"%}
106106
[import:32-38, lang:"swift"](code/swift/tree.swift)
107107
{% sample lang="php"%}
@@ -138,7 +138,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
138138
{% sample lang="rs"%}
139139
[import:25-38, lang:"rust"](code/rust/tree.rs)
140140
{% sample lang="hs"%}
141-
[import:11-15, lang:"haskell"](code/haskell/TreeTraversal.hs)
141+
[import:12-16, lang:"haskell"](code/haskell/TreeTraversal.hs)
142142
{% sample lang="swift"%}
143143
[import:40-53, lang:"swift"](code/swift/tree.swift)
144144
{% sample lang="php"%}
@@ -185,8 +185,7 @@ In code, it looks like this:
185185
{% sample lang="rs"%}
186186
[import:40-47, lang:"rust"](code/rust/tree.rs)
187187
{% sample lang="hs"%}
188-
This has not been implemented in your chosen language, so here is the Julia code
189-
[import:45-56, lang:"julia"](code/julia/Tree.jl)
188+
[import:18-22, lang:"haskell"](code/haskell/TreeTraversal.hs)
190189
{% sample lang="swift"%}
191190
[import:55-67, lang:"swift"](code/swift/tree.swift)
192191
{% sample lang="php"%}
@@ -225,7 +224,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
225224
{% sample lang="rs"%}
226225
[import:49-57, lang:"rust"](code/rust/tree.rs)
227226
{% sample lang="hs"%}
228-
[import:17-20, lang:"haskell"](code/haskell/TreeTraversal.hs)
227+
[import:24-28, lang:"haskell"](code/haskell/TreeTraversal.hs)
229228
{% sample lang="swift"%}
230229
[import:69-81, lang:"swift"](code/swift/tree.swift)
231230
{% sample lang="php"%}

0 commit comments

Comments
 (0)