I have been recently watching a video where Ian Lance Taylor was presenting the new generics implementation in Go.
As an example he shows a simple implementation of a binary tree like this
type Node[T any] struct {
left, right *Node[T]
data T
}
type bTree[T any] struct {
root *Node[T]
compare func(T, T) int
}
and an implementation of a search method for such binary tree like this
func (bt *bTree[T]) findDoublePointer(v T) **Node[T] {
pl := &bt.root
for *pl != nil {
switch cmpRes := bt.compare(v, (*pl).data); {
case cmpRes < 0:
pl = &(*pl).left
case cmpRes > 0:
pl = &(*pl).right
default:
return pl
}
}
return pl
}
The method that I have called findDoublePointer returns a pointer to a pointer to a Node of the tree.
My question is whether this is just an example to explain something else, in this case the generics idea, or whether there is a sound reason to return a pointer to a pointer in this case.
The reason I ask this question is that it seems to me that a simpler version of the find method can be achieved with such code
func (bt *bTree[T]) findPointer(v T) *Node[T] {
pl := bt.root
for pl != nil {
switch cmpRes := bt.compare(v, (*pl).data); {
case cmpRes < 0:
pl = (*pl).left
case cmpRes > 0:
pl = (*pl).right
default:
return pl
}
}
return pl
}