Featured Article

Binary Search Tree C++ Example

William Aug 14, 2026

Binary Search Trees (BSTs) are fundamental data structures in computer science, offering efficient search, insertion, and deletion operations. In C++, we can easily implement a BST to leverage its benefits. Let's explore a basic C++ example of a Binary Search Tree.

a spiral notebook with an image of a tree diagram on the front and bottom page
a spiral notebook with an image of a tree diagram on the front and bottom page

First, we'll define a Node class to represent each tree node, which will contain a data value and pointers to its left and right children. Then, we'll create a BinarySearchTree class with methods for inserting nodes, searching for values, and displaying the tree in-order.

Segment Trees and Binary Indexed Trees
Segment Trees and Binary Indexed Trees

BinarySearchTree Class Implementation

The BinarySearchTree class will have a private Node pointer as its member to represent the root of the tree. It will also have public methods for insert(), search(), and inOrderDisplay().

Binary Tree
Binary Tree

The Node class will have a data member (int) and two smart pointer members (unique_ptr) for its left and right children.

Node Class Definition

Difference between Binary Tree and Binary Search Tree
Difference between Binary Tree and Binary Search Tree

Here's the definition of the Node class:

```cpp struct Node { int data; unique_ptr left; unique_ptr right; }; ```

BinarySearchTree Class Definition

Now, let's define the BinarySearchTree class with its constructor and methods:

Understand Binary Search Tree through Gifs
Understand Binary Search Tree through Gifs

```cpp class BinarySearchTree { private: unique_ptr root; // Private helper functions void insert(Node*& node, int val); bool search(Node* node, int val); void inOrderDisplay(Node* node); public: BinarySearchTree() : root(nullptr) {} void insert(int val) { insert(root.get(), val); } bool search(int val) { return search(root.get(), val); } void inOrderDisplay() { inOrderDisplay(root.get()); } }; ```

The insert() method uses recursion to find the correct position to insert a new node. The search() method also uses recursion to determine if a value exists in the tree. The inOrderDisplay() method is a traversal algorithm that visits nodes in ascending order.

Implementing Helper Functions

Now, let's implement the private helper functions for insert(), search(), and inOrderDisplay().

Binary Search Trees and Recursion
Binary Search Trees and Recursion

insert() Function Implementation

Here's how the insert() function works:

Binary Search Trees in Go
Binary Search Trees in Go
Binary trees
Binary trees
Level Up Coding on LinkedIn: Binary trees explained. A binary tree is a tree data structure where…
Level Up Coding on LinkedIn: Binary trees explained. A binary tree is a tree data structure where…
Heap Sort
Heap Sort
binary search tree introduction
binary search tree introduction
a handwritten text description for the library search tree, which includes several different types of items
a handwritten text description for the library search tree, which includes several different types of items
Binary Search Trees - Adding Nodes - Part 1 - C++ - How to Add Nodes to a Binary Search Tree
Binary Search Trees - Adding Nodes - Part 1 - C++ - How to Add Nodes to a Binary Search Tree
Binary Search Tree
Binary Search Tree
an image of a bunch of circles connected to each other on a white background with blue, red and orange lines
an image of a bunch of circles connected to each other on a white background with blue, red and orange lines
Binary search tree  using JavaScript ES6-Typescript
Binary search tree using JavaScript ES6-Typescript
BFS level order
BFS level order
BT
BT
binary search algorithm
binary search algorithm
a tree diagram with numbers and letters on it
a tree diagram with numbers and letters on it
What Is a Binary Search Tree?
What Is a Binary Search Tree?
How to Implement Binary Search Tree in Java? Example
How to Implement Binary Search Tree in Java? Example
Binary Search Tree (BST) Complete Implementation.
Binary Search Tree (BST) Complete Implementation.
💜 Linear Search vs Binary Search 🔍
💜 Linear Search vs Binary Search 🔍

```cpp void BinarySearchTree::insert(Node*& node, int val) { if (node == nullptr) { node = make_unique(val); } else if (val < node->data) { insert(node->left, val); } else if (val > node->data) { insert(node->right, val); } } ```

search() Function Implementation

Here's the implementation of the search() function:

```cpp bool BinarySearchTree::search(Node* node, int val) { if (node == nullptr) { return false; } else if (val == node->data) { return true; } else if (val < node->data) { return search(node->left, val); } else { return search(node->right, val); } } ```

inOrderDisplay() Function Implementation

Finally, let's implement the inOrderDisplay() function for traversing and displaying the tree's nodes in ascending order:

```cpp void BinarySearchTree::inOrderDisplay(Node* node) { if (node != nullptr) { inOrderDisplay(node->left); cout << node->data << " "; inOrderDisplay(node->right); } } ```

With these implementations, you can now create a BinarySearchTree object and use its methods to insert values, search for values, and display the tree's contents in-order:

```cpp int main() { BinarySearchTree bst; bst.insert(50); bst.insert(30); bst.insert(20); bst.insert(40); bst.insert(70); bst.insert(60); bst.insert(80); cout << "In-order traversal: "; bst.inOrderDisplay(); cout << "\nSearch 40: " << (bst.search(40) ? "Found" : "Not Found") << endl; cout << "Search 90: " << (bst.search(90) ? "Found" : "Not Found") << endl; return 0; } ```

The output of this code will be:

```cpp In-order traversal: 20 30 40 50 60 70 80 Search 40: Found Search 90: Not Found ```

This Binary Search Tree example in C++ demonstrates a simple and efficient data structure for organizing and operating on data. To further explore BSTs, consider studying balanced search trees, tree rotations, and more advanced applications like AVL trees and red-black trees.