I have started dabbling with golang. I wrote up a binary search tree implementation. I was wondering if there is a cleaner way. Here is my implementation:
type BNode struct {
left *BNode
right *BNode
data int64
}
func insert(n **BNode, data int64) {
if *n == nil {
(*n) = &BNode{data: data, left: nil, right: nil}
} else {
if data <= (*n).data {
insert(&(*n).left, data)
} else {
insert(&(*n).right, data)
}
}
}
I don't particularly like &(*n).right
, is there syntax better than this? Something like &(n->right)
client code looks like:
var root *BNode = nil
insert(&root, 42)
insert(&root, 2)
insert(&root, -2)
insert(&root, 97)`