Sitemap
Pawara Gunawardena

Pawara Gunawardena

Department of Computer Science and Engineering, Faculty of Engineering, University of Moratuwa, Sri Lanka.

Binary Search Tree

3 min readOct 25, 2026

A binary search tree is a binary tree that satisfies the following property:

Get Pawara Gunawardena’s stories in your inbox

Join Medium for free to get updates from this writer.

For each node x and any node y in the subtrees of x,

  • If y is in the left subtree of x, then
y.key < x.key
  • If y is in the right subtree of x, then
y.key > x.key

The above comparison condition excludes duplicate entries. If required, the comparison condition can be modified to allow duplicate values.

Press enter or click to view image in full size

The binary search tree insertion, search, and removal operations take O(log(n)) time in the average case and O(n) time in the worst case, where n is the number of elements.

Implementation

Consider Node of the Binary Search Tree.

struct Node {
int value;
Node* left;
Node* right;

Node(int value, Node* left = nullptr, Node* right = nullptr):value(value), left(left), right(right) {}
};

Define BinarySearchTree with a private Node struct, along with other member variables and member functions.

#pragma once

class BinarySearchTree {
private:
struct Node {
int value;
Node* left;
Node* right;

Node(int value, Node* left = nullptr, Node* right = nullptr):value(value), left(left), right(right) {}
};
Node* insert(Node* subtreeRoot, int value);
bool search(Node* subtreeRoot, int value);
Node* remove(Node* subtreeRoot, int value);
Node *findMinNode(Node* subtreeRoot);
void printInOrder(Node* subtreeRoot);
void clear(Node* node);

Node* root;

public:
BinarySearchTree();
~BinarySearchTree();
BinarySearchTree(const BinarySearchTree& other) = delete;
BinarySearchTree& operator=(const BinarySearchTree& other) = delete;
void insert(int value);
bool search(int value);
void remove(int value);
void printInOrder();
};
#include <iostream>
#include "BinarySearchTree.h"

BinarySearchTree::BinarySearchTree() : root(nullptr) {

}

BinarySearchTree::~BinarySearchTree() {
clear(root);
}

void BinarySearchTree::insert(int value) {
root = insert(root, value);
}

bool BinarySearchTree::search(int value) {
return search(root, value);
}

void BinarySearchTree::remove(int value) {
root = remove(root, value);
}

void BinarySearchTree::printInOrder() {
printInOrder(root);
std::cout << std::endl;
}

BinarySearchTree::Node *BinarySearchTree::insert(Node* subtreeRoot, int value) {
if(!subtreeRoot) {
return new Node(value);
}
else if (value < subtreeRoot->value) {
subtreeRoot->left = insert(subtreeRoot->left, value);
}
else if (value > subtreeRoot->value){
subtreeRoot->right = insert(subtreeRoot->right, value);
}
return subtreeRoot;
}

bool BinarySearchTree::search(Node* subtreeRoot, int value) {
if(!subtreeRoot) {
return false;
}
if (value == subtreeRoot->value) {
return true;
}
else if(value < subtreeRoot->value) {
return search(subtreeRoot->left, value);
}
else {
return search(subtreeRoot->right, value);
}
}

BinarySearchTree::Node *BinarySearchTree::findMinNode(Node* subtreeRoot) {
if(!subtreeRoot || !subtreeRoot->left) {
return subtreeRoot;
}
return findMinNode(subtreeRoot->left);
}

BinarySearchTree::Node *BinarySearchTree::remove(Node* subtreeRoot, int value) {
if(!subtreeRoot) {
return subtreeRoot;
}
if(value < subtreeRoot->value) {
subtreeRoot->left = remove(subtreeRoot->left, value);
}
else if(subtreeRoot->value < value) {
subtreeRoot->right = remove(subtreeRoot->right, value);
}
else {
if(!subtreeRoot->left) {
Node* rightTemp = subtreeRoot -> right;
delete subtreeRoot;
return rightTemp;
}
else if(!subtreeRoot->right) {
Node* leftTemp = subtreeRoot -> left;
delete subtreeRoot;
return leftTemp;
}
Node* inOrderSuccessor = findMinNode(subtreeRoot->right);
subtreeRoot -> value = inOrderSuccessor -> value;
subtreeRoot -> right = remove(subtreeRoot -> right, inOrderSuccessor -> value);
}
return subtreeRoot;
}

void BinarySearchTree::printInOrder(Node* subtreeRoot) {
if (!subtreeRoot) {
return;
}
printInOrder(subtreeRoot->left);
std::cout << subtreeRoot->value << " ";
printInOrder(subtreeRoot->right);
}

void BinarySearchTree::clear(BinarySearchTree::Node* subtreeRoot) {
if(!subtreeRoot) {
return;
}
clear(subtreeRoot->left);
clear(subtreeRoot->right);
std::cout << "Deleting " << subtreeRoot->value << std::endl;
delete subtreeRoot;
}

--

--

Pawara Gunawardena
Pawara Gunawardena

Written by Pawara Gunawardena

Department of Computer Science and Engineering, Faculty of Engineering, University of Moratuwa, Sri Lanka.