Showing posts with label codes. Show all posts
Showing posts with label codes. Show all posts

Monday 26 October 2015

Binary tree

#include <iostream>

using namespace std;

struct  node
{
   node* leftchild;
   node* rightchild;
   int data;
};

int count;

node* insert(node*ptr,int x)

     if(ptr==NULL)
       {
          node* temp= new node;
          temp->leftchild=NULL;
          temp->rightchild=NULL;
          temp->data=x;
          ptr=temp;
          count++;
       }
      else if(count%2==0)
          ptr->leftchild=insert(ptr->leftchild,x);

      else
          ptr->rightchild=insert(ptr->rightchild,x);

return ptr;

}

void preorder(node* root)
{
    if(root!=NULL)
{
    cout<<root->data<<endl;
    preorder(root->leftchild);
    preorder(root->rightchild);

}
}

void inorder(node *root)
{
    if (root!= NULL)
    {
        inorder(root->leftchild);
        cout<<root->data<<endl;
        inorder(root->rightchild);
    }
}

void postorder( node *root)
{
    if (root != NULL)
    {
        postorder(root->leftchild);

        postorder(root->rightchild);
        cout<<root->data<<endl;
    }
}

int main()
{
    count=0;int x;
    node* root=NULL;

    char ch='y';
    while(ch!='n')
    {
        cout<<"Enter the element:";
        cin>>x;
        root=insert(root,x);
        cout<<"Do you want to enter more elements? y/n";
        cin>>ch;
    }

cout<<"Preorder traversal:";
preorder(root);
cout<<"Inorder traversal:";
inorder(root);
cout<<"Postorder traversal";
postorder(root);
}

Binary search tree

#include <iostream>

using namespace std;
struct node
{
    int data;
     node* left;
     node* right;
};

void preorder(struct node *root)
{
    if (root != NULL)
    {   cout<<root->data<<endl;
        preorder(root->left);

        preorder(root->right);
    }
}
void inorder(struct node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        cout<<root->data<<endl;
        inorder(root->right);
    }
}

void postorder(struct node *root)
{
    if (root != NULL)
    {
        postorder(root->left);

        postorder(root->right);
        cout<<root->data<<endl;
    }
}

node* insert(node* ptr, int x)
{

    if (ptr == NULL)
       {
            node* temp =  new node;

            temp->data = x;
            temp->left = temp->right = NULL;
            ptr=temp;

       }

    else  if (x < ptr->data)
        ptr->left  = insert(ptr->left,x);
    else if (x> ptr->data)
        ptr->right = insert(ptr->right,x);

    return ptr;
}
int main()
{int x;

    struct node *root = NULL;
    char ch='y';
    while(ch!='n')
    {
        cout<<"Enter the element:";
        cin>>x;
        root= insert(root,x);
        cout<<"Do you want to insert more elements y/n:";
        cin>>ch;

    }

cout<<"Preorder traversal:";
preorder(root);
cout<<"Inorder traversal:";
inorder(root);
cout<<"Postorder traversal";
postorder(root);

    return 0;
}

Monday 21 September 2015

Linked list insertion and deletion at end

#include <iostream>
using namespace std;
struct node
   {
     int data ;
     node* next;
   };
node* head;
void insert(int x)
    {   node * temp=new node();
        temp->data=x;
    node*temp2=head;
          if(head==NULL)
             {
                 head =temp;
                 temp->next=NULL;
             }
  else
     {   while(temp2->next!=NULL)
        temp2=temp2->next;
        temp2->next=temp;
temp->next=NULL;
     }
}
void del()
{ node*temp2=head;
     if(head==NULL)
        cout<<"Linked is          empty"<<endl; 
    else
   
    while(temp2->next->next!=NULL)
       temp2=temp2->next;
     temp2->next=NULL;
   
    }
void show()
{ node*temp1=head;
while(temp1!=NULL)
{cout<<temp1->data<<"->";
       
       
    temp1=temp1->next;
    }
    cout<<"NULL"<<endl;
    }
int main()
{head=NULL;
char ch='y';int n;
    int x;
while(ch=='y'||ch=='Y')
    {
        cout<<"Enter 1 for insertion"<<endl;
        cout<<"Enter 2 for deletion"<<endl;
        cin>>n;
         switch (n)
        { case 1 : cout<<"Enter data:";
                   cin>>x;
                
           insert (x);
            break;
case 2 : del();
        break;
}
        cout<<"The linked list is:"<<endl;
        show();
        cout<<"Do you want to continue operation  y/n"<<endl;
    cin>>ch;
    }
}