How to Implement a Binary Search Tree in Go

Implement a Binary Search Tree in Go using a recursive node struct with insert and search methods.

Implement a Binary Search Tree in Go by defining a node struct with value and left/right pointers, then adding insert and search methods that recursively traverse the tree.

package main

type Node struct {
	Value int
	Left  *Node
	Right *Node
}

func insert(root *Node, value int) *Node {
	if root == nil {
		return &Node{Value: value}
	}
	if value < root.Value {
		root.Left = insert(root.Left, value)
	} else {
		root.Right = insert(root.Right, value)
	}
	return root
}

func search(root *Node, value int) bool {
	if root == nil {
		return false
	}
	if value == root.Value {
		return true
	}
	if value < root.Value {
		return search(root.Left, value)
	}
	return search(root.Right, value)
}