Design a Data Structure for the following operations. The data structure should be efficient enough to accommodate the operations according to their frequency.
1) findMin() : Returns the minimum item. Frequency: Most frequent 2) findMax() : Returns the maximum item. Frequency: Most frequent 3) deleteMin() : Delete the minimum item. Frequency: Moderate frequent 4) deleteMax() : Delete the maximum item. Frequency: Moderate frequent 5) Insert() : Inserts an item. Frequency: Least frequent 6) Delete() : Deletes an item. Frequency: Least frequent.
A simple solution is to maintain a sorted array where smallest element is at first position and largest element is at last. The time complexity of findMin(), findMAx() and deleteMax() is O(1). But time complexities of deleteMin(), insert() and delete() will be O(n).
Can we do the most frequent two operations in O(1) and other operations in O(Logn) time?.
The idea is to use two binary heaps (one max and one min heap). The main challenge is, while deleting an item, we need to delete from both min-heap and max-heap. So, we need some kind of mutual data structure. In the following design, we have used doubly linked list as a mutual data structure. The doubly linked list contains all input items and indexes of corresponding min and max heap nodes. The nodes of min and max heaps store addresses of nodes of doubly linked list. The root node of min heap stores the address of minimum item in doubly linked list. Similarly, root of max heap stores address of maximum item in doubly linked list. Following are the details of operations.
1) findMax(): We get the address of maximum value node from root of Max Heap. So this is a O(1) operation.
1) findMin(): We get the address of minimum value node from root of Min Heap. So this is a O(1) operation.
3) deleteMin(): We get the address of minimum value node from root of Min Heap. We use this address to find the node in doubly linked list. From the doubly linked list, we get node of Max Heap. We delete node from all three. We can delete a node from doubly linked list in O(1) time. delete() operations for max and min heaps take O(Logn) time.
4) deleteMax(): is similar to deleteMin()
5) Insert() We always insert at the beginning of linked list in O(1) time. Inserting the address in Max and Min Heaps take O(Logn) time. So overall complexity is O(Logn)
6) Delete() We first search the item in Linked List. Once the item is found in O(n) time, we delete it from linked list. Then using the indexes stored in linked list, we delete it from Min Heap and Max Heaps in O(Logn) time. So overall complexity of this operation is O(n). The Delete operation can be optimized to O(Logn) by using a balanced binary search tree instead of doubly linked list as a mutual data structure. Use of balanced binary search will not effect time complexity of other operations as it will act as a mutual data structure like doubly Linked List.
Following is C implementation of the above data structure.
Maximum = 50 Minimum = 10 After deleteMax() Maximum = 40 Minimum = 10 After deleteMin() Maximum = 40 Minimum = 20 After Delete() Maximum = 30 Minimum = 20
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Design a data structure for LRU Cache
- Memory efficient doubly linked list
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Applications of Heap Data Structure
- Leaf starting point in a Binary Heap data structure
- Applications of linked list data structure
- Common operations on various Data Structures
- Introduction to Data Structures | 10 most commonly used Data Structures
- Reduce the array to atmost one element by the given operations
- Complexity analysis of various operations of Binary Min Heap
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- Reverse a Doubly Linked List | Set 4 (Swapping Data)
- Real-time application of Data Structures
- Given a string, find its first non-repeating character
- Convert a given Binary Tree to Doubly Linked List | Set 1
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Print all nodes at distance k from a given node
- Inorder predecessor and successor for a given key in BST