top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Short note on Binary Search Tree (BST)

+1 vote
752 views

BINARY SEARCH TREE (BST)

A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties −

  • The left sub-tree of a node has a key less than or equal to its parent node's key.

  • The right sub-tree of a node has a key greater than to its parent node's key.

 

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:-


posted Apr 24, 2017 by Pooja Singh

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value.

A data structure in which insertion and search operations are very fast irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash technique to generate an index where an element is to be inserted or is to be located.

Hashing:-

Hashing is the function or routine used to assign the key values to the each entity in the database. Using hashing, We can easily access or search the values from database.
Hashing is implemented by using Hash Table data structure. Item are in the (key,value) format.

Applications of Hashing:-

  • Hash tables are generally used to implement associative arrays because of their constant amortized time search and insertion. Hashing is used in them to index and retrieve items.
  • Hashing as a method is used in comparing strings by generating rolling hash as part of Rabin-Karp algorithm.
  • Hashing algorithms(Hash functions) are widely used in cryptography. These include the message-digest hash functions like MD5 used for hashing digital signatures into a shorter value called a  message-digest.
  • Consistent hashing, a special kind of hashing is used to map values to  cache machines.

Basic Operations:-

Following are the basic primary operations of a hash table.

  • Search − Searches an element in a hash table.

  • Insert − inserts an element in a hash table.

  • Delete − Deletes an element from a hash table.

Search Operation:-

An element is to be searched, compute the hash code of the key passed and locate the element using that hash code as index in the array. Use linear probing to get the element ahead if the element is not found at the computed hash code.

The code snippet for search operation is given below:-

    struct DataItem *search(int key)

       {
    int hashIndex = hashCode(key);  
//get the hash
    while(hashArray[hashIndex] != NULL)   //move in array until an empty

 

            {
   if(hashArray[hashIndex]->key == key)
   return hashArray[hashIndex];   //go to next cell
   ++hashIndex;   //wrap around the table
    hashIndex %= SIZE;
           }
    return NULL;       
      }

Insert Operation:-

An element is to be inserted, compute the hash code of the key passed and locate the index using that hash code as an index in the array. Use linear probing for empty location, if an element is found at the computed hash code.

The code snippet for Insertion operation is given below:-

void insert(int key,int data)      

{
   struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
   item->data = data; 
   item->key = key;     //get the hash
   int hashIndex = hashCode(key);  //move in array until an empty or deleted cell
   while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1)

           {
   ++hashIndex;  //wrap around the table
   hashIndex %= SIZE;
           }
   hashArray[hashIndex] = item;       

}

Delete Operation:-

An element is to be deleted, compute the hash code of the key passed and locate the index using that hash code as an index in the array.

The code snippet for Delete operation is given below:

struct DataItem* delete(struct DataItem* item)   

{
     int key = item->key;  //get the hash
     int hashIndex = hashCode(key);   //move in array until an empty
     while(hashArray[hashIndex] !=NULL)

        {
     if(hashArray[hashIndex]->key == key)

               {
     struct DataItem* temp = hashArray[hashIndex];   //assign a dummy item at deleted position
     hashArray[hashIndex] = dummyItem;
     return temp;
               }
     ++hashIndex;  //wrap around the table
     hashIndex %= SIZE;
       
 } 
     return NULL;        

 } 

Go through this video:-

https://www.youtube.com/watch?v=MfhjkfocRR0
READ MORE
...