0% found this document useful (0 votes)
21 views35 pages

Exp ALL

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views35 pages

Exp ALL

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

#include <time.

h>

(int i = 0; i < n; i++) {

if (arr[i] == x)

return i;

return -1;

int main() {

int n = 1000;

int arr[n];

for (int i = 0; i < n; i++) {

arr[i] = i + 1;

clock_t start, end;

x = 1;

start = clock();

linearSearch(arr, n, x);

end = clock();

printf("Linear Search Best Case: %lf seconds\n", ((double)(end - start)) / CLOCKS_PER_SEC);

x = n / 2;

start = clock();

linearSearch(arr, n, x);

end = clock();

printf("Linear Search Average Case: %lf seconds\n", ((double)(end - start)) / CLOCKS_PER_SEC);

x = n + 1;

start = clock();

HARSHIT VARSHNEY 2200911540051


linearSearch(arr, n, x);

end = clock();

printf("Linear Search Worst Case: %lf seconds\n", ((double)(end - start)) / CLOCKS_PER_SEC);

return 0;

OUTPUT-

HARSHIT VARSHNEY 2200911540051


EXPERIMENT-2
Aim: To Implement the Following Algorithms: Bubble Sort, Selection Sort,
Insertion Sort.
Bubble Sort is a comparison based simple sorting algorithm that works by comparing the adjacent
elements and swapping them if the elements are not in the correct order. It is an in-place and stable
sorting algorithm that can sort items in data structures such as arrays and linked lists.

Source Code:

#include <stdio.h>

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 60};

int n = sizeof(arr)/sizeof(arr[0]);

int i, j, temp;

printf("Input: \n");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

for (i = 0; i < n-1; i++) {

for (j = 0; j < n-i-1; j++) {

if (arr[j] > arr[j+1]) {

temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

printf("Output: \n");

for (i = 0; i < n; i++) {


printf("%d ", arr[i]);

printf("\n");

HARSHIT VARSHNEY 2200911540051


return 0;

Input-Output:

Input: Output:

64 34 25 12 22 11 60 11 12 22 25 34 64 60

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the
smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion of
the list. The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of
the list and swaps it with the first element of the unsorted part. This process is repeated for the
remaining unsorted portion until the entire list is sorted.
Source Code:

#include <stdio.h>

int main() {

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof(arr)/sizeof(arr[0]);

int i, j, min_idx, temp;

printf("Original array: \n");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);


}

printf("\n");

for (i = 0; i < n-1; i++) {

min_idx = i;

for (j = i+1; j < n; j++) {

if (arr[j] < arr[min_idx]) {

min_idx = j;

temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

HARSHIT VARSHNEY 2200911540051


}

printf("Sorted array: \n");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

}
Input-Output:

Original array: Sorted array:

64 25 12 22 11 11 12 22 25 64

Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an
unsorted list into its correct position in a sorted portion of the list. It is a stable sorting algorithm,
meaning that elements with equal values maintain their relative order in the sorted output.
Source Code:

#include <stdio.h>

int main() {

int arr[] = {12, 11, 13, 5, 6};

int n = sizeof(arr)/sizeof(arr[0]);

int i, j, key;

printf("Original array: \n");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

while (j >= 0 ss arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

HARSHIT VARSHNEY 2200911540051


}

arr[j + 1] = key;

printf("Sorted array: \n");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

Input-Output:

Original array: Sorted array:

5 6 11 12 13 12 11 13 5 6

HARSHIT VARSHNEY 2200911540051


EXPERIMENT-3

Aim: To Implement the Best, Worst and Average Cases of Time Complexity
of Quick Sort.
Best Case:

The array is already sorted, and the pivot always divides the array into two equal halves, resulting in a
time complexity of O(nlogn).
Source Code:

#include <stdio.h>

void swap(int* a, int* b) {

int temp = *a;

*a = *b;

*b = temp;

int partition(int arr[], int low, int high) {

int pivot = arr[high];


int i = (low - 1);

for (int j = low; j <= high - 1; j++) {

if (arr[j] <= pivot) {

i++;
swap(sarr[i], sarr[j]);

swap(sarr[i + 1], sarr[high]);

return (i + 1);

void quickSort(int arr[], int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

HARSHIT VARSHNEY 2200911540051


}

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++)

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {1, 2, 3, 4, 5, 6};

int n = sizeof(arr) / sizeof(arr[0]);

quickSort(arr, 0, n - 1);

printf("Sorted array: \n");

printArray(arr, n);

return 0;
}

Input-Output:

Sorted array:

123456

Average Case:

The array elements are in random order, and the pivot divides the array into two subarrays of random
sizes, leading to an average time complexity of O(nlogn).

Source Code:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

void swap(int* a, int* b) {

int temp = *a;

*a = *b;

*b = temp;

int partition(int arr[], int low, int high) {

HARSHIT VARSHNEY 2200911540051


int pivot = arr[high];

int i = (low - 1);

for (int j = low; j <= high - 1; j++) {

if (arr[j] <= pivot) {

i++;

swap(sarr[i], sarr[j]);

swap(sarr[i + 1], sarr[high]);

return (i + 1);

void quickSort(int arr[], int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++)


printf("%d ", arr[i]);

printf("\n");

}
int main() {

int arr[] = {10, 7, 8, 6, 1, 5};

int n = sizeof(arr) / sizeof(arr[0]);

srand(time(0));

for (int i = 0; i < n; i++) {

int r = rand() % n;

swap(sarr[i], sarr[r]);

HARSHIT VARSHNEY 2200911540051


quickSort(arr, 0, n - 1);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

Input-Output:

Sorted array:

1 5 7 8 6 10

Worst Case:

The array is sorted in reverse order, and the pivot always picks the greatest or smallest element,
resulting in a time complexity of O(n^2).

Source Code:

#include <stdio.h>

void swap(int* a, int* b) {

int temp = *a;

*a = *b;

*b = temp;

int partition(int arr[], int low, int high) {

int pivot = arr[high];

int i = (low - 1);

for (int j = low; j <= high - 1; j++) {

if (arr[j] <= pivot) {

i++;

swap(sarr[i], sarr[j]);

swap(sarr[i + 1], sarr[high]);

return (i + 1);

HARSHIT VARSHNEY 2200911540051


}

void quickSort(int arr[], int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++)

printf("%d ", arr[i]);

printf("\n");

}
int main() {

int arr[] = {6, 5, 4, 3, 2, 1};

int n = sizeof(arr) / sizeof(arr[0]);

quickSort(arr, 0, n - 1);

printf("Sorted array: \n");

printArray(arr, n);

return 0;
}

Input-Output:

Sorted array:

123456

HARSHIT VARSHNEY 2200911540051


EXPERIMENT-4

Aim: To Implement the insertion and deletion in Red Black Tree.


#include <stdio.h>

#include <stdlib.h>

typedef enum { RED, BLACK } Color;

typedef struct Node {

int data;

Color color;

struct Node *left, *right, *parent;

} Node;

Node* createNode(int data) {

Node* node = (Node*)malloc(sizeof(Node));

node->data = data;

node->color = RED;

node->left = node->right = node->parent = NULL;

return node;

void leftRotate(Node** root, Node* x) {

Node* y = x->right;

x->right = y->left;
if (y->left) y->left->parent = x;

y->parent = x->parent;

if (!x->parent)
*root = y;

else if (x == x->parent->left)

x->parent->left = y;

else
x->parent->right = y;

y->left = x;

x->parent = y;
}

HARSHIT VARSHNEY 2200911540051


void rightRotate(Node** root, Node* y) {

Node* x = y->left;

y->left = x->right;

if (x->right) x->right->parent = y;

x->parent = y->parent;

if (!y->parent)

*root = x;

else if (y == y->parent->left)

y->parent->left = x;

else

y->parent->right = x;

x->right = y;

y->parent = x;
}

void fixInsertion(Node** root, Node* z) {

while (z->parent ss z->parent->color == RED) {

if (z->parent == z->parent->parent->left) {

Node* y = z->parent->parent->right; // Uncle

if (y ss y->color == RED) { // Case 1

z->parent->color = BLACK;

y->color = BLACK;

z->parent->parent->color = RED;

z = z->parent->parent;

} else {
if (z == z->parent->right) { // Case 2

z = z->parent;

leftRotate(root, z);
}

z->parent->color = BLACK; // Case 3

z->parent->parent->color = RED;

rightRotate(root, z->parent->parent);

HARSHIT VARSHNEY 2200911540051


}

} else {

Node* y = z->parent->parent->left; // Uncle

if (y ss y->color == RED) { // Case 1

z->parent->color = BLACK;

y->color = BLACK;

z->parent->parent->color = RED;

z = z->parent->parent;

} else {

if (z == z->parent->left) { // Case 2

z = z->parent;
rightRotate(root, z);

z->parent->color = BLACK; // Case 3

z->parent->parent->color = RED;

leftRotate(root, z->parent->parent);

(*root)->color = BLACK;

void insert(Node** root, int data) {

Node* z = createNode(data);

Node* y = NULL;
Node* x = *root;

while (x) {

y = x;

if (z->data < x->data)

x = x->left;

else
x = x->right;

HARSHIT VARSHNEY 2200911540051


}

z->parent = y;

if (!y)

*root = z;

else if (z->data < y->data)

y->left = z;

else

y->right = z;

fixInsertion(root, z);

void transplant(Node** root, Node* u, Node* v) {

if (!u->parent)

*root = v;

else if (u == u->parent->left)

u->parent->left = v;

else

u->parent->right = v;

if (v) v->parent = u->parent;

Node* minimum(Node* node) {

while (node->left)

node = node->left;

return node;

void fixDeletion(Node** root, Node* x) {

while (x != *root ss (!x || x->color == BLACK)) {

if (x == x->parent->left) {

Node* w = x->parent->right;

if (w->color == RED) { // Case 1

w->color = BLACK;

HARSHIT VARSHNEY 2200911540051


x->parent->color = RED;

leftRotate(root, x->parent);

w = x->parent->right;

if ((!w->left || w->left->color == BLACK) ss

(!w->right || w->right->color == BLACK)) { // Case 2

w->color = RED;

x = x->parent;
} else {

if (!w->right || w->right->color == BLACK) { // Case 3

if (w->left) w->left->color = BLACK;

w->color = RED;

rightRotate(root, w);

w = x->parent->right;

w->color = x->parent->color; // Case 4

x->parent->color = BLACK;

if (w->right) w->right->color = BLACK;

leftRotate(root, x->parent);

x = *root;
}

} else {

Node* w = x->parent->left;

if (w->color == RED) { // Case 1

w->color = BLACK;

x->parent->color = RED;

rightRotate(root, x->parent);

w = x->parent->left;
}

if ((!w->right || w->right->color == BLACK) ss

(!w->left || w->left->color == BLACK)) { // Case 2

HARSHIT VARSHNEY 2200911540051


w->color = RED;

x = x->parent;

} else {

if (!w->left || w->left->color == BLACK) { // Case 3

if (w->right) w->right->color = BLACK;

w->color = RED;

leftRotate(root, w);

w = x->parent->left;

w->color = x->parent->color; // Case 4

x->parent->color = BLACK;

if (w->left) w->left->color = BLACK;

rightRotate(root, x->parent);

x = *root;

if (x) x->color = BLACK;

// Delete a node

void delete(Node** root, int data) {

Node* z = *root;

while (z ss z->data != data)

z = (data < z->data) ? z->left : z->right;

if (!z) return;

Node* y = z;
Color yOriginalColor = y->color;

Node* x;

if (!z->left) {
x = z->right;

HARSHIT VARSHNEY 2200911540051


transplant(root, z, z->right);

} else if (!z->right) {

x = z->left;

transplant(root, z, z->left);

} else {

y = minimum(z->right);

yOriginalColor = y->color;

x = y->right;

if (y->parent == z) {

if (x) x->parent = y;

} else {

transplant(root, y, y->right);

y->right = z->right;

y->right->parent = y;

transplant(root, z, y);

y->left = z->left;

y->left->parent = y;

y->color = z->color;

}
if (yOriginalColor == BLACK)

fixDeletion(root, x);

free(z);
}

void inorder(Node* root) {

if (root) {

inorder(root->left);

printf("%d ", root->data);

inorder(root->right);

}
}

HARSHIT VARSHNEY 2200911540051


int main() {

Node* root = NULL;

insert(sroot, 10);

insert(sroot, 20);

insert(sroot, 30);

insert(sroot, 15);

insert(sroot, 25);

printf("In-order after insertions: ");

inorder(root);

printf("\n");

delete(sroot, 20);

printf("In-order after deletion of 20: ");

inorder(root);

printf("\n");

return 0;

OUTPUT-

HARSHIT VARSHNEY 2200911540051


EXPERIMENT-5

Aim: To Implement the insertion and deletion in B Tree.


#include <stdio.h>

#include <stdlib.h>

#define DEGREE 3

typedef struct BTreeNode {

int *keys; // Array of keys

int t; // Minimum degree

struct BTreeNode **children; // Array of child pointers

int n; // Current number of keys

int leaf; // Is true if node is a leaf


} BTreeNode;

BTreeNode *createNode(int t, int leaf) {

BTreeNode *node = (BTreeNode *)malloc(sizeof(BTreeNode));

node->t = t;

node->leaf = leaf;
node->keys = (int *)malloc((2 * t - 1) * sizeof(int));

node->children = (BTreeNode **)malloc(2 * t * sizeof(BTreeNode *));

node->n = 0;

return node;
}

void traverse(BTreeNode *root) {

if (root) {

for (int i = 0; i < root->n; i++) {

if (!root->leaf)

traverse(root->children[i]);

printf("%d ", root->keys[i]);

if (!root->leaf)

traverse(root->children[root->n]);

HARSHIT VARSHNEY 2200911540051


}

BTreeNode *search(BTreeNode *root, int key) {

int i = 0;

while (i < root->n ss key > root->keys[i])

i++;

if (i < root->n ss root->keys[i] == key)

return root;

if (root->leaf)

return NULL;

return search(root->children[i], key);


}

void splitChild(BTreeNode *parent, int i, BTreeNode *child) {

int t = child->t;
BTreeNode *newChild = createNode(t, child->leaf);

newChild->n = t - 1;

for (int j = 0; j < t - 1; j++)

newChild->keys[j] = child->keys[j + t];

if (!child->leaf) {

for (int j = 0; j < t; j++)


newChild->children[j] = child->children[j + t];

child->n = t - 1;

for (int j = parent->n; j >= i + 1; j--)

parent->children[j + 1] = parent->children[j];

parent->children[i + 1] = newChild;

for (int j = parent->n - 1; j >= i; j--)

parent->keys[j + 1] = parent->keys[j];
parent->keys[i] = child->keys[t - 1];

HARSHIT VARSHNEY 2200911540051


parent->n++;

void insertNonFull(BTreeNode *node, int key) {

int i = node->n - 1;

if (node->leaf) {

while (i >= 0 ss key < node->keys[i]) {

node->keys[i + 1] = node->keys[i];

i--;
}

node->keys[i + 1] = key;

node->n++;
} else {

while (i >= 0 ss key < node->keys[i])

i--;

i++;

if (node->children[i]->n == 2 * node->t - 1) {

splitChild(node, i, node->children[i]);

if (key > node->keys[i])

i++;

}
insertNonFull(node->children[i], key);

BTreeNode *insert(BTreeNode *root, int key) {

if (!root) {

root = createNode(DEGREE, 1);

root->keys[0] = key;

root->n = 1;

return root;

}
if (root->n == 2 * DEGREE - 1) {

HARSHIT VARSHNEY 2200911540051


BTreeNode *newRoot = createNode(DEGREE, 0);

newRoot->children[0] = root;

splitChild(newRoot, 0, root);

int i = (key > newRoot->keys[0]) ? 1 : 0;

insertNonFull(newRoot->children[i], key);

return newRoot;

insertNonFull(root, key);

return root;

void merge(BTreeNode *node, int idx) {

BTreeNode *child = node->children[idx];

BTreeNode *sibling = node->children[idx + 1];

int t = child->t;

child->keys[t - 1] = node->keys[idx];

for (int i = 0; i < sibling->n; i++)

child->keys[i + t] = sibling->keys[i];

if (!child->leaf) {

for (int i = 0; i <= sibling->n; i++)

child->children[i + t] = sibling->children[i];

for (int i = idx + 1; i < node->n; i++)

node->keys[i - 1] = node->keys[i];
for (int i = idx + 2; i <= node->n; i++)

node->children[i - 1] = node->children[i];

child->n += sibling->n + 1;

node->n--;

free(sibling);
}

void removeFromNode(BTreeNode *node, int idx) {

for (int i = idx + 1; i < node->n; i++)

HARSHIT VARSHNEY 2200911540051


node->keys[i - 1] = node->keys[i];

node->n--;

BTreeNode *removeKey(BTreeNode *root, int key) {

if (!root)

return root;

int idx = 0;

while (idx < root->n ss root->keys[idx] < key)

idx++;

if (idx < root->n ss root->keys[idx] == key) {

if (root->leaf) {

removeFromNode(root, idx);

} else if (root->children[idx]->n >= root->t) {

BTreeNode *pred = root->children[idx];

while (!pred->leaf)

pred = pred->children[pred->n];

root->keys[idx] = pred->keys[pred->n - 1];

root->children[idx] = removeKey(root->children[idx], pred->keys[pred->n - 1]);

} else if (root->children[idx + 1]->n >= root->t) {

BTreeNode *succ = root->children[idx + 1];

while (!succ->leaf)

succ = succ->children[0];

root->keys[idx] = succ->keys[0];

root->children[idx + 1] = removeKey(root->children[idx + 1], succ->keys[0]);

} else {

merge(root, idx);

root = removeKey(root->children[idx], key);

} else if (!root->leaf) {

if (root->children[idx]->n < root->t) {

if (idx > 0 ss root->children[idx - 1]->n >= root->t) {

HARSHIT VARSHNEY 2200911540051


BTreeNode *child = root->children[idx];

BTreeNode *leftSibling = root->children[idx - 1];

for (int i = child->n; i > 0; i--)

child->keys[i] = child->keys[i - 1];

child->keys[0] = root->keys[idx - 1];

if (!child->leaf)

for (int i = child->n + 1; i > 0; i--)

child->children[i] = child->children[i - 1];

root->keys[idx - 1] = leftSibling->keys[leftSibling->n - 1];

leftSibling->n--;

child->n++;

} else if (idx < root->n ss root->children[idx + 1]->n >= root->t) {

BTreeNode *child = root->children[idx];

BTreeNode *rightSibling = root->children[idx + 1];

child->keys[child->n] = root->keys[idx];

root->keys[idx] = rightSibling->keys[0];

for (int i = 0; i < rightSibling->n - 1; i++)

rightSibling->keys[i] = rightSibling->keys[i + 1];

if (!rightSibling->leaf)

for (int i = 0; i < rightSibling->n; i++)


rightSibling->children[i] = rightSibling->children[i + 1];

child->n++;

rightSibling->n--;
} else {

if (idx < root->n)

merge(root, idx);

else
merge(root, idx - 1);

root->children[idx] = removeKey(root->children[idx], key);

HARSHIT VARSHNEY 2200911540051


}

return root;

int main() {

BTreeNode *root = NULL;

root = insert(root, 10);

root = insert(root, 20);

root = insert(root, 5);

root = insert(root, 6);

root = insert(root, 12);

printf("B-Tree after insertions: ");

traverse(root);

printf("\n");
root = removeKey(root, 10);

printf("B-Tree after deleting 10: ");

traverse(root);

printf("\n");

return 0;}

OUTPUT-

HARSHIT VARSHNEY 2200911540051


EXPERIMENT-6

Aim: To Implement the min heap for- Heap sort , Binomial Sort and Fibonacci
Heap.
Heap Sort

#include <stdio.h>

#include <stdlib.h>

void heapify(int arr[], int n, int i) {

int smallest = i;

int left = 2 * i + 1;

int right = 2 * i + 2;

if (left < n ss arr[left] < arr[smallest])

smallest = left;

if (right < n ss arr[right] < arr[smallest])

smallest = right;

if (smallest != i) {

int temp = arr[i];

arr[i] = arr[smallest];

arr[smallest] = temp;

heapify(arr, n, smallest);
}
}

void heapSort(int arr[], int n) {

for (int i = n / 2 - 1; i >= 0; i--)

heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--) {

int temp = arr[0];

arr[0] = arr[i];

arr[i] = temp;

heapify(arr, i, 0);

}
}

HARSHIT VARSHNEY 2200911540051


void printArray(int arr[], int n) {

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {3, 1, 6, 5, 2, 4};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

printArray(arr, n);

heapSort(arr, n);

printf("Sorted array: ");

printArray(arr, n);

return 0;

OUTPUT-

Binomial Sort

#include <stdio.h>

#include <stdlib.h>

typedef struct BinomialNode {

int key;

int degree;

struct BinomialNode *parent, *child, *sibling;

HARSHIT VARSHNEY 2200911540051


} BinomialNode;

BinomialNode *createNode(int key) {

BinomialNode *node = (BinomialNode *)malloc(sizeof(BinomialNode));

node->key = key;

node->degree = 0;

node->parent = node->child = node->sibling = NULL;

return node;

BinomialNode *mergeHeaps(BinomialNode *h1, BinomialNode *h2) {

if (!h1) return h2;

if (!h2) return h1;

BinomialNode *head;

if (h1->degree <= h2->degree) {

head = h1;

h1 = h1->sibling;

} else {

head = h2;

h2 = h2->sibling;

BinomialNode *tail = head;

while (h1 ss h2) {

if (h1->degree <= h2->degree) {

tail->sibling = h1;

h1 = h1->sibling;

} else {

tail->sibling = h2;

h2 = h2->sibling;
}

tail = tail->sibling;

tail->sibling = (h1) ? h1 : h2;

HARSHIT VARSHNEY 2200911540051


return head;

void linkTrees(BinomialNode *y, BinomialNode *z) {

y->parent = z;

y->sibling = z->child;

z->child = y;

z->degree++;

BinomialNode *unionHeaps(BinomialNode *h1, BinomialNode *h2) {

BinomialNode *head = mergeHeaps(h1, h2);

if (!head) return NULL;


BinomialNode *prev = NULL, *curr = head, *next = head->sibling;

while (next) {

if ((curr->degree != next->degree) ||

(next->sibling ss next->sibling->degree == curr->degree)) {

prev = curr;
curr = next;

} else {

if (curr->key <= next->key) {

curr->sibling = next->sibling;

linkTrees(next, curr);

} else {

if (prev) prev->sibling = next;

else head = next;

linkTrees(curr, next);

curr = next;

}
}

next = curr->sibling;

return head;

HARSHIT VARSHNEY 2200911540051


}
BinomialNode *insert(BinomialNode *heap, int key) {

BinomialNode *node = createNode(key);

return unionHeaps(heap, node);

int extractMin(BinomialNode **heap) {

BinomialNode *prevMin = NULL, *minNode = *heap, *prev = NULL, *curr = *heap;

int min = minNode->key;

while (curr) {
if (curr->key < min) {

min = curr->key;

prevMin = prev;

minNode = curr;
}

prev = curr;

curr = curr->sibling;

if (prevMin) prevMin->sibling = minNode->sibling;

else *heap = minNode->sibling;

BinomialNode *child = minNode->child;

BinomialNode *newHeap = NULL;

while (child) {

BinomialNode *next = child->sibling;

child->sibling = newHeap;

child->parent = NULL;

newHeap = child;

child = next;

*heap = unionHeaps(*heap, newHeap);

free(minNode);

return min;

HARSHIT VARSHNEY 2200911540051


}
void printHeap(BinomialNode *node) {

if (node) {

printf("%d ", node->key);

printHeap(node->child);

printHeap(node->sibling);

int main() {

BinomialNode *heap = NULL;

heap = insert(heap, 10);

heap = insert(heap, 20);

heap = insert(heap, 5);

heap = insert(heap, 15);

printf("Heap after insertions: ");

printHeap(heap);

printf("\n");

printf("Extracted min: %d\n", extractMin(sheap));

printf("Heap after extraction: ");

printHeap(heap);

printf("\n");

return 0;}

OUTPUT-

Fabonacci Heap

#include <stdio.h>

#include <stdlib.h>

HARSHIT VARSHNEY 2200911540051


#include <math.h>

typedef struct FibonacciNode {

int key;

int degree;

int mark;

struct FibonacciNode *parent, *child, *left, *right;


} FibonacciNode;

typedef struct FibonacciHeap {

FibonacciNode *min;

int n;

} FibonacciHeap;

FibonacciHeap *createHeap() {

FibonacciHeap *heap = (FibonacciHeap *)malloc(sizeof(FibonacciHeap));

heap->min = NULL;

heap->n = 0;

return heap;
}

FibonacciNode *createNode(int key) {

FibonacciNode *node = (FibonacciNode *)malloc(sizeof(FibonacciNode));

node->key = key;

node->degree = 0;

node->mark = 0;

node->parent = node->child = NULL;

node->left = node->right = node;

return node;

void insertNode(FibonacciHeap *heap, FibonacciNode *node) {

if (!heap->min) {
heap->min = node;

} else {

node->left = heap->min;

HARSHIT VARSHNEY 2200911540051


node->right = heap->min->right;

heap->min->right->left = node;

heap->min->right = node;

if (node->key < heap->min->key)

heap->min = node;

heap->n++;

void printHeap(FibonacciNode *node) {

if (node) {

FibonacciNode *start = node;

do {

printf("%d ", node->key);

printHeap(node->child);

node = node->right;

} while (node != start);


}

int main() {

FibonacciHeap *heap = createHeap();

FibonacciNode *n1 = createNode(10);

FibonacciNode *n2 = createNode(20);

FibonacciNode *n3 = createNode(5);

insertNode(heap, n1);

insertNode(heap, n2);

insertNode(heap, n3);

printf("Fibonacci Heap: ");

printHeap(heap->min);

printf("\n");

return 0;

HARSHIT VARSHNEY 2200911540051


OUTPUT-

HARSHIT VARSHNEY 2200911540051

You might also like