Back to home

Setting up the testTrees implementationRunning Tests
Trees in Golang main image

Trees in Golang

DFS and BFS tree implementations with Golang.

Setting up the test

Set up trees_test.go with the following file:

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 package trees import "testing" func TestGetTreeRoot(t *testing.T) { n1 := Node{8, []Node{}} tree := Tree{} tree.setRoot(&n1) expected := &n1 if observed := tree.getRoot(); observed != expected { t.Fatalf("Trees()) = %v, want %v", observed, expected) } } func TestAppendChild(t *testing.T) { n1 := Node{8, []Node{}} tree := Tree{} tree.setRoot(&n1) n2 := Node{2, []Node{}} n3 := Node{3, []Node{}} n1.append(&n2) n1.append(&n3) expected := 2 if observed := n1.childLength(); observed != expected { t.Fatalf("Trees()) = %v, want %v", observed, expected) } } // This test only compares returned values, not addresses func TestBFS(t *testing.T) { n1 := Node{8, []Node{}} tree := Tree{} tree.setRoot(&n1) n4 := Node{4, []Node{}} n5 := Node{5, []Node{}} n2 := Node{2, []Node{n4}} n3 := Node{3, []Node{n5}} n1.append(&n2) n1.append(&n3) expected := []int{n1.data, n2.data, n3.data, n4.data, n5.data} observed := tree.bfs() for i := range observed { if observed[i] != expected[i] { t.Fatalf("Failed: bfs => %d, want %d", observed[i], expected[i]) } t.Logf("Success: bfs => %d, want %d", observed[i], expected[i]) } } func TestDFS(t *testing.T) { n1 := Node{8, []Node{}} tree := Tree{} tree.setRoot(&n1) n4 := Node{4, []Node{}} n5 := Node{5, []Node{}} n6 := Node{6, []Node{}} n2 := Node{2, []Node{n4, n6}} n3 := Node{3, []Node{n5}} n1.append(&n2) n1.append(&n3) expected := []int{n1.data, n2.data, n4.data, n6.data, n3.data, n5.data} observed := tree.dfs() for i := range observed { if observed[i] != expected[i] { t.Fatalf("Failed: bfs => %d, want %d", observed[i], expected[i]) } t.Logf("Success: bfs => %d, want %d", observed[i], expected[i]) } }

Trees implementation

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 package trees // Tree is a basic tree structure type Tree struct { root *Node } // Node represents a graph vertex with a data point and children type Node struct { data int children []Node } // Trees should have a comment documenting it. func (t *Tree) setRoot(n *Node) { t.root = n } func (t *Tree) getRoot() *Node { return t.root } func (n *Node) append(c *Node) { n.children = append(n.children, *c) } func (n *Node) childLength() int { return len(n.children) } func (t *Tree) bfs() []int { n := t.getRoot() if n == nil { return []int{} } arr := []Node{*n} res := []int{} // Iterate through, if children, push to end of array for len(arr) > 0 { x := arr[0] arr = arr[1:] res = append(res, x.data) arr = append(arr, x.children...) } return res } func (t *Tree) dfs() []int { n := t.getRoot() if n == nil { return []int{} } arr := []Node{*n} res := []int{} // Iterate through, if children, push to end of array for len(arr) > 0 { x := arr[0] arr = arr[1:] res = append(res, x.data) arr = append(x.children, arr...) } return res }

Running Tests

In the directory, run go test.

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.