August 2019
Beginner to intermediate
798 pages
17h 2m
English
This section will illustrate how to implement a binary tree in Go using the source code found in binTree.go as an example. The contents of binTree.go will be presented in five parts. The first part is next:
package main
import (
"fmt"
"math/rand"
"time"
)
type Tree struct {
Left *Tree
Value int
Right *Tree
}
What you see here is the definition of the node of the tree using a Go structure. The math/rand package is used for populating the tree with random numbers, as we do not have any real data.
The second code portion from binTree.go comes with the next Go code:
func traverse(t *Tree) {
if t == nil {
return
}
traverse(t.Left)
fmt.Print(t.Value, " ")
traverse(t.Right)
}
The traverse() function reveals how ...
Read now
Unlock full access