0% found this document useful (0 votes)
50 views12 pages

Tutorial Sheet 7: - Arjav Kanadia IEC2020101

The document contains code and explanations for priority queue and heapsort algorithms. It asks 3 questions: 1) Insert keys into a max heap and print the heap after deleting twice. 2) Sort numbers in descending order using heapsort and show the steps. Derive the average time complexity of heapsort. 3) Write code to print the level order traversal of a binary tree.

Uploaded by

Arjav Kanadia
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)
50 views12 pages

Tutorial Sheet 7: - Arjav Kanadia IEC2020101

The document contains code and explanations for priority queue and heapsort algorithms. It asks 3 questions: 1) Insert keys into a max heap and print the heap after deleting twice. 2) Sort numbers in descending order using heapsort and show the steps. Derive the average time complexity of heapsort. 3) Write code to print the level order traversal of a binary tree.

Uploaded by

Arjav Kanadia
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/ 12

Tutorial Sheet 7

-Arjav Kanadia
IEC2020101
Q1. Insert the following keys in a Priority Queue implemented using a MAX-HEAP

7, 16, 49, 82, 5, 31, 6, 2, 44


Perform deletion twice and show the contents of the heap after each deletion
#include <stdio.h>

int tree_array_size = 20;


int heap_size = 0;
const int INF = 100000;

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


int t;
t = *a;
*a = *b;
*b = t;
}

int get_right_child(int A[], int index) {


if((((2*index)+1) < tree_array_size) && (index >= 1))
return (2*index)+1;
return -1;
}

int get_left_child(int A[], int index) {


if(((2*index) < tree_array_size) && (index >= 1))
return 2*index;
return -1;
}

int get_parent(int A[], int index) {


if ((index > 1) && (index < tree_array_size)) {
return index/2;
}
return -1;
}

void max_heapify(int A[], int index) {


int left_child_index = get_left_child(A, index);
int right_child_index = get_right_child(A, index);

int largest = index;

if ((left_child_index <= heap_size) && (left_child_index>0)) {


if (A[left_child_index] > A[largest]) {
largest = left_child_index;
}
}

if ((right_child_index <= heap_size && (right_child_index>0))) {


if (A[right_child_index] > A[largest]) {
largest = right_child_index;
}
}

if (largest != index) {
swap(&A[index], &A[largest]);
max_heapify(A, largest);
}
}

void build_max_heap(int A[]) {


int i;
for(i=heap_size/2; i>=1; i--) {
max_heapify(A, i);
}
}

int maximum(int A[]) {


return A[1];
}

int extract_max(int A[]) {


int maxm = A[1];
A[1] = A[heap_size];
heap_size-=2;;
max_heapify(A, 1);
return maxm;
}

void increase_key(int A[], int index, int key) {


A[index] = key;
while((index>1) && (A[get_parent(A, index)] < A[index])) {
swap(&A[index], &A[get_parent(A, index)]);
index = get_parent(A, index);
}
}

void decrease_key(int A[], int index, int key) {


A[index] = key;
max_heapify(A, index);
}

void insert(int A[], int key) {


heap_size++;
A[heap_size] = -1*INF;
increase_key(A, heap_size, key);
}

void print_heap(int A[]) {


int i;
for(i=1; i<=heap_size; i++) {
printf("%d ",A[i]);
}
printf("\n");
}

int main() {
int A[tree_array_size];
insert(A, 7);
insert(A, 16);
insert(A, 49);
insert(A, 82);
insert(A, 5);
insert(A, 31);
insert(A, 6);
insert(A, 2);
insert(A, 44);

print_heap(A);

printf("Maximum in heap %d\n\n", maximum(A));


printf("%d\n\n", extract_max(A));

printf("Heap after twice deletion \n");


print_heap(A);

printf("%d\n", extract_max(A));
print_heap(A);
printf("%d\n", extract_max(A));
print_heap(A);
printf("%d\n", extract_max(A));
print_heap(A);
printf("%d\n", extract_max(A));
print_heap(A);
printf("%d\n", extract_max(A));
print_heap(A);
printf("%d\n", extract_max(A));
print_heap(A);

return 0;
}
.

Q2. Sort the following numbers in descending order using Heapsort.


10, 20, 15, 27, 45, 32, 16
Show stepwise analysis. Derive the average case time complexity of Heapsort.

#include <stdio.h>
int main()
{
int heap[10], array_size, i, j, c, root, temporary;
printf(" Enter size of array to be sorted :");
scanf("%d", &array_size);
printf(" Enter the elements of array : ");
for (i = 0; i < array_size; i++)
scanf("%d", &heap[i]);
for (i = 1; i < array_size; i++)
{
c = i;
do
{
root = (c - 1) / 2;
if (heap[root] < heap[c])
{
temporary = heap[root];
heap[root] = heap[c];
heap[c] = temporary;
}
c = root;
} while (c != 0);
}
printf("\nHeap array : ");
for (i = 0; i < array_size; i++)
printf("%d\t ", heap[i]);
for (j = array_size - 1; j >= 0; j--)
{
temporary = heap[0];
heap[0] = heap[j] ;
heap[j] = temporary;
root = 0;
do
{
c = 2 * root + 1;
if ((heap[c] < heap[c + 1]) && c < j-1)
c++;
if (heap[root]<heap[c] && c<j)
{
temporary = heap[root];
heap[root] = heap[c];
heap[c] = temporary;
}
root = c;
} while (c < j);
}
printf("\n The sorted array is : ");
for (i = 0; i < array_size; i++)
printf("\t %d", heap[i]);
}
Q3. WAP to generate level order traversal of the binary tree.
Example: for the given binary tree the level order traversal is 1, 2, 3, 4, 5, 6

#include <stdio.h>

#include <stdlib.h>

struct node

int info;

struct node *left, *right;

};
struct node *createnode(int key)

struct node *newnode = (struct node*)malloc(sizeof(struct node));

newnode->info = key;

newnode->left = NULL;

newnode->right = NULL;

return(newnode);

int heightoftree(struct node *root)

int max;

if (root!=NULL)

int leftsubtree = heightoftree(root->left);

int rightsubtree = heightoftree(root->right);

if (leftsubtree > rightsubtree)

max = leftsubtree + 1;

return max;

else
{

max = rightsubtree + 1;

return max;

void currentlevel(struct node *root, int level)

if (root != NULL)

if (level == 1)

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

else if (level > 1)

currentlevel(root->left, level-1);

currentlevel(root->right, level-1);

int main()
{

struct node *newnode = createnode(1);

newnode->left = createnode(2);

newnode->right = createnode(3);

newnode->left->left = createnode(4);

newnode->left->right = createnode(5);

newnode->right->left = createnode(6);

newnode->right->right = createnode(7);

printf("Level Order Traversal of Tree 1 is \n");

int i;

int height = heightoftree(newnode);

for(i = 1; i <= height; i++)

currentlevel(newnode,i);

return 0;

You might also like