7/27/2019 Record Print 1
1/29
7. IMPLEMENTATION OF AVL TREE INSERTION
AIM
Write a C program to implement insertion on AVL Tree using single rotation and
double rotation.
ALGORITHM
1. Initialize node structure containing a data element X and two pointers for left
subtree and right subtree.
2. If the choice is Insert,
a. Accept the data element X and subtree Ts address
b. If the subtree T is null, then create a new treenode T and set
X as Ts element and Ts left & Ts right as NULL.
c. Otherwise if X is less than the data element of treenode T, recursively insert
the element as Ts left.
i. Check if the difference between the height of left and right subtree is
equal to 2
a) If insertion is at left subtree do the single rotation with left.
b) Else do the double rotation with left.
d. Otherwise if X is greater than the data element of treenode T, recursivelyinsert the element as Ts left.
i. Check if the difference between the height of left and right subtree is
equal to 2
a) If insertion is at left subtree do the single rotation with right.
b) Else do the double rotation with right.
7/27/2019 Record Print 1
2/29
SOURCE CODE
#include#include
#include
struct node
{int data;
struct node *left;
struct node *right;int ht;
}*root=NULL;
int height(struct node *p){
if(p==NULL)
return -1;else
return p->ht;
}
struct node *singlerotatewithleft(struct node *k2){
struct node *k1;k1=k2->left;
k2->left=k1->right;k1->right=k2;
k2->ht=max(height(k2->left),height(k2->right))+1;k1->ht=max(height(k1->left),k2->ht)+1;
return k1;}
struct node *singlerotatewithright(struct node *k1){
struct node *k2;
k2=k1->right;k1->right=k2->left;
k2->left=k1;k1->ht=max(height(k1->right),height(k1->left))+1;
k2->ht=max(height(k2->right),k1->ht)+1;return k2;
}
struct node *doublerotatewithleft(struct node *k3){
k3->left=singlerotatewithright(k3->left);return singlerotatewithleft(k3);
}
7/27/2019 Record Print 1
3/29
struct node *doublerotatewithright(struct node *k1){
k1->right=singlerotatewithleft(k1->right);return singlerotatewithright(k1);
}
void inorder(struct node *t)
{if(t!=NULL)
{
inorder(t->left);printf("%d ",t->data);
inorder(t->right);}
}
struct node *insert(int x,struct node *t){
if(t==NULL){
t=(struct node *)malloc(sizeof(struct node));t->data=x;
t->left=NULL;t->right=NULL;
t->ht=0;}
else if(xdata){
t->left=insert(x,t->left);
if(height(t->left)-height(t->right)==2)if(xleft->data)
t=singlerotatewithleft(t);
elset=doublerotatewithleft(t);
}
else if(x>t->data){
t->right=insert(x,t->right);
if(height(t->right)-height(t->left)==2)if(x>t->right->data)
t=singlerotatewithright(t);else
t=doublerotatewithright(t);}
t->ht=max(height(t->left),height(t->right))+1;return t;
}
7/27/2019 Record Print 1
4/29
void main(){
int ch,x;struct node *t;
clrscr();do
{
printf("enter the data");scanf("%d",&x);
if(root==NULL){
root=insert(x,root);}
elseroot=insert(x,root);
printf("do u want to continue inserting node? 1/0?");scanf("%d",&ch);
}while(ch!=0);inorder(root);
getch();}
OUTPUT
enter the data3
do u want to continue inserting node? 1/0?1
enter the data2
do u want to continue inserting node? 1/0?1
enter the data1
do u want to continue inserting node? 1/0?1
enter the data4
do u want to continue inserting node? 1/0?1
enter the data 5
do u want to continue inserting node? 1/0?1
enter the data 6
do u want to continue inserting node? 1/0?1
enter the data 7
do u want to continue inserting node? 1/0?1
enter the data 16
do u want to continue inserting node? 1/0?1
enter the data 15
7/27/2019 Record Print 1
5/29
do u want to continue inserting node? 1/0?1
enter the data 14
do u want to continue inserting node? 1/0?1
enter the data 13
do u want to continue inserting node? 1/0?1
enter the data 12
do u want to continue inserting node? 1/0?1
enter the data 11
do u want to continue inserting node? 1/0?1
enter the data 10
do u want to continue inserting node? 1/0?1
enter the data 8
do u want to continue inserting node? 1/0?1
enter the data 9
do u want to continue inserting node? 1/0?0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
RESULT
The AVL Tree insertion is implemented in C using the single rotation, double
rotation operations and is executed and verified.
7/27/2019 Record Print 1
6/29
8. IMPLEMENTATION OF PRIORITY QUEUE USING HEAPS
AIM
Write a C program to implement priority queue using heaps.
ALGORITHM
Insertion
1. Add the element to the bottom level of the heap.
2. Compare the added element with its parent;
i. If they are in the correct order(if min-heap parent should be smaller than
children and in max-heap parent should be greater than children), stop.
ii. If not, swap the element with its parent and return to the previous step.
Deletion
1. Replace the root of the heap with the last element on the last level.
2. Compare the new root with its children;
i. If they are in the correct order, stop.
ii. If not, swap the element with one of its children and return to the
previous step. (Swap with its smaller child in a min-heap and its greater
child in a max-heap.)
Findmin
In a min-heap the minimum element will be at the root;display the root element.
SOURCE CODE
#include
#include #define MinData (-32767)
typedef int ElementType;typedef struct HeapStruct *PriorityQueue;
struct HeapStruct
{
int Capacity;int Size;ElementType *Elements;
};
7/27/2019 Record Print 1
7/29
PriorityQueue Initialize (int MaxElements){
PriorityQueue H;
H = malloc(sizeof ( struct HeapStruct));if (H == NULL)
printf("Out of space!!!");
else{
H->Elements = malloc((MaxElements + 1)*sizeof(ElementType));
if (H->Elements == NULL){
printf("Out of space!!!");}
else{
H->Capacity = MaxElements;H->Size = 0;
H->Elements[ 0 ] = MinData;}
return H;}
}
void MakeEmpty(PriorityQueue H)
{H->Size = 0;
}
void Insert(ElementType X, PriorityQueue H){
int i;
if (IsFull(H))
{printf("Priority queue is full");
return;
}
for (i = ++H->Size; H->Elements[ i / 2 ] > X; i /= 2)H->Elements[ i ] = H->Elements[ i / 2 ];
H->Elements[ i ] = X;}
int IsEmpty(PriorityQueue H)
{return H->Size == 0;
}
7/27/2019 Record Print 1
8/29
7/27/2019 Record Print 1
9/29
void display(PriorityQueue H){
int i;for(i=1;iSize;i++)
printf("%d ",H->Elements[i]);}
void main(){
PriorityQueue h;
int x,y,z,u,v;char ch;
clrscr();printf("\nEnter the maximum number of elements for the Priority Queue: ");
scanf("%d",&x);h=Initialize(x);
menu:printf("\nPriority Queue");
printf("\n1.Insert\n2.Delete\n3.Display\n4.FindMin\n5.Exit");while(1)
{printf("enter the choice ");
scanf("%d",&u);switch(u)
{case 1:printf("Enter the Data: ");
scanf("%d",&z);Insert(z,h);
break;
case 2:v=DeleteMin(h);printf("\nThe deleted element is: %d",v);break;
case 3:display(h);break;
case 4:z=FindMin(h);
printf(The minimum element is %d ,z);break;
case 5:
exit(0);}
}}
7/27/2019 Record Print 1
10/29
OUTPUT
Enter the maximum number of elements for the Priority Queue:8
Priority Queue
1.Insert
2.Delete
3.Display
4.FindMin
4.Exit
enter the choice 1
Enter the Data: 50
enter the choice 1
Enter the Data: 40
enter the choice 1
Enter the Data: 67
enter the choice 1
Enter the Data: 23
enter the choice 1
Enter the Data: 45
enter the choice 1
Enter the Data: 33
enter the choice 1
Enter the Data: 24
enter the choice 1
Enter the Data: 18
enter the choice 3
18 23 24 40 45 67 33 50
enter the choice 1
Enter the Data: 19
Priority queue is full
enter the choice 2
The deleted element is: 18
enter the choice 3
7/27/2019 Record Print 1
11/29
23 40 24 50 45 67 33
enter the choice 4
The minimum element is 23
enter the choice 5
RESULT
Thus a C program is written to implement priority queues using heaps ad executed
and verified.
7/27/2019 Record Print 1
12/29
7/27/2019 Record Print 1
13/29
SOURCE CODE
Separate Chaining method
#include
#includetypedef struct listnode *list,*position;
typedef struct hashtbl* hashtable;
#define tablesize 10
struct listnode{
int element;struct listnode *next;
};
struct hashtbl{
int tabsize;list thelists[tablesize];
};
hashtable initialize_table(int tabsize ){
hashtable h;int i;
h = malloc ( sizeof (struct hashtbl) );if( h == NULL )
printf("Out of space!!!");h->tabsize = tablesize;
for(i=0; itabsize; i++ ){
h->thelists[i] = malloc( sizeof (struct listnode) );
if(h->thelists[i] == NULL )printf("Out of space!!!");
else
h->thelists[i]->next = NULL;}
return h;}
void display(hashtable h)
{position p;
list l;int i;
for(i=0;itabsize;i++){
l = h->thelists[i];p = l->next;
printf("Bucket %d: ",i);while( p != NULL)
7/27/2019 Record Print 1
14/29
7/27/2019 Record Print 1
15/29
7/27/2019 Record Print 1
16/29
7/27/2019 Record Print 1
17/29
Enter the element to be inserted: 9
Enter the element to be inserted: 2Hash table is full
HASH TABLE
Slot Element
0 10
1 11
2 123 22
4 45 5
6 157 6
8 89 9
Collision handling by separate chaining
Table size is 10
enter the element to be inserted 6
enter the element to be inserted 12enter the element to be inserted 25
enter the element to be inserted 15enter the element to be inserted 11
enter the element to be inserted 17
enter the element to be inserted 9enter the element to be inserted 22
enter the element to be inserted 7
enter the element to be inserted 36
Bucket 0:Bucket 1: 11
Bucket 2: 22 12Bucket 3:
Bucket 4:Bucket 5: 15 25
Bucket 6: 36 6
Bucket 7: 7 17Bucket 8:Bucket 9: 9
RESULT
Thus the C program to implement hashing techniques using separate chaining and
linear probing methods is implemented, executed and verified.
7/27/2019 Record Print 1
18/29
7/27/2019 Record Print 1
19/29
7/27/2019 Record Print 1
20/29
7/27/2019 Record Print 1
21/29
12. A. IMPLEMENTATION OF PRIMS ALGORITHM
AIMWrite a C program to implement prims algorithm.
ALGORITHM
1. Get the number of vertices n and the cost adjacency matrix as input.
2. Get the weight for all the edges.
3. Choose a vertex as starting vertex v
4. After a vertex is selected declare it known and update dw =min (dw,cw,v) for each
unknown vertex w adjacent to v.
5. Continue step 4 until all vertices are declared known
SOURCE CODE
#include
#include
int a,b,u,v,n,i,j,ne=1;int visited[10]={0},min,mincost=0,cost[10][10];
void main(){
clrscr();printf("\n Enter the number of vertices:");
scanf("%d",&n);printf("\n Enter the cost adjacency matrix:\n");
for(i=1;i
7/27/2019 Record Print 1
22/29
7/27/2019 Record Print 1
23/29
7/27/2019 Record Print 1
24/29
for(i=1,min=999;i
7/27/2019 Record Print 1
25/29
RESULT
Thus the C program to implement kruskals algorithm to find the minimum spanning
tree is written, executed and verified.
7/27/2019 Record Print 1
26/29
13. IMPLEMENTATION OF BACKTRACKING ALGORITHM FOR
KNAPSACK PROBLEM
AIM
Write a C program to implement backtracking algorithm for knapsack problem.
ALGORITHM
1. To solve the knapsack problem: given n rods, use a bit array A[1..n]. Set A[i] to 1 if
rod i is to be used. Exhaustively search through all binary strings A[1..n] testing for
a fit.
2. Use the binary string algorithm. Pruning: let be length remaining,
A[m] = 0 is always legal
A[m] = 1 is illegal if sm > ; prune here
3. Prints all solutions to knapsack problem with n rods of length s1. . .sn, and knapsack
of length L.
4. Comment solve knapsack problem with m rods, knapsack size
5. If m = 0 then If = 0 then print (A)
6. else . A[m]:= 0; knapsack (m 1, )
7. f sm then A[m]:= 1; knapsack (m 1, sm)
SOURCE CODE
#include#include
int c,c1,n,i,j,k;int q[10],x[10][10],w[10],p[10],max;
void get();void knapsack();
void display();void get()
{printf("\n Enter the number of objects: ");
scanf("%d",&n);printf("\n Enter the size of the knapsack: ");
scanf("%d",&c);printf("\n Enter the weight and profit of the objects ");
for(i=1; i
7/27/2019 Record Print 1
27/29
printf("\n Enter the weight %d: ",i);scanf("%d",&w[i]);
printf("\n Enter the profit of weight %d: ",i);scanf("%d",&p[i]);
}}
void knapsack(){
for(j=1; j
7/27/2019 Record Print 1
28/29
OUTPUT
Enter the number of objects: 5
Enter the size of the knapsack: 12
Enter the weight and profit of the objectsEnter the weight 1: 3
Enter the profit of weight 1: 5
Enter the weight 2: 4
Enter the profit of weight 2: 4
Enter the weight 3: 2
Enter the profit of weight 3: 4
Enter the weight 4: 4
Enter the profit of weight 4: 3
Enter the weight 5: 5
Enter the profit of weight 5: 1
The suboptimal solutions profit
1 0 0 0 0 130 1 0 0 0 11
0 0 1 0 0 80 0 0 1 0 4
0 0 0 0 1 1
Maximum profit is 13
RESULT
Thus the C program to implement backtracking algorithm for knapsack problem is
written, executed and verified.
7/27/2019 Record Print 1
29/29