Double Ended Priority QueueIntroduction to the double-ended priority queueA double-ended priority queue (DEPQ) is a data structure that stores a collection of elements, where each element is associated with a priority or value. Elements can be inserted and removed from both ends of the queue based on their priority. The highest priority element can be accessed from either end of the queue without removing it. In a DEPQ, the elements are stored in a way that maintains their priority order, such that the highest priority element is always at the front or back of the queue, depending on the implementation. Basic Terms: Priority Queue - A priority queue is a collection of items where each item has a priority assigned to it, and the items are handled in priority order. Double-Ended Queue - A double-ende queue is a collection of items that permits insertion and deletion from both the front and the back of the queue Properties of DEPQ
Operations supported by DEPQsThe following operations are performed on a double-ended priority queue (DEPQ):
Self-balancing BST implementation of a DEPQA self-balancing BST implementation of a DEPQ provides effective time complexity for all of its operations, and its space complexity is comparable to that of other implementations. Self-balancing trees automatically retain their balanced structure, ensuring quick and reliable performance while working with big data sets. In C++, a self-balancing binary search tree, such as red-black or AVL trees, is commonly used to implement the set container. These data structures offer set operations with effective worst-case and average-case time complexity. Output DEPQ Operations: 1. Insert an Element 2. Get Minimum Element 3. Get Maximum Element 4. Delete Minimum Element 5. Delete Maximum Element 6. Size 7. Is Empty? 0. Exit Enter your choice: 1 Enter the value to put: 25 Value inserted successfully. Enter your choice: 1 Enter the value to put: 75 Value inserted successfully. Enter your choice: 1 Enter the value to put: 30 Value inserted successfully. Enter your choice: 1 Enter the value to put: 90 Value inserted successfully. Enter your choice: 1 Enter the value to put: 55 Value inserted successfully. Enter your choice: 6 DEPQ size: 5 Enter your choice: 7 DEPQ is not empty. Enter your choice: 2 Minimum value: 25 Enter your choice: 3 Maximum value: 90 Enter your choice: 4 Minimum value deleted successfully. Enter your choice: 5 Maximum value deleted successfully. Enter your choice: 2 Minimum value: 30 Enter your choice: 3 Maximum value: 75 Enter your choice: 0 Successfully Exited! Time Complexity of each function:
Implementation of Double-Ended Priority Queue in JavaIn this implementation, we used a TreeSet to store the elements in a sorted manner. The TreeSet allows us to easily retrieve the minimum and maximum elements and also perform insertion and deletion in O(log n) time complexity. Output Inserted 25 Successfully! Inserted 75 Successfully! Inserted 30 Successfully! Inserted 90 Successfully! Inserted 55 Successfully! Size of the DEPQ : 5 Is empty? : false Minimum Element : 25 Maximum Element : 90 Maximum Element Deleted Successfully! New Maxmium Element : 75 Minimum Element Deleted Successfully! New Minimum Element : 30 Size of the DEPQ now : 3 Implementation of Double-Ended Priority Queue in PythonThe bisect module, which supports maintaining a list in sorted order without requiring the list to be sorted after each insertion, is used in this implementation. Using the bisect.insort() function, the element is inserted in the proper spot to preserve the sorted order. This enables us to insert and delete items in operations of O(log n) time complexity and obtain the minimum and the maximum elements in O(1) time. Output Inserted 25 Successfully! Inserted 75 Successfully! Inserted 30 Successfully! Inserted 90 Successfully! Inserted 55 Successfully! Size of the DEPQ : 5 is Empty? : False Minimum Element : 25 Maximum Element : 90 Maximum Elemented Deleted Successfully! Maximum Element : 75 Minimum Elemented Deleted Successfully! New Minimum Element : 30 Size of the DEPQ now : 3 Comparing Heap implementation vs. BBST implementation of DEPQImplementing a Double-Ended Priority Queue (DEPQ) using a Heap vs. a Balanced Binary Search Tree (BBST): Heap Implementation:
Balanced Binary Search Tree (BBST) Implementation:
Comparing these two implementations, we can see that:
Next TopicB+ Tree Deletion |