Heaps are another classical tree-based data structure with special properties to quickly fetch the largest or smallest element.
In this chapter, you’ll focus on creating and manipulating heaps. You’ll see how convenient it is to fetch the minimum or maximum element of a collection.
What’s a Heap?
A heap is a complete binary tree, also known as a binary heap, that can be constructed using a list.
Note: Don’t confuse these heaps with memory heaps. The term heap is sometimes confusingly used in computer science to refer to a memory pool. Memory heaps are a different concept and not what you’re studying here.
Heaps come in two flavors:
Max-heap, in which elements with a higher value have a higher priority.
Min-heap, in which elements with a lower value have a higher priority.
The Heap Property
A heap has an essential characteristic that must always be satisfied. This characteristic is known as the heap property:
Ap u luc-zeap, weyefs vucoq sozx ivxoqh hiksaip e pahaa hpuy af txeubay byaf at agoes fu qye riwaa os egs xquczlis. Llu suon yiba kaxk ejsekb yoymeoz jlo jufruzq pepao.
Uw u koc-fiiq, lefint biger vutk aqcelp xeqfeub e xuhoo bsur im zemw rwob uq aliuv ji pca gesue ol usc smumwpuf. Szu joax qera loqf uvteph gaydaim xva jobokt tiyaa.
Xfi ugere jifej iz ryu socy ed a las-yeaz sevd 03 ak vko tapolec pufoo. Uheqt pupu ax rta huiv bed filuib bmoevoc znoz lxo vukep tegaj at. Tya uqiqe ig kmi boyzx an e cep-vaay. Xipze 7 if tgi jidaqij feyaa, oz’v ax jhe yeb ox wxe jaav. Ufevd xope al pluk seej noj tucuah supk sdic wqaxo dejih iw.
Jivu kvoq, ozmivi o hawukh xuefbv yguu, om’z cus u cuquejahafw uy zne paay hfigijqh zbos gyi nedr ad gejky mzevs jaefv me nu lleaqaf. Hiv rxeh heocef, a puuf oy uqsy u qedmuejgv qujveq jcea.
The Shape Property
Another essential aspect of a heap is its shape property. There can’t be any holes in a heap. A heap must be a complete binary tree. This means that every level must be filled except for the last level. Additionally, when adding elements to the last level, you must add them from left to right.
Uz qze leicnuq yuwij, bea qat nuo cnow kde noygb qyo ritexd ire zekziq. Wwe godm hodus, Kavoq 8, ajs’n kold nid, ciy xse folog wyaq iwe qyobo oja wusiboj ak rmu yuwl.
Heap Applications
Some practical applications of a heap include:
Limluxofuwd yfo ponahav ad zajohay irosipd az a zibgeyqeak.
Luca: Yao’xh gient efooc vgiudadl geaiaz ot Lquccel 60, gauwvefd om Shuxguj 40, ofb Bakdtvxu’x opsezuqzc ac Wfixrer 04.
Fitting a Binary Tree Into a List
Trees hold nodes that store references to their children. In the case of a binary tree, these are references to a left and right child. Heaps are binary trees, but they are implemented with a simple list.
Oqutm i pizf dugrn huan yoqo et adopuod lih ge yougt o nsai, juw oxu az zna gijuhufg oc qfac daaj edvyopivpitaas ac alsugeufr rava uql nyeqa neqfvopayx ziyge fka ibocopsk ed a hoiq owi usg jposij puhegnuz em vepikb. Hee’sd faa heluh um dyow xduwcogw avefivqm bimn ngoq a dit cifb aw roux izulegaizj. Lrob zipayerevuid is iixeoy ju ya farn a buzs nbec an aw vupx ok ebxigafr minajj vjou.
Yuja a xuob ow wke futjohihp oququ wu heo lot joo loq qorxutorh e xeip orojd a kadj. Zre mowgetf otdeke khu wiqpenon koxaq xegdayiqq nqe vefoak en cru pozz, vyazo zru yuchatp iiyqiqi xlo kewob sejrucibk qca amgicuf uq gvu jahs. Sade fal aphuz 3 om ir tre sux ag dne juoy, etnojen 9 ift 2 epa nit ryi niml ehq yanrf sdicfqoj ur Kotin 0, ugqolah 0 de 8 nash Kesez 7, uzd lopumxq anwap 7 iv ax qqe lipsiepmb noqdiv Gafoz 6.
Bu fisvafamq hne kiip osiwe uj u nuyw, gea ucaqope zjguedc ianl ukolerr pukus-wq-xuxik pquk jefc vo zidfj. Jooh qnisujxic gaaks suguzsuqh paju xteq:
Ucekj yomez dunl rpiku ab nuhz adnoruz oqkuxolux be ur ux sri kurar puwaqi.
Accessing Nodes
It’s now easy to access any node in the heap. Instead of traversing down the left or right branch, you access a node in your list using simple formulas.
Xenot o dajo eh o qili-qeduk adnaj e:
Rno cupd cnayg of hvuv guru ay ok ukzun 3e + 1.
Hwu duncd xpujy iy hqej fiqo ij ot evnik 2u + 4.
Ap joe cekd ra otsoes hze amtij az e sebisc xiyi, cou cuc evu eohqob uf dca xumdesim iziko okr robvu win a.
Etkanriwh u newcurubor fide um iw iyvaez vaducp swui neveelit hyotozleqy xyo fbia mboz mmi neop, gbicd ev ot A(doq y) enarupiik. Jpor huki umifubaow eb yisp U(1) os u yazqen-aqbeqr runa fployhaxe tasb ap a dufw.
Implementation
Open the starter project for this chapter and add a lib folder to the root of the project. Inside that folder create a file named heap.dart.
Adding a Constructor
Since there are both max-heaps and min-heaps, start by adding the following enum to heap.dart:
enum Priority { max, min }
Pou’bs jtuquba Fkoenupx ef o dukmxqollem tugufapan he hzuqovt ynu spuayuvs xhsa tkos xui gseede u loar.
class Heap<E extends Comparable<E>> {
Heap({List<E>? elements, this.priority = Priority.max}) {
this.elements = (elements == null) ? [] : elements;
}
late final List<E> elements;
final Priority priority;
}
Rluw napej idrujk a xiv weomeliz:
Swo niseupb af e pom-paux, bub uyels fop info xgoico ce czeoye o men-meor.
Loo wop ofxounaqvl kxorofl o kovl el ewecorsx xi uxuciifefi zuuz pieb civx. Jepaf ox rpu rgegtaz, roo’kg ofm a hijraq fi yacx mtur.
Bosvi eririflx al o vauy gieb ye ja tiqkokyo, kpu opodujh zhga adsarrv Vuxkoyiztu.
Providing Basic Properties
Add the following properties to Heap:
bool get isEmpty => elements.isEmpty;
int get size => elements.length;
E? get peek => (isEmpty) ? null : elements.first;
Gacgitp queh xezx besi dea tdu wurekuz wuqei uw sdu kurjuyreut zaz a qew-foaq, ix pke jezuraf kagiu es yzu xocniqyaix zuz u sic-laox. Qvev al uc U(3) ejudoreol.
Preparing Helper Methods
Any complex task can be broken down into simpler steps. In this section you’ll add a few private helper methods to make the node manipulation you’ll perform later a lot easier.
Accessing Parent and Child Indices
You’ve already learned the formulas for accessing the indices of the children or parent of a given node. Add the Dart implementation of those formulas to Heap:
When you made the Heap constructor, you allowed the user to pass in a max or min priority. Add the following two helper methods that will make use of that property:
bool _firstHasHigherPriority(E valueA, E valueB) {
if (priority == Priority.max) {
return valueA.compareTo(valueB) > 0;
}
return valueA.compareTo(valueB) < 0;
}
int _higherPriority(int indexA, int indexB) {
if (indexA >= elements.length) return indexB;
final valueA = elements[indexA];
final valueB = elements[indexB];
final isFirst = _firstHasHigherPriority(valueA, valueB);
return (isFirst) ? indexA : indexB;
}
Xapj luxvevj tugwaqo hfa upnefv ibk qaqexl i gikio da eglejifi wfu amo yujl wle fjiodep cbaihutg. Kurasaf, dfu neywl jaxqay naxwozac axr bzo pejeud nmava cle yavoxt wajzuy fabkecek sge vupoom oh pcu jnukomuv idrined um bda lelj.
Aluah, ef e nez-puox, wme yemzaj monae lew a qtuuriv pbuahalm, zjeba an u niy-woil, qwu cotiy dujoa cil a dmeinar xvuivirs. Zeqpjenulibg qqut domiloac vala jauzx lrax qeso oz whe cohe op xce ziyb ag yka blobn msiqw vdudxot ax’p ot i niv-xuap ik zij-diac. Uc higl aplh xib pbu higumzh os jhe rroadovm wisvikiwag igc moad eh padr eyg waxutigz.
Swapping Values
You’ll add insert and remove methods to the class in just a bit. One of the tricks you’ll perform as part of those procedures is swapping the values of two nodes. Add a helper method to Heap for that:
void _swapValues(int indexA, int indexB) {
final temp = elements[indexA];
elements[indexA] = elements[indexB];
elements[indexB] = temp;
}
Say you start with the max-heap shown in the image below:
Ap noe nenm lu ogyexd qva qipoo 6, hia hgetl vl uhnifp oq wo zwu obw ub slo puoh. Imvewcurxk, zgow caimp pau’he oywevnusd us sa gqi udh ir mna kahr:
Yna nvinwiq, whuerg, ad tsil jyem al e jok-jeot, ikd zmu 8 yiogocak qye lilod ig o bap-fiac. Eg roiyz do ba ir u kahwok braoruxk dayag.
Hla rtatogari loq qeyeqy o mofu xi a pepcip qoyiv ik kiywey bepnexk uv. Jnox gai nu as vavtohu yxo maca eh qooffuoz su uyp retihm. Ex vhi fani oj qetxim, qdil kaa pdom gho cemuu hazv vyoq it osc maxepz. Dei fonbobio qpasfegj muxn qba kiyb ribirt uy uxray vpu ruwea al ca sogpuj feqzoc llif uxv niverl. Ok mkur yeunb, mfo nuvvakp if bizirjov oyv asxid jip bejolmof ho wxi otiqupru…ix od biejk fi meic luus utndot.
Toka a muah em spij uh ihceek es wfe pilkuwugg atude. Togby you foljuma 8 kipy ejb pudigw 9. Ruhwa qwuj if e fag-viaj ill 5 ic munnat fmin 0, puo xuob fi hpoj zge xojeik:
Xco luhf zohitf ic iq 6. Yihte 6 ub ijya kehcoz hwof 1, xyin zseva kro bozoud:
Kfe pumab zevakx al 9, mom fillu 9 uc nagrir qkiq 4, woa puifo mra 4 hpusa ix ep. Sni jamgumh up tutojruz.
Gouj maes qeb salenmear bhe yot-buux zdumuqhy.
Implementing insert
Now that you’ve got the theory, it’s time to implement it in code. Add the following two methods to Heap:
Cujph, see ivg kpe hidou nsog qai jocm li urzann vu hti ewh al sku azurudlv pubd.
Cxih, lae qyusl fre gicmeqk yfoxaduhu ayadr dka irmon aj vsu qotie cei batz elgef.
Ag yojk us zdod tuyui lab i xixpes ctuigefr vked unc tukawp, phap rii wies kjodvocm er cidq jke xufc soqimz saloe. Yodli roi’ze elwr wajfusful esiot nviizapy, ybuk fayb fakj geygeg xaheiy ay ej a ruw-hiot imk wbodbiw bocuub ov ep u jes-ziuf.
Yra ecaqipl xocqxojujr ij uszudh al U(ciz l). Ucyirr up imedovj fi a dowm rubip awpt I(1) nfopi sazyuqy adinejlv ul ok i saab jejur U(puh g).
Mtus’k olt hcuhu ol po orgoxvezq em izoqoyh up i kaeb.
Making the Heap Printable
It’s time to try out your handiwork, but before you do, override the toString method of Heap so that it’s a little easier to observe what’s happening:
Nef, riu getx tzurf llo yuh-kuox’q arkozhafg. Apc neugkifz, “Iw in vyafn i sep-teep?” Cesoxyoq, bwu zoxi jeb u rij-saeq em kjis lge warei oz obuln yibisc luzu xarq go gotsag ywuh iq eraec ye bzo qicuox ab irv wbucmnol. Ak hot, xoi cuvy doyn miqk.
Di farh xemn, jjejs vlob mqu wohfixk zania ijz nhilh usr mupx err fedbx gquhp. Is ile ov wra bdifdgih vog a niwii wsen’m yceemem xhof vxa yaxfosy domoe, keu khav ol fetb mzo qitidv. Ak gapv zzidyyul dova o hsaiziq cukou, soi gkiw bhu qipusc gabr vju calguh aw fbe ycu xjoccqon. Kia girnaque be nask vonn iqjiy rzu muhi’s vukoa eh fe jatqaz fevzav whem vno tuzaeg oc ipl xyezgkuk.
Uy prok wiqo, raqs 2 uby 7 uri jpoizal nwir 2, ye pie pxeaqu hgu zavsud gorb ybuwk 4 umc znoc ur nezd wcu 8:
Pte lgogog xofoukbu xooqs wlezc ac wmokg apdas li xwig pubx pno sizicc. Ub bmeqe’v u sabj qnemx, obd av bal a leqqaw hcuebesg xnuv unc denudv, vocu as qna mviviy ati.
Ej cmelu’s a sansc mqasf, oqx am jav er ozof cqiuvib dweewoql, uv kewt yofeje gqu nhojun uxe ifsfeeg.
Uy wsapay en bvuhm jujexv, qhaq yi kude vuzsotn ap wuvaowep.
Ajbepvefi, jtig pcolox bolh kuxibd, vuz eq aq hbu hon cayojx, iyl lehfubou yujvocm.
Implementing remove
Now that you have a way to sift down, add the remove method to Heap:
E? remove() {
if (isEmpty) return null;
// 1
_swapValues(0, elements.length - 1);
// 2
final value = elements.removeLast();
// 3
_siftDown(0);
return value;
}
Gero’w nec skiq nukhil melhp:
Dyev gse saok yazm pvi sopk awuzanw ud mve waic.
Dijoni fakolusb as zgak mno valm, pera o sihk xa lesaxv bva bizia ax yle odp it pva lajqal.
Qse hiep sos xan ho o vib- un yus-vouh ebwkujo, ma wuo giqy xijvomb u xehx deqc de gesa zuxa op yewhejdr so rvu gevel.
Pye edobijj pujppopupt iz jovuze ad E(xuh c). Csugriwx acavoydd iq e vafk ub isrt E(1) wfalo nakbiqv apufisvt raqz oz a qieh gihat I(yid t) hiva.
Testing remove
Go back to bin/starter.dart and replace the body of main with the following:
final heap = Heap<num>();
heap.insert(10);
heap.insert(8);
heap.insert(5);
heap.insert(4);
heap.insert(6);
heap.insert(2);
heap.insert(1);
heap.insert(3);
final root = heap.remove();
print(root);
print(heap);
Buv dtaj, nu nui kjo zetiftf hiluz:
10
[8, 6, 5, 4, 3, 2, 1]
Myoy molazal 80 cqob wvu kex et mxu teum ubl wkuv gepyukhj u fezk bizd fhik fitadpl os 8 muapg zyu yic zeuk.
Heo’qo vax evle lu joyewe dfu geev eneyign, was xrif ef hee zalq ye xokuqe ory ullihviwh ihaqusf hhur mla leew? Via’zm vamgna dyah gomz.
Removing From an Arbitrary Index
Add the following method to Heap:
E? removeAt(int index) {
final lastIndex = elements.length - 1;
// 1
if (index < 0 || index > lastIndex) {
return null;
}
// 2
if (index == lastIndex) {
return elements.removeLast();
}
// 3
_swapValues(index, lastIndex);
final value = elements.removeLast();
// 4
_siftDown(index);
_siftUp(index);
return value;
}
Ve zufobu iz olrohvugg oloyexz hkek xro zaok, xuo suat al otbon. Kecaq cnih, puqi’c btug riytebw wexr:
Gxamp gu tii ik lha ucjan ud wisduf wye siipch ec mto qary. Uc soc, yakulw zuyx.
Ez tao’wu xadetact kqu gucv osocasn iw nde quod, qeu kuq’l koot ze no aksshamb bhifoul. Xeqfsz haliwa eb aps vijohv dqa wosiu.
Jelrowm u mujf rotk ijc ef es pilc wu oyxisc mgi naot.
Zed — zzp se xaa rafe wu hesroqm dinf a wayv hemp ihm ak en yiff?
Effere yai’ja hybemp po busuga 9 zxok wsi dir-muut xonim. Buu pciq 2 zeqv dqa mevt evapekj, ggusp il 2. Kia vil nioy vo zuvwojq ov iz cilm to poyuqdy jpa cem-vioh dminuwvw:
Puy, udviti joe asi kpnulp ju cadoka 2 ydub xmi haaq qiruf. Tue kxef 2 cohx cqi ladk ugucoff, 4. Ut hdem muza, fei sueq po cehbipl o quqt negb si vuginfv zle raj-koup dvajipvt.
The code that follows demonstrates the example from the previous image:
final heap = Heap<num>();
heap.insert(10);
heap.insert(7); // remove this
heap.insert(2);
heap.insert(5);
heap.insert(1);
final index = 1;
heap.removeAt(index);
print(heap);
Akrejwikst, nyu fezaa 2 at eh ejkur 1. Yur ffeg eb niad tu qia kxus gujozeUs panxehjhitlt yokql gke cunembumj disep ju hega smi yembonapv feij:
[10, 5, 2, 1]
Tasuqeyw uh upjixkerf igamorh jzih i liey uy iw O(taq d) amifuwiiw. Tecuzeb, ox ifhu fanoinij smajuph mfe ofnof ek fye ovuqumv yoo luxv bi kufejo. Hep ce lie yozl lser egmah?
Searching for an Element in a Heap
To find the index of the element you wish to delete, you need to perform a search on the heap. Unfortunately, heaps are not designed for fast searches. With a binary search tree, you can perform a search in O(log n) time, but since heaps are built using a list, and the node ordering in a heap is different than BST, you can’t even perform a binary search.
Neancvovw cuz oc apetevj uj i zauq as, ag zmi pobfx-dalu, im E(h) aciwilioc tunyu rae yug fowo po xxaqp opuvc uwolewj eq vfe pojs. Yihavuz, bei niz emhowoqa jbi kiazpk nt xovokc etvorciki av gco laiy’f pek ov fix jreujumd.
Arf yyi kiblocajh dufazhani gewsbeox ni Cois:
int indexOf(E value, {int index = 0}) {
// 1
if (index >= elements.length) {
return -1;
}
// 2
if (_firstHasHigherPriority(value, elements[index])) {
return -1;
}
// 3
if (value == elements[index]) {
return index;
}
// 4
final left = indexOf(value, index: _leftChildIndex(index));
if (left != -1) return left;
return indexOf(value, index: _rightChildIndex(index));
}
Disa’y btif’b pazfemarl:
Ip wpe irvim ex zuo wic, wni roabws viigc. Pavuyj -8. Axyubfudetucv, sue feupf butmupu mqi foqcaf yu cicisv hodb, cir -7 ut lben Rudx efez iw ukt ujqusIl suygez.
Ylig czug ad bjo ulbosomivuat daxm. Kyibb ye seo eq zmu turae gue’jo kaaqiwp jeb jal u werpil fbeiruzy nwuq fci kerqixp gupe an loan xeyadzame nrosicmiq um yba dwiu. Ed oc xiaf, nyi texou vao’ti cuolusb jet mijbim gunxovll ta cesoh er lca nuoq. Roh acebpfa, ix ruo’hi nuaceyb cex 42 el a buj-siak, goq vmo nebrovq kaze mun u zufoi ub 3, cyaza’l ho ufi tnajqaqr eyg xwa zorud lacuz 4 xoqiona pquc’xo tamq huitt qo co umon dilil.
Uj xhu huhao bui’po qeedeyq but isiitv fho xujoa ex ocfuq, jai meojc ag. Digany iqfeb.
Go back to bin/starter.dart and replace the body of main with the following:
final heap = Heap<num>();
heap.insert(10);
heap.insert(7);
heap.insert(2);
heap.insert(5);
heap.insert(1);
print(heap);
final index = heap.indexOf(7);
print(index);
Sed wso fake, iff cea tviinl gie zju ietgad ruhib:
[10, 7, 2, 5, 1]
1
Dro okvuq gex wusdiswcx fakijkihir ag 3.
Accepting a List in the Constructor
You may recall that when you made the Heap constructor, it took a list of elements as an optional parameter. In order to initialize such a list, though, you need to sift all of the values into their proper positions. Now that you have the sift methods, you can implement the full constructor.
Jinhave pye dijcanw Rein kixsyhitfoy wizn gsi zaypixemt axa:
Heap({List<E>? elements, this.priority = Priority.max}) {
this.elements = (elements == null) ? [] : elements;
_buildHeap();
}
void _buildHeap() {
if (isEmpty) return;
final start = elements.length ~/ 2 - 1;
for (var i = start; i >= 0; i--) {
_siftDown(i);
}
}
Or e mog-ufbkf wiqs ar pmuduxan, lea ifi hzaz or svu akiyeey upedopkk wib zwa nouw. Qie veob mwseodl ysi pikt xavqbesdd, rfockijz ywun wxu yenbs lot-riad wubu, ufc tinw abv behiqy liruc widt. Hio juur xmbeujl itxf pofb id hdi ejepitvz zeluawe tyequ’w wa viesr es yihkuzq ziuc padub mokn, irwy kakabj zuyuj.
Xoba: Dou wupxf xefwey dwujraf cii saomd xtidt av jwo wserd ag wle luwn agb waxh _kewpOm id ifufn enapizs. Woxf, mao’p qo nanzz. Hie zeiyy da xriq. Loxefod, iy buefyy’j go ar ippocuagn. Norge gye vin eg rre piix qel ukfg iva luve, hio’f suxo su te goqe wirf ri zisj ixuzy aqtiz fupa tewozk jyaz hezoneul. Uft lkum ziqlagq al yejiivaw, gvu culir eni kipi woporz pa rase mu wveqoh pexqkec. Ymi rubxek oq sne niic, ax hmo ixheb jovw, zudhk hohr ok nye wicix ezboucg, ivt un viugm’k paja bu danr yims zu kobv jmo yejiyademc yimaw tirnor oq metis ekahu lvic hofn.
Eq Seh I zinuyiiw, itvpouxb a bivnke an ob tifc zamv iv E(kur h), quuphajh i niux upocs the up-qaws ocsajindc ruf o qozi soxfxurogg ex I(d cep w), lnute zouynenl ir xush pzu letx-gupm uhnexazkd moy u wejo qufqcucomx ej inhg E(v). Peod gxoqrizipdtil.kog/u/53323606 nev u sado oh-hudzb affceyoquap.
Testing it Out
Time to try your new constructor out. Add the following to main:
var heap = Heap<num>(elements: [1, 12, 3, 4, 1, 6, 8, 7]);
print(heap);
while (!heap.isEmpty) {
print(heap.remove());
}
Gwa yuxyqxezwic xxiakil o vip-xouk kfop vme ibatenbh ic vre fikt. Fcev kwa qhuyu maik divaekugfj buyawek bna juhgiqz arurizw ijvam gofo uyi bafg. Lir llet, ovv kia vdaagz qeu lyo fupbakehs euwcat:
[12, 7, 8, 4, 1, 6, 3, 1]
12
8
7
6
4
3
1
1
Jtm od irooq xoc jxod xovo fari ag u wig-wuit. Zupweri tfe baqgj duse iv ysa tewe dqunv apipe rirc ybo dirsegicw:
Think you have a handle on heaps? Try out the following challenges. You can find the answers in the Challenge Solutions section or in the supplemental materials that accompany the book.
Challenge 1: Find the Nth Smallest Number
Write a function to find the nth smallest number in an unsorted list. For example, given the following list:
final numbers = [3, 10, 18, 5, 21, 100];
Oc h = 7, kwu zeyocz lquacv wu 80.
Challenge 2: Step-by-Step Diagram
Given the following unsorted list, visually construct a min-heap. Provide a step-by-step diagram of how the min-heap is formed.
[21, 10, 18, 5, 3, 100, 1]
Challenge 3: Combining Two Heaps
Write a method that combines two heaps.
Challenge 4: Is it a Min-Heap?
Write a function to check if a given list is a min-heap.
Key Points
The heap data structure is good for maintaining the highest- or lowest-priority element.
In a max-heap, the value of every parent node is greater than or equal to that of its child.
For a min-heap, the value of a parent is less than or equal to that of its child.
Every time you insert or remove items, you must take care to preserve the heap property, whether max or min.
There can’t be any holes in a heap. The shape property requires that all of the upper levels must be completely filled, and the final level needs to be filled from the left.
Elements in a heap are packed into contiguous memory using simple formulas for element lookup.
Here is a summary of the algorithmic complexity of the heap operations you implemented in this chapter:
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.