0% found this document useful (0 votes)
29 views8 pages

Q. Write A Program To Perform The Operations of Insertion and Deletion On The Heap. Code

The document contains code to perform heap operations like insertion and deletion. It includes functions to insert an element, delete the maximum element, and maintain the max heap property. The main function contains a menu to call these functions and test the heap implementation.

Uploaded by

Derik Ryan
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)
29 views8 pages

Q. Write A Program To Perform The Operations of Insertion and Deletion On The Heap. Code

The document contains code to perform heap operations like insertion and deletion. It includes functions to insert an element, delete the maximum element, and maintain the max heap property. The main function contains a menu to call these functions and test the heap implementation.

Uploaded by

Derik Ryan
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/ 8

Q.

Write a program to perform the operations of insertion and deletion on the


Heap.

Code:
#include <stdio.h>

// Function to swap two elements in an array

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

int temp = *a;

*a = *b;

*b = temp;

// Function to maintain max heap property after inserting an element

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

int largest = i;

int left = 2 * i + 1;

int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])

largest = left;

if (right < n && arr[right] > arr[largest])

largest = right;

if (largest != i) {

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

maxHeapify(arr, n, largest);

// Function to insert an element into the max heap

void insert(int arr[], int *n, int value) {

if (*n == 0) {

arr[0] = value;

(*n)++;

return;
}

arr[*n] = value;

int i = *n;

(*n)++;

while (i > 0 && arr[i] > arr[(i - 1) / 2]) {

swap(&arr[i], &arr[(i - 1) / 2]);

i = (i - 1) / 2;

// Function to delete the maximum element from the max heap

void deleteMax(int arr[], int *n) {

if (*n == 0) {

printf("Heap is empty.\n");

return;

if (*n == 1) {

(*n)--;

return;

arr[0] = arr[*n - 1];

(*n)--;

maxHeapify(arr, *n, 0);

// Function to display the elements of the max heap

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

if (n == 0) {

printf("Heap is empty.\n");

return;

printf("Max Heap: ");

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


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

printf("\n");

int main() {

int arr[100], n = 0;

int choice, value;

while (1) {

printf("\nMax Heap Operations:\n");

printf("1. Insert\n");

printf("2. Delete Max\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to insert: ");

scanf("%d", &value);

insert(arr, &n, value);

break;

case 2:

deleteMax(arr, &n);

break;

case 3:

display(arr, n);

break;

case 4:

return 0;

default:

printf("Invalid choice. Please try again.\n");

return 0;
}

Output:

Insertion:

Display:

Deletion:
Q.1) a) Write a menu driven program to compute the factorial and display a
Fibonacci series for the number entered by the user. (Use recursion for both of
them)
Code:
#include <stdio.h>

// Function to compute factorial using recursion

unsigned long long factorial(int n) {

if (n == 0 || n == 1)

return 1;

else

return n * factorial(n - 1);

// Function to display Fibonacci series using recursion

unsigned long long fibonacci(int n) {

if (n <= 1)

return n;

else

return fibonacci(n - 1) + fibonacci(n - 2);

int main() {

int choice, num;

while (1) {

printf("Menu:\n");

printf("1. Compute Factorial\n");

printf("2. Display Fibonacci Series\n");

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter a number: ");

scanf("%d", &num);

printf("Factorial of %d is %llu\n", num, factorial(num));


break;

case 2:

printf("Enter the number of terms in Fibonacci series: ");

scanf("%d", &num);

printf("Fibonacci Series:\n");

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

printf("%llu ", fibonacci(i));

printf("\n");

break;

case 3:

printf("Exiting...\n");

return 0;

default:

printf("Invalid choice! Please try again.\n");

return 0;

Output:
Factorial Option: Fibonacci Series Option:

Exit:
b) Write a program to solve a Tower of Hanoi problem.
Code:
#include <stdio.h>

// Function to solve Tower of Hanoi problem using recursion

void towerOfHanoi(int n, char source, char auxiliary, char destination) {

if (n == 1) {

printf("Move disk 1 from %c to %c\n", source, destination);

return;

towerOfHanoi(n - 1, source, destination, auxiliary);

printf("Move disk %d from %c to %c\n", n, source, destination);

towerOfHanoi(n - 1, auxiliary, source, destination);

int main() {

int numDisks;

printf("Enter the number of disks: ");

scanf("%d", &numDisks);

printf("Steps to solve the Tower of Hanoi with %d disks:\n", numDisks);

towerOfHanoi(numDisks, 'A', 'B', 'C');

return 0;

}
Output:
Number of disks=2

Number of disks=4

Number of disks=3

You might also like