94 94 votes A data structure is required for storing a set of integers such that each of the following operations can be done in $O(\log n)$ time, where $n$ is the number of elements in the set. Deletion of the smallest element Insertion of an element if it is not already present in the set Which of the following data structures can be used for this purpose? A heap can be used but not a balanced binary search tree A balanced binary search tree can be used but not a heap Both balanced binary search tree and heap can be used Neither balanced search tree nor heap can be used Data Structures gatecse-2003 data-structures easy isro2009 binary-search-tree + – Kathleen 31.7k views answer comment Share Follow Print See all 6 Comments 6 6 Comments reply Show 3 previous comments shashankrustagi commented Dec 20, 2020 reply Follow flag heap will give you minimum in O(logn) but it will take O(n) time for build heap after deleting the minimum and fixing the heap property and almost completeness of the Heap. But AVL tree in any case, will give you log(n) time complexity 1 1 replyShare Vaibdoesit commented Jul 17, 2025 reply Follow flag shashank bro you are wrong, deletin will take logn tie because we swp last element with first element if it is min heap ad then apply min heapify- total logn time. Since they have not spefied min or ma we can use any and we do get loogn for min heap, but woud get n for max hep.. 0 0 replyShare DocumentingMyLife360 commented Aug 8 reply Follow flag here Avl tree also possible ryt ? , correct & Tag me if i'm wrong 0 0 replyShare Please log in or register to add a comment.
Best answer 141 141 votes Balanced search tree have height $\log n$ Deletion of smallest element will take $O(\log n)$ time Finding a element is present/not and doing insertion: $O(\log n)$ Heap(MIN) is also an almost complete binary tree have height $\log n$ Deletion of smallest element will take $O(\log n)$ time (root element removal, replace with last element +balancing) Finding a element is present/not and insertion: Finding only takes $O(n)$, insertion then balancing take $O(\log n)$. So, total $O(n)+O(\log n)=O(n).$ Answer is B. (even if its maxheap our ans does not change only time for deletion of min will increase $O(n)$) Anurag_s answered Dec 30, 2015 • edited Jun 13, 2018 by Milicevic3306 Anurag_s comment Share Follow See all 15 Comments 15 15 Comments reply Show 12 previous comments Amcodes commented Jan 17, 2021 reply Follow flag What if tree becomes unbalanced after Insertion. By balanced tree they mean height balanced or weight balanced? Bcoz Complexity to balance the tree again after Insertion Might Vary! 0 0 replyShare Abhrajyoti00 commented Nov 3, 2022 reply Follow flag The only thing to notice here is that searching an element in Heap is O(N). That’s why Insertion of an element if it is not already present in the set takes O(N). Reference: algorithm - Search an element in a heap - Stack Overflow 4 4 replyShare Sumeit Havinnal commented Jun 11 i edited by Sumeit Havinnal Jun 13 reply Follow flag Insertion of element in the heap will take $O (logn)$ if we already know that the element which we want to insert is NOT present in the set (heap) because we don't need to search in this case and searching the element takes $O (n)$ 0 0 replyShare Please log in or register to add a comment.
34 34 votes Insertion of an element if it is not already present in the set Means first search, then insert if needed. Balanced Binary Search Tree: Deletion of the smallest element: $O(logn)$ Search: $O(logn)$ insert: $O(logn)$ It qualifies. Heap Deletion of the smallest element: $O(logn)$ Search: $O(n)$ Insert: $O(logn)$ It doesn't qualify. Option B Reasons for the time complexities:- Balanced Binary Search Tree: The smallest element will ALWAYS be the last left child of the root. ie, the leftmost leaf. Height of a balanced BST is $O(logn)$ hence to reach the node with the smallest value, we need $O(logn)$ time. To search an element, at any point we'll have only one path to go. The length of the path = height of the tree = $O(logn)$. To insert an element, we first need to find its right place in the BBST. Again, same as above. $O(logn)$ Heap If we employ a min heap, the smallest element is the root node. To delete the root, first switch the last indexed element and the root. Then delete the last indexed element (which is the former root) Then call heapify. So, $O(logn)$ A random value in a heap could be anywhere. All we know is that the nth largest value in a max-heap would be in the first n levels. Even that's not enough information. Could be anywhere, so go through all the elements, hence $O(n)$ To insert, insert after the current-last index; then call heapify. So, $O(logn)$ JashanArora answered Dec 6, 2019 JashanArora comment Share Follow See all 4 Comments 4 4 Comments reply Amcodes commented Jan 17, 2021 reply Follow flag What if tree becomes unbalanced after Insertion. By balanced tree they mean height balanced or weight balanced? Bcoz Complexity to balance the tree again after Insertion Might Vary! 0 0 replyShare anon1 commented Mar 19, 2022 reply Follow flag @Amcodes What if tree becomes unbalanced after Insertion If you insert one element in a balanced binary search tree, height will remain O(logn). 0 0 replyShare Pranavpurkar commented Jul 12, 2022 reply Follow flag Amcodes some operations are performed after each and every insertion to keep the tree balanced! 0 0 replyShare Shikhar. commented Nov 17, 2023 reply Follow flag Great Answer 0 0 replyShare Please log in or register to add a comment.
14 14 votes option B balanced binary search tree to find if the element is present it takes O(log n) then if not just insert it takes O(log n). deleting an element takes O(log n) ,so balanced BST is one of our viable data structure now lets move on to heap they have not made a sound whether its max heap or min heap so lets be general .. finding an element if its already present it takes O(n) suppose the element we are searching for is at the lowest level . then we compare our element with root and find out thats its not that element we are searching for ,then we are standing at cross roads whether to go the left subtree or right subtree .so we need to go to both of the subtrees as we dont have clues to which subtree which belongs .. so no need to go further just declare balanced BST is the only possible data structure we are in need of ......... Bhagirathi answered Jan 20, 2016 Bhagirathi comment Share Follow See all 2 Comments 2 2 Comments reply Chetnawadhwa commented Dec 1, 2016 reply Follow flag I knw answer is b.. But my doubt is what will be the complexity of deletion of smallest element in a balanced binary search tree? O(lg n) or O(1)..? I think it can be done in O(1).. AS WE just need to find the leftmost elment as it will be smallest.. 1 1 replyShare rhl commented Sep 27, 2021 reply Follow flag what will be the complexity of deletion of smallest element in a balanced binary search tree? It will be O(logn) because we don’t know how much left we need to go. we may find it just left to root node or maybe left of left of left of …. root node. 1 1 replyShare Please log in or register to add a comment.
0 0 votes Its not specified whether the heap is min or max . If min heap then after deleting we need to adjust the tree hence takes logn bt incase it's max heap no need to take adjustment as min element is on the leaves level , it's then O(1). option C Manali Sikdar answered Dec 22, 2014 Manali Sikdar comment Share Follow See all 10 Comments 10 10 Comments reply Show 7 previous comments Arjun commented May 6, 2015 reply Follow flag Yes. You are correct. But not just the "max element" cause problem. All the elements in the last level can cause this problem. So, only binary search tree is the answer here. 2 2 replyShare User007 commented Apr 25, 2017 reply Follow flag Sir, no doubt regarding O(n) time for finding smallest element in Max Heap but how do we implement it using program? Please help with explanation, program not needed. Thank you. 0 0 replyShare Tesla! commented Oct 31, 2017 reply Follow flag Heap is not possible because for insertion we need to check element is present or not so it will take O(n) time 2 2 replyShare Please log in or register to add a comment.