Implement the insert and delete functions on Priority queue without Array
A priority Queue is a type of queue in which every element is associated with a priority and is served according to its priority.
We will use two popular data structures for implementing priority queues without arrays –
- Fibonacci Heap
- Binomial Heap
Fibonacci heap is a heap data structure that is composed of a collection of min-heap-ordered trees. It has a faster-amortized running time than many other priority queue data structures including the binary heap and binomial heap.
- Insertion in Fibonacci Heap: Insertion in a Fibonacci heap is done by creating a new tree with the key of the inserted element and making it a child of the root list. The tree is then linked to the root list.
- Deletion in Fibonacci Heap: Deletion in a Fibonacci heap is done by first removing the element to be deleted from the root list and then merging the children of the deleted element into the root list. The resulting heap is then consolidated by repeatedly merging roots of the same degree.
Following is a C++ program to demonstrate Insertion() and Deletion() operations on a Fibonacci Heap:
Creating an initial heap The root nodes of Heap are: 2-->5-->8 The heap has 3 node Delete the node 8 Key Deleted Delete the node 5 Key Deleted The root nodes of Heap are: 2 The heap has 1 node
A binomial heap is a heap similar to a binary heap but also supports quick merging of two heaps. It is implemented using a binomial tree. Each node in a binomial tree has exactly one child.
- Insertion in Binomial Heap:
Insertion in a binomial heap is done by creating a new binomial tree with the key of the inserted element and then merging it with the existing binomial trees.
- Deletion in Binomial Heap:
Deletion in a binomial heap is done by first removing the element to be deleted from the root list and then merging its children into the root list. The resulting heap is then consolidated by repeatedly merging roots of the same degree.
Following is a C++ program to demonstrate Insertion() and DeleteMin() operations on a Binomial Heap:
Heap elements after insertion: 30 10 20 Minimum element of heap 10 Heap after deletion of minimum element 20 30
Fibonacci heap and binomial heap are efficient data structures for implementing priority queues. Insertion and deletion in these data structures can be done in logarithmic time. However, the Fibonacci heap has better-amortized running time and is generally considered to be more efficient than a binomial heap.
Please Login to comment...