DS LAB Journal
DS LAB Journal
//multidimensional array in c
// C Program to store and print 12 values entered by the user
#include <stdio.h>
void main()
{
int integer[2][3][2];
printf("Enter 12 values: \n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
scanf("%d", &integer[i][j][k]);
}
}
}
printf("\nDisplaying values:\n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf("Integer[%d][%d][%d] = %d\n", i, j, k, integer[i][j][k]);
}
}
}
}
Output:
Page |1
Name: Anish Anjali Roll No:21MCA005
Page |2
Name: Anish Anjali Roll No:21MCA005
#include <stdio.h>
#define MAX_SIZE 10
void getArray(int arr[], int size);
int main()
{
int arr[MAX_SIZE];
int i;
getArray(arr, MAX_SIZE);
printf("Array outside function:");
for (i = 0; i < MAX_SIZE; i++){
printf("%d ", arr[i]);
}
return 0;
}
void getArray(int arr[], int size)
{
int i;
printf("Enter elements in array: ");
for (i = 0; i < size; i++){
scanf("%d", &arr[i]);
}
printf("Array inside function:");
for (i = 0; i < size; i++){
printf("%d ", arr[i]);
}
}
Output:
Page |3
Name: Anish Anjali Roll No:21MCA005
#include <stdio.h>
void main()
{
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i) {
scanf("%d", x+i);
sum += *(x+i);
}
printf("Sum = %d", sum);
}
Output:
Page |4
Name: Anish Anjali Roll No:21MCA005
Page |5
Name: Anish Anjali Roll No:21MCA005
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
Output:
Page |6
Name: Anish Anjali Roll No:21MCA005
#include <stdio.h>
void main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if (position >= n+1)
printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];
printf("Resultant array:\n");
for (c = 0; c < n - 1; c++)
printf("%d\n", array[c]);
}
}
Output:
Page |7
Name: Anish Anjali Roll No:21MCA005
Output:
Page |8
Name: Anish Anjali Roll No:21MCA005
Page |9
Name: Anish Anjali Roll No:21MCA005
Output:
P a g e | 10
Name: Anish Anjali Roll No:21MCA005
int main()
{
int choice;
while(1)
{
printf("\nOperations performed by Stack");
printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");
printf("\n\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: show();
break;
case 4: exit(0);
void Push()
{
int x;
if(Top==Size-1) {
printf("\n STACK FULL !!");
}
else{
printf("\nEnter element to be inserted to the stack:");
scanf("%d",&x);
Top=Top+1;
array[Top]=x;
}
}
void Pop()
P a g e | 11
Name: Anish Anjali Roll No:21MCA005
{
if(Top==-1) {
printf("\n STACK NOT FULL!!");
}
else {
printf("\n Popped element: %d",array[Top]);
Top=Top-1;
}
}
void show()
{
if(Top==-1){
printf("\nSTACK NOT FULL!!");
}
else{
printf("\nElements present in the stack: \n");
for(int i=Top;i>=0;--i)
printf("%d\n",array[i]);
}
}
Output:
P a g e | 12
Name: Anish Anjali Roll No:21MCA005
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}
}
}
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
P a g e | 13
Name: Anish Anjali Roll No:21MCA005
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
Output:
P a g e | 14
Name: Anish Anjali Roll No:21MCA005
void display()
P a g e | 15
Name: Anish Anjali Roll No:21MCA005
{
int f = front, r = rear;
if (front == -1)
{
printf("Queue is empty");
return;
}
printf("Queue elements are :\n");
if (f <= r)
{
while (f <= r)
{
printf("%d ", cqueue[f]);
f++;
}
}
else
{
while (f <= n - 1)
{
printf("%d ",cqueue[f]);
f++;
}
f = 0;
while (f <= r) {
printf("%d ",cqueue[f]);
f++;
}
}
}
int menu()
{
int choice;
printf("\n 1.Add value to the list");
printf("\n 2. Delete value to the list");
printf("\n 3. View List");
printf("\n 4. Exit\n");
printf("\n Please enter your choice: \t");
scanf("%d",&choice);
return(choice);
}
void main()
{
int value;
while(1)
{
switch(menu())
{
case 1:
printf("Input for insertion: ");
P a g e | 16
Name: Anish Anjali Roll No:21MCA005
scanf("%d",&value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("invalid choice");
}
}
}
Output:
P a g e | 17
Name: Anish Anjali Roll No:21MCA005
void insert();
void display();
void delete();
int count();
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
int data;
int main()
{
int option = 0;
printf("Singly Linked List :\n");
while (option < 5)
{
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
printf("4 : Count Linked List\n");
printf("5: Exit\n");
printf("Enter your option:");
scanf("%d", &option);
switch (option) {
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:count();
break;
case 5:exit(1);
break;
default:printf("Incorrect Choice.\n");
break;
}
}
return 0;
}
void insert() {
P a g e | 18
Name: Anish Anjali Roll No:21MCA005
void delete() {
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\n Enter Position for Delete Element : \n");
scanf("%d", &pos);
void display() {
int count = 0;
temp_node = first_node;
printf("\n Display Linked List : \n");
while (temp_node != 0) {
P a g e | 19
Name: Anish Anjali Roll No:21MCA005
int count() {
int count = 0;
temp_node = first_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\n No Of Items In Linked List : %d\n", count);
return count;
}
Output:
P a g e | 20
Name: Anish Anjali Roll No:21MCA005
P a g e | 21
Name: Anish Anjali Roll No:21MCA005
void insertAtFront()
{
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->prev = NULL;
temp->next = start;
start = temp;
}
void insertAtEnd()
{
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->next = NULL;
trav = start;
P a g e | 22
Name: Anish Anjali Roll No:21MCA005
if (start == NULL) {
start = temp;
}
else {
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}
void insertAtPosition()
{
int data, pos, i = 1;
struct node *temp, *newnode;
newnode = malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;
else if (pos == 1) {
insertAtFront();
}
else {
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
newnode->info = data;
temp = start;
while (i < pos - 1) {
temp = temp->next;
i++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
temp->next->prev = newnode;
}
}
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
P a g e | 23
Name: Anish Anjali Roll No:21MCA005
else {
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
}
}
void deleteEnd()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
temp = start;
while (temp->next != NULL)
temp = temp->next;
if (start->next == NULL)
start = NULL;
else {
temp->prev->next = NULL;
free(temp);
}
}
void deletePosition()
{
int pos, i = 1;
struct node *temp, *position;
temp = start;
if (start == NULL)
printf("\nList is empty\n");
else {
deleteFirst();
if (start != NULL) {
start->prev = NULL;
}
free(position);
return;
}
while (i < pos - 1) {
temp = temp->next;
i++;
}
position = temp->next;
P a g e | 24
Name: Anish Anjali Roll No:21MCA005
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
free(position);
}
int main()
{
int choice;
while (1) {
switch (choice) {
case 1:insertAtFront();
break;
case 2:insertAtEnd();
break;
case 3:insertAtPosition();
break;
case 4:deleteFirst();
break;
case 5:deleteEnd();
break;
case 6:deletePosition();
break;
case 7:traverse();
break;
case 8:exit(1);
break;
default:printf("Incorrect Choice. Try Again \n");
continue;
}
}
return 0;
}
Output:
P a g e | 25
Name: Anish Anjali Roll No:21MCA005
P a g e | 26
Name: Anish Anjali Roll No:21MCA005
Output:
P a g e | 27
Name: Anish Anjali Roll No:21MCA005
Output:
P a g e | 28
Name: Anish Anjali Roll No:21MCA005
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
Output:
P a g e | 29
Name: Anish Anjali Roll No:21MCA005
P a g e | 30
Name: Anish Anjali Roll No:21MCA005
Output:
P a g e | 31
Name: Anish Anjali Roll No:21MCA005
Program 20. Set implementation using bit string (union, insertion, difference)
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,p,ch,n1,n2,set1[10],set2[10], set3[20],flag;
int wish;
do
{
printf("\n Menu for set operations");
printf("\n Press 1 for UNION");
printf("\n press 2 for INTERSECTION");
printf("\n press 3 for DIFFERENCE");
printf("\n Enter your Choice");
scanf("%d",&ch);
switch(ch)
{
case 1://for union
k=0;
for(i=0;i<n1;i++)
{
set3[k]=set1[i];
k++;
}
for(i=0;i<n2;i++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(set2[i]==set1[j])
{
flag=0;
break;
}
}
if(flag==1)
{
set3[k]=set2[i];
k++;
P a g e | 32
Name: Anish Anjali Roll No:21MCA005
}
}
p=k;
for(k=0;k <p;k++)
{
printf(" %d",set3[k]);
}
break;
case 2:
k=0;
for(i=0;i<n2;i++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(set2[i]==set1[j])
{
flag=0;
break;
}
}
if(flag==0)
{
set3[k]=set2[i];
k++;
}
}
p=k;
for(k=0;k <p;k++)
{
printf("%d",set3[k]);
}
break;
case 3:
k=0;
for(i=0;i<n1;i++)
{
flag=1;
for(j=0;j<n2;j++)
{
if(set1[i]==set2[j])
{
flag=0;
break;
}
}
if(flag==1)
{
set3[k]=set1[i];
k++;
}
P a g e | 33
Name: Anish Anjali Roll No:21MCA005
}
p=k;
for(k=0;k <p;k++)
{
printf(" %d",set3[k]);
}
break;
}
printf("\n Do you want to continue(0/1)? ");
scanf("%d",&wish);
}
while(wish==0);
}
Output:
P a g e | 34
Name: Anish Anjali Roll No:21MCA005
P a g e | 35
Name: Anish Anjali Roll No:21MCA005
dis.parent[yset]=xset;
dis.rank[yset]=-1;
}
else
{
dis.parent[yset]=xset;
dis.rank[xset]=dis.rank[xset]+1;
dis.rank[yset]=-1;
}
}
int main()
{
int x,y,n;
printf("\nenter number of elements :\n");
scanf("%d",&dis.n);
makeset();
int ch,w;
do{
printf("\n1.UNION\n2.FIND \n3.DISPLAY");
printf("\nenter choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nenter elements to perform union :");
scanf("%d%d",&x,&y);
Union(x,y);
break;
case 2:
printf("\nenter elements to check if connected components :");
scanf("%d%d",&x,&y);
if(find(x)==find(y))
printf("\nconnected components !");
else
printf("\n no connected components !");
break;
case 3:
displayset();
break;
}
printf("\n do you want to continue ?(1/0)");
scanf("%d",&w);
}
while(w==1);
return 0;
}
P a g e | 36
Name: Anish Anjali Roll No:21MCA005
Output:
P a g e | 37
Name: Anish Anjali Roll No:21MCA005
P a g e | 38
Name: Anish Anjali Roll No:21MCA005
switch(choice)
{
case 1: printf("Enter the element to be Inserted:");
scanf("%d",&n);
root = insert(root, n);
break;
case 2: printf("Enter the element to be Deleted:");
scanf("%d",&n);
root = deleteNode(root, n);
break;
case 3: printf("Inorder traversal: ");
inorder(root);
break;
case 4:
exit(0);
break;
default:
printf("n Wrong Choice:n");
break;
}
}
}
P a g e | 39
Name: Anish Anjali Roll No:21MCA005
Output:
P a g e | 40
Name: Anish Anjali Roll No:21MCA005
return (node);
}
bool checkHeightBalance(struct node *root, int *height) {
int leftHeight = 0, rightHeight = 0;
int l = 0, r = 0;
if (root == NULL) {
*height = 0;
return 1;
}
l = checkHeightBalance(root->left, &leftHeight);
r = checkHeightBalance(root->right, &rightHeight);
else
return l && r;
}
int main() {
int height = 0;
if (checkHeightBalance(root, &height))
printf("The tree is balanced");
else
P a g e | 41
Name: Anish Anjali Roll No:21MCA005
Output:
P a g e | 42
Name: Anish Anjali Roll No:21MCA005
enum nodeColor {
RED,
BLACK
};
struct rbNode {
int data, color;
struct rbNode *link[2];
};
// Insert an node
void insertion(int data) {
struct rbNode *stack[98], *ptr, *newnode, *xPtr, *yPtr;
int dir[98], ht = 0, index;
ptr = root;
if (!root) {
root = createNode(data);
return;
}
stack[ht] = root;
dir[ht++] = 0;
while (ptr != NULL) {
if (ptr->data == data) {
printf("Duplicates Not Allowed!!\n");
return;
}
index = (data - ptr->data) > 0 ? 1 : 0;
stack[ht] = ptr;
ptr = ptr->link[index];
dir[ht++] = index;
}
stack[ht - 1]->link[index] = newnode = createNode(data);
while ((ht >= 3) && (stack[ht - 1]->color == RED)) {
P a g e | 43
Name: Anish Anjali Roll No:21MCA005
if (dir[ht - 2] == 0) {
yPtr = stack[ht - 2]->link[1];
if (yPtr != NULL && yPtr->color == RED) {
stack[ht - 2]->color = RED;
stack[ht - 1]->color = yPtr->color = BLACK;
ht = ht - 2;
} else {
if (dir[ht - 1] == 0) {
yPtr = stack[ht - 1];
} else {
xPtr = stack[ht - 1];
yPtr = xPtr->link[1];
xPtr->link[1] = yPtr->link[0];
yPtr->link[0] = xPtr;
stack[ht - 2]->link[0] = yPtr;
}
xPtr = stack[ht - 2];
xPtr->color = RED;
yPtr->color = BLACK;
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = xPtr;
if (xPtr == root) {
root = yPtr;
} else {
stack[ht - 3]->link[dir[ht - 3]] = yPtr;
}
break;
}
} else {
yPtr = stack[ht - 2]->link[0];
if ((yPtr != NULL) && (yPtr->color == RED)) {
stack[ht - 2]->color = RED;
stack[ht - 1]->color = yPtr->color = BLACK;
ht = ht - 2;
} else {
if (dir[ht - 1] == 1) {
yPtr = stack[ht - 1];
} else {
xPtr = stack[ht - 1];
yPtr = xPtr->link[0];
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = xPtr;
stack[ht - 2]->link[1] = yPtr;
}
xPtr = stack[ht - 2];
yPtr->color = BLACK;
xPtr->color = RED;
xPtr->link[1] = yPtr->link[0];
yPtr->link[0] = xPtr;
if (xPtr == root) {
root = yPtr;
P a g e | 44
Name: Anish Anjali Roll No:21MCA005
} else {
stack[ht - 3]->link[dir[ht - 3]] = yPtr;
}
break;
}
}
}
root->color = BLACK;
}
// Delete a node
void deletion(int data) {
struct rbNode *stack[98], *ptr, *xPtr, *yPtr;
struct rbNode *pPtr, *qPtr, *rPtr;
int dir[98], ht = 0, diff, i;
enum nodeColor color;
if (!root) {
printf("Tree not available\n");
return;
}
ptr = root;
while (ptr != NULL) {
if ((data - ptr->data) == 0)
break;
diff = (data - ptr->data) > 0 ? 1 : 0;
stack[ht] = ptr;
dir[ht++] = diff;
ptr = ptr->link[diff];
}
if (ptr->link[1] == NULL) {
if ((ptr == root) && (ptr->link[0] == NULL)) {
free(ptr);
root = NULL;
} else if (ptr == root) {
root = ptr->link[0];
free(ptr);
} else {
stack[ht - 1]->link[dir[ht - 1]] = ptr->link[0];
}
} else {
xPtr = ptr->link[1];
if (xPtr->link[0] == NULL) {
xPtr->link[0] = ptr->link[0];
color = xPtr->color;
xPtr->color = ptr->color;
ptr->color = color;
if (ptr == root) {
P a g e | 45
Name: Anish Anjali Roll No:21MCA005
root = xPtr;
} else {
stack[ht - 1]->link[dir[ht - 1]] = xPtr;
}
dir[ht] = 1;
stack[ht++] = xPtr;
} else {
i = ht++;
while (1) {
dir[ht] = 0;
stack[ht++] = xPtr;
yPtr = xPtr->link[0];
if (!yPtr->link[0])
break;
xPtr = yPtr;
}
dir[i] = 1;
stack[i] = yPtr;
if (i > 0)
stack[i - 1]->link[dir[i - 1]] = yPtr;
yPtr->link[0] = ptr->link[0];
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = ptr->link[1];
if (ptr == root) {
root = yPtr;
}
color = yPtr->color;
yPtr->color = ptr->color;
ptr->color = color;
}
}
if (ht < 1)
return;
if (ptr->color == BLACK) {
while (1) {
pPtr = stack[ht - 1]->link[dir[ht - 1]];
if (pPtr && pPtr->color == RED) {
pPtr->color = BLACK;
break;
}
if (ht < 2)
break;
P a g e | 46
Name: Anish Anjali Roll No:21MCA005
if (dir[ht - 2] == 0) {
rPtr = stack[ht - 1]->link[1];
if (!rPtr)
break;
if (rPtr->color == RED) {
stack[ht - 1]->color = RED;
rPtr->color = BLACK;
stack[ht - 1]->link[1] = rPtr->link[0];
rPtr->link[0] = stack[ht - 1];
if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
dir[ht] = 0;
stack[ht] = stack[ht - 1];
stack[ht - 1] = rPtr;
ht++;
P a g e | 47
Name: Anish Anjali Roll No:21MCA005
if (rPtr->color == RED) {
stack[ht - 1]->color = RED;
rPtr->color = BLACK;
stack[ht - 1]->link[0] = rPtr->link[1];
rPtr->link[1] = stack[ht - 1];
if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
dir[ht] = 1;
stack[ht] = stack[ht - 1];
stack[ht - 1] = rPtr;
ht++;
P a g e | 48
Name: Anish Anjali Roll No:21MCA005
// Driver code
int main() {
int ch, data;
while (1) {
printf("1. Insertion\t2. Deletion\n");
printf("3. Traverse\t4. Exit");
printf("\nEnter your choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the element to insert:");
scanf("%d", &data);
insertion(data);
break;
case 2:
printf("Enter the element to delete:");
scanf("%d", &data);
deletion(data);
break;
case 3:
inorderTraversal(root);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Not available\n");
break;
}
printf("\n");
}
return 0;
}
P a g e | 49
Name: Anish Anjali Roll No:21MCA005
Output:
P a g e | 50
Name: Anish Anjali Roll No:21MCA005
#define MAX 3
#define MIN 2
struct BTreeNode {
int val[MAX + 1], count;
struct BTreeNode *link[MAX + 1];
};
// Create a node
struct BTreeNode *createNode(int val, struct BTreeNode *child) {
struct BTreeNode *newNode;
newNode = (struct BTreeNode *)malloc(sizeof(struct BTreeNode));
newNode->val[1] = val;
newNode->count = 1;
newNode->link[0] = root;
newNode->link[1] = child;
return newNode;
}
// Insert node
void insertNode(int val, int pos, struct BTreeNode *node,
struct BTreeNode *child) {
int j = node->count;
while (j > pos) {
node->val[j + 1] = node->val[j];
node->link[j + 1] = node->link[j];
j--;
}
node->val[j + 1] = val;
node->link[j + 1] = child;
node->count++;
}
// Split node
void splitNode(int val, int *pval, int pos, struct BTreeNode *node,
struct BTreeNode *child, struct BTreeNode **newNode) {
int median, j;
P a g e | 51
Name: Anish Anjali Roll No:21MCA005
j = median + 1;
while (j <= MAX) {
(*newNode)->val[j - median] = node->val[j];
(*newNode)->link[j - median] = node->link[j];
j++;
}
node->count = median;
(*newNode)->count = MAX - median;
P a g e | 52
Name: Anish Anjali Roll No:21MCA005
// Search node
void search(int val, int *pos, struct BTreeNode *myNode) {
if (!myNode) {
return;
}
return;
}
int main() {
int val, ch;
insert(8);
insert(9);
insert(10);
insert(11);
P a g e | 53
Name: Anish Anjali Roll No:21MCA005
insert(15);
insert(16);
insert(17);
insert(18);
insert(20);
insert(23);
traversal(root);
printf("\n");
search(11, &ch, root);
}
Output:
P a g e | 54
Name: Anish Anjali Roll No:21MCA005
P a g e | 55
Name: Anish Anjali Roll No:21MCA005
int i;
for (i = 0; i < size; i++)
{
if (num == array[i])
break;
}
insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);
deleteRoot(array, 4);
printArray(array, size);
}
Output:
P a g e | 56
Name: Anish Anjali Roll No:21MCA005
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
FIB_HEAP *make_fib_heap();
void insertion(FIB_HEAP *H, NODE *new, int val);
NODE *extract_min(FIB_HEAP *H);
void consolidate(FIB_HEAP *H);
void fib_heap_link(FIB_HEAP *H, NODE *y, NODE *x);
NODE *find_min_node(FIB_HEAP *H);
void decrease_key(FIB_HEAP *H, NODE *node, int key);
void cut(FIB_HEAP *H, NODE *node_to_be_decrease, NODE *parent_node);
void cascading_cut(FIB_HEAP *H, NODE *parent_node);
void Delete_Node(FIB_HEAP *H, int dec_key);
FIB_HEAP *make_fib_heap() {
FIB_HEAP *H;
H = (FIB_HEAP *)malloc(sizeof(FIB_HEAP));
H->n = 0;
H->min = NULL;
H->phi = 0;
H->degree = 0;
return H;
}
P a g e | 57
Name: Anish Anjali Roll No:21MCA005
if (x->child == NULL) {
printf("node with no child (%d) \n", x->key);
} else {
printf("NODE(%d) with child (%d)\n", x->key, x->child->key);
print_heap(x->child);
}
if (x->right_sibling == n) {
break;
}
}
}
// Inserting nodes
void insertion(FIB_HEAP *H, NODE *new, int val) {
new = (NODE *)malloc(sizeof(NODE));
new->key = val;
new->degree = 0;
new->mark = false;
new->parent = NULL;
new->child = NULL;
new->visited = false;
new->left_sibling = new;
new->right_sibling = new;
if (H->min == NULL) {
H->min = new;
} else {
H->min->left_sibling->right_sibling = new;
new->right_sibling = H->min;
new->left_sibling = H->min->left_sibling;
H->min->left_sibling = new;
if (new->key < H->min->key) {
H->min = new;
}
}
(H->n)++;
}
// Union operation
FIB_HEAP *unionHeap(FIB_HEAP *H1, FIB_HEAP *H2) {
FIB_HEAP *Hnew;
Hnew = make_fib_heap();
Hnew->min = H1->min;
P a g e | 58
Name: Anish Anjali Roll No:21MCA005
Hnew->min->right_sibling->left_sibling = H2->min->left_sibling;
Hnew->min->right_sibling = H2->min;
H2->min->left_sibling = Hnew->min;
temp2->right_sibling = temp1;
// Consolidate function
void consolidate(FIB_HEAP *H) {
int degree, i, d;
degree = cal_degree(H->n);
NODE *A[degree], *x, *y, *z;
for (i = 0; i <= degree; i++) {
A[i] = NULL;
}
x = H->min;
do {
d = x->degree;
while (A[d] != NULL) {
y = A[d];
if (x->key > y->key) {
NODE *exchange_help;
exchange_help = x;
x = y;
y = exchange_help;
}
if (y == H->min)
H->min = x;
fib_heap_link(H, y, x);
if (y->right_sibling == x)
H->min = x;
A[d] = NULL;
P a g e | 59
Name: Anish Anjali Roll No:21MCA005
d++;
}
A[d] = x;
x = x->right_sibling;
} while (x != H->min);
H->min = NULL;
for (i = 0; i < degree; i++) {
if (A[i] != NULL) {
A[i]->left_sibling = A[i];
A[i]->right_sibling = A[i];
if (H->min == NULL) {
H->min = A[i];
} else {
H->min->left_sibling->right_sibling = A[i];
A[i]->right_sibling = H->min;
A[i]->left_sibling = H->min->left_sibling;
H->min->left_sibling = A[i];
if (A[i]->key < H->min->key) {
H->min = A[i];
}
}
if (H->min == NULL) {
H->min = A[i];
} else if (A[i]->key < H->min->key) {
H->min = A[i];
}
}
}
}
// Linking
void fib_heap_link(FIB_HEAP *H, NODE *y, NODE *x) {
y->right_sibling->left_sibling = y->left_sibling;
y->left_sibling->right_sibling = y->right_sibling;
if (x->right_sibling == x)
H->min = x;
y->left_sibling = y;
y->right_sibling = y;
y->parent = x;
if (x->child == NULL) {
x->child = y;
}
y->right_sibling = x->child;
y->left_sibling = x->child->left_sibling;
x->child->left_sibling->right_sibling = y;
x->child->left_sibling = y;
if ((y->key) < (x->child->key))
P a g e | 60
Name: Anish Anjali Roll No:21MCA005
x->child = y;
(x->degree)++;
}
// Extract min
NODE *extract_min(FIB_HEAP *H) {
if (H->min == NULL)
printf("\n The heap is empty");
else {
NODE *temp = H->min;
NODE *pntr;
pntr = temp;
NODE *x = NULL;
if (temp->child != NULL) {
x = temp->child;
do {
pntr = x->right_sibling;
(H->min->left_sibling)->right_sibling = x;
x->right_sibling = H->min;
x->left_sibling = H->min->left_sibling;
H->min->left_sibling = x;
if (x->key < H->min->key)
H->min = x;
x->parent = NULL;
x = pntr;
} while (pntr != temp->child);
}
(temp->left_sibling)->right_sibling = temp->right_sibling;
(temp->right_sibling)->left_sibling = temp->left_sibling;
H->min = temp->right_sibling;
if (node_to_be_decrease == node_to_be_decrease->right_sibling)
parent_node->child = NULL;
P a g e | 61
Name: Anish Anjali Roll No:21MCA005
node_to_be_decrease->left_sibling->right_sibling = node_to_be_decrease->right_sibling;
node_to_be_decrease->right_sibling->left_sibling = node_to_be_decrease->left_sibling;
if (node_to_be_decrease == parent_node->child)
parent_node->child = node_to_be_decrease->right_sibling;
(parent_node->degree)--;
node_to_be_decrease->left_sibling = node_to_be_decrease;
node_to_be_decrease->right_sibling = node_to_be_decrease;
H->min->left_sibling->right_sibling = node_to_be_decrease;
node_to_be_decrease->right_sibling = H->min;
node_to_be_decrease->left_sibling = H->min->left_sibling;
H->min->left_sibling = node_to_be_decrease;
node_to_be_decrease->parent = NULL;
node_to_be_decrease->mark = false;
}
else {
if (node_to_be_decrease->key < new_key) {
printf("\n Invalid new key for decrease key operation \n ");
} else {
node_to_be_decrease->key = new_key;
parent_node = node_to_be_decrease->parent;
if ((parent_node != NULL) && (node_to_be_decrease->key < parent_node->key)) {
printf("\n cut called");
cut(H, node_to_be_decrease, parent_node);
printf("\n cascading cut called");
cascading_cut(H, parent_node);
P a g e | 62
Name: Anish Anjali Roll No:21MCA005
}
if (node_to_be_decrease->key < H->min->key) {
H->min = node_to_be_decrease;
}
}
}
}
find_use->visited = false;
}
FIB_HEAP *insertion_procedure() {
FIB_HEAP *temp;
int no_of_nodes, ele, i;
NODE *new_node;
temp = (FIB_HEAP *)malloc(sizeof(FIB_HEAP));
temp = NULL;
if (temp == NULL) {
temp = make_fib_heap();
}
printf(" \n enter number of nodes to be insert = ");
scanf("%d", &no_of_nodes);
for (i = 1; i <= no_of_nodes; i++) {
printf("\n node %d and its key value = ", i);
scanf("%d", &ele);
insertion(temp, new_node, ele);
}
return temp;
}
void Delete_Node(FIB_HEAP *H, int dec_key) {
NODE *p = NULL;
find_node(H, H->min, dec_key, -5000);
p = extract_min(H);
if (p != NULL)
printf("\n Node deleted");
P a g e | 63
Name: Anish Anjali Roll No:21MCA005
else
printf("\n Node not deleted:some error");
}
case 2:
if (heap == NULL) {
heap = make_fib_heap();
}
printf(" enter number of nodes to be insert = ");
scanf("%d", &no_of_nodes);
for (i = 1; i <= no_of_nodes; i++) {
printf("\n node %d and its key value = ", i);
scanf("%d", &ele);
insertion(heap, new_node, ele);
}
break;
case 3:
min_node = find_min_node(heap);
if (min_node == NULL)
printf("No minimum value");
else
printf("\n min value = %d", min_node->key);
break;
case 4:
if (heap == NULL) {
printf("\n no FIbonacci heap created \n ");
break;
}
h1 = insertion_procedure();
heap = unionHeap(heap, h1);
printf("Unified Heap:\n");
print_heap(heap->min);
break;
P a g e | 64
Name: Anish Anjali Roll No:21MCA005
case 5:
if (heap == NULL)
printf("Empty Fibonacci heap");
else {
extracted_min = extract_min(heap);
printf("\n min value = %d", extracted_min->key);
printf("\n Updated heap: \n");
print_heap(heap->min);
}
break;
case 6:
if (heap == NULL)
printf("Fibonacci heap is empty");
else {
printf(" \n node to be decreased = ");
scanf("%d", &dec_key);
printf(" \n enter the new key = ");
scanf("%d", &new_key);
find_use = heap->min;
find_node(heap, find_use, dec_key, new_key);
printf("\n Key decreased- Corresponding heap:\n");
print_heap(heap->min);
}
break;
case 7:
if (heap == NULL)
printf("Fibonacci heap is empty");
else {
printf(" \n Enter node key to be deleted = ");
scanf("%d", &dec_key);
Delete_Node(heap, dec_key);
printf("\n Node Deleted- Corresponding heap:\n");
print_heap(heap->min);
break;
}
case 8:
print_heap(heap->min);
break;
case 9:
free(new_node);
free(heap);
exit(0);
default:
printf("Invalid choice ");
}
}
}
P a g e | 65
Name: Anish Anjali Roll No:21MCA005
Output:
P a g e | 66
Name: Anish Anjali Roll No:21MCA005
return min_index;
}
int printMST(int parent[], int graph[V][V])
{
printf("Edge \tWeight\n");
for (int i = 1; i < V; i++)
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}
void primMST(int graph[V][V])
{
int parent[V];
int key[V];
bool mstSet[V];
for (int i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] = false;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
for (int v = 0; v < V; v++)
if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
}
printMST(parent, graph);
}
int main()
{
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
primMST(graph);
return 0;
}
P a g e | 67
Name: Anish Anjali Roll No:21MCA005
Output:
P a g e | 68