0% found this document useful (0 votes)
16 views61 pages

DAA File (1) Himanshu

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

DAA File (1) Himanshu

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

EXPERIMENT-1

Aim: To implement linear and binary search algorithms and


compute their running time for worst, best and average cases.
#include

<stdio.h>

#include

<stdlib.h>

#include

<time.h>

int linearSearch(int arr[], int n, int

x) { for (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();
Himanshu Jain 2200911540054
CSDS-1 G2
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();

Himanshu Jain 2200911540054


CSDS-1 G2
linearSearch(arr, n,

x); end = clock();

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

CLOCKS_PER_SEC); return 0;

OUTPUT-

Himanshu Jain 2200911540054


CSDS-1 G2
EXPERIMEN
T-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, 90};

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;

}
Himanshu Jain 2200911540054
CSDS-1 G2
printf("Output: \

n"); for (i = 0; i <

n; i++) {

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

printf("\n");

Himanshu Jain 2200911540054


CSDS-1 G2
return 0;

Input-Output:

Input: Output:

64 34 25 12 22 11 90 11 12 22 25 34 64 90

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;

Himanshu Jain 2200911540054


CSDS-1 G2
temp =

arr[min_idx];

arr[min_idx] =

arr[i]; arr[i] =

temp;

Himanshu Jain 2200911540054


CSDS-1 G2
}
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 =
Himanshu 22009115400
Jain
}
arr[i];

j = i - 1;

while (j >= 0 && arr[j] >

key) { arr[j + 1] = arr[j];

j = j - 1;

Himanshu 22009115400
Jain
}
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

Himanshu 22009115400
Jain
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(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[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);

Himanshu 22009115400
Jain
}

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>

Himanshu 22009115400
Jain
void swap(int* a, int*

b) { int temp = *a;

*a = *b;

*b = temp;

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

Himanshu 22009115400
Jain
int pivot =

arr[high]; int i =

(low - 1);

for (int j = low; j <= high - 1;

j++) { if (arr[j] <= pivot) {

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[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, 9, 1, 5};

int n = sizeof(arr) /

sizeof(arr[0]);

srand(time(0));
Himanshu 22009115400
Jain
for (int i = 0; i < n;

i++) { int r =

rand() % n;

swap(&arr[i], &arr[r]);

Himanshu 22009115400
Jain
quickSort(arr, 0, n - 1);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

Input-Output:

Sorted array:

1 5 7 8 9 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(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[high]);

return (i + 1);

Himanshu 22009115400
Jain
}

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: 1 2 3

456

Himanshu 22009115400
Jain
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;
Himanshu 22009115400
Jain
else

x->parent->right

= y; y->left = x;

x->parent = y;

Himanshu 22009115400
Jain
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 && z->parent->color ==

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

>parent->left) {

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

Uncle if (y && 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);
Himanshu 22009115400
Jain
}

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

Case 3 z->parent->parent-

>color = RED; rightRotate(root,

z->parent->parent);

Himanshu 22009115400
Jain
}

} else {

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

Uncle if (y && 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-
Himanshu 22009115400
Jain
>data) x = x-

>left;

else

x = x->right;

Himanshu 22009115400
Jain
}

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 && (!x || x->color ==

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

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

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

Case 1 w->color =
Himanshu 22009115400
Jain
BLACK;

Himanshu 22009115400
Jain
x->parent->color =

RED; leftRotate(root, x-

>parent); w = x-

>parent->right;

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

(!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-
Himanshu 22009115400
Jain
>left;

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

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

Himanshu 22009115400
Jain
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 && z->data != data)

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

>right; if (!z) return;

Node* y = z;
Himanshu 22009115400
Jain
Color yOriginalColor = y-

>color; Node* x;

if (!z->left) {

x = z->right;

Himanshu 22009115400
Jain
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) {
Himanshu 22009115400
Jain
inorder(root->left);

printf("%d ", root-

>data);

inorder(root-

>right);

Himanshu 22009115400
Jain
int main() {

Node* root =

NULL;

insert(&root, 10);

insert(&root, 20);

insert(&root, 30);

insert(&root, 15);

insert(&root, 25);

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

inorder(root);

printf("\n");

delete(&root, 20);

printf("In-order after deletion of 20:

"); inorder(root);

printf("\n");

return 0;

OUTPU

T-

Himanshu 22009115400
Jain
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

Himanshu 22009115400
Jain
", root->keys[i]);

if (!root->leaf)

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

Himanshu 22009115400
Jain
}

BTreeNode *search(BTreeNode *root, int

key) { int i = 0;

while (i < root->n && key > root-

>keys[i]) i++;

if (i < root->n && 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];
Himanshu 22009115400
Jain
parent->keys[i] = child->keys[t - 1];

Himanshu 22009115400
Jain
parent->n++;

void insertNonFull(BTreeNode *node, int

key) { int i = node->n - 1;

if (node->leaf) {

while (i >= 0 && key < node-

>keys[i]) { node->keys[i + 1]

= node->keys[i];

i--;

node->keys[i + 1] =

key; node->n++;

} else {

while (i >= 0 && 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;
Himanshu 22009115400
Jain
}

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

Himanshu 22009115400
Jain
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--;
Himanshu 22009115400
Jain
free(sibling

);

void removeFromNode(BTreeNode *node, int

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

Himanshu 22009115400
Jain
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 && root->keys[idx] <

key) idx++;

if (idx < root->n && 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 && root->children[idx - 1]->n >= root->t) {


Himanshu 22009115400
Jain
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 && 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);

}
Himanshu 22009115400
Jain
root->children[idx] = removeKey(root->children[idx], key);

Himanshu 22009115400
Jain
}

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-

Himanshu 22009115400
Jain
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 && arr[left] <

arr[smallest]) smallest = left;

if (right < n && 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];
Himanshu 22009115400
Jain
arr[0] = arr[i];

arr[i] = temp;

heapify(arr, i,

0);

Himanshu 22009115400
Jain
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;

OUTPU

T-

Himanshu 22009115400
Jain
Binomial Sort

#include

<stdio.h>

#include

<stdlib.h>

typedef struct

BinomialNode { int key;

int degree;

struct BinomialNode *parent, *child, *sibling;

Himanshu 22009115400
Jain
} 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 && h2) {

if (h1->degree <= h2-

>degree) { tail->sibling

= h1;

h1 = h1->sibling;

} else {

tail->sibling =

h2; h2 = h2-

>sibling;

}
Himanshu 22009115400
Jain
tail = tail->sibling;

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

Himanshu 22009115400
Jain
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 && 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;

}
Himanshu 22009115400
Jain
next = curr->sibling;

return head;

Himanshu 22009115400
Jain
}
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;

}
Himanshu 22009115400
Jain
}
*heap = unionHeaps(*heap,

newHeap); free(minNode);

return min;

Himanshu 22009115400
Jain
}
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(&heap)); printf("Heap after

extraction: ");

printHeap(heap);

printf("\n");

return

0;}

OUTPUT-

Himanshu 22009115400
Jain
}

Fabonacci

Heap #include

<stdio.h>

#include

<stdlib.h>

Himanshu 22009115400
Jain
#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;
Himanshu 22009115400
Jain
} else {

node->left = heap->min;

Himanshu 22009115400
Jain
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,
Himanshu 22009115400
Jain
n3);

printf("Fibonacci Heap:

"); printHeap(heap-

>min);

printf("\n");

return 0;

Himanshu 22009115400
Jain
OUTPUT-

Himanshu 22009115400
Jain

You might also like