Snippet Sage (Beta)

AI-powered, tailor-made snippets at your fingertips.

Sure, here's an implementation of a binary search tree in Python:

python
class Node: def __init__(self, value): self.value = value self.left = None self.right = None class BinarySearchTree: def __init__(self): self.root = None def insert(self, value): new_node = Node(value) if self.root is None: self.root = new_node else: current_node = self.root while True: if value < current_node.value: if current_node.left is None: current_node.left = new_node break else: current_node = current_node.left else: if current_node.right is None: current_node.right = new_node break else: current_node = current_node.right def search(self, value): current_node = self.root while current_node is not None: if value == current_node.value: return True elif value < current_node.value: current_node = current_node.left else: current_node = current_node.right return False

This implementation includes a Node class to represent each node in the tree, and a BinarySearchTree class to represent the tree itself. The insert method adds a new node to the tree in the correct position based on its value, and the search method checks if a given value is present in the tree.