A binary search tree is an ordered binary tree. The left subtree contains nodes whose keys are less than the node’s key value, while the right subtree contains nodes whose keys are greater than or equal to the node’s key value. Moreover, both subtrees are also binary search trees. A binary search tree can retrieve data efficiently.

Binary search tree diagram

Table of Content


Map of binary tree implementations

Part 1 – Binary tree implementation

you are here

Part 2 – Binary search tree implementation
Part 3 – Binary search tree with parent pointer


Define classes

The TreeNode in the binary search tree is the same as the regular binary tree. It has three attributes, data, left, and right. data is the data stored in this node. Its data type can be anything such as Integer, String, or Object. left is the left child. right is the right child. Their data type is TreeNode.

After we define TreeNode, we can define the BinarySearchTree class. It has one attribute, root. It is the node from where most tree operations start.

Please note, since the binary search tree is ordered, in Java, BinarySearchTree’s generic data type extends the Comparable interface, so that we can compare data in insert, delete, or search operations.

Java

JavaScript

Python


Insert

To insert a value into a binary search tree, we have to find the right spot. From the root, we compare the value with each node’s data. If the value is larger than the node’s data, we move to the right child. Otherwise move to the left child, until the node’s left or right child is null. Then we can add the new node with the input value as left or right child.

Java

JavaScript

Python

Doodle

Binary search tree insert node


Delete by key

Deleting a node in a binary search tree is easier than deleting a node in a binary tree. We always promote the in-order successor of the deleted node. So it will be the right child’s leftmost descendent. If the right child is null, promote the left child.

Besides, we don’t need to go through all the nodes in the tree to search. We can just follow the path based on the comparison of the node’s data with the input key. So the time complexity is O(h), and h is the height of the tree.

Java

JavaScript

Python

Doodle

Binary search tree delete node


Depth first search

There are two ways to search, Depth first search and Breadth first search.

Depth-first search (DFS) starts from the root and explores the child nodes as far as possible before backtracking. DFS is usually implemented with recursion or stack.

Java

JavaScript

Python


Breadth first search

Breath First Search (BFS) starts from the root and explores all its children before going to the next level. BFS is usually implemented with Queue.

Both DFS and BFS in the binary search tree have a time complexity of O(h). The search doesn’t need to go through all the nodes in the tree. It follows the path based on the comparison of the node’s data with the input key.

Java

JavaScript

Python

Doodle

Binary search tree search


Free download

Download binary search tree implementation in Java, JavaScript and Python
Data structures illustration PDF


You may also like

What is the time complexity of binary search tree time?

The time complexity of insertion, deletion, and search in a binary search tree is O(h), where h is the height of the tree. When the tree is balanced, the time complexity is O(logn).