[Data structure] PriorityQueue and heap

Whenthereisanincomingcallonthemobilephone,themobilephonewillalwaysgiveprioritytotheincomingcall.Inthiscase,aqueuewithpriorityisused.Inadatastructure,astructurethatcanaddnewobjectsandreturnthehighestpriorityobjectiscalledapriorityqueue.TheheapandPriorityQueueareapriorityqueue. Heap Whatisaheap Aheapisacompletebinarytree,andthevalueofeachnodeisalwaysnogreaterthanornolessthanthevalueofitsparentnode.Aheapthatisgreaterthanitsparentnodeiscalledabigrootheap,andaheapthatislessthanitsparentnodeiscalledaheap.Smallrootpile. Heapstoragemethod Becausetheheapisacompletebinarytree,itcanbestoredinthearrayinasequentialmanneraccordingtotheorderoflayer-ordertraversal(ifitisnotacompletebinarytree,emptynodesmustbestoredinthespace,whichwillleadtorelativelylowspaceutilization). Thecompletebinaryisstoredinthearrayinorder.Thesubscriptoftheleftchildnodeofnodeiis2*i+1,thesubscriptoftherightchildnodeis2*i+2,andthesubscriptoftheparentnodeis(i-1)/2. Thefollowingisthestoragestructureofthesimulatedheap: publicclassHeap{ //arraytostoreheap privateint[]elem; //Thenumberofvalidelementsintheheap privateintusedSize; //Defaultsizeofarray privatestaticfinalintDEFAULT_SIZE=10; //Constructorinitializesthearray publicHeap(){ elem=newint[DEFAULT_SIZE]; } } Creationofheap Aheapisaspecialcompletebinarytree.Wecancreateaheapbyadjustingdownwardanordinarybinarytree.Herewetakecreatingasmallrootheapasanexample. Thepicturebelowisaschematicdiagramofadownwardadjustment: Toconvertanordinarybinarytreeintoaheaprequiresmultipledownwardadjustments.Thespecificstepsareasfollows: Twolabels,parentandchild,arerequired.Letparentmarkthenodethatneedstobeadjusted,andchildmarktheleftchildofparent(ifparenthasachild,itmusthavealeftchildfirst).Ifparenthasaleftchild,performthefollowingoperations: 1.Determinewhethertheparent’srightchildexists.Ifitexists,letthechildmarkthesmallestnodeamongtheleftandrightchildnodes. 2.Compareparentandchild.Ifparentissmallerthanchild,theadjustmentends;ifparentislargerthanchild,swapparentandchild. 3.Aftertheexchangeiscompleted,thelargeelementsintheparentmovedownward,whichmaycausethesubtreetonotmeetthepropertiesoftheheap,soitneedstocontinuetoadjustdownward,thatis,parent=child;child=parent*2+1;andthencontinuewith1,2,3steps. //Buildaheap publicvoidcreateHeap(){ //Findthefirstnon-leafnodefromthelast,startfromthenodepositionandgoforwardtotherootnode.Whenencounteringanode,applydownwardadjustment for(intparent=(usedSize-1-1)/2;parent>=0;parent–){ //Adjustdownward shiftDown(parent,usedSize); } } //Adjustdownward publicvoidshiftDown(intparent,intlen){ //childfirstmarkstheleftchildoftheparent,becausetheparentmayhavealeftbutnotarightchild intchild=parent*2+1; while(child<len){ //Iftherightchildexists,findthesmallerchildamongtheleftandrightchildrenandmarkitwithchild if(child+1<len&&elem[child]>elem[child+1]){ child++; } if(elem[parent]>elem[child]){ //Theparentnodeislargerthanitssmallestchildnode //Swapparentnodewithsmallerchildnode inttmp=elem[child]; elem[child]=elem[parent]; elem[parent]=tmp; //Movinglargeelementsintheparentdownwardmaycausethesubtreetonotmeetthepropertiesoftheheap,soitneedstocontinuetobeadjusteddownward. parent=child; child=2*parent+1; }else{ […]

Data structure – sequence table

Article directory 1. Linear table 2. Sequence table 2.1 Concept and structure 2.2 Interface implementation (1) Sequence table initialization (2)Sequence table destruction (3) Sequence table expansion (4) Tail insertion data (5)Head plug data (6) Insert x before the data with subscript pos (7) Tail deleted data (8) Header deletion data (9) Delete the data with […]

Lesson 21 Data storage & structure in memory

Storage of floating point numbers in memory How floating point numbers are stored According to the IEEE 754 standard, floating point numbers can be written as: (-1)^S *M*2 ^E. Just store S, M, and E in the memory. S, M, and E are all binary numbers, so when storing decimal floating-point numbers, they need to […]

Binary tree sequential storage structure

Table of Contents 1. Binary tree sequential storage structure 2. Concept and structure of heap 3. Heap related interface implementation 3.1 Heap insertion and upward adjustment algorithm 3.1.1 Upward adjustment algorithm 3.1.2 Heap insertion 3.2 Heap deletion and downward adjustment algorithm 3.2.1 Downward adjustment algorithm 3.2.2 Heap deletion 3.3 Other interfaces and code implementations 4. […]