0% found this document useful (0 votes)
3 views5 pages

LAB Report 12-2

The lab report focuses on the implementation of Binary Heaps, detailing the functions for inserting and deleting nodes. It also introduces Binomial and Fibonacci heaps, highlighting their properties and efficiency in various operations. The report concludes with an analytical analysis of the implemented functions and suggests further study of advanced heap data structures.

Uploaded by

Muhammad Umar
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)
3 views5 pages

LAB Report 12-2

The lab report focuses on the implementation of Binary Heaps, detailing the functions for inserting and deleting nodes. It also introduces Binomial and Fibonacci heaps, highlighting their properties and efficiency in various operations. The report concludes with an analytical analysis of the implemented functions and suggests further study of advanced heap data structures.

Uploaded by

Muhammad Umar
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/ 5

COMSATS UNIVERSITY

LAB REPORT
Binary Heaps Implementation

Subhan Ali (FA22-BEE-212)

Data Structures &


Algorithms
Mr. Lecturer
Learning Outcomes:
After successfully completing this lab the students will be able to:

1. To understand and implement the Binary Heaps


2. To understand the basic insertion and deletion operations on Binary Heaps

IN-LAB TASK: 1. Complete the function ‘void insert_node(int x, struct heap_struct * H)’ in
the skeleton code provided.

CODE:
void insert_node(int x, struct heap_struct * H){
unsigned int i

if(is_full(H)) {

printf("\nError! Heap is full !!\n");

return;}

else {

i = ++ H->crnt_heap_size;

H->elements[i] = x; /// Insert the item in the first available position.

while(((H->elements[i] > H->elements[i/2])) && (i != 1)) /// Then move it up to its correct position {

swap_ref(&(H->elements[i]), &(H->elements[i/2]));

i = i/2; } }}

2. Complete the function ‘void delete_root(struct heap_struct * H)’ in the skeleton code
provided.

CODE:
int delete_root(struct heap_struct * H){

int index = H->crnt_heap_size;

int root = H->elements[1]; /// We will return this value at the end of this function

H->elements[1] = H->elements[index]; /// Replace the root with the last node

H->elements[index] = EMPTY; /// Make the last slot empty

int i = 1;

while(i<=H->crnt_heap_size/2) /// We just need to check the siblings of the 2nd last level {

if(((H->elements[i])>(H->elements[2*i])) || ((H->elements[i])>(H->elements[(2*i)+1]))) {

if((H->elements[2*i]) > (H->elements[(2*i)+1])) /// If the left child node is larger than {

swap_ref(&(H->elements[i]), &(H->elements[(2*i)+1]));

i = (2*i)+1; }

else {

swap_ref(&(H->elements[i]), &(H->elements[2*i]));

i = 2*i; } }

else
break; }

H->crnt_heap_size --; /// Now there is one less item in the heap.

return(root);}

OUTPUT:

Post Lab: Study Binomial Heaps and Fibonacci Heaps.

Binomial heaps are a type of heap data structure that efficiently supports insertion, deletion,
and merging operations. They are composed of a collection of binomial trees, each satisfying
the binomial tree property. Each binomial tree in the heap is a min-heap (or max-heap,
depending on the implementation), and no two binomial trees in the heap have the same rank
(degree).

Fibonacci heaps are another type of heap data structure that offer amortized constant time for
key operations, making them particularly efficient for algorithms where these operations are
frequently used. A Fibonacci heap is a collection of trees satisfying the min-heap property, and
it supports a set of operations that can be executed in constant or logarithmic amortized time.

Both binomial heaps and Fibonacci heaps are important data structures with their own set of
advantages and use cases. Binomial heaps are simpler to implement and have better worst-case
time complexity for most operations, while Fibonacci heaps offer better amortized time
complexity for certain operations, making them more suitable for applications where these
operations are heavily used.

Analytical Analysis:
The insert_node and delete_root functions in the provided skeleton code
demonstrates an understanding of how to manipulate binomial heaps efficiently.
These tasks involve crucial operations for maintaining the heap's structure and
adhering to its properties. By implementing these functions successfully, you've
gained practical experience in working with heap data structures, which are
fundamental in various algorithms and applications. Moving forward, a deeper
study of binomial heaps and Fibonacci heaps will further enhance your
understanding of advanced data structures and their algorithms, broadening your
toolkit for solving complex computational problems.

You might also like