Sunteți pe pagina 1din 54

ADVANCED DATA STRUCTURES

LAB MANUAL

R16 REGULATION

Department of CSE

NALANDA INSTITUTE OF ENGINEERING AND TECHNOLOGY


Kantepudi, Sattenapalli, GUNTUR-522438.
(Affiliated to JNTU KAKINADA, Kakinada)

1
List of Experiments:

1. To perform various operations i.e., insertions and deletions on AVL trees.

2. To implement operations on binary heap.

3. To implement operations on Graph


i) Vertex insertion
ii) Vertex deletion
iii) Finding vertex
iv) Edge addition and deletion

4. To implement Prim’s algorithm to generate a min-cost spanning tree.

5. To implement Krushkal’s algorithm to generate a min-cost spanning tree.

6. To implement Dijkstra’s algorithm to find shortest path in the graph.

7. To implementation of Static Hashing (Use Linear probing for collision resolution)

8. To implement of Huffmann coding.

9. To implement of B-tree.

2
1. To perform various operations i.e., insertions and deletions on AVL trees.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class avl
{
private:
struct node
{
int data;
node *left,*right;
int ht;
};
public:

node *avlroot;
node *insert1(node *,int);
node *delete1(node *,int);
void preorder1(node *);
void inorder1(node *);

node *rotateright(node *);


node *rotateleft(node *);
node *rr(node *);
node *ll(node *);
node *rl(node *);
node *lr(node *);

int height(node*);
int bf(node *);

avl()
{
avlroot=NULL;
}

void insert(int x)
{
avlroot=insert1(avlroot,x);

1|Page
}

void delet(int x)
{
avlroot=delete1(avlroot,x);
}

void preorder()
{
preorder1(avlroot);
}

void inorder()
{
inorder1(avlroot);
}

void levelwise();

void makenull()
{

avlroot=NULL;
}

};
node *avl::insert1(node *t,int x)
{
if(t==NULL)
{
t=new node;

t->data=x;
t->left=NULL;
t->right=NULL;
}
else
if(x>t->data)
{
t->right=insert1(t->right,x);

2|Page
if(bf(t)==-2)
if(x>t->right->data)
t=rr(t);
else
t=rl(t);
}
else
if(x<t->data)
{
t->left=insert1(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 *avl::delete1(node *t,int x)


{
node *p;
if(t==NULL)
{
return NULL;
}
else
if(x>t->data)
{
t->right=delete1(t->right,x);
if(bf(t)==2)
if(bf(t->left)>=0)
t=ll(t);
else
t=lr(t);
}
else
if(x<t->data)
{

3|Page
t->left=delete1(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=delete1(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 avl::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)

4|Page
rh=0;
else
rh=1+t->right->ht;
if(lh>rh)
return(lh);

return(rh);
}
node *avl::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 *avl::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 *avl::rr(node *t)


{
t=rotateleft(t);
return(t);
}
node *avl::ll(node *t)
{
t=rotateright(t);
return(t);
}

5|Page
node *avl::lr(node *t)
{
t->left=rotateleft(t->left);
t=rotateright(t);
return(t);
}

node *avl::rl(node *t)


{
t->right=rotateright(t->right);
t=rotateleft(t);
return(t);
}

int avl::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 avl::preorder1(node *t)


{
if(t!=NULL)
{
cout<<" "<<t->data<<"(bf="<<bf(t)<<")";
preorder1(t->left);
preorder1(t->right);
}
}

6|Page
void avl::inorder1(node *t)
{
if(t!=NULL)
{
inorder1(t->left);
cout<<" "<<t->data<<"(bf="<<bf(t)<<")";
inorder1(t->right);
}
}

void main()
{
avl a;
int x,n,i,ch;
char choice;
clrscr();
do
{
cout<<"\n1.create\n2.print\n3.insert\n4.delete\n5.quit\n";
cout<<"\n Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n Enter the no.of elements:";
cin>>n;
cout<<"\n Enter tree data:";
a.makenull();
for(i=0;i<n;i++)
{
cin>>x;
a.insert(x);
}
break;

case 2:
cout<<"\npreorder sequence:\n";
a.preorder();
cout<<"\nInorder sequence:\n";

7|Page
a.inorder();
break;
case 3:
cout<<"\nEnter the data:";
cin>>x;
a.insert(x);
break;

case 4:
cout<<"\n Enter the data:";
cin>>x;
a.delet(x);
break;
case 5:
exit(0);
break;
}
cout<<"do u want to continue y/n:";
cin>>choice;
}while(choice=='y'||'Y');

8|Page
Output:

9|Page
10 | P a g e
2. To implement operations on binary heap.

#include<iostream.h>
#define SIZE 10

class BinaryHeap
{
int *data;
int heapSize;
int arrayMaxSize;
public:

BinaryHeap(int size,int *array)


{
data = array;
arrayMaxSize=size;
heapSize =size;
}

virtual ~BinaryHeap()
{
delete[] data;
}
int getLeftChild(int index);
int getRightChild(int index);
int getParent(int index);

void buildHeap();
void heapify(int *arr,int index);
void printHeap();

};

int BinaryHeap::getLeftChild(int index)


{
return (2*index+1);

11 | P a g e
int BinaryHeap::getRightChild(int index)
{
return (2*index+2);
}

int BinaryHeap::getParent(int index)


{
return int((index-1)/2);
}

void BinaryHeap::buildHeap()
{
if(heapSize<=0)
{
return;
}
for(int i=(heapSize-2)/2;i>=0;i--)
{
cout<<" :"<<i;
heapify(data,i);
}
}

void BinaryHeap::heapify(int *arr,int index)


{
if(index>heapSize || index> arrayMaxSize)
{
cout<<"The index you have provide is incorrect"<<endl;
return;
}
int maxindex = index;
int left= getLeftChild(index);
int right = getRightChild(index);
if(left<heapSize && arr[left]>arr[index])
{
maxindex = left;
}
if(right<heapSize && arr[right]>arr[maxindex])
{
maxindex=right;

12 | P a g e
}
if(index != maxindex)
{
int temp = arr[index];
arr[index] = arr[maxindex];
arr[maxindex] = temp;
heapify(arr,maxindex);
}
return;
}

void BinaryHeap::printHeap()
{
int i=0;
cout<<" "<<endl;
while(i<heapSize)
{
cout<<*(data+i)<<", ";
i++;
}
cout<<" "<<endl;
}

int main()
{
int *array = new int[SIZE];
int i =0;
cout<<" enter the data"<<endl;
while(i<SIZE)
{
cin>>array[i];
i++;
}
BinaryHeap* heap = new BinaryHeap(SIZE,array);
heap->printHeap();
heap->buildHeap();
heap->printHeap();
return 1;
}

13 | P a g e
Output:

14 | P a g e
3. To implement operations on Graph
i) Vertex insertion
ii) Vertex deletion
iii) Finding vertex
iv) Edge addition and deletion

#include<iostream.h>
#include<alloc.h>
#include<conio.h>
#include<stdlib.h>
typedef struct graph
{
int vertex,weight;
struct graph *next;
}GRAPH;

GRAPH *adj[100],*ptr;
int n,count=0,a,b,i;

void insertvt()
{
cout<<"Enter vertex to be insert:";
cin>>a;
cout<<" Enter Reference vertex:";
cin>>b;
if(a>n||a<1||b>n||b<1)
{
cout<<"Wrong Input";
return;
}
if(count!=0)
{
ptr=adj[b];
while(ptr->vertex==b)
ptr=ptr->next;
if(ptr==NULL)
{
cout<<"Vertex Not Found";

15 | P a g e
return;
}
}
ptr=(graph*)malloc(sizeof(graph));
ptr->vertex=b;
ptr->weight=1;
ptr->next=adj[a];
adj[a]=ptr;
if(count==0)
count+=2;
else
count++;
cout<<"Vertex"<<a<<"Is inserted and Edge is established b/w "<<a<<"To"<<b;
}

void insertedg()
{
int wt;
cout<<"Enter First vertex:";
cin>>a;
cout<<"Enter Second Vertex:";
cin>>b;
if(a>n||a<1||b<1||b>n)
{
cout<<"Wrong Input";
return;
}
ptr=adj[a];
while(ptr!=NULL)
{
if(ptr->vertex==b)
{
cout<<"Vertex already Existed";
return;
}
ptr=ptr->next;
}
ptr=(graph*)malloc(sizeof(graph));
ptr->vertex=b;

16 | P a g e
ptr->weight=1;
ptr->next=adj[a];
adj[a]=ptr;
cout<<"Edge is inserted";
}

void deletevt()
{
if(count==0)
{
cout<<"Graph is empty";
return;
}
cout<<"Enter vertex to be delete:";
cin>>a;
if(a>n||a<1)
{
cout<<"Vertex Not Found";
return;
}
for(i=1;i<=n;i++)
{
ptr=adj[i];
while(ptr!=NULL)
{
if(ptr==adj[a])
adj[i]=NULL;
ptr=ptr->next;
}
}
count--;
cout<<"Vertex"<<a<<"Is deleted";
}

void deleteedg()
{
GRAPH *prev;
if(count==0)
{
cout<<"Graph is Empty";

17 | P a g e
return;
}
cout<<"Enter First vertex:";
cin>>a;
cout<<"Enter Second Vertex:";
cin>>b;
if(a>n||a<1||b<1||b>n)
{
cout<<"Wrong Input";
return;
}
ptr=adj[a];
while(ptr!=NULL && ptr->vertex!=b)
ptr=ptr->next;
if(ptr==NULL)
{
cout<<"Wrong Input";
return;
}
if(ptr==adj[a])
{
adj[a]=ptr->next;
free(ptr);
return;
}
prev=adj[a];
while(prev->next!=ptr)
prev=prev->next;
prev->next=prev->next;
free(ptr);
cout<<"Edge is deleted";
}

void display()
{
GRAPH *ptr;
int i;

18 | P a g e
cout<<"Edges inthe given graph\n";
for(i=1;i<=n;i++)
{
ptr=adj[i];
while(ptr!=NULL)
{
cout<<i<<"-->"<<ptr->vertex<<" "<<endl;
ptr=ptr->next;
}
}
}

void main()
{
int op,x,i;
clrscr();
cout<<"Enter maximum no of Nodes:";
cin>>n;
for(i=1;i<=n;i++)
adj[i]=NULL;
while(1)
{
cout<<"\n Graph Operations \n 1.Insert vertex \n 2.Insert Edge";
cout<<"\n 3.delete Vertex \n 4.Delete Edge\n 5.Display\n";
cout<<" 6.Exit \n Eneter Your Ooption:";
cin>>op;
switch(op)
{
case 1:insertvt(); break;
case 2:insertedg(); break;
case 3: deletevt(); break;
case 4: deleteedg(); break;
case 5: display(); break;
case 6: exit(0);
default: cout<<"Invalild Option";
}
}
}

19 | P a g e
Output:

20 | P a g e
4. To implement Prim’s algorithm to generate a min-cost spanning tree.

#include<iostream.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
cout<<"\n Implementations of Prime's Algorithm";
cout<<"\nEnter the number of nodes:";
cin>>n;
cout<<"\nEnter the adjacency matrix:\n";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
cout<<"\n";
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]< min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
cout<<"\n Edge: "<<ne++<<"(" <<a <<","<<b <<") cost: "<<min;
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"\n Minimun cost= "<<mincost;
getch();
}

21 | P a g e
OUTPUT:

22 | P a g e
5. To implement Krushkal’s algorithm to generate a min-cost spanning tree.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);

void main()
{
cout<<"\n\tImplementation of Kruskal's algorithm\n";
cout<<"\nEnter the no. of vertices:";
cin>>n;
cout<<"\nEnter the cost adjacency matrix:\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
}
}
cout<<"The edges of Minimum Cost Spanning Tree are\n";
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
cout<<ne++ <<"-edge ("<<a<<","<<b<<")="<< min<<endl;

23 | P a g e
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"\n\tMinimum cost = "<<mincost;
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}

OUTPUT:

24 | P a g e
6. To implement Dijkstra’s algorithm to find shortest path in the graph.

#include<iostream.h>
#include<conio.h>

#define INFINITY 9999


#define MAX 10
void dijikstra(int G[MAX][MAX], int n, int startnode);

void main()
{
int G[MAX][MAX], i, j, n, u;
clrscr();
cout<<"\nEnter the no. of vertices:: ";
cin>>n;
cout<<"\nEnter the adjacency matrix::\n";
for(i=0;i < n;i++)
for(j=0;j < n;j++)
cin>> G[i][j];
cout<<"\nEnter the starting node:: ";
cin>>u;
dijikstra(G,n,u);
getch();
}

void dijikstra(int G[MAX][MAX],int n, int startnode)


{
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(G[i][j]==0)

25 | P a g e
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i< n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count < n-1){
mindistance=INFINITY;
for(i=0;i < n;i++)
if(distance[i] < mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i < n;i++)
if(!visited[i])if(mindistance+cost[nextnode][i] < distance[i])

{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i < n;i++)

26 | P a g e
if(i!=startnode)
{
cout<<"\nDistance of "<<i<<" = "<<distance[i];
cout<<"\nPath = "<< i;
j=i;
do
{
j=pred[j];
cout<< "<-"<< j;
}
while(j!=startnode);
}
}
OUTPUT:-

27 | P a g e
7. To implementation of Static Hashing
(Use Linear probing for collision resolution)
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10

class Hash
{
private:
int a[MAX];
public:
Hash();
int create(int);
void linear_prob(int,int),display();
};

Hash::Hash()
{
int i;
for(i=0;i<MAX;i++)
a[i]=-1;
}

int Hash::create(int num)


{
int key;
key=num%10;
return key;
}

28 | P a g e
void Hash::linear_prob(int key,int num)
{
int flag,i,count=0;
flag=0;
if(a[key]==-1)//if the location indicated by hash key is empty
a[key]=num;//place the number in the hash table
else
{
i=0;
while(i<MAX)
{
if(a[i]!=-1)
count++;
i++;
}
if(count==MAX) //checking for the hash full
{
cout<<"\nHash Table Is Full Hence "<<num<<" Can not Be Inserted";
display();
getch();
exit(1);
}
for(i=key+1;i<MAX;i++)//moving linearly down
if(a[i]==-1) // searching for empty location
{
a[i]=num; //placing the number at empty location
flag=1;
break;
}

for(i=0;i<key&&flag==0;i++)//array from 0th to keyth loaction will be scanned

29 | P a g e
if(a[i]==-1)
{
a[i]=num;
flag=1;
break;
}
} //outer else
}//end

void Hash::display()
{
int i;
cout<<"\n The Hash Table is..."<<endl;
for(i=0;i<MAX;i++)
cout<<"\n "<<i<<" "<<a[i];
}

void main()
{
int num,key;
char ans;
Hash h;
clrscr();
cout<<"\nCollision Handling By Linear Probing";
do
{
cout<<"\n Enter The Number";
cin>>num;
key=h.create(num);//returns hash key
h.linear_prob(key,num);//collision handled by linear probing
cout<<"\n Do U Wish To Continue?(y/n)";

30 | P a g e
ans=getche();
}while(ans=='y');
h.display();//displays hash table
getch();
}

OUTPUT:-

31 | P a g e
8. To implement of Huffmann coding.

#include<iostream.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>

struct tree
{
char a[20];
int s;
struct tree *left,*right;
}*root=NULL,*tt[20]={NULL},*temp,*temp2,*t2,*ri,*le;

struct pqu
{
int info;
char a[20];
struct pqu *ptr;
}*front=NULL,*t,*par,*t1,*p1,*p2;

struct pqu* fp(int info)


{
struct pqu *p=NULL;
for(t1=front;t1->info<info&&t1!=NULL;t1=t1->ptr)
{
p=t1;
}
return (p);

32 | P a g e
void enqu(char a[20],int p)
{
t=(struct pqu*)malloc(sizeof(struct pqu));
strcpy(t->a,a);
t->info=p;
t->ptr=NULL;
if(front==NULL)
{
front=t;
}
else
{
par=fp(p);
if(par==NULL)
{
t->ptr=front;
front=t;
}
else
{
t->ptr=par->ptr;
par->ptr=t;
}
}
}
struct pqu* dequ()
{
t1=front;
front=front->ptr;
return t1;
}

33 | P a g e
void info(char c[2])
{
int m=0,i;
temp2=root;
while(strcmp(c,temp2->a)!=0)
{
t2=temp2->left;
for(i=0;i<strlen(t2->a);i++)
{
if(t2->a[i]==c[0])
{
temp2=temp2->left;
m=1;
cout<<"0";
break;
}
}
if(m!=1)
{
temp2=temp2->right;
cout<<1;
}
m=0;
}
}

void insert()
{
char a1[20],b1[20],v1[20];
int i,j,z=0,l;
while(front!=NULL)

34 | P a g e
{
p1=dequ();
strcpy(a1,p1->a);
l=p1->info;
p2=dequ();
if(p2==NULL)
break;
strcpy(b1,p2->a);
strcpy(v1,a1);
temp=(struct tree*)malloc(sizeof(struct tree));
strcpy(temp->a,strcat(v1,b1));
temp->s=l+p2->info;
temp->left=NULL;
temp->right=NULL;
temp2=temp;
root=temp;
for(i=0;i<z;)
{
if(strcmp(tt[i]->a,a1)==0)
{
temp->left=tt[i];
for(l=i;l<z;l++)
{
tt[l]=tt[l+1];
}
i=0;
continue;
}
else if(strcmp(tt[i]->a,b1)==0)
{
temp->right=tt[i];

35 | P a g e
for(l=i;l<z;l++)
{
tt[l]=tt[l+1];
}
i=0;
continue;
}
i++;
}
if(temp->left==NULL)
{
le=(struct tree*)malloc(sizeof(struct tree));
strcpy(le->a,a1);
le->left=NULL;
le->right=NULL;
temp2->left=le;
}
if(temp->right==NULL)
{
ri=(struct tree*)malloc(sizeof(struct tree));
strcpy(ri->a,b1);
ri->left=NULL;
ri->right=NULL;
temp2->right=ri;
}
if(front!=NULL)
enqu(temp2->a,temp2->s);
tt[z++]=temp2;
}
}

36 | P a g e
void disp(struct tree *rt)
{
if(rt!=NULL)
{
disp(rt->left);
cout<<" "<<rt->a;
disp(rt->right);
}
}
void main()
{
int i=0,g,h,p,y,n;
char m[20],b[20][2],re;
while(1)
{
clrscr();
cout<<"==============HUFFMAN CODING =======================";
cout<<"\n Enter the total no of characters : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the character : ";
cin>>m;
strcpy(b[i],m);
cout<<"Enter frequency for "<<m<<" : ";
cin>>g;
enqu(m,g);
}
insert();
disp(root);
clrscr();

37 | P a g e
cout<<"\n========= The corresponding codes are...==================\n";
for(i=0;i<n;i++)
{
cout<<b[i]<<" : ";
info(b[i]);
cout<<"\n";
}
cout<<"\n DO YOU WANT TO CONTINUE Y OR N: ";
cin>>re;
if(re=='y'||re=='Y')
continue;
else
exit(0);
}
}
OUTPUT:-

38 | P a g e
9. To implement of B-tree.

#include<iostream.h>
#include<conio.h>
#define max 5

class node;
struct pair
{
node *next;
int key;
};

class node
{
public:
int noofkeys;
pair data[max];
node *father;
node *first;
node();
int leafnode();
void insertinanode(pair x);
pair splitanode(pair x);
node *nextindex(int x);
void display();
};

void node::display()
{
cout<<"(";
for(int i=0;i<noofkeys;i++)

39 | P a g e
cout<<data[i].key<<" ";
cout<<")";
}

node *node::nextindex(int x)
{
if(x<data[0].key)
return(first);
for(int i=0;i<noofkeys;i++)
if(x<=data[i].key)
return data[i-1].next;
return data[i-1].next;
}

int node::leafnode()
{
if(data[0].next==NULL)
return 1;
return 0;
}

void node::insertinanode(pair x)
{
for(int i=noofkeys-1;i>=0&&data[i].key>x.key;i--)
data[i+1]=data[i];
data[i+1]=x;
noofkeys++;
}

40 | P a g e
pair node::splitanode(pair x)
{
node *t;
pair mypair;
int i,j,centre;
centre=(noofkeys-1)/2;
t=new node;
if(x.key>data[centre].key)
{
for(i=centre+1,j=0;i<=noofkeys;i++,j++)
t->data[j]=data[i];
t->noofkeys=noofkeys-centre-1;
noofkeys=noofkeys-t->noofkeys;
t->insertinanode(x);
t->first=t->data[0].next;
t->father=father;
mypair.key=t->data[0].key;
mypair.next=t;
for(i=1;i<t->noofkeys;i++)
t->data[i-1]=t->data[i];
t->noofkeys--;
}
else
{
for(i=centre,j=0;i<noofkeys;i++,j++)
t->data[j]=data[i];
t->noofkeys=noofkeys-centre;
noofkeys=noofkeys-t->noofkeys;
insertinanode(x);
t->father=father;
mypair.key=t->data[0].key;

41 | P a g e
mypair.next=t;
for(i=1;i<t->noofkeys;i++)
t->data[i-1]=t->data[i];
t->noofkeys--;
}
return(mypair);
}

node::node()
{
for(int i=0;i<=max;i++)
data[i].next=NULL;
noofkeys=0;
father=NULL;
first=NULL;
}

class q
{
node *data[60];
int r,f;
public:
q()
{
r=f=0;
}
int empty()
{
if(r==f)
return 1;
else

42 | P a g e
return 0;
}
node *deque()
{
return data[f++];
}
void enque(node *x)
{
data[r++]=x;
}
void makeempty()
{
r=f=0;
}
};

class btree
{
int mkeys;
node *root;
public:
btree(int n)
{
mkeys=n;
root=NULL;
}
void insert(int x);
void displaytree();
node *search(int x);
void delet(int x);
};

43 | P a g e
node *btree::search(int x)
{
node *p;
int i;
p=root;
while(p!=NULL)
{
for(i=0;i<p->noofkeys;i++)
if(x==p->data[i].key)
return(p);
p=p->nextindex(x);
}
return NULL;
}

void btree::displaytree()
{
q q1,q2;
node *p;
q1.enque(root);
while(!q1.empty())
{
q2.makeempty();
cout<<"\n";
while(!q1.empty())
{
p=q1.deque();
p->display();
cout<<" ";
if(!p->leafnode())

44 | P a g e
{
q2.enque(p->first);
for(int i=0;i<p->noofkeys;i++)
q2.enque(p->data[i].next);
}
}
q1=q2;
}
}

void btree::insert(int x)
{
int index;
pair mypair;
node *p,*q;
mypair.key=x;
mypair.next=NULL;
if(root==NULL)
{
root=new node;
root->insertinanode(mypair);
}
else
{
p=root;
while(!(p->leafnode()))
p=p->nextindex(x);
if(p->noofkeys<mkeys)
p->insertinanode(mypair);
else
{

45 | P a g e
mypair=p->splitanode(mypair);
while(1)
{
if(p==root)
{
q=new node;
q->data[0]=mypair;
q->first=root;
q->father=NULL;
q->noofkeys=1;
root=q;
q->first->father=q;
q->data[0].next->father=q;
return;
}
else
{
p=p->father;
if(p->noofkeys<mkeys)
{
p->insertinanode(mypair);
return;
}
else
mypair=p->splitanode(mypair);
}
}
}
}
}

46 | P a g e
void btree::delet(int x)
{
node *left,*right;
pair *centre;
node *p,*q;
int i,j,centreindex;
p=search(x);
for(i=0;p->data[i].key!=x;i++)
if(!p->leafnode())
{
q=p->data[i].next;
while(!q->leafnode())
q=q->first;
p->data[i].key=q->data[0].key;
p=q;
x=q->data[0].key;
i=0;
}
for(i=i+1;i<p->noofkeys;i++)
p->data[i-1]=p->data[i];
p->noofkeys--;
while(1)
{
if(p->noofkeys>=mkeys/2)
return;
if(p==root)
if(p->noofkeys>0)
return;
else
{
root=p->first;

47 | P a g e
return;
}
q=p->father;
if(q->first==p||q->data[0].next==p)
{
left=q->first;
right=q->data[0].next;
centre=&(q->data[0]);
centreindex=0;
}
else
{
for(i=1;i<q->noofkeys;i++)
if(q->data[i].next==p)
break;
left=q->data[i-1].next;
right=q->data[i].next;
centre=&(q->data[i]);
centreindex=i;
}
if(left->noofkeys>mkeys/2)
{
right->data[i+1]=right->data[i];
right->noofkeys++;
right->data[0].key=centre->key;
centre->key=left->data[left->noofkeys-1].key;
left->noofkeys--;
return;
}
else
if(right->noofkeys>mkeys/2)

48 | P a g e
{
left->data[left->noofkeys].key=centre->key;
left->noofkeys++;
centre->key=right->data[0].key;
for(i=1;i<right->noofkeys;i++)
right->data[i-1]=right->data[i];
right->noofkeys--;
return;
}
else
{
left->data[left->noofkeys].key=centre->key;
left->noofkeys++;
for(j=0;j<right->noofkeys;j++)
left->data[left->noofkeys+j]=right->data[j];
left->noofkeys+=right->noofkeys;
for(i=centreindex+1;i<q->noofkeys;i++)
q->data[i-1]=q->data[i];
q->noofkeys--;
p=q;
}
}
}

void main()
{
int i,n,x,op;
node *p;
clrscr();
cout<<"\nEnter number of keys in the node:";
cin>>n;

49 | P a g e
btree b(n);
do
{
cout<<"\n*****MENU*****";
cout<<"\n1.Insert \n2.Search \n3.Delete \n4.Display \n5.Quit";
cout<<"\nEnter your choice:";
cin>>op;
switch(op)
{
case 1:
cout<<"\nEnter a data:";
cin>>x;
b.insert(x);
cout<<"\nTree after insertion:";
b.displaytree();
break;

case 2:
cout<<"\nEnter a data:";
cin>>x;
p=b.search(x);
if(p!=NULL)
{
cout<<"Found in the node!";
p->display();
}
else
cout<<"\nElement not found!";
break;

case 3:

50 | P a g e
cout<<"\nEnter a data:";
cin>>x;
p=b.search(x);
if(p!=NULL)
{
b.delet(x);
b.displaytree();
}
else
cout<<"\nNot found!";
break;

case 4:
b.displaytree();
break;
}
}while(op!=5);
}

51 | P a g e
OUTPUT:-

52 | P a g e

S-ar putea să vă placă și