SlideShare a Scribd company logo
1 of 23
11/8/2022 1
Chapter 12 - Heaps
11/8/2022 2
Introduction
►Heaps are largely about priority queues.
►They are an alternative data structure to
implementing priority queues (we had
arrays, linked lists…)
►Recall the advantages and disadvantages of
queues implemented as arrays
 Insertions / deletions? O(n) … O(1)!
►Priority queues are critical to many real-
world applications.
11/8/2022 3
Introduction
► First of all, a heap is a kind of tree that offers
both insertion and deletion in O(log2n) time.
► Fast for insertions; not so fast for deletions.
11/8/2022 4
Introduction to Heaps
► Characteristics:
 1. A heap is ‘complete’ (save for the last row – going
left to right – see figure 12.1, p. 580)
 2. usually implemented as an array
 3. Each node in a heap satisfies the ‘heap condition,’
which states that every node’s key is larger than or
equal to the keys of its children.
  The heap is thus an abstraction; we can draw it
to look like a tree, but recognize that it is the array
that implements this abstraction and that it is
stored and processed in primary memory (RAM).
 No ‘holes’ in the array.
11/8/2022 5
Priority Queues, Heaps, and ADTs
►Heaps are mostly used to implement priority
queues.
►Again, a heap is usually implemented as an
array.
11/8/2022 6
“Weakly Ordered”
► We know how a binary search tree is developed – with lesser keys to the
left; greater keys to the right as we descend.
 Because of this, we have nice, neat algorithms for binary search trees.
► Here: No Strong Ordering:
 But for nodes in a heap, we don’t have this strong ordering - and this can cause us
some difficulty.
► Cannot assert much:
 We can only assert as one descends in the heap, nodes will be in descending order
(see rule 3)
 Equivalently, nodes below a node are <= the parent node.
► Weakly-ordered:
 Heaps are thus said to be weakly ordered…
11/8/2022 7
Weakly Ordered – More
► No Convenient Search Mechanism:
 Because of weak ordering, there is no convenient searching for a specified key as
we have in binary search trees.
 Don’t have enough info at a node to decide whether to descend left or right.
► Delete:
 So to delete a node with a specific key there are issues
 No real slick way to find it.
► Randomness:
 So, a heap’s organization approaches ‘randomness.’
► Sufficient Ordering: Yet, there is ‘sufficient ordering’ to allow
 quick removal (yes, a delete) of the maximum node and
 fast insertion of new nodes.
►  As it turns out, these are the only operations one needs in using a
heap as a priority queue.
► We will discuss the algorithms later…
11/8/2022 8
Removal
►Removal is easy:
 When we remove from a heap, we always
remove the node with the largest key.
 Hence, removal is quite easy and has index 0 of
the heap array.
 maxNode = heapArray[0];
►But tree is then not complete:
 But once root is gone, tree is not complete and
we must fill this cell.
►Now this becomes interesting…
11/8/2022 9
Removal of “maxNode”
►Move ‘last node’ to root.
 Start by moving the ‘last node’ into the root.
 The ‘last’ node is the rightmost node in the lowest
occupied level of the tree.
 This also corresponds to the last filled cell in the
array (ahead).
►Trickle-down:
 Then trickle this last node down until it is
below a larger node and above a smaller one.
11/8/2022 10
100
90
30
80
50 70
45 5
60
20 10 40 55 Last node added
Last node added is 5.
Delete Algorithm: heapArray[0] = heapArray [n-1]
n--; // size of array is decreased by one.
Heap is represented logically as a tree.
(Tree is organized to represent a priority queue. Easy to visualize.)
Heap is physically implemented (here) using an array.
Removal (Delete)
Note: NOT binary search tree!
11/8/2022 11
95
82
63
51
37 10
30
70
43 27 53 34
Last node added
Trickle down swapping node w/larger child until node >= children
As we trickle down, the nodes swap out always swapping the larger node
with the node we are trickling down (so as to maintain the larger nodes above…
We trickle down selecting the largest child with which to swap.
We must compare, but always swap with the larger of the two.
Note: we very well may NOT trickle down to a leaf node
Example
(Different tree)
Delete (Removal)
Trickle Down
11/8/2022 12
82
70
63
51
37 10
53
43 27 30 34
The new arrangement via a delete would be as above.
Go through this…
Node 95 (the largest) is deleted.
Tree remains balanced after delete
and the rules for the heap are preserved.
(95 removed; compare 30 with 82; 82 selected and moved to root;
Compare 30 with 70. 70 moves up.
Compare 30 with 53; 53 moves up. 30 is a leaf node. )
11/8/2022 13
Insertion – Trickle Up
► Pretty easy too. Easier than Deletion.
► Insertions usually trickle up.
► Start at the bottom (first open position) via code:
heapArray[n] = newNode;
n++;
Inserting at bottom will likely destroy the heap condition.
This will happen when the new node is larger than its parent.
Trickle upwards until node is below a node larger than it
and it is above a node smaller (or equal to) it.
Consider the following slides:
11/8/2022 14
82
70
63
51
37 10
55
43 27 30 34 95
Node to be added…
Clearly 95 must trickle up
(and appears it will end up
at the root!)
95
70
63
82
51 10
55
43 27 30 34 37
Resultant heap:
Easy to see swaps…
Trickle Up
Can easily see the swaps.
11/8/2022 15
Insertion - more
► Note that this is easier because we don’t have to compare
which node to swap (as we do in going down).
► We only have ONE parent, and it is equal to or larger!
► Progress until parent is larger, at whatever level that might
occur.
► A side point: Clearly, the same nodes may constitute
different heaps depending upon their arrival.
11/8/2022 16
Insert / Delete – a ‘little bit’ of
Implementation
► While some implementations actually do the swap,
there’s a lot of overhead here for a large heap.
► A gain in efficiency is acquired if we substitute a
‘copy’ for a ‘swap.’ (like the selection sort…)
► Here, we copy the node to be trickled to a temp
area and just do compares and copies (moves) until
the right spot is found. Then we can copy (move)
the node (temp) (to be trickled down) into that node
location.
► More efficient than doing swaps repeatedly.
11/8/2022 17
Heap Applet
►Google: Lafore applets
11/8/2022 18
Java Code for Heaps
► Code is reasonably straightforward.
► We do, however, need to ensure that the array is
not full.
► If full, what do we do?
► Likely if the array is an ArrayList or a Vector
(one dimensional array), we are in good shape
and can (for the first step) merely add the new
entry to the end (bottom) of the Vector.
► No problems.
11/8/2022 19
Heap Implementation
►Easy to discuss heaps as if heaps were
implemented as trees even though the
implementation has been based on an
array or vector.
►As implemented with an array, we can use a
binary tree, but not a binary search
tree, because the ordering is not strong.
►Such a tree, complete at all times – with no
missing nodes – is called a tree-heap.
11/8/2022 20
Class Exercise
►Draw an appropriate array (or vector) to
implement a tree-based heap whose
‘entries’ are sufficient to support
 Inserting into the heap
 Removal from the heap
 Making the structure dynamic
►Pseudo-code your algorithms.
Class Exercise
►Implement a heap with a linked list.
►Draw the structure you design.
►Present pseudocode to
 Insert into
 Delete from
►the physical structure.
11/8/2022 21
11/8/2022 22
Sorting with a Heap:
HeapSort: insert() and remove()
► Very easy to implement.
► We simply insert() all unordered items into the heap
trickling down using an insert() routine….
► Then, repeatedly use the remove() items in sorted
order….
► Both insert() and remove() operate in O(log2 n) time and
this must be applied n times, thus rendering an O(nlog2 n)
time.
► This is the same as a QuickSort
► Not as fast because there are more operations in the
trickledown than in the inner loop of a quicksort.
► Can be done both iteratively (using stacks) and
recursively.
► Code is in your book.
11/8/2022 23
Uses of Heaps
► Use of heap trees can be used to obtain improved
running times for several network optimization algorithms.
► Can be used to assist in dynamically-allocating memory
partitions.
► Lots of variants of heaps (see Google)
► A heapsort is considered to be one of the best sorting
methods being in-place with no quadratic worst-case
scenarios.
► Finding the min, max, both the min and max, median, or
even the k-th largest element can be done in linear time
using heaps.
► And more….

More Related Content

Similar to Chapter 12 - Heaps.ppt

Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
 
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docxajoy21
 
Heapsortokkay
HeapsortokkayHeapsortokkay
HeapsortokkayRemesha
 
CUDA by Example : Thread Cooperation : Notes
CUDA by Example : Thread Cooperation : NotesCUDA by Example : Thread Cooperation : Notes
CUDA by Example : Thread Cooperation : NotesSubhajit Sahu
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdfNash229987
 
graph2tab, a library to convert experimental workflow graphs into tabular for...
graph2tab, a library to convert experimental workflow graphs into tabular for...graph2tab, a library to convert experimental workflow graphs into tabular for...
graph2tab, a library to convert experimental workflow graphs into tabular for...Rothamsted Research, UK
 
How to concatenate two (or more) subvolumes, measured with XCT, using ImageJ
How to concatenate two (or more) subvolumes, measured with XCT, using ImageJHow to concatenate two (or more) subvolumes, measured with XCT, using ImageJ
How to concatenate two (or more) subvolumes, measured with XCT, using ImageJJavier García Molleja
 
High Performance Systems Without Tears - Scala Days Berlin 2018
High Performance Systems Without Tears - Scala Days Berlin 2018High Performance Systems Without Tears - Scala Days Berlin 2018
High Performance Systems Without Tears - Scala Days Berlin 2018Zahari Dichev
 
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNratnapatil14
 
3.3 shell sort
3.3 shell sort3.3 shell sort
3.3 shell sortKrish_ver2
 

Similar to Chapter 12 - Heaps.ppt (20)

Heapsort
HeapsortHeapsort
Heapsort
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Shell sort[1]
Shell sort[1]Shell sort[1]
Shell sort[1]
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
 
Heapsortokkay
HeapsortokkayHeapsortokkay
Heapsortokkay
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
CUDA by Example : Thread Cooperation : Notes
CUDA by Example : Thread Cooperation : NotesCUDA by Example : Thread Cooperation : Notes
CUDA by Example : Thread Cooperation : Notes
 
lecture4.pdf
lecture4.pdflecture4.pdf
lecture4.pdf
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
 
graph2tab, a library to convert experimental workflow graphs into tabular for...
graph2tab, a library to convert experimental workflow graphs into tabular for...graph2tab, a library to convert experimental workflow graphs into tabular for...
graph2tab, a library to convert experimental workflow graphs into tabular for...
 
Leftlist Heap-1.pdf
Leftlist Heap-1.pdfLeftlist Heap-1.pdf
Leftlist Heap-1.pdf
 
How to concatenate two (or more) subvolumes, measured with XCT, using ImageJ
How to concatenate two (or more) subvolumes, measured with XCT, using ImageJHow to concatenate two (or more) subvolumes, measured with XCT, using ImageJ
How to concatenate two (or more) subvolumes, measured with XCT, using ImageJ
 
Stack in C.pptx
Stack in C.pptxStack in C.pptx
Stack in C.pptx
 
High Performance Systems Without Tears - Scala Days Berlin 2018
High Performance Systems Without Tears - Scala Days Berlin 2018High Performance Systems Without Tears - Scala Days Berlin 2018
High Performance Systems Without Tears - Scala Days Berlin 2018
 
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
 
Splay tree
Splay treeSplay tree
Splay tree
 
3.3 shell sort
3.3 shell sort3.3 shell sort
3.3 shell sort
 

More from MouDhara1

datacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptxdatacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptxMouDhara1
 
06 Recursion in C.pptx
06 Recursion in C.pptx06 Recursion in C.pptx
06 Recursion in C.pptxMouDhara1
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
lecture-i-trees.ppt
lecture-i-trees.pptlecture-i-trees.ppt
lecture-i-trees.pptMouDhara1
 
JSP-Servlet.ppt
JSP-Servlet.pptJSP-Servlet.ppt
JSP-Servlet.pptMouDhara1
 
09 Structures in C.pptx
09 Structures in C.pptx09 Structures in C.pptx
09 Structures in C.pptxMouDhara1
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.pptMouDhara1
 
javascript12.pptx
javascript12.pptxjavascript12.pptx
javascript12.pptxMouDhara1
 
Linked list.pptx
Linked list.pptxLinked list.pptx
Linked list.pptxMouDhara1
 
For Client vs.pptx
For Client vs.pptxFor Client vs.pptx
For Client vs.pptxMouDhara1
 
Form tag.pptx
Form tag.pptxForm tag.pptx
Form tag.pptxMouDhara1
 
Web technology.pptx
Web technology.pptxWeb technology.pptx
Web technology.pptxMouDhara1
 
web tech.pptx
web tech.pptxweb tech.pptx
web tech.pptxMouDhara1
 
Introduction to Data Structure.pptx
Introduction to Data Structure.pptxIntroduction to Data Structure.pptx
Introduction to Data Structure.pptxMouDhara1
 

More from MouDhara1 (20)

ppt_dcn.pdf
ppt_dcn.pdfppt_dcn.pdf
ppt_dcn.pdf
 
datacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptxdatacommunicationnetworking-161025074805.pptx
datacommunicationnetworking-161025074805.pptx
 
06 Recursion in C.pptx
06 Recursion in C.pptx06 Recursion in C.pptx
06 Recursion in C.pptx
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
lecture-i-trees.ppt
lecture-i-trees.pptlecture-i-trees.ppt
lecture-i-trees.ppt
 
JSP-Servlet.ppt
JSP-Servlet.pptJSP-Servlet.ppt
JSP-Servlet.ppt
 
09 Structures in C.pptx
09 Structures in C.pptx09 Structures in C.pptx
09 Structures in C.pptx
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
DTD1.pptx
DTD1.pptxDTD1.pptx
DTD1.pptx
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.ppt
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
javascript12.pptx
javascript12.pptxjavascript12.pptx
javascript12.pptx
 
Linked list.pptx
Linked list.pptxLinked list.pptx
Linked list.pptx
 
For Client vs.pptx
For Client vs.pptxFor Client vs.pptx
For Client vs.pptx
 
Form tag.pptx
Form tag.pptxForm tag.pptx
Form tag.pptx
 
Web technology.pptx
Web technology.pptxWeb technology.pptx
Web technology.pptx
 
cse.pptx
cse.pptxcse.pptx
cse.pptx
 
web tech.pptx
web tech.pptxweb tech.pptx
web tech.pptx
 
Introduction to Data Structure.pptx
Introduction to Data Structure.pptxIntroduction to Data Structure.pptx
Introduction to Data Structure.pptx
 

Recently uploaded

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 

Recently uploaded (20)

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 

Chapter 12 - Heaps.ppt

  • 2. 11/8/2022 2 Introduction ►Heaps are largely about priority queues. ►They are an alternative data structure to implementing priority queues (we had arrays, linked lists…) ►Recall the advantages and disadvantages of queues implemented as arrays  Insertions / deletions? O(n) … O(1)! ►Priority queues are critical to many real- world applications.
  • 3. 11/8/2022 3 Introduction ► First of all, a heap is a kind of tree that offers both insertion and deletion in O(log2n) time. ► Fast for insertions; not so fast for deletions.
  • 4. 11/8/2022 4 Introduction to Heaps ► Characteristics:  1. A heap is ‘complete’ (save for the last row – going left to right – see figure 12.1, p. 580)  2. usually implemented as an array  3. Each node in a heap satisfies the ‘heap condition,’ which states that every node’s key is larger than or equal to the keys of its children.   The heap is thus an abstraction; we can draw it to look like a tree, but recognize that it is the array that implements this abstraction and that it is stored and processed in primary memory (RAM).  No ‘holes’ in the array.
  • 5. 11/8/2022 5 Priority Queues, Heaps, and ADTs ►Heaps are mostly used to implement priority queues. ►Again, a heap is usually implemented as an array.
  • 6. 11/8/2022 6 “Weakly Ordered” ► We know how a binary search tree is developed – with lesser keys to the left; greater keys to the right as we descend.  Because of this, we have nice, neat algorithms for binary search trees. ► Here: No Strong Ordering:  But for nodes in a heap, we don’t have this strong ordering - and this can cause us some difficulty. ► Cannot assert much:  We can only assert as one descends in the heap, nodes will be in descending order (see rule 3)  Equivalently, nodes below a node are <= the parent node. ► Weakly-ordered:  Heaps are thus said to be weakly ordered…
  • 7. 11/8/2022 7 Weakly Ordered – More ► No Convenient Search Mechanism:  Because of weak ordering, there is no convenient searching for a specified key as we have in binary search trees.  Don’t have enough info at a node to decide whether to descend left or right. ► Delete:  So to delete a node with a specific key there are issues  No real slick way to find it. ► Randomness:  So, a heap’s organization approaches ‘randomness.’ ► Sufficient Ordering: Yet, there is ‘sufficient ordering’ to allow  quick removal (yes, a delete) of the maximum node and  fast insertion of new nodes. ►  As it turns out, these are the only operations one needs in using a heap as a priority queue. ► We will discuss the algorithms later…
  • 8. 11/8/2022 8 Removal ►Removal is easy:  When we remove from a heap, we always remove the node with the largest key.  Hence, removal is quite easy and has index 0 of the heap array.  maxNode = heapArray[0]; ►But tree is then not complete:  But once root is gone, tree is not complete and we must fill this cell. ►Now this becomes interesting…
  • 9. 11/8/2022 9 Removal of “maxNode” ►Move ‘last node’ to root.  Start by moving the ‘last node’ into the root.  The ‘last’ node is the rightmost node in the lowest occupied level of the tree.  This also corresponds to the last filled cell in the array (ahead). ►Trickle-down:  Then trickle this last node down until it is below a larger node and above a smaller one.
  • 10. 11/8/2022 10 100 90 30 80 50 70 45 5 60 20 10 40 55 Last node added Last node added is 5. Delete Algorithm: heapArray[0] = heapArray [n-1] n--; // size of array is decreased by one. Heap is represented logically as a tree. (Tree is organized to represent a priority queue. Easy to visualize.) Heap is physically implemented (here) using an array. Removal (Delete) Note: NOT binary search tree!
  • 11. 11/8/2022 11 95 82 63 51 37 10 30 70 43 27 53 34 Last node added Trickle down swapping node w/larger child until node >= children As we trickle down, the nodes swap out always swapping the larger node with the node we are trickling down (so as to maintain the larger nodes above… We trickle down selecting the largest child with which to swap. We must compare, but always swap with the larger of the two. Note: we very well may NOT trickle down to a leaf node Example (Different tree) Delete (Removal) Trickle Down
  • 12. 11/8/2022 12 82 70 63 51 37 10 53 43 27 30 34 The new arrangement via a delete would be as above. Go through this… Node 95 (the largest) is deleted. Tree remains balanced after delete and the rules for the heap are preserved. (95 removed; compare 30 with 82; 82 selected and moved to root; Compare 30 with 70. 70 moves up. Compare 30 with 53; 53 moves up. 30 is a leaf node. )
  • 13. 11/8/2022 13 Insertion – Trickle Up ► Pretty easy too. Easier than Deletion. ► Insertions usually trickle up. ► Start at the bottom (first open position) via code: heapArray[n] = newNode; n++; Inserting at bottom will likely destroy the heap condition. This will happen when the new node is larger than its parent. Trickle upwards until node is below a node larger than it and it is above a node smaller (or equal to) it. Consider the following slides:
  • 14. 11/8/2022 14 82 70 63 51 37 10 55 43 27 30 34 95 Node to be added… Clearly 95 must trickle up (and appears it will end up at the root!) 95 70 63 82 51 10 55 43 27 30 34 37 Resultant heap: Easy to see swaps… Trickle Up Can easily see the swaps.
  • 15. 11/8/2022 15 Insertion - more ► Note that this is easier because we don’t have to compare which node to swap (as we do in going down). ► We only have ONE parent, and it is equal to or larger! ► Progress until parent is larger, at whatever level that might occur. ► A side point: Clearly, the same nodes may constitute different heaps depending upon their arrival.
  • 16. 11/8/2022 16 Insert / Delete – a ‘little bit’ of Implementation ► While some implementations actually do the swap, there’s a lot of overhead here for a large heap. ► A gain in efficiency is acquired if we substitute a ‘copy’ for a ‘swap.’ (like the selection sort…) ► Here, we copy the node to be trickled to a temp area and just do compares and copies (moves) until the right spot is found. Then we can copy (move) the node (temp) (to be trickled down) into that node location. ► More efficient than doing swaps repeatedly.
  • 18. 11/8/2022 18 Java Code for Heaps ► Code is reasonably straightforward. ► We do, however, need to ensure that the array is not full. ► If full, what do we do? ► Likely if the array is an ArrayList or a Vector (one dimensional array), we are in good shape and can (for the first step) merely add the new entry to the end (bottom) of the Vector. ► No problems.
  • 19. 11/8/2022 19 Heap Implementation ►Easy to discuss heaps as if heaps were implemented as trees even though the implementation has been based on an array or vector. ►As implemented with an array, we can use a binary tree, but not a binary search tree, because the ordering is not strong. ►Such a tree, complete at all times – with no missing nodes – is called a tree-heap.
  • 20. 11/8/2022 20 Class Exercise ►Draw an appropriate array (or vector) to implement a tree-based heap whose ‘entries’ are sufficient to support  Inserting into the heap  Removal from the heap  Making the structure dynamic ►Pseudo-code your algorithms.
  • 21. Class Exercise ►Implement a heap with a linked list. ►Draw the structure you design. ►Present pseudocode to  Insert into  Delete from ►the physical structure. 11/8/2022 21
  • 22. 11/8/2022 22 Sorting with a Heap: HeapSort: insert() and remove() ► Very easy to implement. ► We simply insert() all unordered items into the heap trickling down using an insert() routine…. ► Then, repeatedly use the remove() items in sorted order…. ► Both insert() and remove() operate in O(log2 n) time and this must be applied n times, thus rendering an O(nlog2 n) time. ► This is the same as a QuickSort ► Not as fast because there are more operations in the trickledown than in the inner loop of a quicksort. ► Can be done both iteratively (using stacks) and recursively. ► Code is in your book.
  • 23. 11/8/2022 23 Uses of Heaps ► Use of heap trees can be used to obtain improved running times for several network optimization algorithms. ► Can be used to assist in dynamically-allocating memory partitions. ► Lots of variants of heaps (see Google) ► A heapsort is considered to be one of the best sorting methods being in-place with no quadratic worst-case scenarios. ► Finding the min, max, both the min and max, median, or even the k-th largest element can be done in linear time using heaps. ► And more….