lecture 18 heaps and priority queues - visionlab.engr.ccny...
TRANSCRIPT
CSC212 Data Structure
- Section FG
Lecture18HeapsandPriorityQueues
Instructor:FengHUDepartmentofComputerScience
CityCollegeofNewYork
Heaps
•Chapter11hasseveralprogrammingprojects,includingaprojectthatusesheaps.
• Thispresentationshowsyouwhataheapis,anddemonstratestwooftheimportantheapalgorithms.
Data Structuresand Other ObjectsUsing C++
Topics
•HeapDefinition•HeapApplications
• priorityqueues(chapter8),sorting(chapter13)• TwoHeapOperations– add,remove
• reheapificationupwardanddownward• whyisaheapgoodforimplementingapriorityqueue?
•HeapImplementation• usingbinary_tree_nodeclass• usingfixedsizeordynamicarrays
HeapsDefinition
Aheapisacertainkindofcompletebinarytree.
Heaps
Aheap isacertainkindofcompletebinarytree.
When a completebinary tree is built,
its first node must bethe root.
Root
Heaps
Completebinarytree. Left childof theroot
The second node isalways the left child
of the root.
Heaps
Completebinarytree. Right childof the
root
The third node isalways the right child
of the root.
Heaps
Completebinarytree.
The next nodesalways fill the next
level from left-to-right.
Heaps
Completebinarytree.
The next nodesalways fill the next
level from left-to-right.
Heaps
Completebinarytree.
The next nodesalways fill the next
level from left-to-right.
Heaps
Completebinarytree.
The next nodesalways fill the next
level from left-to-right.
Heaps
Completebinarytree.
Heaps
Aheapisacertainkindofcompletebinarytree.
Each node in a heapcontains a key thatcan be compared toother nodes' keys.
19
4222127
23
45
35
Heaps
Aheapisacertainkindofcompletebinarytree.
The "heap property"requires that each
node's key is >= thekeys of its children
19
4222127
23
45
35
Whatitisnot:ItisnotaBST
• Inabinarysearchtree,theentriesofthenodescanbecomparedwithastrictweakordering.Tworulesarefollowedforeverynoden:
• TheentryinnodenisNEVERlessthan anentryinitsleftsubtree• Theentryinthenodenislessthan everyentryinitsright subtree.
• BSTisnotnecessarilyacompletetree
Whatitis:HeapDefinition
• Aheapisabinarytreewheretheentriesofthenodescanbecomparedwiththelessthan operatorofastrictweakordering.Inaddition,tworulesarefollowed:
• TheentrycontainedbythenodeisNEVERlessthan theentriesofthenode’schildren
• ThetreeisaCOMPLETEtree.
• Q:whereisthelargestentry?à forwhat....
Application:PriorityQueues
• Apriorityqueueisacontainerclassthatallowsentriestoberetrievedaccordingtosomespecificprioritylevels
• Thehighestpriorityentryisremoved first• Ifthereareseveralentrieswithequallyhighpriorities, thenthepriorityqueue’s implementationdetermineswhichwillcomeoutfirst(e.g.FIFO)
• Heapissuitableforapriorityqueue
ThePriorityQueueADTwithHeaps
• Theentrywiththehighestpriorityisalwaysattherootnode
• Focusontwopriorityqueueoperations• addinganewentry• removetheentrywiththehighestpriority
• Inbothcases,wemustensurethetreestructureremainstobeaheap• wearegoingtoworkonaconceptualheapwithoutworryingaboutthepreciseimplementation
• laterIamgoingtoshowyouhowtoimplement...
AddingaNodetoaHeap
❶ Putthenewnodeinthenextavailablespot.
❷ Pushthenewnodeupward,swappingwithitsparentuntilthenewnodereachesanacceptablelocation.
19
4222127
23
45
35
42
AddingaNodetoaHeap
❶ Putthenewnodeinthenextavailablespot.
❷ Pushthenewnodeupward,swappingwithitsparentuntilthenewnodereachesanacceptablelocation.
19
4222142
23
45
35
27
AddingaNodetoaHeap
❶ Putthenewnodeinthenextavailablespot.
❷ Pushthenewnodeupward,swappingwithitsparentuntilthenewnodereachesanacceptablelocation.
19
4222135
23
45
42
27
AddingaNodetoaHeap
✔Theparenthasakeythatis>=newnode,or
✔Thenodereachestheroot.➚ Theprocessofpushingthe
newnodeupwardiscalledreheapificationupward. 19
4222135
23
45
42
27
Note: Note: we need to easily go from child to parent as well as parent to child.
RemovingtheTopofaHeap
❶Movethelastnodeontotheroot.
19
4222135
23
45
42
27
RemovingtheTopofaHeap
❶Movethelastnodeontotheroot.
19
4222135
23
27
42
RemovingtheTopofaHeap
❶Movethelastnodeontotheroot.
❷ Pushtheout-of-placenodedownward,swappingwithitslargerchilduntilthenewnodereachesanacceptablelocation. 19
4222135
23
27
42
RemovingtheTopofaHeap
❶Movethelastnodeontotheroot.
❷ Pushtheout-of-placenodedownward,swappingwithitslargerchilduntilthenewnodereachesanacceptablelocation. 19
4222135
23
42
27
RemovingtheTopofaHeap
❶Movethelastnodeontotheroot.
❷ Pushtheout-of-placenodedownward,swappingwithitslargerchilduntilthenewnodereachesanacceptablelocation. 19
4222127
23
42
35
RemovingtheTopofaHeap
✔Thechildrenallhavekeys<=theout-of-placenode,or
✔Thenodereachestheleaf.
➘ Theprocessofpushingthenewnodedownwardiscalledreheapificationdownward.
19
4222127
23
42
35
PriorityQueuesRevisited
• Apriorityqueueisacontainerclassthatallowsentriestoberetrievedaccordingtosomespecificprioritylevels
• Thehighestpriorityentryisremoved first• Ifthereareseveralentrieswithequallyhighpriorities,thenthepriorityqueue’simplementationdetermineswhichwill comeoutfirst(e.g.FIFO)
• Heapissuitableforapriorityqueue
AddingaNode:samepriority
❶ Putthenewnodeinthenextavailablespot.
❷ Pushthenewnodeupward,swappingwithitsparentuntilthenewnodereachesanacceptablelocation.
19
4222127
23
45
35
45*
AddingaNode:samepriority
❶ Putthenewnodeinthenextavailablespot.
❷ Pushthenewnodeupward,swappingwithitsparentuntilthenewnodereachesanacceptablelocation.
19
4222145*
23
45
35
27
AddingaNode:samepriority
❶ Putthenewnodeinthenextavailablespot.
❷ Pushthenewnodeupward,swappingwithitsparentuntilthenewnodereachesanacceptablelocation.
19
4222135
23
45
45*
27
AddingaNode:samepriority
✔Theparenthasakeythatis>=newnode,or
✔Thenodereachestheroot.➚ Theprocessofpushingthe
newnodeupwardiscalledreheapificationupward. 19
4222135
23
45
45*
27
Note: Implementation determines which 45 will be in the root, and will come out first when popping.
RemovingtheTopofaHeap
✔Thechildrenallhavekeys<=theout-of-placenode,or
✔Thenodereachestheleaf.➘ Theprocessofpushingthe
newnodedownwardiscalledreheapificationdownward. 19
4222127
23
45*
35
Note: Implementation determines which 45 will be in the root, and will come out first when popping.
HeapImplementation
• Usebinary_tree_nodeclass• node implementation isforageneralbinarytree• butwemayneedtohavedoubly linkednode
• Usearrays(page475)• Aheapisacompletebinarytree• whichcanbeimplementedmoreeasilywithanarraythanwiththenodeclass
• anddotwo-waylinks
Formulasforlocationchildrenandparentsinanarrayrepresentation
• Rootatlocation[0]• Parentofthenodein[i]isat[(i-1)/2]• Childrenofthenodein[i](ifexist)isat[2i+1]and[2i+2]• Test:
• completetreeof10,000nodes• parentof4999isat(4999-1)/2=2499• childrenof4999isat9999(V)and10,000(X)
ImplementingaHeap
• Wewillstorethedatafromthenodesinapartially-filledarray.
An array of data
2127
23
42
35
ImplementingaHeap
• Datafromtherootgoesinthe firstlocationofthearray.
An array of data
2127
23
42
35
42
ImplementingaHeap
• Datafromthenextrowgoes inthenexttwoarraylocations.
An array of data
2127
23
42
35
42 35 23
ImplementingaHeap
• Datafromthenextrowgoes inthenexttwoarraylocations.
An array of data
2127
23
42
35
42 35 23 27 21
ImplementingaHeap
• Datafromthenextrowgoes inthenexttwoarraylocations.
An array of data
2127
23
42
35
42 35 23 27 21
We don't care what's inthis part of the array.
ImportantPointsabouttheImplementation
• Thelinksbetweenthetree'snodesarenot actuallystoredaspointers,orinanyotherway.
• Theonlywaywe"know"that"thearrayisatree"isfromthewaywemanipulatethedata.
An array of data
2127
23
42
35
42 35 23 27 21
ImportantPointsabouttheImplementation
• Ifyouknowtheindexofanode,thenitiseasytofigureouttheindexesofthatnode'sparentandchildren.Formulasaregiveninthebook.
[0] [1] [2] [3] [4]
2127
23
42
35
42 35 23 27 21
Formulasforlocationchildrenandparentsinanarrayrepresentation
• Rootatlocation[0]• Parentofthenodein[i]isat[(i-1)/2]• Childrenofthenodein[i](ifexist)isat[2i+1]and[2i+2]• Test:
• completetreeof10,000nodes• parentof4999isat(4999-1)/2=2499• childrenof4999isat9999(V)and10,000(X)
WrapUp...
• Canyouimplement theaddandremovewiththeknowledgeoftheseformulas?
• Add• putthenewentryinthelastlocation• Pushthenewnodeupward,swappingwithitsparentuntilthenewnodereachesanacceptablelocation
• Remove• movethelastnodetotheroot• Pushtheout-of-placenodedownward,swappingwithits largerchilduntilthenewnodereachesanacceptable location
class heap{public:
....void push(const Item& entry); // addItem& pop(); // remove the highest
private:Item data[CAPACITY];size_type used;
}
WrapUp...
• Canyouimplement theaddandremovewiththeknowledgeoftheseformulas?
• Add(in-classquiz)• putthenewentryinthelastlocation
• Pushthenewnodeupward,swappingwithitsparentuntilthenewnodereachesanacceptable location
• Remove(in-classquiz)• movethelastnodetotheroot• Pushtheout-of-placenodedownward,swappingwithitslargerchilduntilthenewnodereachesanacceptablelocation
template <class Item>class heap{public:
heap () { used = 0;}void push(const Item& entry); // addItem& pop(); // remove the highestsize_t parent (size_t k) const { return (k-1)/2;}size_t l_child (size_t k) const { return 2*k+1;}size_t r_child (size_t k) const { return 2*k+2;}
private:Item data[CAPACITY];size_type used;
}
Summary
•Aheapisacompletebinarytree,wheretheentryateachnodeisgreaterthanorequaltotheentriesinitschildren.
• Toaddanentrytoaheap,placethenewentryatthenextavailablespot,andperformareheapificationupward.
• Toremovethebiggestentry,movethelastnodeontotheroot,andperformareheapificationdownward.
THE END
Presentation copyright 1997 Addison Wesley Longman,For use with Data Structures and Other Objects Using C++by Michael Main and Walter Savitch.
Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).
Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.