Which data structure is the best for implementing a priority queue?
2023
Which data structure is the best for implementing a priority queue?
- A.
Linked list
- B.
Array
- C.
Binary heap
- D.
B-Tree
- E.
None of the above
Attempted by 279 students.
Show answer & explanation
Correct answer: C
A priority queue requires efficient insertion and extraction of the highest (or lowest) priority element. The binary heap data structure is ideal because it maintains the heap property, ensuring the highest priority element is always at the root. Insertion and deletion operations take O(log n) time, which is optimal for dynamic priority queues.
Linked lists allow O(1) insertion but require O(n) time to find the correct position for maintaining order, making them inefficient. Arrays require O(n) time for insertion and deletion to maintain sorted order, and sorting after each operation leads to O(n log n) complexity. Therefore, binary heap is the best choice.
हिन्दी उत्तर: प्राथमिकता भंडारण के लिए डेटा संरचना को तेजी से डेटा डालने और निकालने की क्षमता चाहिए। बाइनरी हीप डेटा संरचना इसके लिए उपयुक्त है क्योंकि यह हीप गुणधर्म को बनाए रखती है और उच्चतम प्राथमिकता वाले तत्व को हमेशा जड़ पर रखती है। डालने और हटाने की ऑपरेशन O(log n) समय में होती है, जो गतिशील प्राथमिकता भंडारण के लिए आदर्श है।
लिंक्ड लिस्ट में डालने के लिए O(1) समय लगता है, लेकिन क्रम को बनाए रखने के लिए सही स्थान ढूंढने में O(n) समय लगता है, जिससे यह अक्षम हो जाती है। एरे में डालने और हटाने के लिए O(n) समय लगता है और क्रम को बनाए रखने के लिए व्यवस्था करने के बाद प्रत्येक ऑपरेशन में O(n log n) समय लगता है। इसलिए, बाइनरी हीप सबसे अच्छा विकल्प है।