Showing posts with label binary. Show all posts
Showing posts with label binary. Show all posts

C++ Program Code to perform binary search on an array of elements, which is arranged in ascending order

Leave a Comment
/* The following C++ Program Code will perform the binary search on a list of elements or array of elements, which is arranged in ascending orderThe user is first asked about the total number of elements & then he is asked to enter those many elements in the list in ascending order. The user can also use any sorting algorithm in order to get rid of this constraint*/
/* See bubble sorting algorithm, insertion sorting algorithm, merge sort algorithm, quick sort algorithm, & selection sort algorithm */

#include<iostream>
#include<stdlib.h>
using namespace std;

template<class T>
T bin(T a[20],T n,T t)
{
                 int l,r,mid;
                 l=0;
                 r=n-1;
                 while(l<=r)
                 {
                                   mid=(l+r)/2;
                                   if(t==a[mid])
                                   return mid;
                                   else
                                   if(t<a[mid])
                                   r=mid-1;
                                   else
                                   l=mid+1;
                 }
                 return -1;
}

int main()
{
    int a[20],n,t,p,i;
    cout<<"\nEnter number of elements :";
    cin>>n;
    cout<<"\nAs binary search algorithm works in sorted array!";
    cout<<"\nEnter "<< n <<" values in ascending order : ";
    for(i=0;i<n;i++)
    cin>>a[i];
    cout<<"\nEnter the element to find : ";
    cin>>t;
    p=bin(a,n,t);
    if(p!=-1)
    cout<<"\nThe element is found at "<<p+1<<" position";
    else
    cout<<"\nElement Not Found!!";
    system("pause");
    return 0;
}


Screenshot:
C++ Program Code to perform binary search on an array of elements, which is arranged in ascending order

Read More...

A C program to implement , Various tree traversal techniques & Various operations on binary search tree

Leave a Comment
//Below a C program to implement
//Various tree traversals
//Operations on binary search tree


#include<stdio.h>                         //for standard input/output functions #include<stdlib.h>                        //for standard library functions
#include<conio.h>                        //for console input/output functions
#include<string.h>                       //for string related functions
 

struct node                                   //node structure is being defined, with character array of 15 length 
{
char data[15];
struct node *left,*right;            
//self referential structure 
 };

void insert(struct node *r, struct node *p) 
//Function to insert nodal element in the tree
 {                                                                      
 if((r->right==NULL)&&(strcmpi(p->data,r->data)>0))
r->right=p;
else if((r->right!=NULL)&&(strcmpi(p->data,r->data)>0))
insert(r->right,p);


if((r->left==NULL)&&(strcmpi(p->data,r->data)< 0))
r->left=p;
else if((r->left!=NULL)&&(strcmpi(p->data,r->data)< 0))
insert(r->left,p);
}

void tree(struct node *r, int c)                     
//Function to create a tree traversal
 {
int top,flag;
struct node *w,*stack[20];
if(r!=NULL)
{
if(c!=4)
{
if(c == 1)
printf(" %s ",r->data);
tree(r->left,c);
if(c == 2)
printf(" %s ",r->data);
tree(r->right,c);
if(c == 3)
printf(" %s ",r->data);
}
}
if(c == 4)
{
top = 0;
w = r;
flag = 0;
while((top != -1)&&(w!=NULL))
{
while((flag == 0) && (w->left!=NULL))
{
stack[top] = w;
top++;
w = w->left;
}
printf(" %s ",w->data);
if(w->right != NULL)
{
w = w->right;
flag = 0;
}
else
{
top--;
w = stack[top];
flag = 1;
}
}
}
}


int main()
{

clrscr();                                                       //For clearing the screen 
int choice,c,i,flag;
char temp='N',temp1[15];
struct node *s,*root,*r,*q;
root = NULL;                                            
//Setting the initial node value to NULL
 do
{                                                                 
//Asking user the choice of operation 
 printf("\n 1. Enter");
printf("\n 2. Delete ");
printf("\n 3. Search ");
printf("\n 4. Display");
printf("\n 5. Exit");                                
//user will enter '5' for exiting the program
 printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)                                          
//The switch case will look up the user's choice
 {
case 1:printf("***** Data Entry ***** ");
do
{
s=(struct node*)malloc(sizeof(struct node));
   //allocating the tree node a memory space 
s->left=NULL;
s->right=NULL;
printf("\nEnter Data : ");
scanf("%s",&s->data);
if(root==NULL)
root=s;
else
insert(root,s);                                                    
//insert() function called
 printf("\nEnter Your Elements[y/n] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;

case 2:printf("****** Delete Operation *******\n");
do
{
printf("\nEnter Element To Be Deleted : ");
scanf("%s",&temp1);
s=root;i=0;flag=0;
do
{
if(strcmpi(s->data,temp1)>0)
{
r=s;
s=s->left;
i=2;
}
if(strcmpi(s->data,temp1)==0)
{
flag=1;
if(i==0)
{
if(root->right!=NULL)
{
q=root->left;
root=root->right;
insert(root,q);
}
if(root->right==NULL)
root=root->left;
}
else
{
if(i==1)
{
q=s->left;
r->right=s->right;
if(s->left!=NULL)
insert(r,q);
}
if(i==2)
{
q=s->right;
r->left=s->left;
if(s->right!=NULL)
insert(r,q);
}
}
}
}
while(flag==0&&s!=NULL);
printf("\n Delete Any More[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;


case 3:printf("****** Search Operation *******\n");
do
{
printf("\n Enter Name To Be Searched");
scanf("%s",temp1);
i=0;
s=root;
while(s!=NULL&&i==0)
{
if(strcmpi(s->data,temp1)< 0)
s=s->right;
if(strcmpi(s->data,temp1)>0)
s=s->left;
if(strcmpi(s->data,temp1)==0)
i=1;
}
if(i==0)
printf("\nElement Not Found\n");
else
printf("\nElement Found\n");
printf("\nEnter More Elements[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;
 

case 4:
do
{
printf("\n 1. Preorder\n 2. Inorder \n 3. Postorder \n 4. Non Recursion \n 5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d",&c);
if(root==NULL)
printf("Tree Not Started Yet");
else
tree(root,c);
                                                                               //Tree function called
 printf("\n Press Any Key To Continue......");
getch();
}
while(c!=5);
break;
}
}
while(choice!=5);
}

//End of program
Read More...

Program In C To Convert Decimal Number To Binary Number

Leave a Comment
#include<math.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("\nEnter the number to convert into binary number:");
scanf("%d",&n);
int r=0;
int s=0,i=0;
while(n!=0)
{
r=n%2;
n=n/2;
s=s+pow(10,i)*r;
++i;
}
printf("\nTHE BINARY EQUIVALAENT OF IS %d",s);
getche();
return 0;
}

Read More...