Binary Search Tree
A Binary Search Tree is a binary tree with a search property where the elements in the left sub-tree are less than the root and elements in the right sub-tree are greater than the root.
For example:

Inserting an element in a BST (Binary Search Tree)
To insert an element in the Binary Search Tree, we first need to find where to insert it. This can be done by traversing left or right as we did for searching for an element.
The following is the /algorithm to do that.
- Check if the root is present or not, if not then it’s the first element.
- If the root is present then we need to find where to insert it.
- Move left or right by comparing until the current node becomes null
- Once the current node becomes null, make it the child of the parent’s node.
The C# implementation of that is as follows.
public void InsertNode (object data)
{
TNode newNode = new TNode(data);
if (root.Data == null) //First node insertion
root = newNode;
else
{
current = root;
while (true)
{
tempParent = current;
if (Convert.ToInt32(newNode.Data) < Convert.ToInt32(current.Data))
{
current = current.Left;
if(current== null)
{
tempParent.Left =newNode;
newNode.Parent =tempParent;
return;
}
}
else
{
current = current.Right;
if(current == null)
{
tempParent.Right= newNode;
newNode.Parent =tempParent;
return;
}
}
}
}
}
The following is the test results of the implementation.
Before Insert:

Insert call:
bst.InsertNode(17);
After Insert:

Deleting an element in a BST (Binary Search Tree)
To delete an element in the Binary Search Tree, we first need to look at the children of it and based on that the method to delete a node is decided. Basically there are three odd cases for deleting a node.
-
The node has no children (in other words it’s a leaf node).
-
The node has either a left or right child.
-
The node has two children.
Now let’s look at each case one by one.
Case 1: The node has no child (in other words it’s a leaf node).
This is the most trivial case where we just need to set the reference of its parent to null.
Case 2: The node has either left or right child.
This is also a simple case where we need to make the children of itself to the children to its parent.
This is similar to how we remove node in Linked List.
Case 3: The node has two children.







Andy SimmonsPosted Jun 28, 2022, 3:33 PM
What if you tried to delete 25 and 22 had a left child? Let's say 22's left child was 21. Wouldn't we end up assigning 21 to 25's right? (predNode.Parent.Right = tempChild;) This works if the predecessor isn't the left child of the node to delete, but if it is the left child of the node to delete and has its own left child, it doesn't work (unless I'm missing something). Thanks!
Priyam PatelPosted Sep 17, 2021, 12:31 AM
How to insert multiple random nodes let's say from 1 to 1000 in binary search tree in c#
Prakash TripathiPosted Mar 6, 2016, 10:25 PM
Thnx Kumaresh.
Prakash TripathiPosted Mar 6, 2016, 10:24 PM
Thnx Karthiga.
Kumaresh RajalingamPosted Mar 6, 2016, 8:25 PM
good one
Kumaresh RajalingamPosted Mar 6, 2016, 8:25 PM
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 8:13 PM
good one
Sr KarthigaPosted Mar 6, 2016, 8:13 PM
Nice explanation
Prakash TripathiPosted Feb 25, 2016, 7:27 AM
Thnx Sonu.
Sonu ChaudharyPosted Feb 25, 2016, 7:16 AM
nice article