Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Binary Search Tree
3.
Inserting a node into Binary Search Tree 
4.
Deleting a node from a Binary Search Tree
4.1.
1. ‘N’ has no children
4.2.
2. ‘N’ has one child
4.3.
3. ‘N’ has two children
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Binary Search Trees

gp-icon
Discrete Mathematics
Free guided path
3 chapters
31+ problems
gp-badge
Earn badges and level up

Introduction

When we are talking about data structures, we cannot forget the name is Tree. The tree is an essential Data Structure, helpful in hierarchically storing data. When we talk about trees in detail, the classification of trees is necessary to discuss. For now, we will only discuss one classification of the tree, i.e., the Binary Search Tree.

This article will help us understand the Binary Search Tree and its properties. So let's start to enhance our knowledge about Binary Search Trees.

Binary Search Tree

The first word of ‘Binary Search Trees’, namely ‘Binary’, tells us that every node in the tree can have two children: the left child and the right child. The data of the right child and the left child must be greater and smaller than the data of the parent node, respectively.

Lets us see an example of a binary search tree.

In this BST, we can observe that the left child of every node has a value less than its parent node, and the right child of every node has a value greater than its parent node.

Now let us understand the procedure to insert and delete a node into the Binary Search Tree.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Inserting a node into Binary Search Tree 

Consider a Binary Search Tree ‘T’ and an arbitrary node ‘N’ having value stored as ‘Val’.  Below is the procedure to insert the node ‘N’ into ‘T’. 

  • First, we will compare the value ‘Val’ with the data stored in the root node. 
  • If ‘Val’ is greater than the root node's value, we will move to its right subtree.
  • If ‘Val’ is smaller than the root node's value, we will move to its left subtree.
  • We will repeat the first to third step until the current root node does not have left and right subtree. 
  • Now, if ‘Val’ is smaller than the value of the current root node, we will insert node ‘N’ as its left child.
  • Else if ‘Val’ is greater than the value of the current root node, we will insert node ‘N’ as its right child.

Now let us try inserting a node with ‘Val’ equal to 5 into the above tree.


Comparing the value ‘5’ from the value of the root and finding it to be greater, so moving to the right subtree. 

Comparing the value ‘5’ from the value of the current root and finding it to be smaller, so moving to the left subtree.

Comparing the value ‘5’ from the value of the current root and finding it to be greater, so moving to the right subtree.

On finding the current root without any subtree, we will insert the node ‘N’ as the right child.

Deleting a node from a Binary Search Tree

Let us consider a Binary Search Tree ‘T’ and the node to be deleted as ‘N’. Deletion of a node can be classified based on the number of children of the node to be deleted.  

1. ‘N’ has no children

When the node ‘N’ has no children, we can simply replace it will the null value.
For Example, the node with the value as ‘8’ is to be removed in the tree below.

Simply the node with the value ‘8’ can be removed.

2. ‘N’ has one child

When the node ‘N’ has only one child, we can replace it with its only child.
For Example, the node having the value as ‘4’ is removed in the below tree.

Simply the node with the value ‘4’ can be replaced with its only child, and then the replaced node will be removed.

3. ‘N’ has two children

When node ‘N’ has both children, we will need to determine the immediate predecessor. To find the immediate predecessor, we need to move to the left child of the node ‘N’ and then keep moving to the right child of the current node unless there is no right child. Then the node we have is the immediate predecessor. 

Now this immediate predecessor can be replaced with the node ‘N’. And then, the replaced node can be removed with the first or second method defined above accordingly. 
For Example, the node with the value as ‘7’ is to be removed in the tree below.

We will have to find its immediate predecessor to replace it with. The node with the value ‘5’ is the immediate predecessor with the above algorithm. 

The node ‘N’ will be replaced with the immediate predecessor.

And then, the replaced node will be removed.

FAQs

  1. What is a tree?
    A tree is a data structure that stores data in the form of nodes to maintain a hierarchy.
     
  2. What do you understand from the term Binary Search Tree?
    A binary Search Tree is that classification of Trees in which every node in the tree can have at most two children, the left child, and the right child. The data of the right child and the left child must be greater and smaller than the data of the parent node, respectively.
     
  3. How many maximum children can each Binary Search Tree node have?
    The Binary Search Tree node can have a maximum of two children. The children are called left and right children.
     
  4. What property must every child node have to follow in a Binary Search Tree?
    The property that every child node has to follow is that if it is a left child, its value should be smaller than the parent node, and if it is a right child, its value should be greater than the parent node.
     
  5. What is the method to find the immediate predecessor?
    To find the immediate predecessor, we need to move to the left child of the node ‘N’ and then keep moving to the right child of the current node unless there is no right child. Then the node we have is the immediate predecessor. 

Key Takeaways

In the above article, we have learned about Binary Search Trees and their properties. We also covered the method to insert a new node and delete a specific node from it.  

 

Recommended problems -

 

If you want to learn more about such concepts, head over to our library section for many such interesting blogs. Keep learning.

Happy Coding!

Previous article
Traversing Binary Trees
Next article
Minimum Spanning Tree
Guided path
Free
gridgp-icon
Discrete Mathematics
3 chapters
31+ Problems
gp-badge
Earn badges and level up
Live masterclass