I'm beginner to Golang and trying to build a Binary Search Tree. My source code to do it:
package main
import (
"fmt"
"math/rand"
"time"
)
type Node struct{
value int
left *Node
right *Node
}
func insert(root *Node,v int){
if root==nil{
root=&Node{v,nil,nil}
} else if v<root.value{
insert(root.left,v)
} else{
insert(root.right,v)
}
}
func inTraverse(root *Node){
if (root==nil){
return
}
inTraverse(root.left)
fmt.Printf("%d",root.value)
inTraverse(root.right)
}
func main() {
var treeRoot *Node
rand.Seed(time.Now().UnixNano())
n:=6
var a[6]int
for i:=0;i<n;i++{
a[i]=rand.Intn(20)+1
}
fmt.Println("Array of integer: ")
for i:=0;i<n;i++{
fmt.Printf("%d ",a[i])
}
fmt.Println()
for i:=0;i<n;i++{
insert(treeRoot,a[i])
}
inTraverse(treeRoot)
fmt.Println()
}
The result shows an empty tree. What's wrong with my code? Does Golang have pass-by-value or pass-by-reference? Please help me solve this.