A Binary Search Tree (BST) is a rooted binary tree, whose nodes each store a key (and optionally, an associated value), and each has two distinguished subtrees, commonly denoted left and right. The tree should satisfy the BST property, which states that each node’s key must be greater than all keys stored in the left subtree and not greater than all keys in the right subtree. Ideally, unique values should be present in the tree.

 
BST

Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets, multisets, and associative arrays (maps, multimaps, etc.).

Practice this problem

Recursive Version

When looking for a place to insert a new key, traverse the tree from root-to-leaf, making comparisons to keys stored in the tree’s nodes and deciding based on the comparison to continue searching in the left or right subtrees. In other words, we examine the root and recursively insert the new node to the left subtree if its key is less than that of the root or the right subtree if its key is greater than or equal to the root.

Insertion in the BST

Following is the implementation of the above approach in C++, Java, and Python:

C++


Download  Run Code

Output:

8 10 12 15 16 20 25

Java


Download  Run Code

Output:

8 10 12 15 16 20 25

Python


Download  Run Code

Output:

8 10 12 15 16 20 25

We can modify the above C++ solution to pass the root node by reference instead of returning it from the function.

Download  Run Code

Iterative Version

Another way to explain the insertion is to insert a new node into the tree. Initially, the key is compared with that of the root. If its key is less than the root’s, it is then compared with the root’s left child’s key. If its key is greater, it is compared with the root’s right child. This process continues until the new node is compared with a leaf node, and then it is added as this node’s right or left child, depending on its key: if the key is less than the leaf’s key, then it is inserted as the leaf’s left child; otherwise, as to the leaf’s right child.

The iterative version is demonstrated below in C++, Java, and Python:

C++


Download  Run Code

Output:

8 10 12 15 16 20 25

Java


Download  Run Code

Output:

8 10 12 15 16 20 25

Python


Download  Run Code

Output:

8 10 12 15 16 20 25

The time complexity of the above solution is O(h), where h is the BST height. The BST height in the worst-case is as much as the total number of keys in the BST. The worst case happens when given keys are sorted in ascending or descending order, and we get a skewed tree (all the nodes except the leaf have one and only one child).

For height-balanced BSTs, with each comparison, skip about half of the tree so that each insertion operation takes time proportional to the logarithm of the total number of items n stored in the tree, i.e., log2n. This is much better than the linear time required to find items by key in an (unsorted) array but slower than the corresponding operations on hash tables.

 
The space used by the recursive routine is also proportional to the tree’s height, whereas the iterative version doesn’t require any extra space.

 
Also See:

Search a given key in BST – Iterative and Recursive Solution

Deletion from BST (Binary Search Tree)

 
Exercise: Modify the solution to construct a height-balanced BST.

 
References: https://en.wikipedia.org/wiki/Binary_search_tree