Wednesday, October 7, 2015

Binary Tree is BST or not c++

#include <iostream>
#include <limits.h>
using namespace std;
struct node
{
int  data;
node *left,*right;
};

void print(node *t)
{
if(t!=NULL)
{
print(t->left);
cout<<t->data<<" ";
print(t->right);

}

}

node * create(int  a)
{
node *t=new node;
t->data=a;
t->left=t->right=NULL;
return t;
}
int isBST(node *t,int min,int max)
{
if(t==NULL)return 1;
if(t!=NULL)
{
if(t->data<min||t->data>max)
return 0;
}

return (isBST(t->left,min,t->data)&&isBST(t->right,t->data,max));
}
int main()
{
node *t=NULL;
t=create(4);
t->left=create(2);
t->right=create(1);
print(t);
if(isBST(t,INT_MIN,INT_MAX))
cout<<" BST \n";
else cout<< "not BST\n";

return 0;
}

String to int and Int to string c++

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream ss;
// string to int
string str="12";
ss<<str;
int a;
ss>>a;
cout<<a<<"\n";
//int to string
ss<<a;
string str1;
ss>>str1;
cout<<str1<<" ";

return 0;
}

Thursday, September 17, 2015

Reverse and Block wise reverse of given Linked List

#include <iostream>
using namespace std;
struct node {
int data;
node *next;
};
void create(node *&t,int a)
{
if(t==NULL)
{
t=new node;
t->data=a;
t->next=NULL;
}
else
{
create(t->next,a);
}
}
void rev(node *&t)
{
node *temp=NULL;
node *next;
node *cur=t;
while(t!=NULL)
{
next=t->next;
t->next=temp;
temp=t;
t=next;
}
t=temp;
}
void print(node *t)
{

while(t!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
cout<<endl;
}
node *blockrev(node *t,int a)
{
node *next,*cur=t;
node *head;
int flag=0;

node *temp=NULL;
node *curhead=t;
for(int i=0;i<a&&t!=NULL;i++)
{
next=t->next;
t->next=temp;
temp=t;
t=next;
}

if(next!=NULL)
curhead->next=blockrev(next,a);

return temp;
}
int main()
{
int a[]={2,3,4,5,6,7,8,'\0'};
int n=7;
node *t=NULL;
for(int i=0;i<n;i++)
{
create(t,a[i]);
}
print(t);
cout<<"reversing the list\n";
rev(t);
print(t);
rev(t);
cout<<"\nblock wise reverse of list\n";

t=blockrev(t,2);
print(t);
rev(t);
print(t);
return 0;
}

Friday, September 4, 2015

first come first serve Operating system c code

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct process
{
 char name;
 double BT;

}p[5];
int turn=0;
void startnext(int tid)
{
 if(tid==1)
  return;
 tid++;
}
void run(void *tid)
{
 int i=(int )tid;
 while(turn!=i)
 {
  sleep(p[i].BT);
  p[i].BT=0;
  startnext(i);
  pthread_exit(0);
 }
}


int main(int argc,char *argv[])
{
 pthread_t thrs[5];
 int i,j;
 if(argc==6)
 {
  for(i=0;i<5;i++)
  {
   p[i].name=65+i;
   p[i].BT=atoi(argv[i+1]);
  }
 }
 for (int i = 0; i < 5; i++)
 {
  pthread_create(&thrs[i],NULL,&run,(void *)i);
  pthread_join(thrs[i],NULL);
 }
 return 0;
}

Tuesday, September 1, 2015

Add all greater values to every node in a given BST

              50
           /      \
         30        70
        /   \      /  \
     20    40    60   80 

The above tree should be modified to following 

              260
           /      \
         330        150
        /   \       /  \
      350   300    210   80


#include <iostream>
using namespace std;
struct node {
int data;
node *left;
node *right;
};

void addgreater(node *&t,int &sum)
{
if(t==NULL)return ;
if(t)
{
addgreater(t->right,sum);
sum=sum+t->data;
t->data=sum;
addgreater(t->left,sum);
}
}
void create(node *&t,int a)
{
if(t==NULL)
{
t=new node;
t->data=a;
t->left=t->right=NULL;

}
else if(a<t->data)
create(t->left,a);
else
create(t->right,a);

}
void print(node *t)
{
if(t)
{
cout<<t->data<<" ";
print(t->left);
print(t->right);
}
}
int main()
{
int a;
node *t=NULL;

cin>>a;
while(a!=-1){
create(t,a);
cin>>a;
}
print(t);
a=0;
addgreater(t,a);
print(t);

}

Wednesday, August 19, 2015

Find all ANAGRAMS for given string

ANAGRAMS - all possible string which can be generated by interchanging positions of the letters of given string.

eg. ABC, ACB , BCA , BAC , CAB , ABA all are anagrams




#include <iostream>
using namespace std;
char a[100];
char A[100];
int size_of_string;
void find_anagram(int n)
{
if(n<1){
for(int i=0;i<size_of_string;i++)

cout<<A[i];
cout<<endl;
}else
{
for(int i=0;i<3;i++)
{
A[n-1]=a[i];
find_anagram(n-1);
}
}
}
int main()
{

cout<<"enter the string \n";
cin>>a;
int size=0;

while(a[size]!='\0')
size++;
size_of_string=size;
find_anagram(size);

}

Sunday, July 26, 2015

check if a binary tree is sub tree of other binary tree

#include <iostream>
#include <fstream>

using namespace std;
int sum[100];
struct node
{
int data;
node *left,*right;
};

void create(node *&t,int k)
{

if(t==NULL)
{
t=new node;
t->data=k;
t->left=NULL;
t->right=NULL;
}
cin>>k;
if(k!=-1)
create(t->right,k);
cin>>k;
if(k!=-1)
create(t->left,k);
}
int max(int a ,int b)
{
if(a>b)
return a;
return b;
}
bool identicle(node *t,node *p)
{
if(t==NULL&&p==NULL)
return true;
if(t->data==p->data&&identicle(t->left,p->left)&&identicle(t->right,p->right))
return true;
return false;
}
bool fun(node *t,node *p)
{
if(p==NULL)
return true;
if(t==NULL)
return false;
return(fun(t->left,p)||fun(t->right,p)||identicle(t,p));
return false;
}
int main()
{
int k;
node *t;
t=NULL;
int a,a1;
cin>>a;
create(t,a);
cout<<"enter for 2nd tree\n";
cin>>a;
node *p=NULL;
create(p,a);
cout<<"trees create compeleted\n";
if(fun(t,p))
cout<<"yes";
else
cout<<"no";
return 0;
}

Contributors

Translate