You are on page 1of 34

Untitled

1.selection sort
#include<stdio.h>
#include<math.h>
#include<time.h>
main(){
int a,b,c,d,e,i,j,k,l;
double z=0,f=0,g;
l=1;
step: printf("\n enter the number of nos.\n");
c=0;
srand (time(NULL));
scanf("%d",&a);
printf("enter the no. of runs\n");
scanf("%d",&b);
int arr[a];
double clkarr[b];
for(k=0;k<b;k++){
for(i=0;i<a;i++)
arr[i]=rand();
time_t begin=time(NULL);
for(d=0;d<a;d++){
for(c=d,e=d+1;c<a,e<a;){
if(arr[c]<arr[e]){
++e;
}
else
{
c=e;
e++;}
}
e--;
j=arr[d];
arr[d]=arr[c];
arr[c]=j;
}
time_t end=time(NULL);
clkarr[k]=(double)end-begin;
printf("\n");
for(c=0;c<a;c++)
Page 1

Untitled
printf("%d\n",arr[c]);
}
printf("\n");
c=0;
for(i=0,c=0;i<b;i++)
c=c+clkarr[i];
double x;
x=0;
x=(double)c/b;
printf("mean time= %lf \n",x);
z=z+x;
printf("total mean time= %lf",z/l);
printf("variance= %lf",z/l-z/(l*l));
l++;
goto step;
}
2.

LL

#include<stdio.h>
#include<stdlib.h>
struct link {
int key;
struct link *next;
};
struct link * create_llist(int max)
{
struct link *head;
struct link *ptr;
int n = 0 ;
if(max<1)return NULL;
ptr=(struct link *)malloc(sizeof(struct
link));
head=ptr;
while(n++ < max-1)
Page 2

Untitled
{
ptr->key=n;
ptr->next=(struct link
*)malloc(sizeof(struct link));
ptr=ptr->next;
}
ptr->key=n;
ptr->next=NULL;
return head;
}
void reverse_llist(struct link *head,struct link
**newHead)
{
struct link **ptr=&head;
if(*ptr == NULL || (*ptr)->next == NULL)
return ;
if((*ptr)->next->next != NULL)
reverse_llist((*ptr)->next,newHead);
else
*newHead = (*ptr)->next ;
(*ptr)->next->next = *ptr ;
(*ptr)->next = NULL;
}
void print_list(struct link *head)
{
struct link *a=head;
if(!a) return ;
while(a->next)
{
printf(" %d\t%d\n", a->key, a->next);
a=a->next;
}
printf(" %d\t%x", a->key, a->next);
printf("\n");
}
Page 3

Untitled
void main(int argc, char*argv[])
{
struct link *head;
//if(!argc) return ;
head=create_llist(5);
print_list(head);
reverse_llist(head,&head);
print_list(head);
}
3.merge
#include<stdio.h>
//int a[8];
void merge(int a[],int i,int m,int j)
{
int p,q,r,c[j+1];
p=i;
q=m+1;
r=i;
while(p<=m&&q<=j)
{
if(a[p]<a[q])
{
c[r] =a[p];
p++;
}
else
{
c[r]=a[q];
q++;
}
r++;
}
for(;p<=m;p++)
{
c[r]=a[p];
r++;
}
for(;q<=j;q++)
{
Page 4

Untitled
c[r]=a[q];
r++;
}
for(r=i;r<=j;r++)
a[r]=c[r];
}
void mergesort(int a[],int i,int j)
{
int m;
if(i==j)
return;
m=(i+j)/2;
mergesort(a,i,m);
mergesort(a,m+1,j);
merge(a,i,m,j);
}
main()
{
int i,m,j,t,b;
printf("\nenter elements\n ");
scanf("%d",&b);
int a[b];
printf("\nenter %d elements\n ",b);
for(t=0;t<b;t++)
scanf("%d",&a[t]);
printf("\nenter i, j\n ");
scanf("%d %d",&i,&j);
//merge(i,m,j);
mergesort(a,i,j);
for(t=0;t<b;t++)
printf(" %d",a[t]);
}
4.radix
#include<stdio.h>
void radix(int* a,int n);
Page 5

Untitled
main()
{
int a,b,c,d;
printf("enter the number of elements");
scanf("%d",&a);
int arr[a];
printf("enter the digit nos.\n");
for(int i=0;i<a;i++)
scanf("%d",&arr[i]);
radix(&arr[0],a);
printf("\n");
for(int i=0;i<a;i++)
printf("%d\n",arr[i]);
}
void radix(int *a,int n)
{
int i,b[n],m=0,exp=1;
for(i=0;i<n;i++)
{
if(a[i]>m)
m=a[i];
}
while(m/exp >0)
{
int count[10]={ 0 };
for(i=0;i<n;i++)
count[a[i]/exp % 10]++;
for(i=1;i<10;i++)
count[i]+=count[i-1];
for(i=n-1;i>=0;i--)
b[--count[a[i] /exp % 10]]
=a[i];
for(i=0;i<n;i++)
a[i]=b[i];
exp*=10;
}
}
5.dfs
Page 6

Untitled
#include<stdio.h>
#include<stdlib.h>
int r;
void printArray(int a[][100],int n);
void AdjacencyMatrix(int a[][100], int n){
int i,j;
for(i = 0;i < n; i++)
{
for(j = 0;j < i; j++)
{
a[i][j] = rand()%2;
a[j][i] = rand()%2;
}
a[i][i] = 0;
}
printArray(a,n);
}
void printArray(int a[][100],int n){
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
void dfs(int a[][100],int n,int *s,int u,int *q){
int v;
s[u]=1;
for(v = 0;v < n; v++)
{
if(a[u][v] == 1 && s[v] == 0)
{
q[++r] = v;
dfs(a,n,s,v,q);
Page 7

Untitled
}
}
}
void printdfs(int a[][100],int n,int *q){
int i,sor,s[100];
printf("Enter source less than %d: ",n);
scanf("%d",&sor);
for(i=0;i<n;i++)
s[i]=0;
q[0]=sor;
dfs(a,n,s,sor,q);
for(i=0;i<n;i++)
{
if(s[i]!=0)
{
printf(" -> %d ",q[i]);
}
}
}
int main()
{
int a[100][100],n,*q;
printf("Enter the number of vertices\n");
scanf("%d",&n);
q = (int *)malloc(sizeof(int)*n);
AdjacencyMatrix(a,n);
printdfs(a,n,q);
return 0;
}
6.MOM
#include<stdio.h>
#include<string.h>
char median[10][40];
void insertion_sort(char arr[][40],int p,int q);
void find_medians(char arr[][40],int k,int q);
char* get_mom(char arr[][40],int p,int q);
Page 8

Untitled
int partition(char arr[][40],int p,int q,char
*mom);
char* select(char arr[][40],int p,int q,int t);
int main()
{
FILE *fp;
char a[160][40];
int i=0;
int size;
int target;
char *result;
printf("Program for finding nth largest
string in alphabetical order\nEnter n : ");
scanf("%d",&target);
fp=fopen("names.txt","r");
while(fgets(a[i],40,fp)!=NULL) {i++;}
size=i;
result=select(a,0,size-1,size-target+1);
printf("The %dth largest string in
alphabetical order is %s\n",target,result);
return(0);
}
void insertion_sort(char arr[][40],int p,int q)
{
int i,j,k;
char key[40];
for(j=p+1;j<=q;j++){
strcpy(key,arr[j]);
i=j-1;
while( i>=p &&
(strcmp(arr[i],key)>0) )
{
Page 9

Untitled
strcpy(arr[i+1],arr[i]);
i-=1;
}
strcpy(arr[i+1],key);
}
}
void find_medians(char arr[][40],int k,int q)
{
int i=0;
int p=k;
while(1){
if(q-p+1<5) {
insertion_sort(arr,p+1,q);
strcpy(median[i],arr[(q+p)/2]);
break;
}
insertion_sort(arr,p,p+4);
strcpy(median[i],arr[p+2]);
i++;
p=p+5;
if(p>q) break;
}
}
char* get_mom(char arr[][40],int p,int q){
if(p==q) {return(median[0]); }
find_medians(arr,p,q);
get_mom(median,0,((q-p)/5));
}

Page 10

Untitled
int partition(char arr[][40],int p,int q,char
*mom){
int i,j;
char temp[40];
i=p-1;
for(j=p;j<=q;j++){
if(strcmp(arr[j],mom)<0){
i+=1;
strcpy(temp,arr[j]);
strcpy(arr[j],arr[i]);
strcpy(arr[i],temp);
}
else if(strcmp(arr[j],mom)==0)
{
strcpy(arr[j],arr[q]);
strcpy(arr[q],mom);
}
}
strcpy(temp,arr[i+1]);
strcpy(arr[i+1],arr[q]);
strcpy(arr[q],temp);
return(i+1);
}
char* select(char arr[][40],int p,int q,int t){
int k;
int num;
char mom[40];
strcpy(mom,get_mom(arr,p,q));
if(p==q) return(arr[p]);
Page 11

Untitled
k=partition(arr,p,q,mom);
num=k-p+1;
if(num==t) return(arr[k]);
else if(t<num)
return(select(arr,p,k-1,t));
else return(select(arr,k+1,q,t-num));
}
7.quick
#include<stdio.h>
#include<time.h>
#include<math.h>
int q1,q2,q3;
void quicksort1(int A[],int p,int r);
void quicksort2(int A[],int p,int r);
void quicksort3(int A[],int p,int r);
int partition1(int A[],int p,int r);
int partition2(int A[],int p,int r);
int partition3(int A[],int p,int r);
main(){
int a,b,c,d;
printf("enter the number of elements in
the array\n");
scanf("%d",&a);
int arr[a];
for(d=0;d<a;d++)
arr[d]=rand();
//printf("\nenter the array\n");
//for(int i=0;i<a;i++)
//scanf("%d",&arr[i]);
quicksort1(arr,0,a-1);
quicksort2(arr,0,a-1);
quicksort3(arr,0,a-1);
printf("the sorted array is\n");
for(int i=0;i<a;i++)
printf("%d\n",arr[i]);
}
void quicksort1(int A[],int p,int r)
Page 12

Untitled
{
if (p<r)
{
q=partition1(A,p,r);
quicksort1(A,p,q-1);
quicksort1(A,q+1,r);
}
}
int partition1(int A[],int p,int r)
{
int x=A[r];
int i=p-1;
int y;
for(int j=p;j<=r-1;j++)
{
if(A[j]<=x)
{
i++;
y=A[j];
A[j]=A[i];
A[i]=y;
}
}
y=A[i+1];
A[i+1]=A[r];
A[r]=y;
return i+1;
}
void quicksort2(int A[],int p,int r)
{
if (p<r)
{
q=partition2(A,p,r);
quicksort2(A,p,q-1);
quicksort2(A,q+1,r);
}
}
int partition2(int A[],int p,int r)
{
int x=A[p];
int i=p;
Page 13

Untitled
int j=r;
int y;
for(;;)
{
if(A[j]>x)
j--;
else if(A[i]<x)
i=i+1;
if(i<j)
{
y=A[i];
A[i]=A[j];
A[j]=y;
}
else
return j;
}
}
void quicksort3(int A[],int p,int r)
{
if (p<r)
{
q=partition3(A,p,r);
quicksort3(A,p,q-1);
quicksort3(A,q+1,r);
}
}
int partition3(int A[],int p,int r)
{
int x=A[(r+p)/2];
A[(r+p)/2]=A[p];
A[p]=x;
int i=p;
int j=r;
int y;
for(;;)
{
if(A[j]>x)
j--;
else if(A[i]<x)
i=i+1;
Page 14

Untitled
if(i<j)
{
y=A[i];
A[i]=A[j];
A[j]=y;
}
else
return j;
}
}
8.kruskal
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void printArray(int a[][100],int n);
void Matrix(int a[][100],int n)
{
// srand(time(NULL));
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=a[j][i]=
rand()%20;
}
a[i][i]=999;
}
printArray(a,n);
}
void printArray(int a[][100],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
Page 15

Untitled
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
int root(int v,int p[])
{
while(p[v]!=v)
v=p[v];
return v;
}
void unionij(int i,int j,int p[])
{
if (j>i)
p[j]=i;
else
p[i]=j;
}
void kruskal(int a[][100],int n)
{
int
count,k,i,j,u,v,t[100][2],p[100],sum,min;
count=k=sum=0;
for(i=0;i<n;i++)
{
p[i]=i;
}
while(count<n)
{
min=999;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
Page 16

Untitled
if(a[i][j]<min)
{
min=a[i][j];
u=i;
v=j;
}
}
}
if(min!=999)
{
i=root(u,p);
j=root(v,p);
//
printf("%d %d

%d

%d %d %d %d %d
\n",u,v,i,j,p[0],p[1],p[2],p[3]);
if(i!=j)
{
t[k][0]=u;
t[k][1]=v;
k++;
sum+=min;
unionij(i,j,p);
}
a[u][v]=a[v][u]=999;
}
count++;
}
if(count!=n)
printf("no min spanning tree
exist\n");
else
{
printf("edges spanning
tree are \n");
Page 17

Untitled
for(k=0;k<n-1;k++)
{
printf("%d<->%d\t",t[k][0],t[k][1]);
}
printf("\n cost =%d
\n",sum);
}
}
main()
{
int a[100][100],n;
printf("enter the no. of vertices");
scanf("%d",&n);
Matrix(a,n);
kruskal(a,n);
}
9.prims
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
int nv;
void printArray(int a[][100],int n);
void Matrix(int a[][100],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=a[j][i]=
rand()%50;
}
a[i][i]=999;
}
printArray(a,n);
}
void printArray(int a[][100],int n)
Page 18

Untitled
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
int root(int v,int p[])
{
while(p[v]!=v)
v=p[v];
return v;
}
void unionij(int i,int j,int p[])
{
if (j>i)
p[j]=i;
else
p[i]=j;
}
void prims(int a[][100],int n)
{
int
count,z,l,k,i,j,u,v,t[100][2],p[100],sum,min,q[nv]
,length=1;
count=k=sum=0;
for(i=0;i<n;i++)
{
p[i]=i;
}
q[0]=0;
Page 19

Untitled
while(count<n)
{
min=999;
for(i=0;i<=count;i++)
{
z=q[i];
for(j=0;j<n;j++)
{
if(a[z][j]<min)
{
min=a[z][j];
u=z;
v=j;
}
}
}
q[length]=v;
length++;
if(min!=999)
{
z=root(u,p);
j=root(v,p);
printf("\n %d
%d",z,j);
if(z!=j)
{
t[k][0]=u;
t[k][1]=v;
printf("\n
%d

%d",u,v);
k++;

sum+=min;
Page 20

Untitled
unionij(z,j,p);
}
a[u][v]=a[v][u]=999;
}
count++;
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
if(count!=n)
printf("no min spanning tree
exist\n");
else
{
printf("edges spanning
tree are \n");
for(k=0;k<n-1;k++)
{
printf("%d<->%d\t",t[k][0],t[k][1]);
}
printf("\n cost =%d
\n",sum);
}
}
main()
{
int a[100][100];
printf("enter the no. of vertices");
scanf("%d",&nv);
Matrix(a,nv);
Page 21

Untitled
prims(a,nv);
}
10.stamps
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int price,n,i,j,temp;
int d[100];
int ch[100];
int a[100];
int min(int b[])
{
int mini=999,k;
for(k=0;k<n;k++)
{
if(b[k]<mini)
{
mini=b[k];
}
}
return mini;
}
main()
{
printf("Enter the total amount of
money:\n");
scanf("%d",&price);
printf("\n");
printf("Enter no of stamps available\n");
scanf("%d",&n);
printf("Enter the values of each
individual stamps(Except for 999)\n");
for(i=0;i<n;i++)
{
Page 22

Untitled
scanf("%d",&d[i]);
}
a[0]=0;
for(i=0;i<n;i++)
{
ch[i]=999;
}
for(i=1;i<price+1;i++)
{
for(j=0;j<n;j++)
{
if((i-d[j])>=0)
{
temp=i-d[j];
ch[j]=a[temp];
}
else
{
}
}
a[i]=1+min(ch);
//
printf("\n%d",a[i]);
}
printf("The minimum number of stamps
required to make the total equal to price is\n");
printf("%d",a[price]);
}
11.bfs
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int a[100][100],n;
int ans[100];
int count=0;
void printmatrix(int r);
int order(int r);
int BST(int r);
Page 23

Untitled
void matrix(int r)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
a[i][j]=a[j][i]=rand()%2;
}
a[i][i]=0;
}
printmatrix(r);
}
void printmatrix(int r)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
int order(int r)
{
int q[r],sum=0,min,i,j,temp;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
sum=sum+a[i][j];
}
printf("%d\n",sum);
q[i]=sum;
sum=0;
}
min=q[0];
for(i=0;i<r;i++)
Page 24

Untitled
{
if(q[i]<min)
{
min=q[i];
temp=i;
}
}
printf("the minimun is %d\n",min);
printf("the position is %d\n",temp);
return temp;
}
int BST(int r)
{
int i,j,count1=0,k,x=0;
for(j=0;j<n;j++)
{
if(a[r][j]==1)
{
count1++;
count++;
for(k=0;k<count;k++)
{
if(ans[k]==j)
{
x=1;
count--;
count1--;
}
else
{
x=0;
}
}
printf("x is %d\n",x);
if(x==0)
{
ans[count]=j;
for(i=0;i<=count;i++)
{
Page 25

Untitled
printf("%d
",ans[i]);
}
printf("\n");
}
else
{
}
a[r][j]=a[j][r]=0;
}
}
return count1;
}
main()
{
int i=1,j,c,d[n],k;
int least;
printf("Enter the number of nodes\n");
scanf("%d",&n);
matrix(n);
least=order(n);
ans[0]=least;
c=BST(least);
c=c+1;
while(c<n)
{
d[i]=BST(ans[0+i]);
c=c+d[i];
i++;
}
printf("The vertices are displayed in the
order in which they are traversed\n");
printf("\n");
for(j=0;j<n;j++)
{
printf("%d-->",ans[j]);
}
Page 26

Untitled
}
15.
#include <stdlib.h>
#include "fatal.h"

02
03
04
typedef int ElementType;
05
#define NegInfinity (-10000)
06
07
#ifndef _RedBlack_H
08
#define _RedBlack_H
09
10
struct RedBlackNode;
11
typedef struct RedBlackNode *Position;
12
typedef struct RedBlackNode *RedBlackTree;
13
14
RedBlackTree MakeEmpty(RedBlackTree T);
15
Position Find(ElementType X, RedBlackTree
T);
16
Position FindMin(RedBlackTree T);
17
Position FindMax(RedBlackTree T);
18
RedBlackTree Initialize(void);
19
RedBlackTree Insert(ElementType X,
RedBlackTree T);
20
RedBlackTree Remove(ElementType X,
RedBlackTree T);
21
ElementType Retrieve(Position P);
22
void PrintTree(RedBlackTree T);
23
24
#endif /* _RedBlack_H */
001
#include "redblack.h"
002
#include <stdlib.h>
003
#include "fatal.h"
004
005
typedef enum ColorType {
006
Red, Black
007
} ColorType;
008
009
struct RedBlackNode {
010
ElementType Element;
011
RedBlackTree Left;
Page 27

Untitled
RedBlackTree Right;
ColorType Color;

012
013
014
};
015
016
static Position NullNode = NULL; /* Needs
initialization */
017
018
/* Initialization procedure */
019
RedBlackTree
020
Initialize(void) {
021
RedBlackTree T;
022
023
if (NullNode == NULL) {
024
NullNode = malloc(sizeof ( struct
RedBlackNode));
025
if (NullNode == NULL)
026
FatalError("Out of space!!!");
027
NullNode->Left = NullNode->Right =
NullNode;
028
NullNode->Color = Black;
029
NullNode->Element = 12345;
030
}
031
032
/* Create the header node */
033
T = malloc(sizeof ( struct
RedBlackNode));
034
if (T == NULL)
035
FatalError("Out of space!!!");
036
T->Element = NegInfinity;
037
T->Left = T->Right = NullNode;
038
T->Color = Black;
039
040
return T;
041
}
042
043
/* END */
044
045
void
046
Output(ElementType Element) {
047
printf("%d\n", Element);
048
}
Page 28

Untitled
049
050
051
*/
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088

/* Print the tree, watch out for NullNode,


/* and skip header */
static void
DoPrint(RedBlackTree T) {
if (T != NullNode) {
DoPrint(T->Left);
Output(T->Element);
DoPrint(T->Right);
}
}
void
PrintTree(RedBlackTree T) {
DoPrint(T->Right);
}
/* END */
static RedBlackTree
MakeEmptyRec(RedBlackTree T) {
if (T != NullNode) {
MakeEmptyRec(T->Left);
MakeEmptyRec(T->Right);
free(T);
}
return NullNode;
}
RedBlackTree
MakeEmpty(RedBlackTree T) {
T->Right = MakeEmptyRec(T->Right);
return T;
}
Position
Find(ElementType X, RedBlackTree T) {
Page 29

Untitled
if (T == NullNode)
return NullNode;
if (X < T->Element)
return Find(X, T->Left);
else
if (X > T->Element)
return Find(X, T->Right);
else
return T;

089
090
091
092
093
094
095
096
097
098
}
099
100
Position
101
FindMin(RedBlackTree T) {
102
T = T->Right;
103
while (T->Left != NullNode)
104
T = T->Left;
105
106
return T;
107
}
108
109
Position
110
FindMax(RedBlackTree T) {
111
while (T->Right != NullNode)
112
T = T->Right;
113
114
return T;
115
}
116
117
/* This function can be called only if K2
has a left child */
118
/* Perform a rotate between a node (K2)
and its left child */
119
120
/* Update heights, then return new root */
121
122
static Position
123
SingleRotateWithLeft(Position K2) {
124
Position K1;
125
126
K1 = K2->Left;
127
K2->Left = K1->Right;
Page 30

Untitled
K1->Right = K2;

128
129
130
return K1; /* New root */
131
}
132
133
/* This function can be called only if K1
has a right child */
134
/* Perform a rotate between a node (K1)
and its right child */
135
136
/* Update heights, then return new root */
137
138
static Position
139
SingleRotateWithRight(Position K1) {
140
Position K2;
141
142
K2 = K1->Right;
143
K1->Right = K2->Left;
144
K2->Left = K1;
145
146
return K2; /* New root */
147
}
148
149
150
/* Perform a rotation at node X */
151
/* (whose parent is passed as a parameter)
*/
152
153
/* The child is deduced by examining Item
*/
154
155
static Position
156
Rotate(ElementType Item, Position Parent)
{
157
158
if (Item < Parent->Element)
159
return Parent->Left = Item <
Parent->Left->Element ?
160
SingleRotateWithLeft(Parent->Left) :
161
Page 31

Untitled
SingleRotateWithRight(Parent->Left);
162
else
163
return Parent->Right = Item <
Parent->Right->Element ?
164
SingleRotateWithLeft(Parent->Right) :
165
SingleRotateWithRight(Parent->Right);
166
}
167
168
169
170
static Position X, P, GP, GGP;
171
172
static
173
void HandleReorient(ElementType Item,
RedBlackTree T) {
174
X->Color = Red; /* Do the color flip
*/
175
X->Left->Color = Black;
176
X->Right->Color = Black;
177
178
if (P->Color == Red) /* Have to rotate
*/ {
179
GP->Color = Red;
180
if ((Item < GP->Element) != (Item
< P->Element))
181
P = Rotate(Item, GP); /* Start
double rotate */
182
X = Rotate(Item, GGP);
183
X->Color = Black;
184
}
185
T->Right->Color = Black; /* Make root
black */
186
}
187
188
RedBlackTree
189
Insert(ElementType Item, RedBlackTree T) {
190
X = P = GP = T;
191
NullNode->Element = Item;
192
while (X->Element != Item) /* Descend
Page 32

Untitled
down the tree */ {
193
GGP = GP;
194
GP = P;
195
P = X;
196
if (Item < X->Element)
197
X = X->Left;
198
else
199
X = X->Right;
200
if (X->Left->Color == Red &&
X->Right->Color == Red)
201
HandleReorient(Item, T);
202
}
203
204
if (X != NullNode)
205
return NullNode; /* Duplicate */
206
207
X = malloc(sizeof ( struct
RedBlackNode));
208
if (X == NULL)
209
FatalError("Out of space!!!");
210
X->Element = Item;
211
X->Left = X->Right = NullNode;
212
213
if (Item < P->Element) /* Attach to
its parent */
214
P->Left = X;
215
else
216
P->Right = X;
217
HandleReorient(Item, T); /* Color it
red; maybe rotate */
218
219
return T;
220
}
221
222
RedBlackTree
223
Remove(ElementType Item, RedBlackTree T) {
224
printf("Remove is unimplemented\n");
225
if (Item)
226
return T;
227
return T;
228
}
Page 33

Untitled
229
230
ElementType
231
Retrieve(Position P) {
232
return P->Element;
233
}
01
#include "redblack.h"
02
#include <stdio.h>
03
04
#define N 800
05
06
main() {
07
RedBlackTree T;
08
Position P;
09
int i;
10
int j = 0;
11
12
T = Initialize();
13
T = MakeEmpty(T);
14
15
for (i = 0; i < N; i++, j = (j + 7) %
N)
16
T = Insert(j, T);
17
printf("Inserts are complete\n");
18
19
for (i = 0; i < N; i++)
20
if ((P = Find(i, T)) == NULL ||
Retrieve(P) != i)
21
printf("Error at %d\n", i);
22
23
24
printf("Min is %d, Max is %d\n",
Retrieve(FindMin(T)),
25
Retrieve(FindMax(T)));
26
27
return 0;
28
}

Page 34

You might also like