Hi there, I am trying to implement a binary tree in go using generics.
type Node[T comparable] struct {
value T
left *Node[T]
right *Node[T]
}
func (n *Node[T]) insert(value T) {
if n == nil {
return
}
if value > n.value {
if n.right == nil {
n.right = &Node[T]{value: value}
} else {
n.right.insert(value)
}
} else {
if n.left == nil {
n.left = &Node[T]{value: value}
} else {
n.left.insert(value)
}
}
}
type BinarySearchTree[T comparable] struct {
root Node[T]
}
The code doesn't compile and gives this error: invalid operation: value > n.value (type parameter T is not comparable with >). I already specified the type constraint of Node above to be comparable. What am I doing wrong?