I am new to the Golang world & trying to implement a BST in Golang. But while inserting/updating pointers values are not getting updated/inserted. For example in the following code the output is:
3
[]
my code:
package main
import "fmt"
var inOrderTrace []int = []int{}
type node struct {
value int
leftChild *node
rightChild *node
}
type tree struct {
root *node
len int
}
func (myTree tree) inOrderTraverse(node *node) {
if node == nil {
return
}
myTree.inOrderTraverse(node.leftChild)
inOrderTrace = append(inOrderTrace, node.value)
myTree.inOrderTraverse(node.rightChild)
}
func (myTree *tree) insertNode(nodeToManipulate *node, toInsert int) {
if nodeToManipulate == nil {
nodeToManipulate = &node{toInsert, nil, nil}
myTree.len++
return
}
if nodeToManipulate.value > toInsert {
myTree.insertNode(nodeToManipulate.leftChild, toInsert)
} else {
myTree.insertNode(nodeToManipulate.rightChild, toInsert)
}
}
func main() {
myTree := &tree{nil, 0}
var elements []int = []int{1, 0, 2}
for _, element := range elements {
myTree.insertNode(myTree.root, element)
}
myTree.inOrderTraverse(myTree.root)
fmt.Println(myTree.len)
fmt.Println(inOrderTrace)
}
I am expecting the inserted values to be printed.Thanks.