Escolar Documentos
Profissional Documentos
Cultura Documentos
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct list
{
int data;
list *link;
}*p;
void create(struct list*p)
{
int no,n,i;
struct list *a;
cout<<"\nENTER THE ELEMENT:";
cin>>n;
a->data=n;
a->link=NULL;
p->link=a;
}
void display(struct list*p)
{
struct list *d;
cout<<"\nTHE ELEMENTS ARE:";
d=p;
while(d->link!=NULL)
{
d=d->link;
cout<<"\t"<<d->data;
}
}
void inbeg(struct list*p,int a)
{
struct list *x;
x=new list;
x->data=a;
x->link=p->link;
p->link=x;
display(p);
}
void inend(struct list*p,int a)
{
struct list *x,*y;
x=new list;
x->data=a;
x->link=NULL;
y=p;
while(y->link!=NULL)
{
y=y->link;
}
y->link=x;
display(p);
}
void inpos(struct list*,int a,int b)
{
int count=1;
struct list *x,*y;
y=p;
while(count!=b)
{
y=y->link;
count++;
}
x=new list;
x->data=a;
x->link=y->link;
y->link=x;
display(p);
}
void delbeg(struct list*p)
{
if(p->link!=NULL)
{
p->link=p->link->link;
}
else
p=NULL;
display(p);
}
void delend(struct list*p)
{
struct list *x,*y;
y=p;
x=y->link;
while(x->link!=NULL)
{
x=x->link;
y=y->link;
}
y->link=NULL;
display(p);
}
void delpos(struct list*p,int a)
{
int count=1;
struct list *x;
x=p;
while(count!=a)
{
x=x->link;
count++;
}
x->link=x->link->link;
display(p);
}
void search(struct list*p,int a)
{
int count=0,f=0;
struct list *x;
x=p;
while(x->link!=NULL)
{
x=x->link;
count++;
if(x->data==a)
{
cout<<"\nELEMENT FOUND AT POSITION:"<<count;
f=1;
break;
}
}
if(f==0)
{
cout<<"\nELEMENT NOT FOUND";
}
}
void main()
{
int choice,n,m,ps,p1,no;
clrscr();
p=new list;
while(1)
{
cout<<"\n\n\tLINKED LIST MENU\n";
cout<<"\n 1.CREATE";
cout<<"\n 2.INSERT AT BEGINNING";
cout<<"\n 3.INSERT AT END";
cout<<"\n 4.INSERT AT DESIRED POSITION";
cout<<"\n 5.DELETE AT BEGINING";
cout<<"\n 6.DELETE AT END";
cout<<"\n 7.DELETE AT DESIRED POSITION";
cout<<"\n 8.SEARCH ";
cout<<"\n 9.DISPLAY THE LIST";
cout<<"\n 10.EXIT";
cout<<"\n\n ENTER THE CHOICE:";
cin>>choice;
switch(choice)
{
case 1 :create(p);
break;
case 2 :cout<<"\nENTER THE ELEMENT TO BE INSERTED AT THE
BEGINING:";
cin>>n;
inbeg(p,n);
break;
case 3 :cout<<"\nENTER THE ELEMENT AT END:";
cin>>m;
inend(p,m);
break;
case 4 : cout<<"\nENTER THE ELEMENT:";
cin>>m;
cout<<"\nPOSITION:";
cin>>ps;
inpos(p,m,ps);
break;
case 5 :delbeg(p);
break;
case 6 :delend(p);
break;
case 7 : cout<<"\nPOSITION:";
cin>>p1;
delpos(p,p1);
break;
case 8 : cout<<"\nENTER THE NO TO SEARCH:";
cin>>no;
search(p,no);
break;
case 9 :display(p);
break;
case 10 : exit(0);
}
}
getch();
}
OUTPUT:
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
POSITION:3
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
POSITION:2
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
POSITION:2
NO ELEMENT
LINKED LIST MENU
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
ENTER THE CHOICE:9
THE ELEMENTS ARE: 12
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
1.CREATE
2.INSERT AT BEGINNING
3.INSERT AT END
4.INSERT AT DESIRED POSITION
5.DELETE AT BEGINING
6.DELETE AT END
7.DELETE AT DESIRED POSITION
8.SEARCH
9.DISPLAY THE LIST
10.EXIT
RESULT:
The above program coding was executed and the result is obtained
successfully.
PROGRAM CODING:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *llink;
node *rlink;
};
node *p=NULL;
node *insert(node *p,int val)
{
if(p==NULL)
{
p=new node;
p->llink=p->rlink=NULL;
p->data=val;
}
else
{
if(p->data>val)
p->llink=insert(p->llink,val);
else if(p->data<val)
p->rlink=insert(p->rlink,val);
else if(p->data==val)
{
cout<<"\n DUPLICATE VALUE EXISTS ";
getch();
}
}
return(p);
}
/*void disp(node *p)
{
int i,n,cnt=0,cnt1=0;
node *q,*r;
for(i=0;i<n;i++)
{
cout<<"\t";
}
cout<<p->data;
if(cnt==cnt1)
{
q=p->llink;
cnt++;
cout<<"\n";
n-1;
disp(q);
}
r=p->rlink;
cnt1++;
n=n+2;
disp(r);
} */
void printtree(node *p)
{
static int cnt = 0,x=18,y=1,max=0;
if(max<=cnt)
{
max = cnt;
}
if(p!=NULL)
{
y=y+3;
cnt++;
printtree(p->llink);
gotoxy(x,y);
cout<<p->data;
x=x+3;
printtree(p->rlink);
cnt--;
y=y-3;
}
}
void preorder(node *p)
{
//cout<<"\n";
if(p!=NULL)
{
cout<<" "<<p->data;
// cout<<"\n";
preorder(p->llink);
// cout<<"\t";
preorder(p->rlink);
}
}
void search1(struct node *p,int v)
{
int cnt=0;
while(p!=NULL && cnt==0)
{
if(v==p->data)
{
cout<<"\n NODE FOUND";
cnt++;
}
else if(v<p->data)
p=p->llink;
else
p=p->rlink;
}
if(cnt==0)
cout<<"\n NODE NOT FOUND";
}
int leftsize(struct node *q)
{
int cnt=0;
while(q!=NULL)
{
q=q->llink;
cnt++;
}
return(cnt);
}
void search2(struct node *p,int r)
{
int cnt=0;
while(p!=NULL && cnt==0)
{
if(r==leftsize(p))
{
cnt++;
cout<<"\nNODE FOUND WITH RANK "<<r<<"\t is "<<p->data;
}
else if(r<leftsize(p))
p=p->llink;
else
{
r=r-leftsize(p);
p=p->rlink;
}
}
if(cnt==0)
cout<<"\n NODE NOT FOUND";
}
node *del(struct node *p,int v)
{
struct node *q;
q=p;
if(p==NULL)
{
cout<<"\nDELETION NOT POSSIBLE";
}
else if(v<p->data)
p->llink=del(p->llink,v);
else if(v>p->data)
p->rlink=del(p->rlink,v);
else if(p->llink&&p->rlink)
{
q=p->rlink;
p->data=q->data;
p->rlink=del(p->rlink,p->data);
}
else
{
if(p->llink==NULL)
p=p->rlink;
else if(p->rlink==NULL)
p=p->llink;
free(q);
}
return(p);
}
void search(struct node *p,int v)
{
int cnt=0;
if(p==NULL)
{
if(cnt==0)
cout<<"\n\t NODE NOT FOUND";
}
else if(v==p->data)
{
cout<<"\n\tNODE FOUND";
cnt++;
}
else if(v<p->data)
search(p->llink,v);
else
search(p->rlink,v);
}
void main()
{
int ch,v,a;
clrscr();
do
{
cout<<"\n\n\t\t BINARY TREE TRAVERSAL ";
cout<<"\n\n\t1.INSERT";
cout<<"\n\t2.DELETE";
cout<<"\n\t3.RECURSIVE SEARCH";
cout<<"\n\t4.ITERATIVE SEARCH";
cout<<"\n\t5.SEARCH BY RANK";
cout<<"\n\t6.DISPLAY";
cout<<"\n\t7.EXIT";
cout<<"\n\n ENTER CHOICE -> ";
cin>>ch;
switch(ch)
{
case 1:cout<<"\n\t TO INSERT";
cout<<"\n\n Enter an element:";
cin>>v;
p=insert(p,v);
preorder(p);
getch();
break;
case 2:cout<<"\n\t TO DELETE";
cout<<"\n\nEnter the node to be deleted:";
cin>>v;
p=del(p,v);
preorder(p);
getch();
break;
case 3:cout<<"\n\tRECURSIVE SEARCH ";
cout<<"\n\n Enter the node to be searched : ";
cin>>v;
search(p,v);
getch();
break;
case 4: cout<<"\n\t ITERATIVE SEARCH ";
cout<<"\n\n Enter the node to be searched: ";
cin>>v;
search1(p,v);
getch();
break;
case 5: cout<<"\n\t BY RANK ";
cout<<"\n\nEnter the rank of the node to be searched :";
cin>>a;
search2(p,a);
getch();
break;
case 6: clrscr();
cout<<"\n\n\t\t BINARY TREE "<<"\n";
printtree(p);
getch();
break;
case 7:exit(1);
default:cout<<"INVALID INPUT";
}
}while(ch!=7);
getch();
}
OUTPUT:
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
BINARY TREE
12
6 23
10
TO INSERT
Enter an element:2
12 6 2 10 23
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
Enter an element:15
12 6 2 10 23 15
BINARY TREE TRAVERSAL
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
BINARY TREE
12
6 23
2 10 15
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
BINARY TREE
23
6 15
2 10
BINARY TREE TRAVERSAL
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
1.INSERT
2.DELETE
3.RECURSIVE SEARCH
4.ITERATIVE SEARCH
5.SEARCH BY RANK
6.DISPLAY
7.EXIT
RESULT:
The above program coding was executed and the result is obtained
successfully.
PROGRAM CODING:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int a[10],b[10][10],n;
void insert()
{
int i,j;
cout<<"\nNO OF NODES IN GRAPH :";
cin>>n;
cout<<"\n ELMENTS :";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n\n\t IF PATH EXIST PRESS 1 OR PRESS 0";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\n\n\t LINK ("<<a[i]<<","<<a[j]<<"):"<<"\t";
cin>>b[i][j];
}
}
}
void dsearch()
{
int a1[10],b1[10][10],i,j,k,x,cnt=0,m[10],co=0,p=-1,y;
for(x=0;x<n;x++)
{
cnt=0;co=0;p=-1;
cout<<"\n\n DFS FOR NODE "<<a[x]<<" :";
for(i=0;i<n;i++)
{
a1[i]=a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b1[i][j]=b[i][j];
}
}
// x=0;
i=x;
cout<<"\t"<<a1[i];
cnt++;
a1[i]=0;
for(y=0;y<n;y++)
{
b1[y][i]=0;
}
while(cnt!=n)
{
for(j=0;j<n;j++)
{
if(b1[i][j]!=0)
{
cout<<" "<<a1[j];
cnt++;
a1[j]=0;
for(y=0;y<n;y++)
{
b1[y][j]=0;
}
p++;
m[p]=i;
i=j;
break;
}
}
loop:
co=0;
for(k=0;k<n;k++)
{
if(b1[i][k]!=0)
co++;
}
if(co==0)
{
i=m[p];
p--;
}
if(co!=0)
goto end;
goto loop;
end:
co=0;
}
}
}
void bsearch()
{
int a1[10],b1[10][10],i,j,k,x,z,cnt=0,cnt1,a2[10],y,l;
for(x=0;x<n;x++)
{
j=0,cnt=0,cnt1=0;
for(i=0;i<n;i++)
{
a2[i]=0;
}
cout<<"\n\n BFS FOR NODE "<<a[x]<<"-";
for(i=0;i<n;i++)
{
a1[i]=a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b1[i][j]=b[i][j];
}
}
k=x;
j=0;
while(j!=n)
{
if(cnt1==0)
{
a2[j]=a1[k];
j++;
for(y=0;y<n;y++)
{
b1[y][k]=0;
}
cnt1++;
}
for(l=0;l<n;l++)
{
if(b1[k][l]!=0)
{
a2[j]=a1[l];
j++;
for(y=0;y<n;y++)
{
b1[y][l]=0;
}
}
}
cnt++;
for(i=0;i<n;i++)
{
if(a2[cnt]==a1[i])
k=i;
}
}
cout<<"\t";
for(i=0;i<n;i++)
{
cout<<" "<<a2[i];
}
}
}
void main()
{
int ch;
do
{
clrscr();
cout<<"\n\n\t\t\t GRAPH TRAVERSAL ";
cout<<"\n\n\t\t\t 1 -> INSERT";
cout<<"\n\n\t\t\t 2 -> DFS(depth first traversal)";
cout<<"\n\n\t\t\t 3 -> BFS(breadth first traversal)";
cout<<"\n\n\t\t\t 4 ->exit";
cout<<"\n\nEnter choice:";
cin>>ch;
switch(ch)
{
case 1:insert();
getch();
break;
case 2:dsearch();
getch();
break;
case 3:bsearch();
getch();
break;
case 4:exit(1);
break;
}
}while(ch!=4);
getch();
}
OUTPUT:
GRAPH TRAVERSAL
1 -> INSERT
2 -> DFS(depth first traversal)
3 -> BFS(breadth first traversal)
4 ->exit
Enter choice:1
NO OF NODES IN GRAPH :8
ELMENTS :1 2 3 4 5 6 7 8
IF PATH EXIST PRESS 1 OR PRESS 0
LINK (1,1): 0
LINK (1,2): 1
LINK (1,3): 1
LINK (1,4): 0
LINK (1,5): 0
LINK (1,6): 0
LINK (1,7): 0
LINK (1,8): 0
LINK (2,1): 1
LINK (2,2): 0
LINK (2,3): 0
LINK (2,4): 1
LINK (2,5): 1
LINK (2,6): 0
LINK (2,7): 0
LINK (2,8): 0
LINK (3,1): 1
LINK (3,2): 0
LINK (3,3): 0
LINK (3,4): 0
LINK (3,5): 0
LINK (3,6): 1
LINK (3,7): 0
LINK (3,8): 0
LINK (4,1): 0
LINK (4,2): 1
LINK (4,3): 0
LINK (4,4): 0
LINK (4,5): 0
LINK (4,6): 0
LINK (4,7): 0
LINK (4,8): 1
LINK (5,1): 0
LINK (5,2): 1
LINK (5,3): 0
LINK (5,4): 0
LINK (5,5): 0
LINK (5,6): 0
LINK (5,7): 0
LINK (5,8): 1
LINK (6,1): 0
LINK (6,2): 0
LINK (6,3): 1
LINK (6,4): 0
LINK (6,5): 0
LINK (6,6): 0
LINK (6,7): 0
LINK (6,8): 1
LINK (7,1): 0
LINK (7,2): 0
LINK (7,3): 1
LINK (7,4): 0
LINK (7,5): 0
LINK (7,6): 0
LINK (7,7): 0
LINK (7,8): 1
LINK (8,1): 0
LINK (8,2): 0
LINK (8,3): 0
LINK (8,4): 1
LINK (8,5): 1
LINK (8,6): 1
LINK (8,7): 1
LINK (8,8): 0
GRAPH TRAVERSAL
1 -> INSERT
2 -> DFS(depth first traversal)
3 -> BFS(breadth first traversal)
4 ->exit
Enter choice:2
1 -> INSERT
2 -> DFS(depth first traversal)
3 -> BFS(breadth first traversal)
4 ->exit
Enter choice:3
RESULT:
The above program coding was executed and the result is obtained
successfully.
PROGRAM CODING;
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int element;
node *left;
node *right;
int height;
};
typedef struct node *nodeptr;
void disp(nodeptr &p);
void insert(int,nodeptr &);
void del(int, nodeptr &);
int delmin(nodeptr &);
int bsht(nodeptr);
nodeptr srl(nodeptr &);
nodeptr drl(nodeptr &);
nodeptr srr(nodeptr &);
nodeptr drr(nodeptr &);
int max(int,int);
void ins(int x,nodeptr &p)
{
if(p == NULL)
{
p = new node;
p->element = x;
p->left=NULL;
p->right = NULL;
p->height=0;
if (p==NULL)
cout<<"\n\nEMPTY TREE(insert nodes)";
}
else
{
if (x<p->element)
{
ins(x,p->left);
if ((bsht(p->left) - bsht(p->right))==2)
{
if (x < p->left->element)
p=srl(p);
else
{
p = drl(p);
cout<<"\nNODE INSERTED";
}
}
}
else if (x>p->element)
{
ins(x,p->right);
if ((bsht(p->right) - bsht(p->left))==2)
{
if (x > p->right->element)
p=srr(p);
else
p = drr(p);
cout<<"\nNODE INSERTED";
}
}
else
cout<<"NODE ALREADY EXIST ";
}
int m,n,d;
m=bsht(p->left);
n=bsht(p->right);
d=max(m,n);
p->height = d + 1;
}
void del(int x,nodeptr &p)
{
nodeptr d;
if (p==NULL)
cout<<"ELEMENT NOT FOUND";
else if ( x < p->element)
del(x,p->left);
else if (x > p->element)
del(x,p->right);
else if ((p->left == NULL) && (p->right == NULL))
{
d=p;
free(d);
p=NULL;
cout<<"NODE DELETED";
}
else if (p->left == NULL)
{
d=p;
free(d);
p=p->right;
cout<<"NODE DELETED";
}
else if (p->right == NULL)
{
d=p;
p=p->left;
free(d);
cout<<"NODE DELETED";
}
else
p->element = delmin(p->right);
}
int delmin(nodeptr &p)
{
int c;
if (p->left == NULL)
{
c=p->element;
p=p->right;
return c;
}
else
{
c=delmin(p->left);
return c;
}
}
int max(int value1, int value2)
{
return ((value1 > value2) ? value1 : value2);
}
int bsht(nodeptr p)
{
int t;
if (p == NULL)
return -1;
else
{
t = p->height;
return t;
}
}
void main()
{
clrscr();
nodeptr root,root1,min,max;
int a,delt;//lefte,righte,fl;
int ch;
root = NULL;
root1=NULL;
while(1)
{
cout<<"\n\n\t AVL TREE ";
cout<<"\n\n\t\t\t 1->TO INSERT \n \n\t\t\t 2 -> TO DELETE \n\n\t\t\t 3 ->TO
DISPLAY \n\n\t\t\t 4 -> TO EXIT";
cout<<"\nEnter ur choice:";
cin>>ch;
switch(ch)
{
case 1:cout<<"\n TO INSERT :";
cin>>a;
ins(a,root);
break;
case 2:cout<<"\n TO DELETE:";
cin>>delt;
del(delt,root);
break;
case 3:clrscr();
disp(root);
break;
case 4:exit(0);
}
}
getch();
}
OUTPUT:
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:1
TO INSERT :12
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:1
TO INSERT :7
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:1
TO INSERT :23
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:1
TO INSERT :8
TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:2
12
7 23
8
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:1
TO INSERT :22
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:1
TO INSERT :27
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:1
TO INSERT :1
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:2
12
7 23
1 8 22 27
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:2
TO DELETE: 12
22
7 23
1 8 27
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:2
TO DELETE :22
23
7 27
1 8
AVL TREE
1 -> TO INSERT
2 -> TO DELETE
3 ->TO DISPLAY
4 -> TO EXIT
Enter ur choice:4
RESULT:
The above program coding was executed and the result is obtained
successfully.
PROGRAM CODING;
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int a[50],n;
void insert(int v)
{
int r,i,cnt=0;
r=v%n;
if(a[r]==NULL)
a[r]=v;
else
{
for(i=r;i<n;i++)
{
if(a[i]==NULL)
{
a[i]=v;
cout<<a[i];
break;
}
}
}
for(i=0;i<n;i++)
{
if(a[i]==v)
cnt++;
}
if(cnt==0)
cout<<"\n\n INSERTION CANNOT TAKE PLACE";
}
void display()
{
int i;
cout<<"\n\n TABLE:";
for(i=0;i<n;i++)
{
if(a[i]!=NULL)
cout<<" "<<a[i];
else
cout<<" -";
}
}
void main()
{
int ch,ele,i;
clrscr();
cout<<"\n\t\t\t HASH TABLE ";
cout<<"\n\n\t ARRAY SIZE :";
cin>>n;
for(i=0;i<n;i++)
{
a[i]=NULL;
}
do
{
clrscr();
cout<<"\n\n\t\t\t 1 -> TO INSERT";
cout<<"\n\n\t\t\t 2 -> TO DISPLAY";
cout<<"\n\n\t\t\t 3 -> TO EXIT";
cout<<"\n\n\t ENTER CHOICE:";
cin>>ch;
switch(ch)
{
case 1:cout<<"\n TO INSERT \t";
cin>>ele;
insert(ele);
getch();
break;
case 2:display();
getch();
break;
case 3:exit(1);
}
}while(ch!=3);
getch();
}
OUTPUT:
HASH TABLE
ARRAY SIZE :5
1 -> TO INSERT
2 -> TO DISPLAY
3 -> TO EXIT
ENTER CHOICE:1
TO INSERT 12
1 -> TO INSERT
2 -> TO DISPLAY
3 -> TO EXIT
ENTER CHOICE:1
TO INSERT 34
1 -> TO INSERT
2 -> TO DISPLAY
3 -> TO EXIT
ENTER CHOICE:1
TO INSERT 17
1 -> TO INSERT
2 -> TO DISPLAY
3 -> TO EXIT
ENTER CHOICE:2
TABLE: - - 12 17 34
1 -> TO INSERT
2 -> TO DISPLAY
3 -> TO EXIT
ENTER CHOICE:1
TO INSERT 6
1 -> TO INSERT
2 -> TO DISPLAY
3 -> TO EXIT
ENTER CHOICE:2
TABLE: - 6 12 17 34
1 -> TO INSERT
2 -> TO DISPLAY
3 -> TO EXIT
ENTER CHOICE:1
TO INSERT 7
INSERTION CANNOT TAKE PLACE
1 -> TO INSERT
2 -> TO DISPLAY
3 -> TO EXIT
ENTER CHOICE:1
TO INSERT 45
1 -> TO INSERT
2 -> TO DISPLAY
3 -> TO EXIT
ENTER CHOICE:2
TABLE: 45 6 12 17 34
1 -> TO INSERT
2 -> TO DISPLAY
3 -> TO EXIT
ENTER CHOICE:1
TO INSERT 9
RESULT:
The above program coding was executed and the result is obtained
successfully.