Heap and Heap Sort algorithm in Hindi


Heap data structure एक बहुत ही important और powerful tool है, जो विभिन्न algorithms में use होता है, जैसे कि priority queues, sorting algorithms, और graph algorithms। इस blog में हम heap और heap tree को detail में समझेंगे, साथ ही इसके advantages, disadvantages, और applications को भी discuss करेंगे।

 

What is a Heap? (Heap क्या है?)

 

Heap एक complete binary tree है, जो एक specific order को follow करता है। Heaps दो प्रकार के होते हैं: Max Heap और Min Heap.

 

  1. Max Heap:

    1. Max Heap में हर parent node की value उसके child nodes से बड़ी होती है।

    2. Root node की value हमेशा tree की सबसे बड़ी value होती है।

 

Example (Max Heap):

         50

         /  \

      30    20

      /  \      / \

   10 15   5   7

 

2. Min Heap:

  1. Min Heap में हर parent node की value उसके child nodes से छोटी होती है।

  2. Root node की value हमेशा tree की सबसे छोटी value होती है।

 

Example (Min Heap):

 

       5

      /  \

   15    20

   /\         /\

30 40   50 70


 

Heap tree हमेशा complete binary tree होता है, जिसका मतलब है कि tree के सभी levels पूरी तरह भरे होते हैं, सिवाय last level के, जो left से fill होता है।

 

Heap Operations (Heap के Operations)

 

1. Insertion in Heap (Heap में Insertion)

Heap में element को insert करने के लिए, उसे सबसे last available position में रखा जाता है। इसके बाद heapify operation के द्वारा उसे सही position पर लाया जाता है ताकि heap की properties maintained रहें। Insertion के बाद element को ऊपर की तरफ adjust किया जाता है (up-heapify).

 

Steps for Insertion:

  1. Element को tree के last node पर insert करें।

  2. Compare the inserted node with its parent.

  3. अगर parent से छोटा (min heap) या बड़ा (max heap) है, तो उसे swap करें।

  4. Repeat this process until the correct position is found.

 

Example (Max Heap):

 

Insert 60 in Max Heap:

       50

      /  \

    30    20

   /  \      / \

  10  15 5   7

 

After inserting 60:

       60

      /  \

    50    20

   /  \      / \

  30  15 5   7

 / 

10



 

2. Deletion in Heap (Heap में Deletion)

Heap में हमेशा root node (जिसकी value सबसे बड़ी या सबसे छोटी होती है) को delete किया जाता है। Deletion के बाद last node को root पर place किया जाता है, फिर down-heapify operation किया जाता है ताकि heap की properties maintained रहें।

 

Steps for Deletion:

  1. Root node को delete करें।

  2. Last node को root पर shift करें।

  3. Root से शुरू करके, node को अपने children से compare करें।

  4. अगर heap property टूट रही है, तो उसे children के साथ swap करें।

  5. Repeat this process until the node is in the correct position.

 

Example (Max Heap Deletion):

 

Delete 60 from Max Heap:

       60

      /  \

    50    20

   /  \      / \

  30  15 5   7

 / 

10

 

After deletion and heapify:

       50

       /  \

    30    20

   /  \       / \

  10  15  5   7


 

3. Heapify

Heapify operation का उपयोग heap की properties को maintain करने के लिए किया जाता है। Insertion के बाद, new node को up-heapify किया जाता है, और deletion के बाद last node को down-heapify किया जाता है।

Heapify में सबसे बड़ा (Max Heap) या सबसे छोटा (Min Heap) element को root की ओर लाने का काम किया जाता है, ताकि tree का structure सही रहे।

 

Types of Heap (Heap के प्रकार)

 

1. Max Heap

Max Heap में, हर parent node का value उसके child nodes से बड़ी होती है। Root node की value सबसे बड़ी होती है।

 

2. Min Heap

Min Heap में, हर parent node का value उसके child nodes से छोटी होती है। Root node की value सबसे छोटी होती है।

 

Heap Sort Algorithm

Heap sort एक comparison-based sorting algorithm है, जो heap data structure का उपयोग करता है। इसे O(n log n) complexity में किया जाता है, जो इसे efficient बनाता है।

 

Steps of Heap Sort:

  1. Build a Max Heap: Input array को Max Heap में convert करें।

  2. Swap root with last element: Root (largest element) को array के last element के साथ swap करें।

  3. Heapify: Remaining heap को heapify करें।

  4. Repeat: जब तक सभी elements sorted न हो जाएं, step 2 और 3 को repeat करें।

 

Applications of Heap (Heap के उपयोग)

 

1. Priority Queue:

   Heaps को priority queue implement करने के लिए use किया जाता है, जिसमें हर element को एक priority दी जाती है। Max Heap में, highest priority element root पर होता है और Min Heap में, lowest priority element root पर होता है।

   

2. Heap Sort:

   Heap sort एक efficient sorting algorithm है, जो heap की properties का use करके elements को ascending या descending order में sort करता है।

 

3. Graph Algorithms:

   Dijkstra’s और Prim’s जैसे graph algorithms में heap का उपयोग shortest path या minimum spanning tree find करने के लिए किया जाता है।

 

4. Scheduling Systems:

   Heaps को CPU scheduling systems और job scheduling algorithms में use किया जाता है, जहाँ tasks को उनके priorities के अनुसार schedule किया जाता है।

 

Advantages of Heap (Heap के फायदे)

 

1. Efficient Access to Max/Min Element:  

   Heap में max या min element तक पहुंचना बहुत efficient होता है क्योंकि वह हमेशा root node पर होता है। Searching time complexity O(1) होती है।

   

2. Efficient Insertion and Deletion:  

   Heap में insertion और deletion logarithmic time complexity (O(log n)) में होती है, क्योंकि heapify operation हमेशा balanced binary tree पर perform होता है।

 

3. Priority Queue:  

   Heap, priority queue को efficiently implement करता है, जहाँ हमें highest या lowest priority element को access और remove करना होता है।

 

Disadvantages of Heap (Heap के नुकसान)

 

1. Linear Search:  

   Heap में किसी arbitrary element को search करना inefficient होता है, क्योंकि हमें O(n) time complexity में पूरा heap traversal करना पड़ता है।

   

2. Not Ideal for Ordered Data:  

   Heap data structure ordered data को efficiently store नहीं कर सकता, क्योंकि यह सिर्फ root element को maintain करता है। अगर हमें sorted order में elements चाहिए, तो हमें additional sorting algorithm implement करना पड़ता है।

 

3. Space Complexity:  

   Heap में एक extra array या list की जरूरत होती है, जिससे space complexity slightly बढ़ जाती है।


 

Conclusion

 

Heap algorithm एक versatile और efficient data structure है, जो कई complex problems को हल करने में मदद करता है। चाहे priority queue implement करनी हो, sorting करनी हो, या graph-based problems को solve करना हो, heap algorithm हमेशा एक महत्वपूर्ण भूमिका निभाता है। Heapify operations की वजह से insertion और deletion logarithmic time complexity में किए जा सकते हैं, जिससे यह large datasets पर भी efficient रहता है।

 

Heap के कई advantages हैं, जैसे quick access to max/min elements, लेकिन linear search जैसी कुछ limitations भी हैं। फिर भी, heap algorithm विभिन्न applications में एक महत्वपूर्ण role निभाता है, जो इसे एक must-know data structure बनाता है।