You are on page 1of 80

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct treenode
{
int data;
struct treenode *left;
struct treenode *right;
}tnode;
tnode *insertion(int,tnode*);
void preorder(tnode *);
void inorder(tnode *);
void postorder(tnode *);
void main()
{
tnode *T=NULL;
int ch1,n;
char ch2;
do
{
clrscr();
printf("\n\t\t****Operation With Tree****");
printf("\n\t1.Insertion");
printf("\n\t2.Inorder Traversal");
printf("\n\t3.Preorder Traversal");
printf("\n\t4.Postorder Traversal");
printf("\n\tEnter Your Choice :");
scanf("%d",&ch1);
switch(ch1)
{case 1:
printf("\n\nenter the element to be inserted :");
scanf("%d",&n);
T=insertion(n,T);
break;
case 2:
inorder(T);
break;
case 3:
preorder(T);
break;
case 4:
postorder(T);
break;
default:
printf("\n\nInvalid Option");
break;}

printf("\n\nDo you want to continue y/n : ");


scanf("%s",&ch2);
}while(ch2=='y');
getch();
}

tnode *insertion(int x,tnode *T)


{
if(T==NULL)
{
T=(tnode *)malloc(sizeof(tnode));
if(T==NULL)
printf("\nout of space");
else
{
T->data=x;
T->left=T->right=NULL;
}
}
else
{
if(x<(T->data))
T->left=insertion(x,T->left);
else
{
if(x>T->data)
T->right=insertion(x,T->right);
}
}
return T;
}

void preorder(tnode *T)


{
if(T!=NULL)
{
printf("\t%d",T->data);
preorder(T->left);
preorder(T->right);
}
}

void postorder(tnode *T)


{
if(T!=NULL)
{
postorder(T->left);
postorder(T->right);
printf("\t%d",T->data);
}
}
void inorder(tnode *T)
{
if(T!=NULL)
{
inorder(T->left);
printf("\t%d",T->data);
inorder(T->right);
}
}

OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
struct tree
{
int data;
struct tree *lchild;
struct tree *rchild;
}*t,*temp;

int element;
void inorder(struct tree *);
void preorder(struct tree *);
void postorder(struct tree *);
struct tree * create(struct tree *, int);
struct tree * find(struct tree *, int);
struct tree * insert(struct tree *, int);
struct tree * del(struct tree *, int);
struct tree * findmin(struct tree *);
struct tree * findmax(struct tree *);
void main()
{
int ch;

do
{
printf("\n\t\t\tBINARY SEARCH TREE");
printf("\n\t\t\t****** ****** ****");
printf("\nMain Menu\n");
{
printf("\n1.Create\n2.Insert\n3.Delete\n4.Find\n5.FindMin\n6.FindMax");
printf("\n7.Inorder\n8.Preorder\n9.Postorder\n10.Exit\n");
printf("\nEnter ur choice :");
scanf("%d",&ch);
switch(ch)
case 1:
printf("\nEnter the data:");
scanf("%d",&element);
t=create(t,element);
inorder(t);
break;
case 2:
printf("\nEnter the data:");
scanf("%d",&element);
t=insert(t,element);
inorder(t);
break;
case 3:
printf("\nEnter the data:");
scanf("%d",&element);
t=del(t,element);
inorder(t);
break;
case 4:
printf("\nEnter the data:");
scanf("%d",&element);
temp=find(t,element);
if(temp->data==element)
printf("\nElement %d is at %d",element,temp);
else
printf("\nElement is not found");
break;
case 5:
temp=findmin(t);
printf("\nMin element=%d",temp->data);
break;
case 6:
temp=findmax(t);
printf("\nMax element=%d",temp->data);
break;
case 7:
inorder(t);
break;
case 8:
preorder(t);
break;
case 9:
postorder(t);
break;
case 10:
exit(0);
}
}while(ch<=10);
}

struct tree * create(struct tree *t, int element)


{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL;
t->rchild=NULL;
return t;
}
struct tree * find(struct tree *t, int element)
{
if(t==NULL)
return NULL;
if(element<t->data)
return(find(t->lchild,element));
else
if(element>t->data)
return(find(t->rchild,element));
else
return t;
}

struct tree *findmin(struct tree *t)


{
if(t==NULL)
return NULL;
else
if(t->lchild==NULL)
return t;
else
return(findmin(t->lchild));
}

struct tree *findmax(struct tree *t)


{
if(t!=NULL)
{
while(t->rchild!=NULL)
t=t->rchild;
}
return t;
}
struct tree *insert(struct tree *t,int element)
{
if(t==NULL)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL;
t->rchild=NULL;
return t;
}
else
{
if(element<t->data)
{
t->lchild=insert(t->lchild,element);
}
else
if(element>t->data)
{
t->rchild=insert(t->rchild,element);
}
else
if(element==t->data)
{
printf("element already present\n");
}
return t;
}
}
struct tree * del(struct tree *t, int element)
{
if(t==NULL)
printf("element not found\n");
else
if(element<t->data)
t->lchild=del(t->lchild,element);
else
if(element>t->data)
t->rchild=del(t->rchild,element);
else
if(t->lchild&&t->rchild)
{
temp=findmin(t->rchild);
t->data=temp->data;
t->rchild=del(t->rchild,t->data);
}
else
{
temp=t;
if(t->lchild==NULL)
t=t->rchild;
else
if(t->rchild==NULL)
t=t->lchild;
free(temp);
}
return t;
}
void inorder(struct tree *t)
{
if(t==NULL)
return;
else
{
inorder(t->lchild);
printf("\t%d",t->data);
inorder(t->rchild);
}
}
void preorder(struct tree *t)
{
if(t==NULL)
return;
else
{
printf("\t%d",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void postorder(struct tree *t)
{
if(t==NULL)
return;
else
{
postorder(t->lchild);
postorder(t->rchild);
printf("\t%d",t->data);
}
}
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
char stack[100];
int top=0;
char exp[100];
struct table
{
char s[2];
int isp;
int icp;
}pr[7];
int isp(char c)
{
int i;
for(i=0;i<=6;i++)
if(pr[i].s[0]==c)
return(pr[i].isp);
return 0;
}
int icp(char c)
{
int i;
for(i=0;i<=6;i++)
if(pr[i].s[0]==c)
return(pr[i].icp);
return 0;
}
void main()
{
int i;
//clrscr();
strcpy(pr[0].s,"^");
pr[0].isp=3;
pr[0].icp=4;
strcpy(pr[1].s,"*");
pr[1].isp=2;
pr[1].icp=2;
strcpy(pr[2].s,"/");
pr[2].isp=2;
pr[2].icp=2;
strcpy(pr[3].s,"+");
pr[3].isp=1;
pr[3].icp=1;
strcpy(pr[4].s,"-");
pr[4].isp=1;
pr[4].icp=1;
strcpy(pr[5].s,"(");
pr[5].isp=0;
pr[5].icp=4;
strcpy(pr[6].s,"=");
pr[6].isp=-1;
pr[6].icp=0;
clrscr();
stack[top]='=';
printf("enter the infix expression");
gets(exp);
i=0;
printf("the postfix expression is ");
while(i<strlen(exp))
{
if(isalpha(exp[i])==0)
{
if(exp[i]==')')
{
while(stack[top]!='(')
{

printf("%c",stack[top]);
top--;
}
top--;

}
else
{
while(isp(stack[top])>=icp(exp[i]))
{
printf("%c",stack[top]);
top--;
}
top++;
stack[top]=exp[i];
}
}
else
printf("%c",exp[i]);
i++;
}
while(top>0)
{
printf("%c",stack[top]);
top--;
}
getch();

OUTPUT:
PROGRAM:

#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{

int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{

for(i=front; i<rear; i++)

{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}
OUTPUT:
PROGRAM:

#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is
%d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{ printf("\n The STACK is empty");
}
}
OUTPUT:
PROGRAM:

#include<stdio.h>
#include<conio.h>
#define maxsize 10
int list[maxsize],n;
void Create();
void Insert();
void Delete();
void Display();
void Search();
void main()
{
int choice;
do
{
printf("\nArray implementation of List\n");
printf("1.create 2.insert 3.delete\n");
printf("4.search 5.exit\n");
printf("Enter your choice:");
scanf("%d",&choice);

switch(choice)
{
case 1:
Create();
break;
case 2:
Insert();
break;
case 3:
Delete();
break;
case 4:
Search();
break;
case 5:
exit(1);
default:
printf("Enter option between 1-6\n");
break;
}
}while(choice<7);
}
void Create()
{
int i;
printf("\nEnter the element to be added: ");
scanf("%d",&n);
printf("\nEnter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
Display();
}
void Insert()
{
int i,data,pos;
printf("\nEnter the data to be inserted:" );
scanf("%d",&data);
printf("\nEnter position at which element to be inserted: ");
scanf("%d",&pos);
for(i=n-1;i>=pos;i--)

list[i+1]=list[i];
list[pos-1]=data;
n+=1;
Display();
}
void Delete()
{
int i,pos;
printf("\nEnter the position to be deleted:");
scanf("%d",&pos);
printf("\nThe data deleted is : %d",list[pos-1]);
for(i=pos-1;i<n-1;i++)
list[i]=list[i+1];
n=n-1;
Display();
}
void Display()
{
int i;
printf("\n********** Elements in array **********\n");
for(i=0;i<n;i++)
printf("%d",list[i]);
}
void Search()
{
int search,i,count=0;
printf("\nEnter the element to be searched :");
scanf("%d",&search);
for(i=0;i<=n;i++)
{
if(search==list[i])
count++;
}
if(count==0)
printf("Element not present in the list");
else
printf("Element present in the list");
}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#define NULL 0
struct info
{
int data;
struct info *next;
};
struct info *head,*temp,*disp;
void additem();
void delitem();
void display();
int size();
void search();
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n1.Add records");
printf("\n2.Delete records");
printf("\n3.Display records");
printf("\n4.Count no. of items in the list");
printf("\n5.Searching an item in the list");
printf("\n6.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
fflush(stdin);
switch(choice)
{
case 1:
additem();
break;
case 2:
delitem();
break;
case 3:
display();
break;
case 4:
printf("\nThe size of the list is %d",size());
break;
case 5:
search();
break;
case 6:
exit(0);
}
}
}
void additem()
{
struct info *add;
char proceed='y';
while(toupper(proceed)=='Y')
{
add=(struct info*)malloc(sizeof(struct info));
printf("Enter data:");
scanf("%d",&add->data);
fflush(stdin);
if(head==NULL)
{
head=add;
add->next=NULL;
temp=add;
}
else
{
temp->next=add;
add->next=NULL;
temp=add;
}
printf("\nWant to proceed y/n");
proceed=getchar();
fflush(stdin);
}
}
void delitem()
{
struct info *curr,*prev;
int tdata;
if(head==NULL)
{
printf("\nNo records to delete");
return;
}
printf("\nEnter the data to delete");
scanf("%d",&tdata);
fflush(stdin);
prev=curr=head;
while((curr!=NULL)&&(curr->data!=tdata))
{
prev=curr;
curr=curr->next;
}
if(curr==NULL)
{
printf("\nData not found");
return;
}
if(curr==head)
head=head->next;
else
{
/*for inbetween element deletion*/
prev->next=curr->next;
/*for the last element deletion*/
if(curr->next==NULL)
temp=prev;
}
free(curr);
}
void display()
{
if(head==NULL)
{
printf("\nNo data to display");
return;
}
for(disp=head;disp!=NULL;disp=disp->next)
{
printf("Data->%d",disp->data);
}
}
int size()
{
int count=0;
if(head==NULL)
return count;
for(disp=head;disp!=NULL;disp=disp->next)
count++;
return count;
}
void search()
{
int titem,found=0;
if(head==NULL)
{
printf("\nNo data in the list");
return;
}
printf("\Enter the no. to search:");
scanf("%d",&titem);
for(disp=head;disp!=NULL&&found==0;disp=disp->next)
{
if(disp->data==titem)
found=1;
}
if(found==0)
printf("\nSearch no. is not present in the list");
else
printf("\nSearch no. is present in the list");
return;
}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define NULL 0
struct info
{
int data;
struct info *next;
struct info *prev;
};
struct info *head,*temp,*disp;
void additem();
void delitem();
void display();
int size();
void search();
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n1.Add records");
printf("\n2.Delete records");
printf("\n3.Display records");
printf("\n4.Count no. of items in the list");
printf("\n5.Searching an item in the list");
printf("\n6.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
fflush(stdin);
switch(choice)
{
case 1:
additem();
break;
case 2:
delitem();
break;
case 3:
display();
break;
case 4:
printf("\nThe size of the list is %d",size());
break;
case 5:
search();
break;
case 6:
exit(0);
}
}
}
void additem()
{
struct info *add;
char proceed='y';
while(toupper(proceed)=='Y')
{
add=(struct info*)malloc(sizeof(struct info));
printf("Enter data:");
scanf("%d",&add->data);
fflush(stdin);
if(head==NULL)
{
head=add;
add->next=NULL;
add->prev=NULL;
temp=add;
}
else
{
temp->next=add;
add->prev=temp;
add->next=NULL;
temp=add;
}
printf("\nWant to proceed y/n");
proceed=getchar();
fflush(stdin);
}
}
void delitem()
{
int x;
struct info *p;;
if(head==NULL)
{
printf("\nNo items in the list");
return;
}
printf("\nEnter the data to delete");
scanf("%d",&x);
//fflush(stdin);
p=(struct info *)malloc(sizeof(struct info));
p=head->next;
if(head->data==x)
{
head=head->next;
return;
}
while(p)
{
if(p->data==x)
{
p->prev->next=p->next;
if(p->next!=NULL)
p->next->prev=p->prev;
else
temp=p->prev;
return;
}
else
{
p=p->next;
}
}
printf("\nInvalid input");
}
void display()
{
if(head==NULL)
{
printf("\nNo data to display");
return;
}
printf("\nFrom forward direction\n");
for(disp=head;disp!=NULL;disp=disp->next)
{
printf("Data->%d",disp->data);
}
printf("\nFrom backward direction\n");
for(disp=temp;disp!=NULL;disp=disp->prev)
{
printf("Data->%d",disp->data);
}
}
int size()
{
int count=0;
if(head==NULL)
return count;
for(disp=head;disp!=NULL;disp=disp->next)
count++;
return count;
}
void search()
{
int titem,found=0;
if(head==NULL)
{
printf("\nNo data in the list");
return;
}
printf("\Enter the no. to search:");
scanf("%d",&titem);
for(disp=head;disp!=NULL&&found==0;disp=disp->next)
{
if(disp->data==titem)
found=1;
}
}
if(found==0)
printf("\nSearch no. is not present in the list");
else
printf("\nSearch no. is present in the list");
return;}
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

// Node structure to represent a term in the polynomial


typedef struct Node {
int coefficient;
int exponent;
struct Node* next;
} Node;

// Function to create a new node with given coefficient and exponent


Node* createNode(int coeff, int exp) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coefficient = coeff;
newNode->exponent = exp;
newNode->next = NULL;
return newNode;
}

// Function to insert a new term into the polynomial


void insertTerm(Node** poly, int coeff, int exp) {
Node* newNode = createNode(coeff, exp);
if (*poly == NULL) {
*poly = newNode;
} else {
Node* current = *poly;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}

// Function to add two polynomials and return the result


Node* addPolynomials(Node* poly1, Node* poly2) {
Node* result = NULL;
Node* current1 = poly1;
Node* current2 = poly2;

while (current1 != NULL || current2 != NULL) {


int coeff1 = (current1 != NULL) ? current1->coefficient : 0;
int exp1 = (current1 != NULL) ? current1->exponent : 0;
int coeff2 = (current2 != NULL) ? current2->coefficient : 0;
int exp2 = (current2 != NULL) ? current2->exponent : 0;

int sumCoeff = coeff1 + coeff2;


int sumExp = exp1;

if (exp1 == exp2) {
sumExp = exp1; // Exponents are equal
current1 = current1->next;
current2 = current2->next;
} else if (exp1 > exp2) {
current1 = current1->next;
} else {
sumExp = exp2;
current2 = current2->next;
}

insertTerm(&result, sumCoeff, sumExp);


}
return result;
}

// Function to display a polynomial


void displayPolynomial(Node* poly) {
Node* current = poly;
while (current != NULL) {
printf("%dx^%d", current->coefficient, current->exponent);
current = current->next;
if (current != NULL) {
printf(" + ");
}
}
printf("\n");
}

// Function to free memory allocated for the polynomial


void freePolynomial(Node* poly) {
Node* current = poly;
Node* next;

while (current != NULL) {


next = current->next;
free(current);
current = next;
}
}
int main() {
Node* poly1 = NULL;
Node* poly2 = NULL;

// Get input for the first polynomial


int numTerms1;
printf("Enter the number of terms in Polynomial 1: ");
scanf("%d", &numTerms1);

int coeff, exp;


int i;
for (i = 0; i < numTerms1; ++i) {
printf("Enter the coefficient and exponent of term %d: ", i + 1);
scanf("%d %d", &coeff, &exp);
insertTerm(&poly1, coeff, exp);
}

// Get input for the second polynomial


int numTerms2;
printf("Enter the number of terms in Polynomial 2: ");
scanf("%d", &numTerms2);

for (i = 0; i < numTerms2; ++i) {


printf("Enter the coefficient and exponent of term %d: ", i + 1);
scanf("%d %d", &coeff, &exp);
insertTerm(&poly2, coeff, exp);
}
// Display the input polynomials
printf("\nPolynomial 1: ");
displayPolynomial(poly1);

printf("Polynomial 2: ");
displayPolynomial(poly2);

// Add the polynomials and display the result


Node* result = addPolynomials(poly1, poly2);
printf("\nSum of Polynomials: ");
displayPolynomial(result);

// Free allocated memory


freePolynomial(poly1);
freePolynomial(poly2);
freePolynomial(result);

return 0;
}
OUTPUT:
PROGRAM:

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

typedef struct node {


int data;
struct node *left, *right;
int ht;
} node;

node *insert(node *, int);


node *Delete(node *, int);
void preorder(node *);
void inorder(node *);
int height(node *);
node *rotateright(node *);
node *rotateleft(node *);
node *RR(node *);
node *LL(node *);
node *LR(node *);
node *RL(node *);
int BF(node *);
int main() {
node *root = NULL;
int x, n, i, op;
do {
printf("\n1)Create:");
printf("\n2)Insert:");
printf("\n3)Delete:");
printf("\n4)Print:");
printf("\n5)Quit:");
printf("\n\nEnter Your Choice:");
scanf("%d", &op);
switch (op) {
case 1:
printf("\nEnter no. of elements:");
scanf("%d", &n);
printf("\nEnter tree data:");
root = NULL;
for (i = 0; i < n; i++) {
scanf("%d", &x);
root = insert(root, x);
}
break;
case 2:
printf("\nEnter a data:");
scanf("%d", &x);
root = insert(root, x);
break;
case 3:
printf("\nEnter a data:");
scanf("%d", &x);
root = Delete(root, x);
break;
case 4:
printf("\nPreorder sequence:\n");
preorder(root);
printf("\n\nInorder sequence:\n");
inorder(root);
printf("\n");
break;
}
} while (op != 5);
return 0;
}
node *insert(node *T, int x) {
if (T == NULL) {
T = (node *)malloc(sizeof(node));
T->data = x;
T->left = NULL;
T->right = NULL;
} else if (x > T->data) {
T->right = insert(T->right, x);
if (BF(T) == -2)
if (x > T->right->data)
T = RR(T);
else
T = RL(T);
} else if (x < T->data) {
T->left = insert(T->left, x);
if (BF(T) == 2)
if (x < T->left->data)
T = LL(T);
else
T = LR(T);
}
T->ht = height(T);
return (T);
}

node *Delete(node *T, int x) {


node *p;
if (T == NULL) {
return NULL;
} else if (x > T->data) {
T->right = Delete(T->right, x);
if (BF(T) == 2)
if (BF(T->left) >= 0)
T = LL(T);
else
T = LR(T);
} else if (x < T->data) {
T->left = Delete(T->left, x);
if (BF(T) == -2)
if (BF(T->right) <= 0)
T = RR(T);
else
T = RL(T);
} else {
if (T->right != NULL) {
p = T->right;
while (p->left != NULL)
p = p->left;
T->data = p->data;
T->right = Delete(T->right, p->data);
if (BF(T) == 2)
if (BF(T->left) >= 0)
T = LL(T);
else
T = LR(T);
} else
return (T->left);
}
T->ht = height(T);
return (T);
}
int height(node *T) {
int lh, rh;
if (T == NULL)
return (0);
if (T->left == NULL)
lh = 0;
else
lh = 1 + T->left->ht;
if (T->right == NULL)
rh = 0;
else
rh = 1 + T->right->ht;
if (lh > rh)
return (lh);
return (rh);
}
node *rotateright(node *x) {
node *y;
y = x->left;
x->left = y->right;
y->right = x;
x->ht = height(x);
y->ht = height(y);
return (y);
}

node *rotateleft(node *x) {


node *y;
y = x->right;
x->right = y->left;
y->left = x;
x->ht = height(x);
y->ht = height(y);
return (y);
}
node *RR(node *T) {
T = rotateleft(T);
return (T);
}
node *LL(node *T) {
T = rotateright(T);
return (T);
}
node *LR(node *T) {
T->left = rotateleft(T->left);
T = rotateright(T);
return (T);
}

node *RL(node *T) {


T->right = rotateright(T->right);
T = rotateleft(T);
return (T);
}
int BF(node *T) {
int lh, rh;
if (T == NULL)
return (0);
if (T->left == NULL)
lh = 0;
else
lh = 1 + T->left->ht;
if (T->right == NULL)
rh = 0;
else
rh = 1 + T->right->ht;
return (lh - rh);
}
void preorder(node *T) {
if (T != NULL) {
printf("%d(Bf=%d)", T->data, BF(T));
preorder(T->left);
preorder(T->right);
}}
void inorder(node *T) {
if (T != NULL) {
inorder(T->left);
printf("%d(Bf=%d)", T->data, BF(T));
inorder(T->right);
}
}

OUTPUT:
PROGRAM:

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

void insert();
void del();
void display();

struct node {
int priority;
int info;
struct node *next;
};

typedef struct node N;

N *start = NULL, *q, *temp, *new;

int main() {
int ch;
do {
printf("\n[1] INSERTION\t[2] DELETION\t[3] DISPLAY [4]
EXIT\t:");
scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
break;
}
} while (ch < 4);

return 0;
}

void insert() {
int item, itprio;
new = (N *)malloc(sizeof(N));

printf("ENTER THE ELEMENT TO BE INSERTED:\t");


scanf("%d", &item);

printf("ENTER ITS PRIORITY:\t");


scanf("%d", &itprio);

new->info = item;
new->priority = itprio;
new->next = NULL;

if (start == NULL) {
start = new;
} else if (start != NULL && itprio <= start->priority) {
new->next = start;
start = new;
} else {
q = start;
while (q->next != NULL && q->next->priority <= itprio) {
q = q->next;
}
new->next = q->next;
q->next = new;
}
}

void del() {
if (start == NULL) {
printf("\nQUEUE UNDERFLOW\n");
} else {
new = start;
printf("\nDELETED ITEM IS %d\n",
new->info);
start = start->next;
free(new);
}
}

void display() {
temp = start;
if (start == NULL) {
printf("QUEUE IS EMPTY\n");
} else {
printf("QUEUE IS:\n");
for (temp = start; temp != NULL; temp
= temp->next) {
printf("\n%d priority = %d\n", temp-
>info, temp->priority);
}
}
}
OUTPUT:
PROGRAM:

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

void DFS(int);
int G[10][10], visited[10], n;

int main() {
int i, j;
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("\nEnter adjacency matrix of the
graph:\n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &G[i][j]);
for (i = 0; i < n; i++)
visited[i] = 0;

DFS(0);

return 0;
}

void DFS(int i) {
int j;
printf("\n%d", i);
visited[i] = 1;

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


if (!visited[j] && G[i][j] == 1)
DFS(j);
}
OUTPUT:
PROGRAM:

#include <stdio.h>

int n, i, j, visited[10], queue[10], front = -1, rear = -1;


int adj[10][10];

void bfs(int v) {
for (i = 1; i <= n; i++)
if (adj[v][i] && !visited[i])
queue[++rear] = i;
if (front <= rear) {
visited[queue[front]] = 1;
bfs(queue[front++]);
}
}

int main() {
int v;
printf("Enter the number of vertices: ");
scanf("%d", &n);

for (i = 1; i <= n; i++) {


queue[i] = 0;
visited[i] = 0;
}
printf("Enter graph data in matrix form:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &adj[i][j]);

printf("Enter the starting vertex: ");


scanf("%d", &v);

bfs(v);

printf("The nodes which are reachable are:\n");


for (i = 1; i <= n; i++)
if (visited[i])
printf("%d\t", i);

if (front > rear)


printf("BFS is not possible. Not all nodes are reachable.\n");

return 0;
}
OUTPUT:
PROGRAM:

#include <stdio.h>
#define INFINITY 9999
#define MAX 10
void Dijkstra(int Graph[MAX][MAX], int n, int start);
void Dijkstra(int Graph[MAX][MAX], int n, int start) {
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;

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


for (j = 0; j < n; j++)
if (Graph[i][j] == 0)
cost[i][j] = INFINITY;
else
cost[i][j] = Graph[i][j];

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


distance[i] = cost[start][i];
pred[i] = start;
visited[i] = 0;
}

distance[start] = 0;
visited[start] = 1;
count = 1;

while (count < n - 1) {


mindistance = INFINITY;

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


if (distance[i] < mindistance && visited[i] == 0) {
mindistance = distance[i];
nextnode = i;
}

visited[nextnode] = 1;

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


if (visited[i] == 0)
if (mindistance + cost[nextnode][i] < distance[i]) {
distance[i] = mindistance + cost[nextnode][i];
pred[i] = nextnode;
}

count++;
}

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


if (i != start) {
printf("\nDistance from source to %d: %d", i, distance[i]);
}
}

int main() {
int Graph[MAX][MAX], i, j, n;
n = 4;

Graph[0][0] = 0;
Graph[0][1] = 2;
Graph[0][2] = 0;
Graph[0][3] = 1;

Graph[1][0] = 0;
Graph[1][1] = 0;
Graph[1][2] = 0;
Graph[1][3] = 2;
Graph[2][0] = 3;
Graph[2][1] = 0;
Graph[2][2] = 0;
Graph[2][3] = 0;

Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 1;
Graph[3][3] = 0;

int start = 0;
Dijkstra(Graph, n, start);

return 0;
}

OUTPUT:
PROGRAM:
#include <stdio.h>

int main() {
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d integers: ", n);


for (c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("Enter value to find: ");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first + last) / 2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle + 1);
break;
} else {
last = middle - 1;
middle = (first + last) / 2;
}
}

if (first > last)


printf("Not found! %d is not present in the list.\n", search);
return 0;
}
OUTPUT:
PROGRAM:

#include<stdio.h>
#include<conio.h>
int main()
{
int data[100],n,temp,i,j;

printf("Enter number of terms(should be less than 100");


scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&data[i]);
for(i=1;i<n;i++)
{
temp=data[i];
j=i-1;
while(temp<data[j] && j>=0)
{
data[j+1]=data[j];
--j;
}
data[j+1]=temp;
}
printf("In ascending order :");
for(i=0;i<n;i++)
printf("%d",data[i]);
getch();
return 0;
}
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

struct data {
int key;
int value;
};

struct hashtable_item {
int flag; // 0: data not present, 1: data present, 2: data was present but deleted
struct data *item;
};

struct hashtable_item *array;


int max = 7;
int size = 0;
int prime = 3;

int hashcode1(int key) {


return (key % max);
}

int hashcode2(int key) {


return (prime - (key % prime));
}
void insert(int key, int value) {
int hash1 = hashcode1(key);
int hash2 = hashcode2(key);
int index = hash1;
struct data *new_item = (struct data *)malloc(sizeof(struct data));
new_item->key = key;
new_item->value = value;
if (size == max) {
printf("\nHash Table is full, cannot insert more items\n");
return;
}
while (array[index].flag == 1) {
if (array[index].item->key == key) {
printf("\nKey already present, updating its value\n");
array[index].item->value = value;
return;
}
index = (index + hash2) % max;
if (index == hash1) {
printf("\nInsertion failed\n");
return;
}
printf("\nProbing\n");
}

array[index].item = new_item;
array[index].flag = 1;
size++;
printf("\nKey (%d) has been inserted\n", key);
}

void remove_element(int key) {


int hash1 = hashcode1(key);
int hash2 = hashcode2(key);
int index = hash1;
if (size == 0) {
printf("\nHash Table is empty\n");
return;
}
while (array[index].flag != 0) {
if (array[index].flag == 1 && array[index].item->key == key) {
array[index].item = NULL;
array[index].flag = 2;
size--;
printf("\nKey (%d) has been removed\n", key);
return;
}
index = (index + hash2) % max;
if (index == hash1) {
break;
}
}

printf("\nKey (%d) does not exist\n", key);


}

int size_of_hashtable() {
return size;
}

void display() {
int i;
for (i = 0; i < max; i++) {
if (array[i].flag != 1) {
printf("\nArray[%d] has no elements\n", i);
} else {
printf("\nArray[%d] has elements\nKey (%d) and Value (%d)\n", i,
array[i].item->key, array[i].item->value);
}
}
}
void init_array() {
int i;
for (i = 0; i < max; i++) {
array[i].item = NULL;
array[i].flag = 0;
}
prime = get_prime();
}

int get_prime() {
int i, j;
for (i = max - 1; i >= 1; i--) {
int flag = 0;
for (j = 2; j <= (int)sqrt(i); j++) {
if (i % j == 0) {
flag++;
}
}
if (flag == 0) {
return i;
}
}
return 3;
}

int main() {
int choice, key, value, n, c;
clrscr();

array = (struct hashtable_item *)malloc(max * sizeof(struct hashtable_item));


init_array();
do {
printf("Implementation of Hash Table in C with Double Hashing.\n\n");
printf("MENU-:\n1. Insert item in the Hash Table\n2. Remove item from
the Hash Table\n3. Check the size of Hash Table\n4. Display Hash
Table\n\nPlease enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Inserting element in Hash Table\nEnter key and value: ");
scanf("%d %d", &key, &value);
insert(key, value);
break;

case 2:
printf("Deleting in Hash Table\nEnter the key to delete: ");
scanf("%d", &key);
remove_element(key);
break;

case 3:
n = size_of_hashtable();
printf("Size of Hash Table is: %d\n", n);
break;

case 4:
display();
break;

default:
printf("Wrong Input\n");
}
printf("\nDo you want to continue? (press 1
for yes): ");
scanf("%d", &c);

} while (c == 1);

getch();
return 0;
}

OUTPUT:

You might also like