AfterAcademy Tech
•
11 Feb 2020

Binary Search Tree is a special type of binary tree that has a specific order of elements in it. It follows three basic properties:-

This is the simple structure followed by Binary Search Tree(BST). It is named so because it increases the efficiency of the search operation in a tree(logarithmic) just like in Binary Search.
Important Property: Inorder Traversal of BST returns a sorted arrangement of its values
Since BST is a type of Binary Tree, the same operations are performed in BST too. Although, insertion and deletion in BST are much stricter with predetermined conventions so that even after performing an operation, the properties of BST are not violated.
Let us first see the main operation in BST → Searching.
In a normal binary tree, if we need to search for an element, we need to traverse the entire tree(in the worst case). But in BST, we can optimize this by using its properties to our advantage. Any guesses to how we can achieve that?
The basic idea is quite simple really. Let’s assume we need to search for value item in a BST. When we compare it with the root of the tree, we are left with three cases →
We keep on recursing in this manner and this decreases our search time complexity up to a great extent as we just need to look at one subtree and reject the other subtree saving us the trouble of comparing a batch of values.
Let’s implement this now.
Solution Steps
Our objective is to return True if a node exists with the value equal to the item, else return False.
Pseudo-Code
bool search(TreeNode root, int item)
{
if ( root == NULL )
return False
if ( item == root.val )
return True
else if ( item > root.val )
return search(root.right, item)
else if ( item < root.val )
return search(root.left, item)
}
Complexity Analysis
Upon Analyzing the execution of the above code, we can draw up the conclusion that we scan one level at a time. It works like a binary search in an array with the root acting like the middle element of the tree. We just look at left or right subtree saving us a lot of time. Once we compare the node we select, we move on to either the left or right subtree, hence, moving one level up.
→ Now, we can easily conclude that the worst-case time complexity is the highest level number possible which is equal to the total height of the tree(H).
★ Now, what is the height of BST in terms of N, the total number of nodes? Well, a height-balanced BST has a maximum height of logN.
But in a normal binary search tree, the worst-case time complexity of the searching operation is still O(N), same as the binary tree.
Can you guess the tree structure in the worst-case?
→ It's a left-skewed or right-skewed tree.

Insertion in Binary Search Tree, just like Binary Tree is done at the leaf. But since it needs to maintain the properties of BST, the convention is decided in this case.
Solution Steps
We need to insert a node in BST with value item and return the root of the new modified tree.
Pseudo-Code
TreeNode insert_element(TreeNode root, item)
{
if ( root == NULL )
{
root = TreeNode(item)
return root
}
if ( root.val < item )
root.right = insert_element(root.right, item)
else if ( root.val > item )
root.left = insert_element(root.left, item)
return root
}
Complexity Analysis
Since the new node is inserted at the leaf, the highest value possible for this is the height of the tree. And therefore, just like above the worst-case complexity for this is O(N).
When we delete a node in BST, we may encounter three cases →

Suppose we have to delete the node with value 6, which node should be selected ideally to replace this node?
→ The inorder successor of this node would be the aptest choice to replace this node as its inorder successor is the smallest element that is greater than this node.
Note: The inorder predecessor can be used to replace this node too. But conventionally, we use the inorder successor to replace the node.
Solution Steps
You need to delete the node with value item and then return the root of the modified tree. First, we need to find the node to be deleted and then replace it by the appropriate node if needed.
9. Return the root.
Pseudo-Code
TreeNode delete_element(TreeNode root, int item)
{
if ( root == NULL )
return root
if ( root.val < item)
root.right = delete_element(root.right, item)
else if ( root.val > item )
root.left = delete_element(root.left, item)
else if ( root.val == item )
{
// No child
if ( root.left == NULL and root.right == NULL )
delete root
// Only one child
else if ( root.left == NULL or root.right == NULL )
{
if(root.left != NULL)
{
TreeNode new_root = root.left
delete root
return new_root
}
else
{
TreeNode new_root = root.right
delete root
return new_root
}
}
// Both left and right child exist
else
{
TreeNode new_root = find_inorder_successor(root.right)
root.val = new_root.val
root.right = delete_element(root.right, new_rot.val)
}
}
return root
}
TreeNode find_inorder_successor(TreeNode root)
{
TreeNode curr = root
while ( curr.left != NULL )
curr = curr.left
return curr
}
★ Don’t forget to deallocate memory according to the requirements of the programming language of your choice.
Complexity Analysis
Deletion has similar complexity as insertion because, in this, the worst case is that the node to be deleted is a leaf node or else it has two children and the inorder successor will be at the highest level. Therefore, for deletion too, the worst-case time complexity is O(N).
There are certain variations applied to Binary Search Trees to improve efficiency for certain common operations such as insertion, deletion, etc.
These two are the two most common self-balancing BSTs which allow us to optimize the height of BSTs by changing the structure of BST such that it accommodates all the nodes in a BST tree with minimum height possible.
BSTs are used for a lot of applications due to its ordered structure.
AfterAcademy Tech
Tree Data Structure supports various operations such as insertion, deletion and searching of elements along with few others. This blogs discussed those operations along with explaining its applications.

AfterAcademy Tech
Given a Binary Search Tree such that two of the nodes of this tree have been swapped by mistake. You need to write a program that will recover this BST while also maintaining its original structure

AfterAcademy Tech
In this blog, we will see the difference between a binary search tree and a hash table. We will see which data structure should be used when to solve our problems.

AfterAcademy Tech
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. This is a very famous interview problem and previously asked in Microsoft and Amazon.
