
arr will store the element's of the priority queue The maximum priority element will be present as rootNode. Insert the new element at the end of the tree. Inserting an element in the priority queue(max heap) is done as There are variations of Binary Heap like Fibonacci Heap that can support insert and decrease-key in Θ(1) time. The operations are of same time complexity but the constants in Binary Search Tree are higher. The heaps can be build in O(N) time, but Self Balancing BST requires O(nLogn) time to construct. The parent-child relation is maintained in heaps by indexes. Heaps are implemented using arrays, so it does not require any extra space for the pointers. This is because of the following reasons:

The BST and heaps take the same time complexity to perform the operations of priority queue but heaps are prefered when priority queue is implemented. The heap can be built on the property that top priority element will be at the root Node so that the top priority element can be found in constant time. The top priority element will be present at the root Node. In the heaps, the enqueue() and dequeue() can be performed in O(log n) time. Heaps are generally preferred for the implementation of a priority queue.
#Time complexity of enqueue and dequeue update#
With deletion operation, we will update the extra pointer by finding the inorder successor. We can keep an extra pointer to store the highest priority element and that can be updated when the insertion and deletion is performed. The time to find the top priority element can be done in constant time. We can also use the self-balancing BST like AVL Tree, Red-Black Tree, etc to support the o(logn) complexity even in worst-case. A typical BST takes O(logn) for both insertion and deletion operation. We can also implement the functionality of the priority queue using a Binary Search Tree. the dequeue() and top() can be done in constant time. This will take linear time but the other two operations i.e. Operation, we need to traverse the linked list and find the proper position to insert the node such that the overall order of priority queue is maintained. The linked list can be arranged in the order of descending priority. The linked list can be created in such a way that the highest priority element should be at the front. Now, if we maintain the order of the array with respect to priority, the

Will also take linear time to search for the highest priority element. The deletion and the search operation in the array take linear time. So, the enqueue operation will take place at the end of the array which is a constant time operation.īut, for the dequeue operation, we have to find the highest priority element in the array and delete it from the array. We will take an array that will store the elements of the priority queue. The structure of the element of the priority queue will be We can choose various data structures like an array, linked list, BST, heaps to make the operations of the priority queues efficient. We can think of various ways of implementing the priority queues. Returning the maximum priority element in the Priority Queue. Deleting the maximum priority element from the Priority Queue. Inserting the new element in the Priority Queue. Priority Queue supports the following functionality
