Showing posts with label data structure. Show all posts
Showing posts with label data structure. Show all posts

Wednesday, 23 May 2018

how to create mirror image of tree in c++

#include<iostream>
#include<cmath>
#include<queue>
using namespace std;

struct tree

{

 int num;

 tree* left,*right;



}*root;

tree * create(int n)

{

 tree *t= new tree;

 t->num=n;

 t->left=t->right=NULL;

 return t;



}

tree * add(tree * r,int nu)

{

      if(r==NULL)

      {

        r= create(nu);

   

   }

   else if(nu<=r->num)

   {

     r->left= add(r->left,nu);

   }

   else

   r->right=add(r->right,nu);

   return r;

}

void disply(tree * t)

{

 if(t==NULL)

 {

  return;

 }

 else

 {

    cout<<t->num<<"   ";

    disply(t->left);



    disply(t->right);

 }

 }

tree * deletes(tree * t)

{

 while(t->left!=NULL)

 {

   t=t->left;

 }return t;

}

tree * deleteNode(tree * r,int d)

{

 if(r==NULL)

 {

  return r;

 }

 else if(d<r->num)

 {

  r->left=deleteNode(r->left,d);



 }

 else if(d>r->num)

 {

  r->right=deleteNode(r->right,d);



 }

 else {

  if(r->left==NULL&&r->right==NULL)

  {

  delete r;

  r=NULL;

  }

  else if(r->right==NULL)

  {

   tree * temp;

   temp=r;

   r=r->left;

   delete temp;

  }

  else if(r->left==NULL)

  {

   tree * temp;

   temp=r;

   r=r->right;

   delete temp;

  }

  else

  {

   tree *temp=deletes(r->right);

   r->num=temp->num;



   r->right=deleteNode(temp,temp->num);

  }



 }

 return r;

}
void printLevelOrder(tree *root) {
  if (!root) return;
  queue<tree*> nodesQueue;
  int nodesInCurrentLevel = 1;
  int nodesInNextLevel = 0;
  nodesQueue.push(root);
  while (!nodesQueue.empty()) {
   
    tree *currNode = nodesQueue.front();
    nodesQueue.pop();
    nodesInCurrentLevel--;
    if (currNode) {
      cout << currNode->num << " ";
      nodesQueue.push(currNode->left);
      nodesQueue.push(currNode->right);
      nodesInNextLevel += 2;
    }
    if (nodesInCurrentLevel == 0) {
      cout <<endl;
   
      nodesInCurrentLevel = nodesInNextLevel;
      nodesInNextLevel = 0;
    }
  }
}
void CMI(tree *r)
{
tree*temp,*temp1;
temp=r->left;
r->left=r->right;
r->right=temp;
   
}
int main()

{

root==NULL;

int n,a,m=1;



while(1){



cout<<"Enter 1 for add\n Enter 2 for display\n Enter 3  for delete node\n Enter 4 for createMirrorImage"<<endl;

cin>>n;

switch(n)

{

case 1:
 cout<<"Enter the number for add"<<endl;
 cin>>a;

root=add(root,a);

break;
case 2:
printLevelOrder(root);
break;

case 3:

 cout<<"Enter the number for delete"<<endl;

 cin>>a;
 root=deleteNode(root,a);
 break;
case 4:
CMI(root);
printLevelOrder(root);
break;

}

}

}

Wednesday, 16 May 2018

assignment

#include<iostream>
#include<cmath>
using namespace std;

struct tree

{

 int num;

 tree* left,*right;



}*root;

tree * create(int n)

{

 tree *t= new tree;

 t->num=n;

 t->left=t->right=NULL;

 return t;



}

tree * add(tree * r,int nu)

{

      if(r==NULL)

      {

        r= create(nu);

    

   }

   else if(nu<=r->num)

   {

     r->left= add(r->left,nu);

   }

   else

   r->right=add(r->right,nu);

   return r;

}

void disply(tree * t)

{

 if(t==NULL)

 {

  return;

 }

 else

 {

    cout<<t->num<<"   ";

    disply(t->left);



    disply(t->right);

 }

 }

tree * deletes(tree * t)

{

 while(t->left!=NULL)

 {

   t=t->left;

 }return t;

}

tree * deleteNode(tree * r,int d)

{

 if(r==NULL)

 {

  return r;

 }

 else if(d<r->num)

 {

  r->left=deleteNode(r->left,d);

 

 }

 else if(d>r->num)

 {

  r->right=deleteNode(r->right,d);

 

 }

 else {

  if(r->left==NULL&&r->right==NULL)

  {

  delete r;

  r=NULL;

  }

  else if(r->right==NULL)

  {

   tree * temp;

   temp=r;

   r=r->left;

   delete temp;

  }

  else if(r->left==NULL)

  {

   tree * temp;

   temp=r;

   r=r->right;

   delete temp;

  }

  else

  {

   tree *temp=deletes(r->right);

   r->num=temp->num;

 

   r->right=deleteNode(temp,temp->num);

  }



 }

 return r;

}



int main()

{

root==NULL;

int n,a,m=1;



while(1){



cout<<"Enter 1 for add Enter 2 for display Enter 3  for delete node"<<endl;

cin>>n;

switch(n)

{

case 1:
 cout<<"Enter the number for add"<<endl;
 cin>>a;
 for(int i=0;i<=a;i++)
{
  if(a==pow(2,i))
{
root=add(root,a);
m=0;
break;

}

}
if (m==0)
{
cout<<"Number is added"<<endl;
m=1;
}
else
    cout<<"Number is not added"<<endl;

break;
case 2:

  disply(root);

  cout<<endl;

break;

case 3:

 cout<<"Enter the number for delete"<<endl;

 cin>>a;
 for(int i=0;i<=a;i++)
{
  if(a==pow(2,i))
{
 root=deleteNode(root,a);
m=0;
break;}

}
if (m==0)
{
cout<<"Number is deleted"<<endl;
m=1;
}
else
    cout<<"Number is not deleted "<<endl;


 break;

}

}

}

Monday, 12 February 2018

how to print tree in level order c++

#include<iostream>
#include<queue>
using namespace std;

struct tree
{
int num;
tree *left,*right;
}*root;
  void levelOrder(tree *r)
  {
  queue <tree*> q;
  q.push(r);
  while(!q.empty())
  {
   tree *d =q.front();
            q.pop();
            cout<<d->num<<" ";
            if(d->left) q.push(d->left);
            if(d->right) q.push(d->right);
}
 
 
  }
 
 
 
 
 
tree * create(int n)
{
    tree *t = new tree;
    t->left=t->right=NULL;
 
    t->num=n;
    return t;
}

tree * add(tree *r,int n)
{
     if(r==NULL)
     {
        r=create(n);
}
else if(r->num>n)
{
r->right=add(r->right,n);
}
else
r->left=add(r->left,n);
return r;
}
void display(tree * t)
{
       if(t==NULL)
       {
        return ;
   }
   else
   {
     cout<<t->num<<"   ";
     display(t->left);
     display(t->right);
   }
}
int main()
{
     root =NULL;
   
     int n,t;
 while(true)
 {

     cout<<"Enter 1 for add Enter 2 for diplay "<<endl;
  cin>>n;
  switch(n)
  {
    case 1:
    cout<<"Enter the number "<<endl;
    cin>>t;
    root=add(root,t);
    break;
    case 2:
    display(root);
    break;
    case 3:
    levelOrder(root);
    break;
   
} 
}}

add show add delete node from tree

#include<iostream>
using namespace std;
struct tree
{
 int num;
 tree* left,*right;

}*root;
tree * create(int n)
{
 tree *t= new tree;
 t->num=n;
 t->left=t->right=NULL;
 return t;

}
tree * add(tree * r,int nu)
{
      if(r==NULL)
      {
        r= create(nu);
     
   }
   else if(nu<=r->num)
   {
     r->left= add(r->left,nu);
   }
   else
   r->right=add(r->right,nu);
   return r;
}
void disply(tree * t)
{
 if(t==NULL)
 {
  return;
 }
 else
 {
    cout<<t->num<<"   ";
    disply(t->left);
 
    disply(t->right);
 }
 }
tree * deletes(tree * t)
{
while(t->left!=NULL)
{
  t=t->left;
}return t;
}
tree * deleteNode(tree * r,int d)
{
 if(r==NULL)
 {
  return r;
 }
 else if(d<r->num)
 {
  r->left=deleteNode(r->left,d);
 
 }
 else if(d>r->num)
 {
  r->right=deleteNode(r->right,d);
 
 }
 else {
  if(r->left==NULL&&r->right==NULL)
  {
  delete r;
  r=NULL;
}
else if(r->right==NULL)
{
tree * temp;
temp=r;
r=r->left;
delete temp;
}
  else if(r->left==NULL)
{
tree * temp;
temp=r;
r=r->right;
delete temp;
}
else
{
  tree *temp=deletes(r->right);
  r->num=temp->num;
 
  r->right=deleteNode(temp,temp->num);
}

 }
 return r;
}

int main()
{
root==NULL;
int n,a;
while(1){

cout<<"Enter 1 for add Enter 2 for display Enter 3  for delete node"<<endl;
cin>>n;
switch(n)
{
case 1:
 cout<<"Enter the number for add"<<endl;
 cin>>a;
root=add(root,a);
break;
case 2:
  disply(root);
  cout<<endl;
break;
case 3:
cout<<"Enter the number for delete"<<endl;
 cin>>a;
root=deleteNode(root,a);
break;
}


}
}

game of death in the circlar link list

THis is a death game all the peoples are atand in a circle and start from 1 1 kill the second 3 kill the 4 and so on
this is circular link list the rmaining person is one
  for  Example
     1            2           3       4   
   are stand in a circle
        1        3  are  kill 2 and 4
 and then      1 kill the 3   
#include<iostream>
using namespace std;
int nums=0;
struct death
{
   int num;
   death *next;
}*head,*tail;
void addnumber(int num)
{
nums=num;
  for(int i=1;i<=num;i++)
  {
        death *d= new death;
         if(head==NULL)
         {
         d->num=i;
         head =tail=d;
         tail->next=head;
         }
         else
         {
              d->num=i;
              tail->next=d;
              tail=d;
              tail->next=head;
         }
       
  }

}
void display()
{
death * temp;
temp =head;
    while(temp->next!=head)
    {
        cout<<temp->num<<"    ";
        temp=temp->next;
    }
    cout<<temp->num<<"    ";
    cout<<endl;
}
void playGame()
{
 death * temp,*temp1;
temp =head;
for(int i=0;i<nums;i++)
{


       cout<<temp->num<<"    ";
       temp1= temp->next;
     
       temp->next=temp1->next;
      
        temp=temp->next;
  
        temp1=NULL;
            delete temp1;
      
} cout<<endl;
cout<<"So the remaining number is "<<temp->num<<"    ";
}
int main()
{
    head=tail=NULL;
    int num;
    while(1){
  
    cout<<"Enter the number please "<<endl;
    cin>>num;
    switch(num)
    {
       case 1:
           int num1;
           cout<<"Enter the number of persons in this game"<<endl;
           cin>>num1;
         
           addnumber(num1);
           break;
           case 2:
               display();
               break;
               case 3:
     playGame();
     break;
                   
    }}
}                               
                                           https://getcryptotab.com/900383

a program that convert decimal to using recursion

#include <iostream>
using namespace std;
void decToBin(int num, int base);
int main()
{
int decimalNum;
int base;
base = 2;
cout << "Enter number in decimal: ";
cin >> decimalNum;
cout << endl;
cout << "Decimal " << decimalNum << " = ";
decToBin(decimalNum, base);
cout << " binary" << endl;
return 0;
}
void decToBin(int num, int base)
{
if (num > 0)
{
decToBin(num / base, base);
cout << num % base;
}
}

program that convert decimal number into binary

#include<iostream>
using namespace std;
struct decimal
{
  int num;
  decimal *next;
 
}*head;
void convert( int num)
{ decimal  * d= new decimal ;

     while(num!=0)
    
     {
     decimal  * d= new decimal ;
        d->next=NULL;
     if(head==NULL)
{
            d->num=num%2;
      head=d;
      num/=2;
     }
     else {
      d->num=num%2;
      d->next=head;
      head=d;
   
      num/=2;
     }
   
}}
void display()
{
 decimal * temp;
 temp=head;

 while(temp!=NULL)
 {
   cout<<temp->num;
   temp=temp->next;
 }
}

int main()
{  head =NULL;
     int num;
     cout<<"Enter the number please "<<endl;
     cin>>num;
     cout<<"The decimal in binary =";
    convert(num);
    display();
}

Honoli tower problem in data structure

#include<iostream>
using namespace std;
void HanoliTower(int numf,string a,string b,string c)
{
  if(numf==1)
  {
        cout<<"Move Plate "<<numf<<" from "<<a<<"  to "<<c<<endl;

  }
  else {
 
  HanoliTower(numf-1,a,c,b);
 
  cout<<"Move Plate "<<numf<<" from "<<a<<" to "<<c<<endl;

      HanoliTower(numf-1,b,a,c);
      }
}
int main()
{
    HanoliTower(3,"Tower 01","Tower 02","Tower 03");

}

Tuesday, 9 January 2018

data structure

A program that add new node if the all node in the 





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

BTNode* getnewNode(int a){

BTNode *n=new BTNode;

if(a % 2==0)
{
cout<<"num is even: not added"<<endl;
}else
{

n->data=a;
}
n->right=NULL;
n->left=NULL;
return n;


}
BTNode* insertNode (BTNode* r,int d){
if(r==NULL)
{
r=getnewNode(d);


}else
{
if( d<=r->data)
{
r->left=insertNode(r->left,d);
}else
{
r->right=insertNode(r->right,d);
}
return r;

}

}
void in_orderdisplay( BTNode* r)
{
if(r==NULL)
{
return;

}
in_orderdisplay(r->left);
cout<<r->data;
in_orderdisplay(r->right);
}
void pre_orderdisplay(BTNode* r)
{
if(r==NULL)
{
return ;

}
cout<<r->data<<endl;
pre_orderdisplay(r->left);
pre_orderdisplay(r->right);
}
 void post_orderdisplay(BTNode* r)
{
if(r==NULL)
{
return;
}
post_orderdisplay(r->left);
post_orderdisplay(r->right);
cout<<r->data;

}
void level_orderdisplay(BTNode* r){
if(r==NULL)
{

return;

}
queue<BTNode*> Q;
Q.push(r);
while(!Q.empty())
{
BTNode* temp=Q.front();
cout<<temp->data;
if(temp->left!=NULL)
{
Q.push(temp->left);
}
if( temp->right!=NULL)
{
Q.push(temp->right);

}
Q.pop();
}


}

int main()
{
int choice,a ;
root=NULL;

char ch='y';
while(ch='y')
{

cout<<"enter 1 to add node "<<endl;
cout<<"enter 2 to display"<<endl;
cout<<"enter 3 to inorder dispaly"<<endl;
cout<<"enter 4 to postorder dispaly"<<endl;
cout<<"enter 5 to level wise display"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter value"<<endl;
cin>>a;
root=insertNode(root,a);
break;
case 2:
pre_orderdisplay(root);
break;
case 3:
in_orderdisplay(root);
break;
case 4:
post_orderdisplay(root);
break;
case 5:
level_orderdisplay(root);
break;
default:
break;

}
cout<<"prees y to continue"<<endl;
cout<<"press n to exist"<<endl;
cin>>ch;
}
}