You are on page 1of 9

Ans 1

#include<stdio.h>
#include<malloc.h>
typedef struct avl
{
int data;
struct avl *right,*left;
}AVL;
AVL *root,*x,*y,*parent;
AVL *check(AVL *p,AVL *q);
AVL *insert(AVL *root,int x)
{
if(root==NULL)
{
root=(AVL*) malloc(sizeof(AVL));
root->data=x;
root->left=NULL;
root->right=NULL;
y=root;
}
else if(x>root->data)
{
root->right=insert(root->right,x);
root=check(root,root->right);
}
else if(x<root->data)
{
root->left=insert(root->left,x);
root=check(root,root->left);
}
return root;
}
AVL *LL(AVL *x)
{
AVL *p=x->left;
AVL *q=p->right;
p->right=x;
x->left=q;
return p;
}
AVL *RR(AVL *x)
{
AVL *p=x->right;
AVL *q=p->left;
p->left=x;
x->right=q;
return p;
}
AVL *LR(AVL *x)
{
x->left=RR(x->left);
return(LL(x));
}
AVL *RL(AVL *x)
{
x->right=LL(x->right);
return(RR(x));
}
AVL *modify(AVL *x,AVL *y)
{
if(x->data>y->data)
{
if(x->left->data>y->data)
return(LL(x));
else return(LR(x));
}
else
{
if(x->right->data>y->data)
return(RL(x));
else return(RR(x));
}
}
int height(AVL *root)
{
if(root==NULL)
return 0;
else
{
int lheight=height(root->left);
int rheight=height(root->right);
if(lheight>rheight)
return (lheight+1);
else return(rheight+1);
}
}
AVL *check(AVL *root,AVL *p)
{
if(p)
{
int l=height(p->left);
int r=height(p->right);
if((l-r)==-2||(l-r)==2)
{
printf("unbalancing detected\n");
x=p;
if(root==p)
{
root=modify(x,y);
}
else if(root->left==p)
root->left=modify(x,y);
else if(root->right==p)
root->right=modify(x,y);
printf("modified (-_-)\n");
}
else
{
check(p,p->left);
check(p,p->right);
}
}
return root;
}
AVL *del_check(AVL *root,AVL *p)
{
if(p)
{
int l=height(p->left);
int r=height(p->right);
if((l-r)==-2)
{
printf("unbalancing -2 detected\n");
x=p;
if(root==p)
{
root=RR(x);
}
else if(root->left==p)
root->left=RR(x);
else if(root->right==p)
root->right=RR(x);
printf("modified (-_-)\n");
}
else if((l-r)==2)
{
printf("unbalancing 2 detected\n");
x=p;
if(root==p)
{
root=LL(x);
}
else if(root->left==p)
root->left=LL(x);
else if(root->right==p)
root->right=LL(x);
printf("modified (-_-)\n");
}
else
{
check(p,p->left);
check(p,p->right);
}
}
return root;
}
AVL *insuc(AVL *root)
{
AVL *p=root->right;
while(p->left!=NULL)
p=p->left;
return p;
}
AVL *delete(AVL *root, int x)
{
if(root==NULL)
{printf("empty tree");
return NULL;}
if(x>root->data)
root->right=delete(root->right,x);
else if(x<root->data)
root->left=delete(root->left,x);
else
{

if(root->left==NULL)
{
AVL *q=root->right;
free(root);return q;
}
else if(root->right==NULL)
{
AVL *q=root->left;
free(root);return q;
}
else
{
AVL *q=insuc(root);
root->data=q->data;
root->right=delete(root->right,q->data);
}
}
return root;
}
void dis_inorder(AVL *root)
{
if(root)
{
dis_inorder(root->left);
printf("%3d",root->data);
dis_inorder(root->right);
}
}
int main()
{int k;
root=NULL;
printf("enter the number to insert or -1 to stop:\n");
scanf("%d",&k);
do
{
root=insert(root,k);
root=check(root,root);
scanf("%d",&k); }
while(k!=-1);
int ch,x;
do
{
printf("1.insert 2.delete 3.dis_inorder 4.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("insert a number:");
scanf("%d",&x);
root=insert(root,x);
root=check(root,root);
break;
case 2:printf("insert number to delete:");
scanf("%d",&x);
root=delete(root,x);
root=del_check(root,root);
break;
case 3:dis_inorder(root);printf("\n");break;
default: printf("invalid choice\n");
}
}while(ch!=4);
return 0;
}

Ans 2
//hash table using open hashing
#include<stdio.h>
#include<malloc.h>
int n,x,i,s;
typedef struct node
{
int data;
struct node *next;
}LIST;
void insert(LIST *head[])
{
printf("insert the number:");
scanf("%d",&x);
LIST *p,*q;
p=(LIST*)malloc(sizeof(LIST));
p->data=x;
p->next=head[x%n];
head[x%n]=p;
}
void search(LIST *head[])
{
printf("enter the value to search:");
scanf("%d",&x);
if(head[x%n]==NULL)
printf("element not present");
else
{
LIST *p=head[x%n];
while(p->data!=x)
{
p=p->next;
if(p==NULL)
{
printf("Element not found");
return;
}
}
printf("element found");
}
}
void delete(LIST *head[],int n)
{
printf("enter number to delete:");
scanf("%d",&x);
if(head[x%n]==NULL)
printf("element not present");
else
{
LIST *p=head[x%n],*q=p;
while(p->data!=x)
{
q=p;
p=p->next;
if(p==NULL)
{
printf("Element not found");
return;
}
}

q->next=p->next;
}
}
void display(LIST *head[])
{
for(i=0;i<n;i++)
{ LIST *p=head[i];
if(p==NULL)
printf("No data");
else
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
printf("\n");
}
}
int main()
{

printf("enter the size of the hash table:");


scanf("%d",&n);
LIST *head[n];
for(i=0;i<n;i++)
{
head[i]=NULL;
}
int ch;
do
{
printf("1.search 2.delete 3.display 4.insert 5.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: search(head);
break;
case 2:
delete(head,n);
break;
case 3:display(head);
break;
case 4:insert(head);
break;
case 5:return 0;
default: printf("invalid choice\n");
}
}while(ch!=5);
return 0;
}

You might also like