LAB Report 12-2
LAB Report 12-2
LAB REPORT
Binary Heaps Implementation
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)) {
return;}
else {
i = ++ H->crnt_heap_size;
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 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
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:
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.