Featured Article

Binary Search Tree Array Implementation in C++

William Aug 14, 2026

Binary Search Trees (BSTs) are efficient data structures that provide a balanced way to store and retrieve data. In programming, we often use arrays to represent BSTs, combining the benefits of both trees and arrays. In this article, we'll delve into the implementation of binary search trees using arrays in C++.

Binary Tree
Binary Tree

Before we dive into the implementation, let's recall the basics of binary search trees and arrays. A binary search tree is a node-based data structure that has at most two children, referred to as the left child and the right child. Meanwhile, an array is a collection of items stored at contiguous memory locations. Now, let's explore how we can represent a BST using an array in C++.

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

Array Representation of Binary Search Trees

The array representation of a BST is also known as an implicit tree, where the array is used instead of explicit links between nodes. The key concept is that the left and right children of a node at index i are stored at indices 2i + 1 and 2i + 2, respectively.

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

Here's a simple C++ implementation of a binary search tree using an array:

```cpp const int MAX_N = 1e5 + 5; int array[MAX_N]; void insert(int value, int& root) { if (root == -1) { root = 0; array[root] = value; return; } if (value < array[root]) { insert(value, array[2 * root + 1]); } else if (value > array[root]) { insert(value, array[2 * root + 2]); } } bool search(int value, int root) { if (root == -1) return false; if (value == array[root]) return true; if (value < array[root]) return search(value, array[2 * root + 1]); return search(value, array[2 * root + 2]); } ```

Insertion

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

The `insert` function adds a new value to the BST. It starts with an empty tree, represented by a root index of -1. If the value is smaller than the root node, it's added to the left subtree; otherwise, it's added to the right subtree. The indices for the left and right children are calculated using the formula `2i + 1` and `2i + 2`, respectively.

Example: To insert {10, 5, 15, 3, 6}:

```cpp int root = -1; insert(10, root); insert(5, root); insert(15, root); insert(3, root); insert(6, root); ```

Searching

How to Implement Binary Search Using Iterative Method
How to Implement Binary Search Using Iterative Method

The `search` function checks whether a given value exists in the BST. It starts from the root node and recursively explores the left or right subtree based on whether the target value is smaller or larger than the current node.

Example: Search for 6:

```cpp bool found = search(6, root); // Should return true ```

Using Arrays for Inorder Traversal

How to Implement Binary Search Tree in Java? Example
How to Implement Binary Search Tree in Java? Example

Inorder traversal of a binary search tree results in a sorted array. This property can be used to easily convert a BST to an array and vice versa. Here's a simple implementation of converting a BST to an array using inorder traversal:

```cpp void inorderTraversal(int root, int arr[], int& index) { if (root == -1) return; inorderTraversal(array[2 * root + 1], arr, index); arr[index++] = array[root]; inorderTraversal(array[2 * root + 2], arr, index); } ```

With this understanding of representing binary search trees using arrays in C++, you can now leverage the strengths of both data structures to optimize your algorithms. Understanding this representation enables you to work with trees in a more space-efficient way, as you're not explicitly storing links between nodes.

Binary trees
Binary trees
Difference between Binary Tree and Binary Search Tree
Difference between Binary Tree and Binary Search Tree
Binary Search Tree (BST) Complete Implementation.
Binary Search Tree (BST) Complete Implementation.
Binary Search Trees in Go
Binary Search Trees in Go
Binary Tree Array Implementation Example
Binary Tree Array Implementation Example
Representing binary trees with arrays
Representing binary trees with arrays
binary search algorithm
binary search algorithm
Binary Search Trees and Recursion
Binary Search Trees and Recursion
Linear And Binary Search Array in C - Probabu
Linear And Binary Search Array in C - Probabu
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…
Binary Search Explained 🔍 | DSA Algorithm Guide for Coding Interviews
Binary Search Explained 🔍 | DSA Algorithm Guide for Coding Interviews
BT
BT
Sorted Array to Balanced  BST - GeeksforGeeks
Sorted Array to Balanced BST - GeeksforGeeks
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
Introduction to Binary Search Trees with Python
Introduction to Binary Search Trees with Python
What Is a Binary Search Tree?
What Is a Binary Search Tree?
Understanding The Binary Search Algorithm In Python | Towards Data Science
Understanding The Binary Search Algorithm In Python | Towards Data Science
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

Keep exploring, keep coding, and happy tree-traversing!