From df1fa1262c0c9012a4ef5b028bcf29a4b933a78a Mon Sep 17 00:00:00 2001 From: Julian Date: Tue, 25 Sep 2018 22:05:20 +0200 Subject: [PATCH] add the golang files --- CONTRIBUTORS.md | 2 + .../code/golang/treetraversal.go | 90 +++++++++++++++++++ contents/tree_traversal/tree_traversal.md | 14 +++ 3 files changed, 106 insertions(+) create mode 100644 contents/tree_traversal/code/golang/treetraversal.go diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 1cbaed966..9c3abd464 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -65,4 +65,6 @@ Trashtalk Cyrus Burt
Patrik Tesarik +
+Liikt
\ No newline at end of file diff --git a/contents/tree_traversal/code/golang/treetraversal.go b/contents/tree_traversal/code/golang/treetraversal.go new file mode 100644 index 000000000..548f552f7 --- /dev/null +++ b/contents/tree_traversal/code/golang/treetraversal.go @@ -0,0 +1,90 @@ +package main + +import "fmt" + +type node struct { + id int + children []*node +} + +func dfsRecursive(n *node) { + fmt.Println(n.id) + for _, child := range n.children { + dfsRecursive(child) + } +} + +func dfsRecursivePostorder(n *node) { + for _, child := range n.children { + dfsRecursive(child) + } + fmt.Println(n.id) +} + +func dfsRecursiveInorderBtree(n *node) { + switch len(n.children) { + case 2: + dfsRecursiveInorderBtree(n.children[0]) + fmt.Println(n.id) + dfsRecursiveInorderBtree(n.children[1]) + case 1: + dfsRecursiveInorderBtree(n.children[0]) + fmt.Println(n.id) + case 0: + fmt.Println(n.id) + default: + fmt.Println("This is not a binary tree") + } +} + +func dfsStack(n *node) { + stack := []*node{n} + + for len(stack) > 0 { + cur := stack[0] + stack = stack[1:] + fmt.Println(cur.id) + stack = append(cur.children, stack...) + } +} + +func bfsQueue(n *node) { + queue := []*node{n} + + for len(queue) > 0 { + cur := queue[0] + queue = queue[1:] + fmt.Println(cur.id) + queue = append(queue, cur.children...) + } +} + +func createTree(numRow, numChild int) *node { + if numRow == 0 { + return &node{id: 0} + } + + cur := new(node) + cur.id = numRow + + for x := 0; x < numChild; x++ { + cur.children = append(cur.children, createTree(numRow-1, numChild)) + } + return cur +} + +func main() { + root := createTree(3, 3) + binTree := createTree(3, 2) + + fmt.Println("DFS recursive:") + dfsRecursive(root) + fmt.Println("DFS post order recursive:") + dfsRecursivePostorder(root) + fmt.Println("DFS inorder binary tree:") + dfsRecursiveInorderBtree(binTree) + fmt.Println("DFS stack:") + dfsStack(root) + fmt.Println("BFS queue:") + bfsQueue(root) +} diff --git a/contents/tree_traversal/tree_traversal.md b/contents/tree_traversal/tree_traversal.md index 5d77b75c3..e2dcb3eb1 100644 --- a/contents/tree_traversal/tree_traversal.md +++ b/contents/tree_traversal/tree_traversal.md @@ -32,6 +32,8 @@ This has not been implemented in your chosen language, so here is the Julia code [import:3-27, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:1-5, lang:"crystal"](code/crystal/tree-traversal.cr) +{% sample lang="go" %} +[import:5-8, lang:"golang"](code/golang/treetraversal.go) {% endmethod %} 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 [import:31-35, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:7-10, lang:"crystal"](code/crystal/tree-traversal.cr) +{% sample lang="go" %} +[import:10-15, lang:"golang"](code/golang/treetraversal.go) {% endmethod %} 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 [import:37-41, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:12-15, lang:"crystal"](code/crystal/tree-traversal.cr) +{% sample lang="go" %} +[import:17-22, lang:"golang"](code/golang/treetraversal.go) {% endmethod %}

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

@@ -193,6 +201,8 @@ This has not been implemented in your chosen language, so here is the Julia code [import:64-73, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:33-41, lang:"crystal"](code/crystal/tree-traversal.cr) +{% sample lang="go" %} +[import:40-49, lang:"golang"](code/golang/treetraversal.go) {% endmethod %} 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: @@ -232,6 +242,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can [import:65-74, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import:43-51, lang:"crystal"](code/crystal/tree-traversal.cr) +{% sample lang="go" %} +[import:51-60, lang:"golang"](code/golang/treetraversal.go) {% endmethod %} ## Example Code @@ -273,6 +285,8 @@ The code snippets were taken from this [Scratch project](https://scratch.mit.edu [import, lang:"php"](code/php/tree_traversal.php) {% sample lang="crystal" %} [import, lang:"crystal"](code/crystal/tree-traversal.cr) +{% sample lang="go" %} +[import, lang:"golang"](code/golang/treetraversal.go) {% endmethod %}