Saturday 7 November 2015

rb tree insertion


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

//A Red-Black tree node structure
struct node
{
    int data;     // for data part
    char color;  // for color property

    //links for left, right children and parent
    struct node *left, *right, *parent;
};


// Left Rotation
void LeftRotate(struct node **root,struct node *x)
{
    //y stored pointer of right child of x
    struct node *y = x->right;

    //store y's left subtree's pointer as x's right child
    x->right = y->left;

    //update parent pointer of x's right
    if (x->right != NULL)
        x->right->parent = x;

    //update y's parent pointer
    y->parent = x->parent;

    // if x's parent is null make y as root of tree
    if (x->parent == NULL)
        (*root) = y;

    // store y at the place of x
    else if (x == x->parent->left)
        x->parent->left = y;
    else    x->parent->right = y;

    // make x as left child of y
    y->left = x;

    //update parent pointer of x
    x->parent = y;
}


// Right Rotation (Similar to LeftRotate)
void rightRotate(struct node **root,struct node *y)
{
    struct node *x = y->left;
    y->left = x->right;
    if (x->right != NULL)
        x->right->parent = y;
    x->parent =y->parent;
    if (x->parent == NULL)
        (*root) = x;
    else if (y == y->parent->left)
        y->parent->left = x;
    else y->parent->right = x;
    x->right = y;
    y->parent = x;
}

// Utility function to fixup the Red-Black tree after standard BST insertion
void insertFixUp(struct node **root,struct node *z)
{
    // iterate until z is not the root and z's parent color is red
    while (z != *root && z->parent->color == 'R')
    {
        struct node *y;

        // Find uncle and store uncle in y
        if (z->parent == z->parent->parent->left)
            y = z->parent->parent->right;
        else
            y = z->parent->parent->left;

        // If uncle is RED, do following
        // (i)  Change color of parent and uncle as BLACK
        // (ii) Change color of grandparent as RED
        // (iii) Move z to grandparent
        if (y->color == 'R')
        {
            y->color = 'B';
            z->parent->color = 'B';
            z->parent->parent->color = 'R';
            z = z->parent->parent;
        }

        // Uncle is BLACK, there are four cases (LL, LR, RL and RR)
        else
        {
            // Left-Left (LL) case, do following
            // (i)  Swap color of parent and grandparent
            // (ii) Right Rotate Grandparent
            if (z->parent == z->parent->parent->left &&
                z == z->parent->left)
            {
                char ch = z->parent->color ;
                z->parent->color = z->parent->parent->color;
                z->parent->parent->color = ch;
                rightRotate(root,z->parent->parent);
            }

            // Left-Right (LR) case, do following
            // (i)  Swap color of current node  and grandparent
            // (ii) Left Rotate Parent
            // (iii) Right Rotate Grand Parent
            if (z->parent == z->parent->parent->left &&
                z == z->parent->right)
            {
                char ch = z->color ;
                z->color = z->parent->parent->color;
                z->parent->parent->color = ch;
                LeftRotate(root,z->parent);
                rightRotate(root,z->parent->parent);
            }

            // Right-Right (RR) case, do following
            // (i)  Swap color of parent and grandparent
            // (ii) Left Rotate Grandparent
            if (z->parent == z->parent->parent->right &&
                z == z->parent->right)
            {
                char ch = z->parent->color ;
                z->parent->color = z->parent->parent->color;
                z->parent->parent->color = ch;
                LeftRotate(root,z->parent->parent);
            }

            // Right-Left (RL) case, do following
            // (i)  Swap color of current node  and grandparent
            // (ii) Right Rotate Parent
            // (iii) Left Rotate Grand Parent
            if (z->parent == z->parent->parent->right &&
                z == z->parent->left)
            {
                char ch = z->color ;
                z->color = z->parent->parent->color;
                z->parent->parent->color = ch;
                rightRotate(root,z->parent);
                LeftRotate(root,z->parent->parent);
            }
        }
    }
    (*root)->color = 'B'; //keep root always black
}

// Utility function to insert newly node in RedBlack tree
void insert(struct node **root, int data)
{
    // Allocate memory for new node
    struct node *z = (struct node*)malloc(sizeof(struct node));
    z->data = data;
    z->left = z->right = z->parent = NULL;

     //if root is null make z as root
    if (*root == NULL)
    {
        z->color = 'B';
        (*root) = z;
    }
    else
    {
        struct node *y = NULL;
        struct node *x = (*root);

        // Follow standard BST insert steps to first insert the node
        while (x != NULL)
        {
            y = x;
            if (z->data < x->data)
                x = x->left;
            else
                x = x->right;
        }
        z->parent = y;
        if (z->data > y->data)
            y->right = z;
        else
            y->left = z;
        z->color = 'R';

        // call insertFixUp to fix reb-black tree's property if it
        // is voilated due to insertion.
        insertFixUp(root,z);
    }
}

// A utility function to traverse Red-Black tree in inorder fashion
void inorder(struct node *root)
{
    if (root == NULL)
        return;
    inorder(root->left);
    printf("%d ", root->data);
    inorder(root->right);
}

/* Drier program to test above function*/
int main()
{
    struct node *root = NULL;
    insert(&root,5);
    insert(&root,3);
    insert(&root,7);
    insert(&root,2);
    insert(&root,4);
    insert(&root,6);
    insert(&root,8);
    insert(&root,11);
    printf("inorder Traversal Is : ");
    inorder(root);

    return 0;
}

lcs


#include<stdio.h>
#include<string.h>

int i,j,m,n,c[20][20];
char x[20],y[20],b[20][20];

void print(int i,int j)
{
                if(i==0 || j==0)
                                return;
                if(b[i][j]=='c')
                {
                                print(i-1,j-1);
                                printf("%c",x[i-1]);
                }
                else if(b[i][j]=='u')
                                print(i-1,j);
                else
                                print(i,j-1);
}

void lcs()
{
                m=strlen(x);
                n=strlen(y);
                for(i=0;i<=m;i++)
                                c[i][0]=0;
                for(i=0;i<=n;i++)
                                c[0][i]=0;
                               
                //c, u and l denotes cross, upward and downward directions respectively
                for(i=1;i<=m;i++)
                                for(j=1;j<=n;j++)
                                {
                                                if(x[i-1]==y[j-1])
                                                {
                                                                c[i][j]=c[i-1][j-1]+1;
                                                                b[i][j]='c';
                                                }
                                                else if(c[i-1][j]>=c[i][j-1])
                                                {
                                                                c[i][j]=c[i-1][j];
                                                                b[i][j]='u';
                                                }
                                                else
                                                {
                                                                c[i][j]=c[i][j-1];
                                                                b[i][j]='l';
                                                }
                                }
}

int main()
{
                printf("Enter 1st sequence:");
                scanf("%s",x);
                printf("Enter 2nd sequence:");
                scanf("%s",y);
                printf("\nThe Longest Common Subsequence is ");
                lcs();
                print(m,n);
        return 0;
}

tower of honoi


#include <stdio.h>




void
towers(
int
,
char
,
char
,
char
);




int
main()

{

 
int
num;




 
printf
(
"Enter the number of disks : "
);

 
scanf
(
"%d"
, &num);

 
printf
(
"The sequence of moves involved in the Tower of Hanoi are :\n"
);

 
towers(num,
'A'
,
'C'
,
'B'
);

 
return
0;

}

void
towers(
int
num,
char
frompeg,
char
topeg,
char
auxpeg)

{

 
if
(num == 1)

 
{

     
printf
(
"\n Move disk 1 from peg %c to peg %c"
, frompeg, topeg);

     
return
;

 
}

 
towers(num - 1, frompeg, auxpeg, topeg);

 
printf
(
"\n Move disk %d from peg %c to peg %c"
, num, frompeg, topeg);

 
towers(num - 1, auxpeg, topeg, frompeg);

}

travelling saleman problem


#include<stdio.h>
#include<conio.h>
int
main()
{

int
cost[20][20],min,l,m,sr[20],sc[20],flag[20][20],i,j,k,rf[20],cf[20],n;

int
nrz[20],ncz[20],cn,a,noz,nrz1[20],ncz1[20],counter =0;

printf
(
"-----------------------------------------------------------\n"
);

printf
(
"----------------------Made by C code champ-----------------\n"
);

printf
(
"-----------------------------------------------------------\n\n"
);

printf
(
"\n\tC PROGRAM FOR TRAVELLING SALESMAN PROBLEM"
);

printf
(
"\n\nEnter the total number of assignments:"
);

scanf
(
"%d"
,&n);

/* Enter the cost matrix*/

printf
(
"\nEnter the cost matrix\n"
);

for
(i=0;i<n;i++)

{
   
printf
(
"\n"
);
   
for
(j=0;j<n;j++)
   
{
     
printf
(
"cost[%d][%d] = "
,i,j);
     
scanf
(
"%d"
,&cost[i][j]);
   
}

}

printf
(
"\n\n"
);

/* Display the entered cost matrix*/

printf
(
"Cost matrix:\n"
);

for
(i=0;i<n;i++)

{
   
for
(j=0;j<n;j++)
   
printf
(
"\t%d\t"
,cost[i][j]);
   
printf
(
"\n"
);

}

/* operation on rows*/

for
(i=0;i<n;i++)

{
 
min=cost[i][0];
 
/* find the minmum element in each row*/
 
for
(j=0;j<n;j++)
 
{
     
if
(min>cost[i][j])
     
min=cost[i][j];
 
}
 
/*subtract the minimum element from each element of the respective rows*/
 
for
(j=0;j<n;j++)
   
cost[i][j]=cost[i][j]-min;

}

/* operation on colums*/

for
(i=0;i<n;i++)

{
 
min=cost[0][i];
 
/* find the minimum element in each column*/
 
for
(j=0;j<n;j++)
 
{
   
if
(min>cost[j][i])
     
min=cost[j][i];
 
}
 
/*subtract the minimum element from each element of the respective columns*/
 
for
(j=0;j<n;j++)
   
cost[j][i]=cost[j][i]-min;

}

printf
(
"\n\n"
);

printf
(
"Cost matrix after row & column operation:\n"
);

for
(i=0;i<n;i++)

{
for
(j=0;j<n;j++)
   
printf
(
"\t%d\t"
,cost[i][j]);
 
printf
(
"\n"
);

}

repeatx:;

/*Draw minimum number of horizontal and vertical lines to cover all zeros in

resulting matrix*/

a=0;noz=0,min=1000;

for
(i=0;i<n;i++)

{
for
(j=0;j<n;j++)
   
flag[i][j]=0;

}

for
(i=0;i<n;i++)

{ cn=0;
 
for
(j=0;j<n;j++)
 
{
if
(cost[i][j]==0)
   
{ cn++;
 
flag[i][j]=1;
   
}
 
}
 
nrz[i]=cn;
 
noz=noz+cn;

}

for
(i=0;i<n;i++)

{ cn=0;
 
for
(j=0;j<n;j++)
 
{
if
(cost[j][i]==0)
   
{ cn++;
 
flag[j][i]=1;
   
}
 
}
 
ncz[i]=cn;
 
noz=noz+cn;

}

for
(i=0;i<n;i++)

{ nrz1[i]=nrz[i];
 
ncz1[i]=ncz[i];

}

k=0;

while
(nrz[k]!=0||ncz[k]!=0)

{

for
(i=0;i<n;i++)
 
{ cn=0;
 
for
(j=0;j<n;j++)
 
{
if
(flag[i][j]==1)
     
cn++;
   
nrz[i]=cn;
 
}
 
if
(nrz[i]==1)
 
{
for
(j=0;j<n;j++)
   
{
if
(flag[i][j]==1)
 
{ flag[i][j]=2;
   
for
(k=0;k<n;k++)
   
{
if
(flag[k][j]==1)
       
flag[k][j]=0;
   
}
 
}
   
}
 
}

}

for
(i=0;i<n;i++)

{ cn=0;
 
for
(j=0;j<n;j++)
 
{
if
(flag[j][i]==1)
     
cn++;
   
ncz[i]=cn;
 
}
 
if
(ncz[i]==1)
 
{
for
(j=0;j<n;j++)
   
{
if
(flag[j][i]==1)
 
{ flag[j][i]=2;
   
for
(k=0;k<n;k++)
   
{
if
(flag[j][k]==1)
       
flag[j][k]=0;
   
}
 
}
   
}
 
}

}

k++;

}

for
(i=0;i<n;i++)

{
for
(j=0;j<n;j++)
 
{
if
(flag[i][j]==2)
 
a++;
 
}

}


/* If minimum number of lines, a is equal to the order of the matrix n then

assignment can be optimally completed.*/

if
(a==n)

{
 
printf
(
"\nAssignments completed in order!!\n"
);
 
/* Display the order in which assignments will be completed*/
 
for
(i=0;i<n;i++)
 
{
for
(j=0;j<n;j++)
   
{
if
(flag[i][j]==2)
   
printf
(
" %d->%d "
,i+1,j+1);
   
}
   
printf
(
"\n"
);
 
}
 
getch();
 
exit
(0);

}

/* if order of matrix and number of lines is not same then its difficult to

find the optimal solution.

Now determine the smallest uncovered element i.e. element not covered by lines

and then subtract this minimum element from all uncovered elements and add the

same elements at the intersection of horizontal and vertical lines.*/

else

{
for
(i=0;i<n;i++)
 
{ rf[i]=0,sr[i]=0;
   
cf[i]=0,sc[i]=0;
 
}
 
for
(k=n;(k>0&&noz!=0);k--)
 
{
for
(i=0;i<n;i++)
   
{ m=0;
 
for
(j=0;j<n;j++)
 
{
if
((flag[i][j]==4)&&(cost[i][j]==0))
     
m++;
 
}
     
sr[i]=m;
   
}
   
for
(i=0;i<n;i++)
   
{
if
(nrz1[i]==k&&nrz1[i]!=sr[i])
 
{  rf[i]=1;
     
for
(j=0;j<n;j++)
     
{
if
(cost[i][j]==0)
         
flag[i][j]=4;
     
}
     
noz=noz-k;
 
}
   
}
   
for
(i=0;i<n;i++)
   
{
   
l=0;
   
for
(j=0;j<n;j++)
   
{
if
((flag[j][i]==4)&&(cost[j][i]==0))
     
l++;
   
}
     
sc[i]=l;
   
}
   
for
(i=0;i<n;i++)
   
{
if
(ncz1[i]==k&&ncz1[i]!=sc[i])
 
{  cf[i]=1;
     
for
(j=0;j<n;j++)
     
{
if
(cost[j][i]==0)
         
flag[j][i]=4;
     
}
     
noz=noz-k;
 
}
   
}
   
for
(i=0;i<n;i++)
   
{
for
(j=0;j<n;j++)
 
{
if
(flag[i][j]!=3)
   
{
if
(rf[i]==1&&cf[j]==1)
     
{  flag[i][j]=3;
         
if
(cost[i][j]==0)
       
noz=noz+1;
     
}
   
}
 
}
   
}
 
}
 
for
(i=0;i<n;i++)
 
{
for
(j=0;j<n;j++)
   
{
if
(rf[i]!=1&&cf[j]!=1)
 
{
if
(min>cost[i][j])
     
min=cost[i][j];
 
}
   
}
 
}
 
for
(i=0;i<n;i++)
 
{
for
(j=0;j<n;j++)
   
{
if
(rf[i]!=1&&cf[j]!=1)
   
cost[i][j]=cost[i][j]-min;
   
}
 
}
 
for
(i=0;i<n;i++)
 
{
for
(j=0;j<n;j++)
   
{
if
(flag[i][j]==3)
   
cost[i][j]=cost[i][j]+min;
   
}
 
}

}

printf
(
"\n\n"
);

if
(counter < 10)

{
 
counter = counter+1;

printf
(
"\n\nIntermediate Matrix: \n"
);

for
(i=0;i<n;i++)

{        
 
for
(j=0;j<n;j++)
 
printf
(
"\t%d\t"
,cost[i][j]);
 
printf
(
"\n"
);

}

}

else

{
   
printf
(
"\n\nOptimal solution to given problem is not possible"
);
   
getch();
   
return
0;
   
}

goto
repeatx;
}