Skip to content

Commit fc53db1

Browse files
Julianleios
Julian
authored andcommitted
add the golang files (#406)
1 parent f9158d4 commit fc53db1

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type node struct {
6+
id int
7+
children []*node
8+
}
9+
10+
func dfsRecursive(n *node) {
11+
fmt.Println(n.id)
12+
for _, child := range n.children {
13+
dfsRecursive(child)
14+
}
15+
}
16+
17+
func dfsRecursivePostorder(n *node) {
18+
for _, child := range n.children {
19+
dfsRecursive(child)
20+
}
21+
fmt.Println(n.id)
22+
}
23+
24+
func dfsRecursiveInorderBtree(n *node) {
25+
switch len(n.children) {
26+
case 2:
27+
dfsRecursiveInorderBtree(n.children[0])
28+
fmt.Println(n.id)
29+
dfsRecursiveInorderBtree(n.children[1])
30+
case 1:
31+
dfsRecursiveInorderBtree(n.children[0])
32+
fmt.Println(n.id)
33+
case 0:
34+
fmt.Println(n.id)
35+
default:
36+
fmt.Println("This is not a binary tree")
37+
}
38+
}
39+
40+
func dfsStack(n *node) {
41+
stack := []*node{n}
42+
43+
for len(stack) > 0 {
44+
cur := stack[0]
45+
stack = stack[1:]
46+
fmt.Println(cur.id)
47+
stack = append(cur.children, stack...)
48+
}
49+
}
50+
51+
func bfsQueue(n *node) {
52+
queue := []*node{n}
53+
54+
for len(queue) > 0 {
55+
cur := queue[0]
56+
queue = queue[1:]
57+
fmt.Println(cur.id)
58+
queue = append(queue, cur.children...)
59+
}
60+
}
61+
62+
func createTree(numRow, numChild int) *node {
63+
if numRow == 0 {
64+
return &node{id: 0}
65+
}
66+
67+
cur := new(node)
68+
cur.id = numRow
69+
70+
for x := 0; x < numChild; x++ {
71+
cur.children = append(cur.children, createTree(numRow-1, numChild))
72+
}
73+
return cur
74+
}
75+
76+
func main() {
77+
root := createTree(3, 3)
78+
binTree := createTree(3, 2)
79+
80+
fmt.Println("DFS recursive:")
81+
dfsRecursive(root)
82+
fmt.Println("DFS post order recursive:")
83+
dfsRecursivePostorder(root)
84+
fmt.Println("DFS inorder binary tree:")
85+
dfsRecursiveInorderBtree(binTree)
86+
fmt.Println("DFS stack:")
87+
dfsStack(root)
88+
fmt.Println("BFS queue:")
89+
bfsQueue(root)
90+
}

contents/tree_traversal/tree_traversal.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ This has not been implemented in your chosen language, so here is the Julia code
3232
[import:3-27, lang:"php"](code/php/tree_traversal.php)
3333
{% sample lang="crystal" %}
3434
[import:1-5, lang:"crystal"](code/crystal/tree-traversal.cr)
35+
{% sample lang="go" %}
36+
[import:5-8, lang:"golang"](code/golang/treetraversal.go)
3537
{% endmethod %}
3638

3739
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:
@@ -66,6 +68,8 @@ Because of this, the most straightforward way to traverse the tree might be recu
6668
[import:31-35, lang:"php"](code/php/tree_traversal.php)
6769
{% sample lang="crystal" %}
6870
[import:7-10, lang:"crystal"](code/crystal/tree-traversal.cr)
71+
{% sample lang="go" %}
72+
[import:10-15, lang:"golang"](code/golang/treetraversal.go)
6973
{% endmethod %}
7074

7175
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:
@@ -108,6 +112,8 @@ Now, in this case the first element searched through is still the root of the tr
108112
[import:37-41, lang:"php"](code/php/tree_traversal.php)
109113
{% sample lang="crystal" %}
110114
[import:12-15, lang:"crystal"](code/crystal/tree-traversal.cr)
115+
{% sample lang="go" %}
116+
[import:17-22, lang:"golang"](code/golang/treetraversal.go)
111117
{% endmethod %}
112118

113119
<p>
@@ -145,6 +151,8 @@ In this case, the first node visited is at the bottom of the tree and moves up t
145151
[import:43-62, lang:"php"](code/php/tree_traversal.php)
146152
{% sample lang="crystal" %}
147153
[import:17-31, lang:"crystal"](code/crystal/tree-traversal.cr)
154+
{% sample lang="go" %}
155+
[import:24-38, lang:"golang"](code/golang/treetraversal.go)
148156
{% endmethod %}
149157

150158
<p>
@@ -192,6 +200,8 @@ In code, it looks like this:
192200
[import:64-73, lang:"php"](code/php/tree_traversal.php)
193201
{% sample lang="crystal" %}
194202
[import:33-41, lang:"crystal"](code/crystal/tree-traversal.cr)
203+
{% sample lang="go" %}
204+
[import:40-49, lang:"golang"](code/golang/treetraversal.go)
195205
{% endmethod %}
196206

197207
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:
@@ -231,6 +241,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
231241
[import:65-74, lang:"php"](code/php/tree_traversal.php)
232242
{% sample lang="crystal" %}
233243
[import:43-51, lang:"crystal"](code/crystal/tree-traversal.cr)
244+
{% sample lang="go" %}
245+
[import:51-60, lang:"golang"](code/golang/treetraversal.go)
234246
{% endmethod %}
235247

236248
## Example Code
@@ -272,6 +284,8 @@ The code snippets were taken from this [Scratch project](https://scratch.mit.edu
272284
[import, lang:"php"](code/php/tree_traversal.php)
273285
{% sample lang="crystal" %}
274286
[import, lang:"crystal"](code/crystal/tree-traversal.cr)
287+
{% sample lang="go" %}
288+
[import, lang:"golang"](code/golang/treetraversal.go)
275289
{% endmethod %}
276290

277291

0 commit comments

Comments
 (0)