grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026+ øg Dõà¬ãó€Rt.ROtRtRtRtRtRtRtRtRt R t R t R t R t R tRtRRRR/RltRRltRRlt^RI5]t]t] t] t] t]R8Xd^RIt]!]P:!44R#R# ]dL9i;i)uxHeap queues [explanation by François Pinard] Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for all k, counting elements from 0. For the sake of comparison, non-existing elements are considered to be infinite. The interesting property of a heap is that a[0] is always its smallest element. The strange invariant above is meant to be an efficient memory representation for a tournament. The numbers below are 'k', not a[k]: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 In the tree above, each cell 'k' is topping '2*k+1' and '2*k+2'. In a usual binary tournament we see in sports, each cell is the winner over the two cells it tops, and we can trace the winner down the tree to see all opponents s/he had. However, in many computer applications of such tournaments, we do not need to trace the history of a winner. To be more memory efficient, when a winner is promoted, we try to replace it by something else at a lower level, and the rule becomes that a cell and the two cells it tops contain three different items, but the top cell "wins" over the two topped cells. If this heap invariant is protected at all time, index 0 is clearly the overall winner. The simplest algorithmic way to remove it and find the "next" winner is to move some loser (let's say cell 30 in the diagram above) into the 0 position, and then percolate this new 0 down the tree, exchanging values, until the invariant is re-established. This is clearly logarithmic on the total number of items in the tree. By iterating over all items, you get an O(n ln n) sort. A nice feature of this sort is that you can efficiently insert new items while the sort is going on, provided that the inserted items are not "better" than the last 0'th element you extracted. This is especially useful in simulation contexts, where the tree holds all incoming events, and the "win" condition means the smallest scheduled time. When an event schedules other events for execution, they are scheduled into the future, so they can easily go into the heap. So, a heap is a good structure for implementing schedulers (this is what I used for my MIDI sequencer :-). Various structures for implementing schedulers have been extensively studied, and heaps are good for this, as they are reasonably speedy, the speed is almost constant, and the worst case is not much different than the average case. However, there are other representations which are more efficient overall, yet the worst cases might be terrible. Heaps are also very useful in big disk sorts. You most probably all know that a big sort implies producing "runs" (which are pre-sorted sequences, whose size is usually related to the amount of CPU memory), followed by a merging passes for these runs, which merging is often very cleverly organised[1]. It is very important that the initial sort produces the longest runs possible. Tournaments are a good way to achieve that. If, using all the memory available to hold a tournament, you replace and percolate items that happen to fit the current run, you'll produce runs which are twice the size of the memory for random input, and much better for input fuzzily ordered. Moreover, if you output the 0'th item on disk and get an input which may not fit in the current tournament (because the value "wins" over the last output value), it cannot fit in the heap, so the size of the heap decreases. The freed memory could be cleverly reused immediately for progressively building a second heap, which grows at exactly the same rate the first heap is melting. When the first heap completely vanishes, you switch heaps and start a new run. Clever and quite effective! In a word, heaps are useful memory structures to know. I use them in a few applications, and I think it is good to keep a 'heap' module around. :-) -------------------- [1] The disk balancing algorithms which are current, nowadays, are more annoying than clever, and this is a consequence of the seeking capabilities of the disks. On devices which cannot seek, like big tape drives, the story was quite different, and one had to be very clever to ensure (far in advance) that each tape movement will be the most effective possible (that is, will best participate at "progressing" the merge). Some tapes were even able to read backwards, and this was also used to avoid the rewinding time. Believe me, real good tape sorts were quite spectacular to watch! From all times, sorting has always been a Great Art! :-) cób€VPV4\V^\V4^, 4R#©éN)ÚappendÚ _siftdownÚlen©ÚheapÚitems&&Ú,/opt/alt/python314/lib64/python3.14/heapq.pyÚheappushr …s"€à‡K�K�ÔÜ ˆd�A”s˜4“y •{Ö#ócól€VP4pV'dV^,pW^&\V^4V#V#©r)ÚpopÚ_siftup©r ÚlasteltÚ returnitems& r ÚheappoprŠs5€à�h‰h‹j€Gß Ø˜!•Wˆ ؈Q‰Ü��aÔØÐØ €Nr có8€V^,pW^&\V^4V#r©r©r r rs&& r Ú heapreplacer”s$€ð�a•€J؈�GÜ ˆD�!ÔØ Ðr cóf€V'd)V^,V8dV^,Tuq^&\V^4V#rrrs&&r Ú heappushpopr¤s/€ç ��Q•˜$”ؘQ� ˆ ˆ�1‰gÜ��aÔØ €Kr cóv€\V4p\\V^,44Fp\W4K R#©éN)rÚreversedÚranger©ÚxÚnÚis& r Úheapifyr%«s+€ä ˆA‹€Aô ”e˜A˜q�D“kÖ "ˆÜ�Ž ó#r cól€VP4pV'dV^,pW^&\V^4V#V#r)rÚ _siftup_maxrs& r Ú heappop_maxr(¶s5€à�h‰h‹j€Gß Ø˜!•Wˆ ؈Q‰Ü�D˜!ÔØÐØ €Nr có8€V^,pW^&\V^4V#r©r'rs&& r Úheapreplace_maxr+Às"€à�a•€J؈�GÜ��aÔØ Ðr cób€VPV4\V^\V4^, 4R#r)rÚ _siftdown_maxrrs&&r Ú heappush_maxr.Çs"€à‡K�K�ÔÜ�$˜œ3˜t›9 Q�;Ö'r cód€V'd(W^,8dV^,Tuq^&\V^4V#rr*rs&&r Úheappushpop_maxr0Ìs-€ç �˜A•w”ؘQ� ˆ ˆ�1‰gÜ�D˜!ÔØ €Kr cóv€\V4p\\V^,44Fp\W4K R#r)rrr r'r!s& r Ú heapify_maxr2Ós*€ä ˆA‹€AÜ ”e˜A˜q�D“kÖ "ˆÜ�AÖó#r cóx€W,pW!8”d(V^, ^, pW,pW58d WPV&TpK,W0V&R#©éN©©r ÚstartposÚposÚnewitemÚ parentposÚparents&&& r rrÝsC€Ø�i€Gð Œ.ؘ1•W •Nˆ Ø•ˆØ Ô Ø�‰I؈CÙ Ø ØˆƒIr có€\V4pTpW,p^V,^,pWR8dCV^,pWb8dW,W,8gTpW,W&Tp^V,^,pKHW@V&\WV4R#r)rr©r r9Úendposr8r:ÚchildposÚrightposs&& r rrsw€Ü �‹Y€FØ€HØ�i€Gà��u�q�y€HØ Ô à˜a•<ˆØ Ô  T¥^°dµnÔ%D؈Hà•Nˆ‰ ؈Ø�S•5˜1•9Šðˆ�IÜ ˆd˜cÖ"r cóx€W,pW!8”d(V^, ^, pW,pWS8d WPV&TpK,W0V&R#r4r6r7s&&& r r-r-&sC€à�i€Gð Œ.ؘ1•W •Nˆ Ø•ˆØ Ô Ø�‰I؈CÙ Ø ØˆƒIr có€\V4pTpW,p^V,^,pWR8dCV^,pWb8dW,W,8gTpW,W&Tp^V,^,pKHW@V&\WV4R#r)rr-r>s&& r r'r'5sw€ä �‹Y€FØ€HØ�i€Gà��u�q�y€HØ Ô à˜a•<ˆØ Ô  T¥^°dµnÔ%D؈Hà•Nˆ‰ ؈Ø�S•5˜1•9Šðˆ�IÜ�$ #Ö&r ÚkeyNÚreverseFc'óB"€.pVPpV'd\p\p\pRpM\p\ p\ p^pVf³\\\V44F(wršV Pp V!V !4W˜,V .4K* V!V4\V4^8”d(V^,;wrÉr½V x€V !4V ^&V!W=4K&V'd%V^,wrÉp V x€V PRjx€L R#\\\V44F0wršV Pp V !4p V!V!V 4W˜,WË.4K2 V!V4\V4^8”d5V^,;wrérËp V x€V !4p V!V 4V ^&WÍ^&V!W=4K3V'd'V^,wrérËV x€V PRjx€L R#R# \dEKqi;i \dT!T4EKXi;iLý \dKîi;i \d T!T4KÌi;iLd5i)r5Néÿÿÿÿ)rr2r(r+r%rrÚ enumerateÚmapÚiterÚ__next__Ú StopIterationrÚ__self__)rDrEÚ iterablesÚhÚh_appendÚ_heapifyÚ_heappopÚ _heapreplaceÚ directionÚorderÚitÚnextÚvalueÚsÚ key_values$$* r Úmerger[Jsé€ð$ €AØ�x‰x€Hç܈܈Ü&ˆ ؉ äˆÜˆÜ"ˆ ؈ à ‚{Ü"¤3¤t¨YÓ#7Ö8‰IˆEð Ø—{‘{�Ù™$›& %Õ"3°TÐ:Ö;ñ9ñ �Œ Ü�!‹f�qŒjð ØØ-.¨q­TÐ1Ñ&�E $Ø’KÙ›6�A�a‘DÙ  Ö&÷ à!" 1¥Ñ ˆE˜$ØŠKØ—}‘}× $Ð $Ùäœs¤4¨Ó3Ö4‰ ˆð Ø—;‘;ˆDÙ“FˆEÙ ‘c˜%“j %Õ"3°UÐAÖ Bñ 5ñ ˆQ„KÜ ˆa‹&�1Œ*ð ØØ45°aµDÐ8Ñ-�  %°Ø’ Ù›�Ù˜5“z��!‘Ø�!‘Ù˜QÖ"÷ Ø()¨!­Ñ%ˆ ˜%ØŠ Ø—=‘=× Ò ñ øôM!ô Ûð ûô!ô Ù˜— � ð úñ %øôô Ú ð ûôô Ù �Q�Kð úñ !ùsº‚A"HÁ%"GÂHÂ$&Gà HÃHÃ1G/Ã2$HÄ*G1ÅHÅ3HÆHÆHÆ8HÆ9HÇ GÇ HÇGÇHÇG,Ç'HÇ+G,Ç,HÇ1 HÇ<HÇ?HÈHÈHÈHÈHÈHcó€V^8Xd-\V4p\4p\W4VR7pWTJd.#V.#\V4pW8¼d\ WR7RV#Vf·\V4p\\V4V4UUu.uFwrxW‡3NK pppV'gV#\V4V^,^,p Tp \p VF)pW‰8gK V !WXV 34V^,wrœV ^, p K+ VP4VUU u.uFwrŠVNK up p#\V4p\\V4V4UUu.uFwrxV!V4Wx3NK pppV'gV#\V4V^,^,p Tp \p VF2pV!V4p WÙ8gKV !W]W¨34V^,wrœpV ^, p K4 VP4VU U Uu.uFwrÚqˆNK upp p # \ \ 3dEL–i;iuuppiuup piuuppiuupp p i)r5©ÚdefaultrD)rDN) rJÚobjectÚminrÚsortedÚ TypeErrorÚAttributeErrorÚzipr r2r+Úsort©r#ÚiterablerDrVÚsentinelÚresultÚsizer$ÚelemÚtoprUrSÚ_orderÚkÚ_elems&&& r Ú nsmallestrpÝs€ð ˆA„vÜ �(‹^ˆÜ“8ˆÜ�R¨sÔ3ˆØÓ'ˆrÐ5¨f¨XÐ5ð1Ü�8‹}ˆð Œ9ܘ(Ô,¨R¨aÐ0Ð 0ð ð ‚{Ü �(‹^ˆô,/¬u°Q«x¸Ô+<Ô=Ñ+<¡ �4“)Ñ+<ˆÑ=ߨˆMÜ�FÔØ�Q�i˜�lˆØˆÜ&ˆ ÛˆDØŽzÙ˜V¨E ]Ô3Ø$ Q�i‘ �ؘ• ’ñ ð � ‰ Œ Ù*0Ô1©&™˜$“©&Ò1Ð1ô ˆh‹€BÜ25´e¸A³hÀÔ2CÔ DÑ2C¡w q‰s�4‹y˜!Ó"Ñ2C€FÑ Dß Øˆ Ü�ÔØ ��)�A�,€CØ €EÜ"€LÛˆÙ �‹IˆØ Ž7Ù ˜ UÐ!1Ô 2Ø!'¨¥Ñ ˆC˜Ø �Q�JŠEñ ð  ‡K�K„MÙ)/Õ 0©Ñ%�a ŠD©Ó 0Ð0øôU ”~Ð &ô Ú ð üó>ùó2ùóEùô 1s)µ GÁ=G0Ã? G6Ä3G<Ç HÇG-Ç,G-có,€V^8Xd-\V4p\4p\W4VR7pWTJd.#V.#\V4pW8¼d\ WRR7RV#Vf½\V4p\\^V)R4V4UUu.uFwrxW‡3NK pppV'gV#\V4V^,^,p V)p \p VF)pW˜8gK V !WXV 34V^,wrœV ^,p K+ VPRR7VUU u.uFwrŠVNK up p#\V4p\\^V)R4V4UUu.uFwrxV!V4Wx3NK pppV'gV#\V4V^,^,p V)p \p VF2pV!V4p W�8gKV !W]W¨34V^,wrœpV ^,p K4 VPRR7VU U Uu.uFwrÚqˆNK upp p # \ \ 3dEL¢i;iuuppiuup piuuppiuupp p i)r5r]T)rDrEN)rErG) rJr_Úmaxrrarbrcrdr r%rrerfs&&& r Únlargestrss€ð ˆA„vÜ �(‹^ˆÜ“8ˆÜ�R¨sÔ3ˆØÓ'ˆrÐ5¨f¨XÐ5ð?Ü�8‹}ˆð Œ9ܘ(°TÔ:¸2¸AÐ>Ð >ð ð ‚{Ü �(‹^ˆÜ+.¬u°Q¸¸¸BÓ/?ÀÔ+DÔEÑ+D¡ �4“)Ñ+DˆÑEߨˆMÜ�ŒØ�Q�i˜�lˆØ�ˆÜ"ˆ ÛˆDØŽzÙ˜V¨E ]Ô3Ø$ Q�i‘ �ؘ• ’ñ ð � ‰ ˜Dˆ Ô!Ù*0Ô1©&™˜$“©&Ò1Ð1ô ˆh‹€BÜ25´e¸AÀ¸rÀ2Ó6FÈÔ2KÔ LÑ2K¡w q‰s�4‹y˜!Ó"Ñ2K€FÑ Lß Øˆ Ü ˆF„OØ ��)�A�,€CØ ˆB€EÜ€LÛˆÙ �‹IˆØ Ž7Ù ˜ UÐ!1Ô 2Ø!'¨¥Ñ ˆC˜Ø �Q�JŠEñ ð  ‡K�K˜€KÔÙ)/Õ 0©Ñ%�a ŠD©Ó 0Ð0øôQ ”~Ð &ô Ú ð üóFùó2ùóMùô 1s)µ G%ÂG=Ä HÄ=H Ç HÇ%G:Ç9G:)Ú*Ú__main__) r rr%rrr.r(r2r+r0rsrpr[)N)Ú __about__Ú__all__r rrrr%r(r+r.r0r2rrr-r'r[rprsÚ_heapqÚ ImportErrorÚ _heappop_maxÚ_heapreplace_maxÚ _heappush_maxÚ_heappushpop_maxÚ _heapify_maxÚ__name__ÚdoctestÚprintÚtestmodr6r r ÚrƒsÙððF\ € ò| @€ò$ò òò ò òòò(ò òò òj#ò( ò'ð*N!˜$ðN!¨ôN!ôf:1ôx81ðv Üð € Ø"ÐØ€ Ø"ÐØ€ à ˆzÔãÙ ˆ'�/Š/Ó Öñøðô Ùð úsÁA:Á:BÂB