Gain Infiniti: C
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

PROGRAM OF COUNTSORT USING C++

#include <iostream>
using namespace std;
#define SIZE 5

class CountSort
{
    public:
    int *array;
    int *array2;
    int max_elem;

    void initial();
    void display();
    void Sort_Start();
    void Count_Sort(int*,int*,int);
    void max();

    CountSort()
    {
        array=new int[SIZE];
        array2=new int[SIZE];
       
    }

    ~CountSort()
    {
        delete[] array;
        delete[] array2;
    }
};

void CountSort::initial()
{
    for(int i=0;i<SIZE;i++)
    {
        cout<<"Enter the "<<i<<"th Index = ";
        cin>>array[i];
    }
}

void CountSort::display()
{
    for(int i=1;i<=SIZE;i++)
    {
        cout<<"The Value of "<<i<<"th Index = ";
        cout<<array2[i]<<"\n";
    }
}

void CountSort::max()
{
    max_elem=array[0];
    for(int i=1;i<SIZE;i++)
    {
        if(array[i]>max_elem)
        max_elem=array[i];
    }
}

void CountSort::Sort_Start()
{
    max();
    Count_Sort(array,array2,max_elem);
}

void CountSort::Count_Sort(int *a,int *b,int k)
{
    int *c;
    c=new int[k+1];
   
    for(int i=0;i<=k;i++)
    c[i]=0; //Initial the Array
   
    for(int j=0;j<SIZE;j++)
    c[a[j]]=c[a[j]]+1;   //Contains number of elements equal to the index
   
    for(int m=1;m<=k;m++)
    c[m]=c[m]+c[m-1];   //Cumulative frequence - No. of elements smaller that i
   
    //a[n]=Index to c[i]
    //c[a[n]]=position of a[n] in b[]

    for(int n=SIZE-1;n>=0;n--)
    {
        b[c[a[n]]]=a[n];//InPlace Sorting
        c[a[n]]=c[a[n]]-1;
    }
}

int main()
{
    CountSort obj;

    obj.initial();
    obj.Sort_Start();
    obj.display();

    return 0;
}
/*
OUTPUT:-

Enter the 0th Index = 10
Enter the 1th Index = 42
Enter the 2th Index = 43
Enter the 3th Index = 545
Enter the 4th Index = 3

The Value of 1th Index = 3
The Value of 2th Index = 10
The Value of 3th Index = 42
The Value of 4th Index = 43
The Value of 5th Index = 545

*/

PROGRAM OF QUEUE USING C++

#include <iostream>
using namespace std;
#define size 10

class queue
{
    public:
    int first,last;
    int info;
    int array[size];
    queue()
    {
        first=last=-1;
    }
    void qInsert(int el)
    {
        if(last==size-1)
        {
cout<<"QUEUE FULL !";
        }
        else if(first==-1)
        {
            array[++last]=el;
            first++;
        }
        else
        array[++last]=el;
    }
    void qDelete()
    {
        if(first == -1)
        cout<<"QUEUE EMPTY !";
        else
        {
            first++;
        }
    }
    void display()
    {
        for(int i=first;i<last;i++)
        cout<<array[i]<<"->";
        cout<<array[last];
        cout<<endl;
    }
};

main()
{
    queue list;
    list.qInsert(10);
    list.qInsert(20);
    list.qInsert(30);
    list.qInsert(40);
    list.display();
    list.qDelete();
    list.qDelete();
    list.display();
}
       
/*OUTPUT

10->20->30->40
30->40

*/

PROGRAM OF QUICKSORT USING C++

#include <iostream>
using namespace std;
//#define SIZE 10
class QuickSort
{
    public:
    int *array;
    void sort(int*,int,int);
    int partition(int*,int,int);
    void initial();
    void display();
    void sort_start();
    QuickSort()
    {
        array=new int[SIZE];
    }
    ~QuickSort()
    {
        delete[] array;
    }
};
// for inputting the elements;
void QuickSort::initial()
{
    for(int i=0;i<SIZE;i++)
    {
        cout<<"Enter the "<<i<<" th index = ";
        cin>>array[i];
    }
}
void QuickSort::display()
{
    for(int i=0;i<SIZE;i++)
    {
        cout<<"The "<<i<<" th index = "<<array[i]<<"\n";
    }
}
int QuickSort::partition(int* ar,int p,int r)
{
    int x=ar[r];
    int i=p-1;
    for(int j=p;j<r;j++)
    {
        if(ar[j] <= x)
        {
        i++;
            int t=ar[j];

            ar[j]=ar[i];
            ar[i]=t;
        }
       
    }
    int t=ar[i+1];
    ar[i+1]=ar[r];
    ar[r]=t;
    return (i+1);
}
void QuickSort::sort(int* ar,int p,int r)
{
    int q;
    if(p<r)
    {
        q=partition(ar,p,r);
        sort(ar,p,q-1);
        sort(ar,q+1,r);
    }
}
void QuickSort::sort_start()
{
    sort(array,0,SIZE-1);
}

 main()
{
    QuickSort object;
    object.initial();
    object.sort_start();
    object.display();
}


PROGRAM FOR RANDOMIZED QUICKORT USING C++

//The Advantages of Randomization in QS :
//--> The Pivot is chosen randomly.It is equilikely to be
//      any element between p and r.
//--> The Probability that the partition will be unbalanced is minimized.
//      Since choosing the pivot as the last/first element of the array
//      increases the chance of encountering the worst case (sorted Array).

#include <iostream>
#include <stdlib>
#include <time>
using namespace std;
#define SIZE 5

class RandomizedQuickSort
{
    public:
    int *array;
    void sort(int*,int,int);
    int partition(int*,int,int);
    int randomizedPartition(int*,int,int);
    void init();
    void disp();
    void sort_start();
    RandomizedQuickSort()
    {
        srand(time(NULL));
        array=new int[SIZE];
    }
    ~RandomizedQuickSort()
    {
        delete[] array;
    }
};

void RandomizedQuickSort::initial()
{
    for(int i=0;i<SIZE;i++)
    {
        cout<<"Enter the "<<i<<" th index = ";
        cin>>array[i];
    }
}
void RandomizedQuickSort::display()
{
    for(int i=0;i<SIZE;i++)
    {
        cout<<"The "<<i<<" th index = "<<array[i]<<"\n";
    }
}

//T(n)=O(n)
int RandomizedQuickSort::partition(int* ar,int p,int r)
{
    int x=ar[r];
    int i=p-1;
    for(int j=p;j<r;j++)
    {
        if(ar[j] <= x)
        {
            i++;
            int t=ar[j];
            ar[j]=ar[i];
            ar[i]=t;
        }
       
    }
    int t=ar[i+1];
    ar[i+1]=ar[r];
    ar[r]=t;
    return (i+1);
}

//T(n)=O(n)
int RandomizedQuickSort::randomizedPartition(int* ar,int p,int r)
{
    int pos;
    int i=p+ rand()%(r-p+1);  //randomizedposition
    int tmp=ar[r];
    ar[r]=ar[i];
    ar[i]=tmp;
    pos=partition(ar,p,r);
    return pos;
}

//WorstCase T(n)=O(n^2)
//BestCase T(n)=O(lg n)
//AverageCase T(n)=O(n lg n)

void RandomizedQuickSort::sort(int* ar,int p,int r)
{
    int q;
    if(p<r)
    {
        q=randomizedPartition(ar,p,r);
        sort(ar,p,q-1);
        sort(ar,q+1,r);
    }
}

void RandomizedQuickSort::sort_start()
{
    sort(array,0,SIZE-1);
}
 main()
{
    RandomizedQuickSort object;
    object.initial();
    object.sort_start();
    object.display();
}

/*
OUTPUT:-

Enter the 0 th index = 5
Enter the 1 th index = 4
Enter the 2 th index = 3
Enter the 3 th index = 2
Enter the 4 th index = 1
The 0 th index = 1
The 1 th index = 2
The 2 th index = 3
The 3 th index = 4
The 4 th index = 5

*/

PROGRAM FOR RADIXSORT USING C++

#include<iostream>
using namespace std;
static int max_digits;

int pow(int x, int y)
{
     int i,powerValue=1;
    
    if (y==0)
      return 1;

    for (i=1;i<=y;i++)
    powerValue*=x;

      return powerValue;
}


void radixSort(int inputarray[], int n)
{
   int *outputarray, c[10];   //c can have values between 0 and 9
   int j,k,p,digit;
  
   outputarray=new int[n];
   
   //Iterate for the number of Max-Digits in the input array
   for ( p = 0; p < max_digits; p++)
   {
        //Create an array for the digits(0-9)
        for ( k = 0; k < 10; k++)
        c[k] = 0;//Init
       
        //Create A Frequency Array for the digits
        for ( j = 0; j < n; j++)
        {   
            digit = (inputarray[j] % pow(10,p+1) - inputarray[j] % pow (10, p)) / pow(10,p);   //Extract Values of Digits
            c[digit] = c[digit] + 1;    //Calculate Frequency
        }
       
        //Create A Cumulative Frequency Array for the digits
        for ( k = 1; k < 10; k++)
        c[k] = c[k] + c[k-1];
       
        //OutPut Array is formed with stable sort.
        for ( j = n-1; j >= 0; j--)
        {
            digit = (inputarray[j] % pow(10,p+1) - inputarray[j] % pow (10, p)) / pow(10,p);     //Extract Values of Digits
            outputarray[c[digit]] = inputarray[j];
            c[digit] = c[digit]- 1;
        }
       
        for ( j = 1; j <=n; j++)
        inputarray[j] = outputarray[j];        
    }
    delete[] outputarray;
  
}

int main()
{
    int size,*radixSortArray;

    cout<<"Enter the number of Digits in the MaxElement = ";
    cin>>max_digits;
    cout<<"Enter the size of the array = ";
    cin>>size;

    radixSortArray=new int[size];

    for(int i=0;i<size;i++)
    {
        cout <<"Enter the radixSortArray["<<i<<"] = ";
        cin >> radixSortArray[i];
    }

    radixSort(radixSortArray,size);

    cout<<"Array after sorting\n";

    for(int j=1;j<=size;j++)
    cout <<"Enter the radixSortArray["<<j<<"] = "<<radixSortArray[j]<<"\n";

    delete[] radixSortArray;
    return 0;
}
/*
OUTPUT:-

Enter the number of Digits in the MaxElement = 3
Enter the size of the array = 5
Enter the radixSortArray[0] = 555
Enter the radixSortArray[1] = 234
Enter the radixSortArray[2] = 232
Enter the radixSortArray[3] = 323
Enter the radixSortArray[4] = 123
Array after sorting
Enter the radixSortArray[1] = 123
Enter the radixSortArray[2] = 232
Enter the radixSortArray[3] = 234
Enter the radixSortArray[4] = 323
Enter the radixSortArray[5] = 555

*/

PROGRAM FOR REDBLACK TREE USING C++

#include<iostream>
using namespace std;
class RedBlackNode {
         public:
           int key;
           RedBlackNode *left;
           RedBlackNode *right;
           RedBlackNode *parent;
           char color;
           RedBlackNode (int data,RedBlackNode *leftc=NULL,RedBlackNode *rightc=NULL,RedBlackNode *prnt=NULL)
            {
                key=data;
                left=leftc;
                right=rightc;
                parent=prnt;
            };
class RedBlackTree
{
    public:
    RedBlackNode *root;
    RedBlackTree()
    {
        root=NULL;
    }
    void Left_Rotate(RedBlackNode *x);
    void Right_Rotate(RedBlackNode *x);
    void RB_Insert(RedBlackNode *z);
    void Insert(int inp);
    void RB_Insert_Fixup(RedBlackNode *z);
    RedBlackNode* RB_Delete(RedBlackNode *z);
    void RB_Delete_Fixup(RedBlackNode *x);
    RedBlackNode* Tree_Successor(RedBlackNode *x);
    RedBlackNode* RB_Search(int val);
};

void RedBlackTree::Left_Rotate(RedBlackNode *x)
{
    RedBlackNode *y= x->right;
    x->right=y->left;
    y->left->parent=x;
    y->parent=x->parent;
    if (x->parent==NULL) {
        root= y;
    }
    else if (x==x->parent->left) {
        x->parent->left=y;
    }
    else x->parent->right=y;
    y->left=x;
    x->parent=y;
}

void RedBlackTree::Right_Rotate(RedBlackNode *x)
{
    RedBlackNode *y= x->left;
    x->left=y->right;
    y->right->parent=x;
    y->parent=x->parent;
    if (x->parent==NULL) {
        root= y;
    }
    else if (x==x->parent->right) {
        x->parent->right=y;
    }
    else x->parent->left=y;
    y->right=x;
    x->parent=y;
}
void RedBlackTree::RB_Insert_Fixup(RedBlackNode *z)
{
    while (z->parent->color=='R')
    {
        if (z->parent == z->parent->parent->left) {
            RedBlackNode *y=z->parent->parent->right;
            if (y->color=='R') {
                z->parent->color='B';
                y->color='B';
                z->parent->parent->color='R';
                z=z->parent->parent;
            }
            else if (z==z->parent->right) {
                z=z->parent;
                Left_Rotate(z);
            }
            z->parent->color='B';
            z->parent->parent->color='R';
            Right_Rotate(z->parent->parent);
        }
        else
        {
            RedBlackNode *y=z->parent->parent->left;
            if (y->color=='R') {
                z->parent->color='B';
                y->color='B';
                z->parent->parent->color='R';
                z=z->parent->parent;
            }
            else if (z==z->parent->left) {
                z=z->parent;
                Right_Rotate(z);
            }
            z->parent->color='B';
            z->parent->parent->color='R';
            Left_Rotate(z->parent->parent);
        }
    }
    root->color='B';
}

void RedBlackTree::RB_Insert(RedBlackNode *z)
{
    RedBlackNode *y=NULL;
    RedBlackNode *x=root;
    while (x!=NULL) {
        y=x;
        if (z->key<x->key)
            x=x->right;
        else x=x->right;
    }
    z->parent=y;
    if (y==NULL)
        root=z;
    else if (z->key<y->key)
        y->left=z;
    else y->right=z;
    z->left= NULL;
    z->right= NULL;
    z->color='R';
    RB_Insert_Fixup(z);
}
void RedBlackTree::Insert(int inp)
{
    RedBlackNode *z=new RedBlackNode(inp);
    RB_Insert(z);
}
RedBlackNode* RedBlackTree::RB_Delete(RedBlackNode* z)
{    RedBlackNode *y, *x;
    if ((z->left==NULL) || (z->right==NULL))
        y=z;
    else y=Tree_Successor(z);
    if (y->left!=NULL)
        x=y->left;
    else x=y->right;
    x->parent=y->parent;
    if (y->parent==NULL)
        root=x;
    else if (y=y->parent->left)
        y->parent->left=x;
    else y->parent->right=x;
    if (y!=z)
     z->key=y->key;
    if (y->color=='B')
        RB_Delete_Fixup(x);
    return y;
}
void RedBlackTree::RB_Delete_Fixup(RedBlackNode* x)
{
    RedBlackNode *w;
     while ((x!=root) && (x->color=1))
    {
        if (x==x->parent->left) {
            w=x->parent->right;
            if (w->color=='R') {
                w->color='B';
                x->parent->color='R';
                Left_Rotate(x->parent);
                w=x->parent->right;
            }
            if ((w->left->color=='B') && (w->right->color=='B')) {
                w->color=0;
                x=x->parent;
            }
            else if (w->right->color=='B') {
                w->left->color='B';
                w->color='R';
                Right_Rotate(w);
                w=x->parent->right;
            }
            w->color=x->parent->color;
            x->parent->color='B';
            w->right->color='B';
            Left_Rotate(x->parent);
            x=root;
        }
        else {
            w=x->parent->left;
            if (w->color=='R') {
                w->color='B';
                x->parent->color='R';
                Right_Rotate(x->parent);
                w=x->parent->left;
            }
            if ((w->right->color=='B') && (w->left->color=='B')) {
                w->color='R';
                x=x->parent;
            }
            else if (w->left->color=='B'){
                w->right->color=1;
                w->color='R';
                Left_Rotate(w);
                w=x->parent->left;
            }
            w->color=x->parent->color;
            x->parent->color='B';
            w->left->color='B';
            Right_Rotate(x->parent);
            x=root;
        }
    }
}
RedBlackNode* RedBlackTree::Tree_Successor(RedBlackNode *x)
{
    if (x->right!=NULL)
    {
        while (x->left!=NULL)
            x=x->left;
        return x;
        RedBlackNode *y=x->parent;
        while((y!=NULL) && (x==y->right))
        {
            x=y;
            y=y->parent;
        }
        return y;
    }
}
RedBlackNode* RedBlackTree::RB_Search(int val)
{
    RedBlackNode* x=root;
    while (x!=NULL)
    {
        if (val==x->key)
            return x;
        if (val<x->key)
            x=x->left;
        else x=x->right;
    }
    if (x==NULL)
        return NULL;
}
int main()
{
    RedBlackTree T;
    int inp;
    do
    {
    cout<<"The following operations may be performed on the binary search tree"<<endl;
    cout<<"Insertion - 1"<<endl;
    if (T.root!=NULL)
        cout<<"Deletion - 2"<<endl;
    cout<<"Search - 3"<<endl;
    cout<<"Exit - 4"<<endl;
    cout<<"Enter an option:";
    cin>>inp;
    switch(inp)
    {
        case 1:
        cout<<"Enter a value to insert";
        cin>>inp;
        T.Insert(inp);
        inp=1;
        break;
        case 2:
        cout<<"Enter the value of the node which needs deletion:";
        cin>>inp;
        T.RB_Delete(T.RB_Search(inp));
        inp=2;
        break;
        case 3:
        cout<<"Enter the value of the node that needs to be looked up:";
        cin>>inp;
        RedBlackNode *srch=T.RB_Search(inp);
        if (srch==NULL)
            cout<<"Not found";
        else
        {
            cout<<"Value:"<<inp<<" Color:"<<srch->color;
            cout<<"Parent:"<<srch->parent->key<<" Color:"<<srch->parent->color;
        }
        inp=3;
        break;
    }
    } while(inp!=4);
}

program to compute the sum of first n terms of the following series s=1-2+3-4+5+...


   #include<stdio.h>
   #include<conio.h>      
   main()
  {
     clrscr();
   int i,n,sum=1,sine=-1;
   printf("\n enter any integer:");
   scanf("%d",&n);
   for(i=2;i<=n;i++)
   {
   sum=sum+(i*sine);
   sine=sine*(-1);
   }
   printf("\n series is %d",sum);
   getch();
   return 0;
  }

Program for search the number using binary search.


#include<stdio.h>
#include<conio.h>
main()
{
        clrscr();
                int i,f,l,n,mid,a[20],y;
                printf("\n Enter the sizre of array");
                scanf("%d",&n);
                printf("\n Enter the size of element \n");
                for(i=0;i<n;i++)
                scanf("%d",&a[i]);
                printf("\n Enter the no. to be search");
                scanf("%d",&y);
                f=1;
                l=n;
                mid=(f+l)/2;
                while(f<=l&&a[mid]!=y)
     {
                if(y>a[mid])
                f=mid+1;
                if(y<a[mid])
                l=mid-1;
     }
     if(y==a[mid])
     printf("\n no.is found at position %d",mid+1);
     else
     printf("\n no.is not found");
     getch();
     return 0;
 }

Program to CONVERT Fernahite INTO Celcius .


  #include<stdio.h>
    #include<conio.h>
    main()
{
                  clrscr();
    float feh, cel;
    printf(" enter tem in feh:");
    scanf("%f",&feh);
    cel=5*(feh-32)/9;
    printf("\n tem in cel iS   %f",cel);
    getch();
    return 0;
  }

Program For convert a decimal number into hexadecimal no.


       #include<stdio.h>
       #include<conio.h>
       main()
       {
       clrscr();
       int n,r,a[20],i=0,j;
       printf("ente a number");
       scanf("%d",&n);
       while( n>0)
       {
       r=n%16;
       n=n/16;
       a[i]=r;
       i++;
       }
                i--;
       for(j=i; j>=0; j--)
       {
       if(a[j]<10)
       printf("%d",a[j]);
       else
       printf("%c",a[j]-10+'a');
       }
       getch();
      
       return 0;
     }


progrm to find sum of given series:1+1/2+1/3...


  #include<stdio.h>
  #include<conio.h>
  main()
 {
    clrscr();
  float i,sum=0,n;

  printf(" enter any integer");
  scanf(" %f",&n);
  for(i=1;i<=n;i++)
  {
  sum=sum+(1/i);
  }

  printf("sum of series  %f",sum);
  getch();
  return 0;
 }

Program to remove duplicate from an ordered array.


include<stdio.h#>
#include<conio.h>
main()
{
  clrscr();
   int a[40],i,n,k,j;
   printf("\n enter the size of array:");
   scanf("%d",&n);
   printf("\n enter the element:");
   for(i=0;i<n;i++)
   scanf("%d",&a[i]);
   {
   for(i=0;i<n;i++)
   {
   for(j=i+1;j<n;j++)
   {
   if(a[i]==a[j])
   {
   for(k=j;k<n;k++)
   a[k]=a[k+1];
   }
   }
   }
   }
   n--;
   printf("\n after remove duplicate no.\n");
   for(i=0;i<n;i++)
   {
   printf("\n %d\n",a[i]);
   }
getch();
    return 0;
}

PROGRAM FOR REDBLACK TREE USING C++

#include<iostream>
using namespace std;
class RedBlackNode {
         public:
           int key;
           RedBlackNode *left;
           RedBlackNode *right;
           RedBlackNode *parent;
           char color;
           RedBlackNode (int data,RedBlackNode *leftc=NULL,RedBlackNode *rightc=NULL,RedBlackNode *prnt=NULL)
            {
                key=data;
                left=leftc;
                right=rightc;
                parent=prnt;
            };
class RedBlackTree
{
    public:
    RedBlackNode *root;
    RedBlackTree()
    {
        root=NULL;
    }
    void Left_Rotate(RedBlackNode *x);
    void Right_Rotate(RedBlackNode *x);
    void RB_Insert(RedBlackNode *z);
    void Insert(int inp);
    void RB_Insert_Fixup(RedBlackNode *z);
    RedBlackNode* RB_Delete(RedBlackNode *z);
    void RB_Delete_Fixup(RedBlackNode *x);
    RedBlackNode* Tree_Successor(RedBlackNode *x);
    RedBlackNode* RB_Search(int val);
};

void RedBlackTree::Left_Rotate(RedBlackNode *x)
{
    RedBlackNode *y= x->right;
    x->right=y->left;
    y->left->parent=x;
    y->parent=x->parent;
    if (x->parent==NULL) {
        root= y;
    }
    else if (x==x->parent->left) {
        x->parent->left=y;
    }
    else x->parent->right=y;
    y->left=x;
    x->parent=y;
}

void RedBlackTree::Right_Rotate(RedBlackNode *x)
{
    RedBlackNode *y= x->left;
    x->left=y->right;
    y->right->parent=x;
    y->parent=x->parent;
    if (x->parent==NULL) {
        root= y;
    }
    else if (x==x->parent->right) {
        x->parent->right=y;
    }
    else x->parent->left=y;
    y->right=x;
    x->parent=y;
}
void RedBlackTree::RB_Insert_Fixup(RedBlackNode *z)
{
    while (z->parent->color=='R')
    {
        if (z->parent == z->parent->parent->left) {
            RedBlackNode *y=z->parent->parent->right;
            if (y->color=='R') {
                z->parent->color='B';
                y->color='B';
                z->parent->parent->color='R';
                z=z->parent->parent;
            }
            else if (z==z->parent->right) {
                z=z->parent;
                Left_Rotate(z);
            }
            z->parent->color='B';
            z->parent->parent->color='R';
            Right_Rotate(z->parent->parent);
        }
        else
        {
            RedBlackNode *y=z->parent->parent->left;
            if (y->color=='R') {
                z->parent->color='B';
                y->color='B';
                z->parent->parent->color='R';
                z=z->parent->parent;
            }
            else if (z==z->parent->left) {
                z=z->parent;
                Right_Rotate(z);
            }
            z->parent->color='B';
            z->parent->parent->color='R';
            Left_Rotate(z->parent->parent);
        }
    }
    root->color='B';
}

void RedBlackTree::RB_Insert(RedBlackNode *z)
{
    RedBlackNode *y=NULL;
    RedBlackNode *x=root;
    while (x!=NULL) {
        y=x;
        if (z->key<x->key)
            x=x->right;
        else x=x->right;
    }
    z->parent=y;
    if (y==NULL)
        root=z;
    else if (z->key<y->key)
        y->left=z;
    else y->right=z;
    z->left= NULL;
    z->right= NULL;
    z->color='R';
    RB_Insert_Fixup(z);
}
void RedBlackTree::Insert(int inp)
{
    RedBlackNode *z=new RedBlackNode(inp);
    RB_Insert(z);
}
RedBlackNode* RedBlackTree::RB_Delete(RedBlackNode* z)
{    RedBlackNode *y, *x;
    if ((z->left==NULL) || (z->right==NULL))
        y=z;
    else y=Tree_Successor(z);
    if (y->left!=NULL)
        x=y->left;
    else x=y->right;
    x->parent=y->parent;
    if (y->parent==NULL)
        root=x;
    else if (y=y->parent->left)
        y->parent->left=x;
    else y->parent->right=x;
    if (y!=z)
     z->key=y->key;
    if (y->color=='B')
        RB_Delete_Fixup(x);
    return y;
}
void RedBlackTree::RB_Delete_Fixup(RedBlackNode* x)
{
    RedBlackNode *w;
     while ((x!=root) && (x->color=1))
    {
        if (x==x->parent->left) {
            w=x->parent->right;
            if (w->color=='R') {
                w->color='B';
                x->parent->color='R';
                Left_Rotate(x->parent);
                w=x->parent->right;
            }
            if ((w->left->color=='B') && (w->right->color=='B')) {
                w->color=0;
                x=x->parent;
            }
            else if (w->right->color=='B') {
                w->left->color='B';
                w->color='R';
                Right_Rotate(w);
                w=x->parent->right;
            }
            w->color=x->parent->color;
            x->parent->color='B';
            w->right->color='B';
            Left_Rotate(x->parent);
            x=root;
        }
        else {
            w=x->parent->left;
            if (w->color=='R') {
                w->color='B';
                x->parent->color='R';
                Right_Rotate(x->parent);
                w=x->parent->left;
            }
            if ((w->right->color=='B') && (w->left->color=='B')) {
                w->color='R';
                x=x->parent;
            }
            else if (w->left->color=='B'){
                w->right->color=1;
                w->color='R';
                Left_Rotate(w);
                w=x->parent->left;
            }
            w->color=x->parent->color;
            x->parent->color='B';
            w->left->color='B';
            Right_Rotate(x->parent);
            x=root;
        }
    }
}
RedBlackNode* RedBlackTree::Tree_Successor(RedBlackNode *x)
{
    if (x->right!=NULL)
    {
        while (x->left!=NULL)
            x=x->left;
        return x;
        RedBlackNode *y=x->parent;
        while((y!=NULL) && (x==y->right))
        {
            x=y;
            y=y->parent;
        }
        return y;
    }
}
RedBlackNode* RedBlackTree::RB_Search(int val)
{
    RedBlackNode* x=root;
    while (x!=NULL)
    {
        if (val==x->key)
            return x;
        if (val<x->key)
            x=x->left;
        else x=x->right;
    }
    if (x==NULL)
        return NULL;
}
int main()
{
    RedBlackTree T;
    int inp;
    do
    {
    cout<<"The following operations may be performed on the binary search tree"<<endl;
    cout<<"Insertion - 1"<<endl;
    if (T.root!=NULL)
        cout<<"Deletion - 2"<<endl;
    cout<<"Search - 3"<<endl;
    cout<<"Exit - 4"<<endl;
    cout<<"Enter an option:";
    cin>>inp;
    switch(inp)
    {
        case 1:
        cout<<"Enter a value to insert";
        cin>>inp;
        T.Insert(inp);
        inp=1;
        break;
        case 2:
        cout<<"Enter the value of the node which needs deletion:";
        cin>>inp;
        T.RB_Delete(T.RB_Search(inp));
        inp=2;
        break;
        case 3:
        cout<<"Enter the value of the node that needs to be looked up:";
        cin>>inp;
        RedBlackNode *srch=T.RB_Search(inp);
        if (srch==NULL)
            cout<<"Not found";
        else
        {
            cout<<"Value:"<<inp<<" Color:"<<srch->color;
            cout<<"Parent:"<<srch->parent->key<<" Color:"<<srch->parent->color;
        }
        inp=3;
        break;
    }
    } while(inp!=4);
}

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 

Design By Manish and Ranjan