Binary Search Tree: Search Operation Insertion Operation Deletion Operation
Binary Search Tree: Search Operation Insertion Operation Deletion Operation
A binary search tree (BST), also known as an ordered binary tree, is a node-based
data structure in which each node has no more than two child nodes. Each child
must either be a leaf node or the root of another binary search tree. The left sub-tree
contains only nodes with keys less than the parent node; the right sub-tree contains
only nodes with keys greater than the parent node.
The BST data structure is the basis for a number of highly efficient sorting and
searching algorithms, and it can be used to construct more abstract data structures
including sets, and associative arrays.
To enhance the performance of binary tree, we use special type of binary tree known
as Binary Search Tree. Binary search tree mainly focuses on the search operation
Binary Search Tree is a binary tree in which every node contains only smaller
values in its left subtree and only larger values in its right subtree.
In a binary search tree, all the nodes in left subtree of any node contains smaller
values and all the nodes in right subtree of any node contains larger values as
Example
The following tree is a Binary Search Tree. In this tree, left subtree of every node
contains nodes with smaller values and right subtree of every node contains larger
values.
Operations on a Binary Search Tree
The following operations are performed on a binary search tree...
1. Search
2. Insertion
3. Deletion
Step 2 - Compare the search element with the value of root node in the tree.
Step 3 - If both are matched, then display "Given node is found!!!" and
Step 4 - If both are not matched, then check whether search element is
Step 5 - If search element is smaller, then continue the search process in left
subtree.
Step 6- If search element is larger, then continue the search process in right
subtree.
Step 7 - Repeat the same until we find the exact element or until the search
Step 8 - If we reach to the node having the value equal to the search value
Step 9 - If we reach to the leaf node and if it is also not matched with the
search element, then display "Element is not found" and terminate the
function.
Insertion Operation in BST
complexity. In binary search tree, new node is always inserted as a leaf node. The
its left and rightto NULL.
Step 4 - If the tree is Not Empty, then check whether the value of newNode
Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches
to NULL).
Step 7 - After reaching the leaf node, insert the newNode as left childif the
newNode is smaller or equal to that leaf node or else insert it as right child.
complexity. Deleting a node from Binary search tree includes following three cases...
Step 2 - Delete the node using free function (If it is a leaf) and terminate the
function.
We use the following steps to delete a node with one child from BST...
Step 2 - If it has only one child then create a link between its parent node and
child node.
We use the following steps to delete a node with two children from BST...
step.
Step 4 - Then check whether deleting node came to case 1 or case 2or else
goto step 2
Step 7 - Repeat the same process until the node is deleted from the tree.
Example
10,12,5,4,20,8,7,15 and 13
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
int main()
{
int n , i;
struct node *p , *q , *root;
printf("Enter the number of nodes to be insert: ");
scanf("%d",&n);
return 0;
}
Output
Enter the number of nodes to be insert: 6
Submitted by-
Komal Nandgadkar (25)
Nikita shinde(31)
Pooja malai(33)
Rakshita prabhu(36)
Supriya palkar(45)