BINARY SEARCH TREE (BST)
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties −
Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree and can be defined as −
left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
Representation
BST is a collection of nodes arranged in a way where they maintain BST properties. Each node has a key and an associated value. While searching, the desired key is compared to the keys in BST and if found, the associated value is retrieved.
Following is a pictorial representation of BST −
observe that the root node key (27) has all less-valued keys on the left sub-tree and the higher valued keys on the right sub-tree.
Various Operations on a Binary Search Tree
The various operations that can be performed on a binary search tree are --
a)searching for a data
b)inserting any data into it
c)deleting any data from it
d)traversing the tree
a)Searching in a Binary Tree - Searching for a data in a binary search tree is much faster than in arrays or linked lists.
Algorithm- The sequence of steps to be followed in searching the binary search are as follows:
1)Start from the root node.
2)Suppose if the data to be searched is ITEM then,it is compared with the value of the root node. If the ITEM is less than the value in the root node, choose the left sub-tree of the root node. If the data to be searched (ITEM) is greater than the value in the root node, choose the right sub tree of that root node.
3) Step 2 is repeated till the ITEM is not found or we reach the leaf node.
b)Insertion of Data into a Binary search Tree - The insertion operation on a binary search tree is very simple.
Algorithm- The sequence of steps to be followed in performing the insertion operation on a binary search tree are as follows.
1)Start from the root node.
2)If the data to be inserted is ITEM, compare this with the value of the root node.
a)If they are the same, just stop because the item already exists.
b)If they are different and if the data to be inserted(ITEM) is less than the value of the root node, choose the left sub-tree.
c)If the data to be inserted (ITEM) is greater than the value of the root node, choose the right sub-tree.
3) Repeat Step 2 until the leaf node is encountered where the item has to be inserted.
c) Deletion of a Node from a Binary Search Tree - Another useful operation that can be performed on a binary search tree is to delete a node from it. Here, deletion of a node N depends on the number of its children.
There are 3 cases that can occur.
case 1: N is a leaf node.
case 2: N has exactly one child.
case 3: N has two children.
d) Traversal Operation on Binary Search Tree - All the traversal operations are applicable in binary search trees. The in-order traversal on the binary search tree gives the sorted order of data in ascending order.
Applications of a Binary Search Tree (BST)
A Binary Search Tree (BST) can have the following applications.
1) Searching for an element.
2) Sorting a set of data
3) Removing the redundancies from a collection of data.
/* Program to build a binary search tree from arrays */
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct node
{
struct node *left ;
char data ;
struct node *right ;
} ;
struct node * buildtree ( int ) ;
void inorder ( struct node * ) ;
char a[ ] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
} ;
void main( )
{
struct node *root ;
clrscr( ) ;
root = buildtree ( 0 ) ;
printf ( "In-order Traversal:\n" ) ;
inorder ( root ) ;
getch( ) ;
}
struct node * buildtree ( int n )
{
struct node *temp = NULL ;
if ( a[n] != '\0' )
{
temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
temp -> left = buildtree ( 2 * n + 1 ) ;
temp -> data = a[n] ;
temp -> right = buildtree ( 2 * n + 2 ) ;
}
return temp ;
}
void inorder ( struct node *root )
{
if ( root != NULL )
{
inorder ( root -> left ) ;
printf ( "%c\t", root -> data ) ;
inorder ( root -> right ) ;
}
}
Go through this video:-
https://www.youtube.com/watch?v=MVAlC3zQBYg