0% found this document useful (0 votes)
53 views

Data Structure

This document contains code for several algorithms including knapsack, quicksort, minheap, matrix multiplication, AVL tree, 8 queen problem, and convex hull. The knapsack code implements 0-1 knapsack problem using dynamic programming. The quicksort code implements quicksort algorithm to sort an array of integers. The minheap code implements functions to insert, delete, and output the sorted elements of a min heap.

Uploaded by

Priya Vadhana
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Data Structure

This document contains code for several algorithms including knapsack, quicksort, minheap, matrix multiplication, AVL tree, 8 queen problem, and convex hull. The knapsack code implements 0-1 knapsack problem using dynamic programming. The quicksort code implements quicksort algorithm to sort an array of integers. The minheap code implements functions to insert, delete, and output the sorted elements of a min heap.

Uploaded by

Priya Vadhana
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 36

\*Knapsack *\

#include<iostream.h>
#include<conio.h>
int maxq=99999;
int c[20][1000];
int vpw[20]={0};
int max(int x,int y)
{
if(x>y)
return x;
else
return y;
}
inline void swap(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
}
void sort(int *a,int *v,int *w,int n)
{
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
if(a[i]<a[j])
{
swap(a[i],a[j]);
swap(v[i],v[j]);
swap(w[i],w[j]);
}
}
void sortvpw(int *vpw,int *v,int *w,int n)
{
int i;
for(i=0;i<=n;i++)
vpw[i]=v[i]/w[i];
sort(vpw,v,w,n);
}
int knapsack01(int *v,int *w,int n,int maxweight)
{
int i,j;
for(i=0;i<=n;i++)
c[i][0]=0;
for(j=0;j<=maxweight;j++)

c[0][j]=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if((j-w[i])<0)
continue;
c[i][j]=max(c[i-1][j-w[i]]+v[i],c[i-1][j]);
}
return c[n][maxweight];
}
float knapsack(int *v,int *w,int n,int maxweight)
{
sortvpw(vpw,v,w,n);
int weight=0;
float value=0;
int i=1;
cout<<"Value "<<"Weight "<<endl;
while(weight+w[i]<=maxweight)
{
value+=v[i];
weight+=w[i];
cout<<v[i]<<"
"<<w[i]<<endl;
i++;
}
if(weight!=maxweight)
value+=float((maxweight-weight)/w[i]*v[i]);
return value;
}
void main()
{
int v[]={0,100,180,225,200,50};
int w[]={maxq,50,30,45,25,5};
clrscr();
cout<<"Total value"<<knapsack(v,w,5,100);
getch();
}
OUTPUT:
Value Weight
50
5
200
25
180
30
Total value430

\* QUICK SORT *\

#include<iostream.h>
#include<conio.h>
class quicksort
{
public:
void quick(int *,int,int);
};
void quicksort::quick(int a[],int l,int u)
{
int i,j,p,temp;
if(l<u)
{
p=a[l];
i=l;
j=u;
while(i<j)
{
while(a[i]<=p && i<j)
i++;
while(a[j]>p && i<=j)
j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
temp=a[j];
a[j]=a[l];
a[l]=temp;
cout<<"\n";
quick(a,l,j-1);
quick(a,j+1,u);
}
}
}

void main()
{
int i;
int x[20],n;
quicksort q;
clrscr();
cout<<"Enter the number of Elements to be Sorted:";
cin>>n;
cout<<"Enter " <<n<<"elements:";
for(i=0;i<n;i++)
cin>>x[i];
q.quick(x,0,n-1);
cout<<"The Soreted list are\n";
for(i=0;i<n;i++)
cout<<x[i]<<"\t";
getch();
}
OUTPUT:
Enter the number of Elements to be Sorted:5
Enter 5 elements: 7
3
9
2
5
The sorted list are 2 3 5 7 9

\* MINHEAP *\
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class minheap
{
private:
int n,size;
int *minheap;
public:
void insert_minheap(int);
int delete_minheap();
void input();
void output();
};
void minheap::insert_minheap(int n)
{
if(size>=9)
{
cout<<"\nArray Overflow";
exit(0);
}
minheap[++size]=n;
int k=size;
while(k>1)
{
if(minheap[k]<minheap[k/2])
{
int t=minheap[k];
minheap[k]=minheap[k/2];
minheap[k/2]=t;
k=k/2;
}
else
break;
}
}
int minheap::delete_minheap()
{
if(size<1)
return -1;
int val;
val=minheap[1];

minheap[1]=minheap[size];
size--;
int k=1;
int newk;
while(2*k <= size)
{
if(2*k == size)
newk=2*k;
else
{
if(minheap[2*k]<minheap[2*k+1])
newk=2*k;
else
newk=2*k+1;
}
if(minheap[k]<minheap[newk])
break;
else
{
int t;
t=minheap[k];
minheap[k]=minheap[newk];
minheap[newk]=t;
k=newk;
}
}
return val;
}
void minheap::input()
{
cout<<"Enter number of elements in Minheap";
cin>>n;
minheap=new int[n+1];
size=0;
cout<<"Enter the elements";
int number;
for(int i=1;i<=n;i++)
{
cin>>number;
insert_minheap(number);
}
}

void minheap::output()
{
cout<<"the sorted numbers are";
for(int i=1;i<=n;i++)
cout<<delete_minheap()<<"\t";
cout<<endl;
}
void main()
{
clrscr();
minheap o;
o.input();
o.output();
getch();
}
OUTPUT:
Enter number of elements in Minheap 5
Enter the elements 12
8
6
9
7
the sorted numbers are6 7
8
9
12

\* STRASSEN MATRIX MULTIPLICATION *\


#include<iostream.h>
#include<conio.h>
class Strassen
{
public:
int a[20][20],b[20][20],c[20][20],m,n;
void multiply();
void getinput();
void display();
};
void Strassen::getinput()
{
int i,j;
cout<<"Enter the number of row and column value:";
cin>>n;
cin>>m;
cout<<"\nEnter the first matrix values:";
for(i=0;i<n;i++)
for(j=0;j<m;j++)
cin>>a[i][j];
cout<<"\nEnter the second matrix value:";
for(i=0;i<n;i++)
for(j=0;j<m;j++)
cin>>b[i][j];
}
void Strassen::multiply()
{
int p,q,r,s,t,u,v;
p=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
q=(a[1][0]+a[1][1])*b[0][0];
r=a[0][0]*(b[0][1]-b[1][1]);
s=a[1][1]*(b[1][0]-b[0][0]);
t=(a[0][0]+a[0][1])*b[1][1];
u=(a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
v=(a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=p+s-t+v;
c[0][1]=r+t;
c[1][0]=q+s;
c[1][1]=p+r-q+u;
}

void Strassen::display()
{
int i,j;
cout<<"The resultant matrix is\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<c[i][j]<<"\t";
cout<<"\n";
}
}
void main()
{
Strassen s;
s.getinput();
s.multiply();
s.display();
getch();
}
OUTPUT:
Enter the number of row and column value: 2 2
Enter the first matrix values: 2 2
22
Enter the second matrix value: 2 2
22
The resultant matrix is
8
8
8
8

\* AVL TREE *\
# include <iostream.h>
# include <conio.h>
# include <stdlib.h>
class avl_s
{
public:
struct node
{
int data;
int bf;
struct node *left;
struct node *right;
};
struct node *t,*root,*temp,*n;
int sx,sy;
avl_s()
{ t=NULL; root=NULL; temp=NULL; n=NULL;
sx=40; sy=5; }
~avl_s(){}
struct node * create(int k)
{
t=new node;
t->data=k;
t->left=NULL; t->right=NULL;
return t;
}
void insertavl(int);
void traverse(int);
void display(struct node *);
};
void avl_s::display(struct node *t1)
{
if(t1!=NULL)
{
gotoxy(sx,sy);
cout<<t1->data;
sx=sx-4; sy=sy+1;
display(t1->left);
sx=sx+4; sy=sy-1;
sx=sx+4; sy=sy+1;
display(t1->right);
sx=sx-4; sy=sy-1;
}

}
void avl_s::insertavl(int d)
{
int flag;
if(root==NULL)
{
root=create(d);
}
else
{
n=root;
traverse(d);
if(d < n->data)
n->left=create(d);
else
n->right=create(d);
}
}
void avl_s::traverse(int d1)
{
if(d1 < n->data)
{
if(n->left==NULL)
return;
else
n=n->left;
traverse(d1);
}
else
{
if(n->right==NULL)
return;
else
n=n->right;
traverse(d1);
}
}
void main()
{
int i,dat=0;
avl_s o;
clrscr();
while(dat!=-999)
{
gotoxy(1,2);

cprintf("Enter data");
gotoxy(15,2);
cin>>dat;
gotoxy(15,2);
clreol();
if(dat!=-999)
{
o.insertavl(dat);
}
o.t=o.root;
o.display(o.t);
}
cout<<"\n end of AVL"; getch();
}
OUTPUT:
Enter data 8 10 5 15 3 9 12 11 1 4 -999
8
5
3
1

10
9

4
11

End of AVL

15
12

17

\* 8 QUEEN PROBLEM *\
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class queen_8
{
private:
int mat[8][8];
int top_i,top_j;
int pos_i[8],pos_j[8];
int Qi,Qj;
public:
queen_8()
{
top_i=-1;
top_j=-1;
Qi=0;
Qj=0;
}
~queen_8()
{
}
void display(void);
void place_Q(void);
int Q_attack(int,int);
void setboard(void);
void push(void);
void pop(void);
};
int flag=1,count=0;
void queen_8::setboard()
{
int i,j;
for(i=0;i<=7;i++)
{
for(j=0;j<=7;j++)
mat[i][j]=0;
}
}
void queen_8::place_Q()
{
if(Qj>7||count<0)
{
cout<<"\n End of queue";
exit(0);
}

if(Qj==0&&Qi>7)
{
cout<<"\n End of Queue";
exit(0);
}
if(Qi>7)
{
cout<<"\n";
pop();
display();
cout<<"back tracking of queue poisition"<<Qi<<","<<Qj;
mat[Qi][Qj]=0;
Qi++;
place_Q();
}
flag=Q_attack(Qi,Qj);
if(flag==1)
{
count++;
Qi=0;
Qj++;
flag=0;
place_Q();
}
else
{
Qi++;
place_Q();
}
}
int queen_8::Q_attack(int i,int j)
{
int k,l;
if(mat[i][j]==1)
{
return 0;
}
else
{
k=i;
while(k<7)
{
k++;
if(mat[k][j]==1)
return 0;
}
k=i;
while(k>0)

{
k--;
if(mat[i][j]==1)
return 0;
}
k=j;
while(k<7)
{
k++;
if(mat[i][j]==1)
return 0;
}
k=j;
while(k>0)
{
k--;
if(mat[i][k]==1)
return 0;
}
k=i;
l=j;
while(k<7&&l<7)
{
k++;
l++;
if(mat[k][l]==1)
return 0;
}
k=i;
l=j;
while(k<7&&l<7)
{
k++;
l++;
if(mat[k][l]==1)
return 0;
}
while(k>0&&l<7)
{
k--;
l++;
if(mat[k][l]==1)
return 0;
}

k=i;l=j;
while(k>0&&l>0)
{
k--;
l--;
if(mat[k][l]==1)
return 0;
}
k=i;
l=j;
while(k<7&&l>0)
{
k++;
l--;
if(mat[k][l]==1)
return 0;
}
push();
mat[Qi][Qj]=1;
cout<<"\n Queen placed at position"<<Qi<<","<<Qj;
display();
cout<<"\n";
for(int x=0;x<=7;x++)
{
for(int y=0;y<=7;y++)
{
cout<<mat[x][y]<<" ";
}
cout<<"\n";
}
getch();
}
return 1;
}
void queen_8::push()
{
top_i++;
top_j++;
pos_i[top_i]=Qi;
pos_j[top_j]=Qj;
}
void queen_8::pop()
{
if(!(top_i==-1)||(top_j==-1))
{
Qi=pos_i[top_i];
Qj=pos_j[top_j];
top_i--;

top_j--;
}
}
void queen_8::display()
{
if((top_i==-1)||(top_j==-1))
{
cout<<"\n stacks are empty";
}
else
{
cout<<"\n";
for(int z=0;z<=top_i;z++)
cout<<pos_i[z]<<" ";
cout<<"..end\n";
for(z=0;z<=top_j;z++)
cout<<pos_j[z]<<" ";
cout<<"..end\n";
}
}
void main()
{
queen_8 obj;
clrscr();
obj.setboard();
obj.place_Q();
getch();
}

OUTPUT:
0 1 2 3 4 5 6 ..end
10000000
00000010
00001000
00000000
01000000
00010000
00000100
00100000
Queen placed at position3,7
0 4 7 5 2 6 1 3 ..end
0 1 2 3 4 5 6 7 ..end
10000000
00000010
00001000
00000001
01000000
00010000
00000100
00100000
End of queue

\* CONVEX HULL *\
#include<iostream.h>
#include<conio.h>
#include<math.h>
int i,j,tx,ty,t;
class convex
{
private:
int x[10],y[10],pos[10];
float ang[10];
public:
void input(void);
void sort(void);
void angle(void);
void con_hull(void);
convex()
{}
~convex()
{}
};
void convex::input()
{
int i;
cout<<"\n enter the 10 co-ordinates"<<endl;
for(i=0;i<=9;i++)
{
cout<<"\n x"<<i+1<<"y"<<i+1<<" ";
cin>>x[i];
cin>>y[i];
}
}
void convex::angle()
{
for(i=0;i<=9;i++)
{
for(j=i+1;j<=9;j++)
{
if(y[i]>y[j])
{
ty=y[i];
tx=x[i];
y[i]=y[j];
x[i]=x[j];
y[j]=ty;
x[j]=tx;

}
}
}
cout<<"\n the point p:"<<x[0]<<","<<y[0];
for(j=1;j<=9;j++)
{
ang[j]=acos((x[j]-x[0])/sqrt(pow(y[j]-y[0],2)+pow(x[j]-x[0],2)))*(180/3.14159);
}
}
void convex::sort()
{
for(i=1;i<=9;i++)
{
for(j=i+1;j<=9;j++)
{
if(ang[i]>ang[j])
{
t=ang[i];
ty=y[i];
tx=x[i];
ang[i]=ang[j];
y[i]=y[j];
x[i]=x[j];
ang[j]=t;
y[j]=ty;
x[j]=tx;
}
}
}
cout<<"\n ordered list of co-ordinates based on point p";
for(i=1;i<=9;i++)
cout<<"\n("<<x[i]<<","<<y[i]<<")angle"<<ang[i];
getch();
}
void convex::con_hull()
{
int cp;
i=0;j=1;t=0;
while(j<=9)
{
if(j==9)
{
cp=((x[j]-x[j])*(y[0]-y[i]))-((x[0]-x[i])*(y[j]-y[i]));
}
else
{
cp=((x[j]-x[i])*(y[j+1]-y[i]))-((x[j+1]-x[i])*(y[j]-y[i]));
}

if(cp>0)
{
if(j==9)
{
pos[t]=i;
t++;
pos[t]=j;
t++;
i=j;
j++;
}
else
{
pos[t]=i;
t++;
i=j;
j++;
}
}
if(cp<0)
{
j++;
}
}
cout<<"\n the points in convex hull:";
for(int z=0;z<t;z++)
cout<<"\n("<<x[pos[z]]<<","<<y[pos[z]]<<")";
getch();
}
void main()
{
convex o;
clrscr();
o.input();
o.angle();
o.sort();
o.con_hull();
}

OUTPUT:
enter the 10 co-ordinates
x1y1 1 1
x2y2 2 2
x3y3 3 3
x4y4 4 4
x5y5 5 5
x6y6 6 6
x7y7 7 7
x8y8 8 8
x9y9 9 9
x10y10 10 10
the point p:1,1
ordered list of co-ordinates based on point p
(2,2)angle45.000038
(3,3)angle45.000038
(4,4)angle45.000038
(5,5)angle45.000038
(6,6)angle45.000038
(7,7)angle45.000038
(8,8)angle45.000038
(9,9)angle45.000038
(10,10)angle45.000038

\* GRAPH COLORING *\
#include<iostream.h>
#include<conio.h>
int G[50][50],num_edges,num_nodes;
int diff_color(int node,int color,int colored[])
{
for(int v=0;v<node;v++)
if(G[v][node] && colored[v]==color)
return 0;
return 1;
}
int backtracking(int colored[],int node)
{
for(int color=0;color<2;color++)
if(diff_color(node,color,colored))
{
colored[node]=color;
if(node==num_nodes-1)
return 1;
else
return(backtracking(colored,node+1));
}
return 0;
}
void main()
{
int u,v;
int colored[50];
clrscr();
cout<<"Enter the number of nodes and number of edges\n";
cin>>num_nodes>>num_edges;
cout<<"Existing edges in grAPH";
for(int i=0;i<50;i++)
for(int j=0;j<50;j++)
G[i][j]=0;
while(num_edges>0)
{
num_edges--;
cin>>u>>v;
G[u][v]=1;
G[v][u]=1;
}

if(backtracking(colored,0))
cout<<"The graph can bicolored";
else
cout<<"the graph cannot be bicolored";
getch();
}
OUTPUT:
Enter the number of nodes and number of edges
4
4
Existing edges 0 1
12
23
30
The graph can bicolored
Enter the number of nodes and number of edges
4
5
Existing edges 0 1
12
23
30
02
the graph cannot be bicolored

\* N QUEEN *\
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
int k,x[20],c,n;
void printing();
int place(int,int);
void nqueen(int k,int n) // NQUEEN()FN., FOR THE PLACEMENT OF ALL N QUEENS
{
int i;
for(i=1;i<=n;i++)
{
// TO CHECK THE {K,I}th POSITION IS FREE OR NOT ,IIF RETURN IS 1
,CONDITION //SATISFIEDELSE IF RETURN IS 0 . CONDITION FAILS
if(place(k,i))
{
x[k]=i;
if(k==n) // TO CHECK IF ALL THE QUEENS ARE PLACED
{
c++;
printing();
}
else
nqueen(k+1,n);
}
}
}
int place(int k,int i)
{
for(int j=1;j<=k-1;j++)
if (x[j]==i || (abs(x[j]-i)==abs(j-k))) // TO CHECK FOR ALL THE MAJOR
CONSTRAINTS
return 0;
return 1;
}
void printing()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(x[i]==j) // WHEN THERE IS NO CLASH FOR POSITION , QUEENS ARE PLACED
cout<<" Q " ;
else
cout<<" # ";
}
cout<<"\n";
}

cout<<"\n\n";
}
void main()
{
cout<<"\n\t\t N- QUEEN'S PROBLEM USING BACKTRACKING METHOD \n ";
cout<<"\n\n ENTER THE NUMBER OF QUEEN'S : ";
cin>>n;
nqueen(1,n);
cout<<"\n\n TOTAL NUMBER OF COMBINATIONS : "<<c;
getch();
}
OUTPUT:
ENTER THE NUMBER OF QUEEN'S : 8
Q
#
#
#
#
#

# # # # # # #
# # # # Q # #
Q # # # # # #
# # # Q # # #
# # # # # Q #
# # Q # # # #

# # # # # # # Q
# # # Q # # # #
Q # # # # # # #
# # Q # # # # #
# # # # # Q # #
# Q # # # # # #
# # # # # # Q #
# # # # Q # # #

TOTAL NUMBER OF COMBINATIONS : 92


N- QUEEN'S PROBLEM USING BACKTRACKING METHOD

\* AVL *\
# include <iostream.h>
# include <conio.h>
# include <stdlib.h>
# include <stdio.h>
class avl_s
{
public:
struct node
{
int data;
int bf;
int nh;
struct node *left;
struct node *right;
};
struct node *t,*root,*temp,*n;
int sx,sy,lh,rh,h;
avl_s()
{ t=NULL; root=NULL; temp=NULL; n=NULL;
sx=40; sy=25; lh=0; rh=0; h=0;}
~avl_s(){}
struct node * create(int k)
{
// t=(struct node *)malloc(sizeof(struct node));
t=new node;
t->data=k;
t->left=NULL; t->right=NULL;
return t;
}
void insertavl(int);
void traverse(int);
void display(struct node *);
void s_level(struct node *,int);
int s_bf(struct node *);
};
int avl_s::s_bf(struct node *f)
{
int lh=0,rh=0;
if( f->left==NULL && f->right==NULL)
{
f->bf=0;

return f->nh;
}
if(f->left!=NULL)
{
lh=s_bf(f->left);
}
if(f->right!=NULL)
{
rh=s_bf(f->right);
}
if(lh==0)
{ f->bf=f->nh - rh; }
else if(rh==0)
{ f->bf=lh - f->nh; }
else
{ f->bf=lh-rh; }
if(lh>rh)
return lh;
else
return rh;
}
void avl_s::s_level(struct node *r,int h)
{
if(r->left!=NULL)
{
r->nh=h+1;
s_level(r->left,r->nh);
}
if(r->right!=NULL)
{
r->nh=h+1;
s_level(r->right,r->nh);
}
if( r->left==NULL && r->right==NULL)
{
r->bf=0;
r->nh=h+1;
}
}
void avl_s::display(struct node *t1)
{
if(t1!=NULL)

{
gotoxy(sx,sy);
cout<<t1->data<<"_"<<t1->bf;
sx=sx-4; sy=sy+2;
display(t1->left);
sx=sx+4; sy=sy-2;
sx=sx+4; sy=sy+2;
display(t1->right);
sx=sx-4; sy=sy-2;
}
}
void avl_s::insertavl(int d)
{
int flag;
if(root==NULL)
{
root=create(d);
}
else
{
n=root;
traverse(d);
if(d < n->data)
n->left=create(d);
else
n->right=create(d);
}
}
void avl_s::traverse(int d1)
{
if(d1 < n->data)
{
if(n->left==NULL)
return;
else
n=n->left;
traverse(d1);
}
else
{
if(n->right==NULL)
return;
else

n=n->right;
traverse(d1);
}
}
void main()
{
int i,dat=0;
avl_s o;
clrscr();
while(dat!=-999)
{
gotoxy(1,2);
cprintf("Enter data");
gotoxy(15,2);
cin>>dat;
gotoxy(15,2);
clreol();
if(dat!=-999)
{
o.insertavl(dat);
}
o.t=o.root;
o.t->nh=0;
o.s_level(o.t,o.t->nh);
i=o.s_bf(o.t);
gotoxy(100,100); cout<<"\n"<<i;
o.display(o.t);
}
cout<<"\n end of AVL"; getch();
}

\* AVL DOUBLE ROTATION*\


# include <iostream.h>
# include <conio.h>
# include <stdlib.h>
# include <stdio.h>
class avl_s
{
public:
struct node
{
int data;
int bf;
int nh;
struct node *left;
struct node *right;
};
struct node *t,*root,*temp,*n;
int sx,sy,lh,rh,h,rot;
avl_s()
{ t=NULL; root=NULL; temp=NULL; n=NULL;
sx=40; sy=25; lh=0; rh=0; h=0; rot=0;}
~avl_s(){}
struct node * create(int k)
{
// t=(struct node *)malloc(sizeof(struct node));
t=new node;
t->data=k;
t->left=NULL; t->right=NULL;
return t;
}
void insertavl(int);
void traverse(int);
void display(struct node *);
void s_level(struct node *,int);
int s_bf(struct node *);
void d_rot(struct node *);
void l_high(struct node *,struct node *);
void r_high(struct node *,struct node *);
void child(struct node *);
};
void avl_s::child(struct node *c)
{

if(c->bf >0)
{
l_high(c->left,c);
}
if(c->bf<0)
{
r_high(c->right,c);
}
}
void avl_s::l_high(struct node *t2, struct node *y)
{
struct node *t3;
if(t2->right==NULL)
{ t2->right=y; y->left=NULL; }
else
{ t3=t2->right;
t2->right=y;
y->left=t3;
}
temp=t2; rot=1;
}
void avl_s::r_high(struct node *t2, struct node *y)
{
struct node *t3;
if(t2->left==NULL)
{ t2->left=y; y->right=NULL; }
else
{ t3=t2->left;
t2->left=y;
y->right=t3;
}
temp=t2; rot=1;
}
void avl_s::d_rot(struct node *x)
{
if(x->left!=NULL)
{ d_rot(x->left);
if(rot==1)
{
x->left=temp;
rot=0;
s_level(root,0); s_bf(root);
return;
}

if( x->bf > 1 && x->data==root->data)


{
child(x->left);
x->left=temp;
l_high(x->left,x);
root=temp; rot=0;
return;
}
else if( x->bf > 1)
{
child(x->left);
x->left=temp;
l_high(x->left,x);
}
}
if(x->right!=NULL)
{ d_rot(x->right);
if(rot==1)
{
x->right=temp;
rot=0;
s_level(root,0); s_bf(root);
return;
}
if( x->bf< -1 && x->data==root->data)
{
child(x->right);
x->right=temp;
r_high(x->right,x);
root=temp; rot=0;
return;
}
else if( x->bf < -1)
{
child(x->right);
x->right=temp;
r_high(x->right,x);
}
}
if(x->left==NULL && x->right==NULL)
{
return;
}
}

int avl_s::s_bf(struct node *f)


{
int lh=0,rh=0;
if( f->left==NULL && f->right==NULL)
{
f->bf=0;
return f->nh;
}
if(f->left!=NULL)
{
lh=s_bf(f->left);
}
if(f->right!=NULL)
{
rh=s_bf(f->right);
}
if(lh==0)
{ f->bf=f->nh - rh; }
else if(rh==0)
{ f->bf=lh - f->nh; }
else
{ f->bf=lh-rh; }
// cout<<"\n"<<"L="<<lh<<" R="<<rh;
if(lh>rh)
return lh;
else
return rh;
}
void avl_s::s_level(struct node *r,int h)
{
if(r->left!=NULL)
{
r->nh=h+1;
s_level(r->left,r->nh);
}
if(r->right!=NULL)
{
r->nh=h+1;
s_level(r->right,r->nh);
}
if( r->left==NULL && r->right==NULL)
{
r->bf=0;

r->nh=h+1;
}
}
void avl_s::display(struct node *t1)
{
if(t1!=NULL)
{
gotoxy(sx,sy);
//cout<<t1->data<<"["<<t1->nh<<"]"<<" "<<t1->bf;
cout<<t1->data<<"_"<<t1->bf;
sx=sx-4; sy=sy+2;
display(t1->left);
sx=sx+4; sy=sy-2;
sx=sx+4; sy=sy+2;
display(t1->right);
sx=sx-4; sy=sy-2;
}
}
void avl_s::insertavl(int d)
{
int flag;
if(root==NULL)
{
root=create(d);
}
else
{
n=root;
traverse(d);
if(d < n->data)
n->left=create(d);
else
n->right=create(d);
}
}
void avl_s::traverse(int d1)
{
if(d1 < n->data)
{
if(n->left==NULL)
return;
else
n=n->left;

traverse(d1);
}
else
{
if(n->right==NULL)
return;
else
n=n->right;
traverse(d1);
}
}
void main()
{
int i,dat=0;
avl_s o;
clrscr();
while(dat!=-999)
{
gotoxy(1,2);
cprintf("Enter data");
gotoxy(15,2);
fflush(stdin);
cin>>dat;
gotoxy(15,2);
clreol();
if(dat!=-999)
{
o.insertavl(dat);
o.s_level(o.root,0);
cout<<"\n"<<o.s_bf(o.root);
o.d_rot(o.root);
o.s_level(o.root,0);
cout<<"\n"<<o.s_bf(o.root);
o.display(o.root);
}
}
gotoxy(2,35);
cout<<"\n end of AVL"; getch();
}

You might also like