SlideShare a Scribd company logo
1 of 42
Data Structures
(Heap Tree )
Dr.N.S.Nithya
ASP/CSE
12/17/2023 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.
12/17/2023 3
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….
Heap Data Structures
 Heap data structure is a specialized
binary tree-based data structure. Heap
is a binary tree with special
characteristics. In a heap data
structure, nodes are arranged based
on their values. A heap data structure
some times also called as Binary
Heap.
Properties of Heap data
Structure
 Every heap data structure has the
following properties...
 Property #1 (Structural): All levels in
a heap must be full except the last
level and all nodes must be filled from
left to right strictly.
 Property #2 (Ordering): Nodes must
be arranged in an order according to
their values based on Max heap or
Min heap.
Heaps
A heap is a certain kind
of complete binary tree.
Heaps
A heap is a
certain kind
of complete
binary tree.
When a complete
binary tree is built,
its first node must be
the root.
Root
Heaps
Complete
binary tree.
Left child
of the
root
The second node is
always the left child
of the root.
Heaps
Complete
binary tree.
Right child
of the
root
The third node is
always the right child
of the root.
Heaps
Complete
binary tree.
The next nodes
always fill the next
level from left-to-
right.
Heaps
Complete
binary tree.
The next nodes
always fill the next
level from left-to-
right.
Heaps
Complete
binary tree.
The next nodes
always fill the next
level from left-to-
right.
Heaps
Complete
binary tree.
The next nodes
always fill the next
level from left-to-
right.
Heaps
Complete
binary tree.
Heaps
A heap is a
certain kind
of complete
binary tree.
Each node in a heap
contains a key that
can be compared to
other nodes' keys.
19
4
22
21
27
23
45
35
Heaps
A heap is a
certain kind
of complete
binary tree.
The "heap property"
requires that each
node's key is >= the
keys of its children
19
4
22
21
27
23
45
35
Max Heap
 Max-Heap: In this type of heap, the
value of parent node will always be
greater than or equal to the value of child
node across the tree and the node with
highest value will be the root node of the
tree.
Min Heap
 Min-Heap: In this type of heap, the
value of parent node will always be less
than or equal to the value of child node
across the tree and the node with
lowest value will be the root node of
tree.
Heapify
 Heapify is the process of creating a heap
data structure from a binary tree. It is used
to create a Min-Heap or a Max-Heap.
 Let the input array be
Create a complete binary
tree from the array
Adding a Node to a Max-Heap
 Put the new node (42)
in the next available
spot.
 Push the new node
upward, swapping with
its parent until the new
node satisfy the heap
order property.
19
4
22
21
27
23
45
35
42
Adding a Node to a Max-Heap
 Put the new node in
the next available
spot.
 Push the new node
upward, swapping with
its parent until the new
node reaches an
acceptable location.
19
4
22
21
42
23
45
35
27
Adding a Node to a Heap
 Put the new node in
the next available
spot.
 Push the new node
upward, swapping with
its parent until the new
node reaches an
acceptable location.
19
4
22
21
35
23
45
42
27
Adding a Node to a Max -Heap
 The node move
upwards until it satisfy
the condition
 The parent has a key
that is >= new node
 The process of
pushing the new node
upward is called
reheapification
upward.
19
4
22
21
35
23
45
42
27
Deleting a Max element from Max-
Heap
 To remove the
biggest entry,
move the last
node onto the root,
and perform a
reheapification
downward.
19
4
22
21
35
23
45
42
27
Deleting a Max element from Max-
Heap
 Move the last node
onto the root.
19
4
22
21
35
23
27
42
Deleting a Max element from Max-
Heap
 Move the last node
onto the root.
 Push the out-of-place
node downward,
swapping with its
larger child until the
new node reaches an
acceptable location.
19
4
22
21
35
23
27
42
Deleting a Max element from Max-
Heap
 Move the last node
onto the root.
 Push the out-of-place
node downward,
swapping with its
larger child until the
new node reaches an
acceptable location.
19
4
22
21
35
23
42
27
Deleting a Max element from Max-
Heap
 Move the last node
onto the root.
 Push the out-of-place
node downward,
swapping with its
larger child until the
new node reaches an
acceptable location.
19
4
22
21
27
23
42
35
Deleting a Max element from Max-
Heap
 The children all have
keys <= the out-of-
place node, or
 The node reaches the
leaf.
 The process of
pushing the new node
downward is called
reheapification
downward.
19
4
22
21
27
23
42
35
Adding a Node to a Min-Heap
 Put the new node (5)
in the next available
spot.
 Push the new node
upward, swapping with
its parent until the new
node satisfy the heap
order property.
19
24
20
10
14
18
10
12
5
Adding a Node to a Min-Heap
 Put the new node in
the next available
spot.
 Push the new node
upward, swapping with
its parent until the new
node reaches an
acceptable location.
19
24
20
15
5
18
10
12
14
Adding a Node to a Min-Heap
 Put the new node in
the next available
spot.
 Push the new node
upward, swapping with
its parent until the new
node reaches an
acceptable location.
19
24
20
15
12
18
10
5
14
Adding a Node to a Min-Heap
 Put the new node in
the next available
spot.
 Push the new node
upward, swapping with
its parent until the new
node reaches an
acceptable location.
19
24
20
15
12
18
10
14
5
Adding a Node to a Min -Heap
 The node move
upwards until it satisfy
the condition
 The parent has a key
that is <= new node
 The process of
pushing the new node
upward is called
reheapification
upward.
19
24
20
15
12
18
5
10
14
Deleting a Min element from
Min- Heap
 To remove the
biggest entry,
move the last
node onto the root,
and perform a
reheapification
downward.
19
24
20
15
12
18
4
10
14
Deleting a Min element from
Min- Heap
 Move the last node
onto the root.
19
24
20
15
12
18
14
10
Deleting a Min element from Min-
Heap
 Move the last node
onto the root.
 Push the out-of-place
node downward,
swapping with its
larger child until the
new node reaches an
acceptable location.
19
24
20
15
12
18
14
10
Deleting a Min element from
Min- Heap
 Move the last node
onto the root.
 Push the out-of-place
node downward,
swapping with its
larger child until the
new node reaches an
acceptable location.
19
24
20
15
12
18
10
14
Deleting a Min element from
Min- Heap
 Move the last node
onto the root.
 Push the out-of-place
node downward,
swapping with its
larger child until the
new node reaches an
acceptable location.
19
24
20
15
14
18
10
12
Deleting a Min element from
Min- Heap
 The children all have
keys <= the out-of-
place node, or
 The node reaches the
leaf.
 The process of
pushing the new node
downward is called
reheapification
downward.
19
24
20
15
14
18
10
12
Decrease-Key
 To decrease the value of a certain
key, we’ll use the map to locate its
index. After we locate its index, we’ll
change its value, and start moving it
up the tree if needed. The reason we’re
moving the key up the tree is that its
value got reduced. Therefore, it’ll either
stay in its place or move up.
42
Heap-Priority Queues
 Supports the following operations.
◦ Insert element x.
◦ Return min element.
◦ Return and delete minimum element.
◦ Decrease key of element x to k.
 Applications.
◦ Dijkstra's shortest path algorithm.
◦ Prim's MST algorithm.
◦ Event-driven simulation.
◦ Huffman encoding.
◦ Heapsort.
◦ . . .

More Related Content

Similar to Data structures trees and graphs - Heap Tree.pptx

Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
MouDhara1
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
chidabdu
 
Heap Data Structure Tutorial
Heap Data Structure Tutorial Heap Data Structure Tutorial
Heap Data Structure Tutorial
Simplilearn
 
CSE680-06HeapSort.ppt
CSE680-06HeapSort.pptCSE680-06HeapSort.ppt
CSE680-06HeapSort.ppt
AmitShou
 

Similar to Data structures trees and graphs - Heap Tree.pptx (20)

Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
 
Heaps
HeapsHeaps
Heaps
 
Priority queue
Priority queuePriority queue
Priority queue
 
5-heap.ppt
5-heap.ppt5-heap.ppt
5-heap.ppt
 
5-heap.ppt
5-heap.ppt5-heap.ppt
5-heap.ppt
 
Heap sort
Heap sortHeap sort
Heap sort
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
 
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.pptfdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
 
HeapSort
HeapSortHeapSort
HeapSort
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
Heapsort
HeapsortHeapsort
Heapsort
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
 
Heap Data Structure Tutorial
Heap Data Structure Tutorial Heap Data Structure Tutorial
Heap Data Structure Tutorial
 
Heaps.pdf
Heaps.pdfHeaps.pdf
Heaps.pdf
 
CSE680-06HeapSort.ppt
CSE680-06HeapSort.pptCSE680-06HeapSort.ppt
CSE680-06HeapSort.ppt
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Heaps
HeapsHeaps
Heaps
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Ch15 Heap
Ch15 HeapCh15 Heap
Ch15 Heap
 

More from MalligaarjunanN

More from MalligaarjunanN (20)

bro_nodejs-1 front end development .pdf
bro_nodejs-1 front end development  .pdfbro_nodejs-1 front end development  .pdf
bro_nodejs-1 front end development .pdf
 
Microprocessor and microcontroller record.pdf
Microprocessor and microcontroller record.pdfMicroprocessor and microcontroller record.pdf
Microprocessor and microcontroller record.pdf
 
8087 MICROPROCESSOR and diagram with definition.pdf
8087 MICROPROCESSOR and diagram with definition.pdf8087 MICROPROCESSOR and diagram with definition.pdf
8087 MICROPROCESSOR and diagram with definition.pdf
 
8089 microprocessor with diagram and analytical
8089 microprocessor with diagram and analytical8089 microprocessor with diagram and analytical
8089 microprocessor with diagram and analytical
 
English article power point presentation eng.pptx
English article power point presentation eng.pptxEnglish article power point presentation eng.pptx
English article power point presentation eng.pptx
 
Digital principle and computer design Presentation (1).pptx
Digital principle and computer design Presentation (1).pptxDigital principle and computer design Presentation (1).pptx
Digital principle and computer design Presentation (1).pptx
 
Technical English grammar and tenses.pptx
Technical English grammar and tenses.pptxTechnical English grammar and tenses.pptx
Technical English grammar and tenses.pptx
 
Polymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptxPolymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptx
 
Chemistry iconic bond topic chem ppt.pptx
Chemistry iconic bond topic chem ppt.pptxChemistry iconic bond topic chem ppt.pptx
Chemistry iconic bond topic chem ppt.pptx
 
C programming DOC-20230723-WA0001..pptx
C programming  DOC-20230723-WA0001..pptxC programming  DOC-20230723-WA0001..pptx
C programming DOC-20230723-WA0001..pptx
 
Chemistry fluorescent topic chemistry.pptx
Chemistry fluorescent topic  chemistry.pptxChemistry fluorescent topic  chemistry.pptx
Chemistry fluorescent topic chemistry.pptx
 
C programming power point presentation c ppt.pptx
C programming power point presentation c ppt.pptxC programming power point presentation c ppt.pptx
C programming power point presentation c ppt.pptx
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptx
 
Python programming file handling mhhk.pptx
Python programming file handling mhhk.pptxPython programming file handling mhhk.pptx
Python programming file handling mhhk.pptx
 
Computer organisation and architecture updated unit 2 COA ppt.pptx
Computer organisation and architecture updated unit 2 COA ppt.pptxComputer organisation and architecture updated unit 2 COA ppt.pptx
Computer organisation and architecture updated unit 2 COA ppt.pptx
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
 
Data structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxData structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptx
 
Computer organisation and architecture .
Computer organisation and architecture .Computer organisation and architecture .
Computer organisation and architecture .
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
pythoncommentsandvariables-231016105804-9a780b91 (1).pptx
pythoncommentsandvariables-231016105804-9a780b91 (1).pptxpythoncommentsandvariables-231016105804-9a780b91 (1).pptx
pythoncommentsandvariables-231016105804-9a780b91 (1).pptx
 

Recently uploaded

Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
MaherOthman7
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
BalamuruganV28
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
MohammadAliNayeem
 

Recently uploaded (20)

SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
Circuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringCircuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineering
 
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoning
 
Piping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfPiping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdf
 
Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdf
 
How to Design and spec harmonic filter.pdf
How to Design and spec harmonic filter.pdfHow to Design and spec harmonic filter.pdf
How to Design and spec harmonic filter.pdf
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
 
Electrical shop management system project report.pdf
Electrical shop management system project report.pdfElectrical shop management system project report.pdf
Electrical shop management system project report.pdf
 
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
Complex plane, Modulus, Argument, Graphical representation of a complex numbe...
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
 
Electrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission lineElectrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission line
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
BURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdfBURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdf
 

Data structures trees and graphs - Heap Tree.pptx

  • 1. Data Structures (Heap Tree ) Dr.N.S.Nithya ASP/CSE
  • 2. 12/17/2023 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. 12/17/2023 3 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….
  • 4. Heap Data Structures  Heap data structure is a specialized binary tree-based data structure. Heap is a binary tree with special characteristics. In a heap data structure, nodes are arranged based on their values. A heap data structure some times also called as Binary Heap.
  • 5. Properties of Heap data Structure  Every heap data structure has the following properties...  Property #1 (Structural): All levels in a heap must be full except the last level and all nodes must be filled from left to right strictly.  Property #2 (Ordering): Nodes must be arranged in an order according to their values based on Max heap or Min heap.
  • 6. Heaps A heap is a certain kind of complete binary tree.
  • 7. Heaps A heap is a certain kind of complete binary tree. When a complete binary tree is built, its first node must be the root. Root
  • 8. Heaps Complete binary tree. Left child of the root The second node is always the left child of the root.
  • 9. Heaps Complete binary tree. Right child of the root The third node is always the right child of the root.
  • 10. Heaps Complete binary tree. The next nodes always fill the next level from left-to- right.
  • 11. Heaps Complete binary tree. The next nodes always fill the next level from left-to- right.
  • 12. Heaps Complete binary tree. The next nodes always fill the next level from left-to- right.
  • 13. Heaps Complete binary tree. The next nodes always fill the next level from left-to- right.
  • 15. Heaps A heap is a certain kind of complete binary tree. Each node in a heap contains a key that can be compared to other nodes' keys. 19 4 22 21 27 23 45 35
  • 16. Heaps A heap is a certain kind of complete binary tree. The "heap property" requires that each node's key is >= the keys of its children 19 4 22 21 27 23 45 35
  • 17. Max Heap  Max-Heap: In this type of heap, the value of parent node will always be greater than or equal to the value of child node across the tree and the node with highest value will be the root node of the tree.
  • 18. Min Heap  Min-Heap: In this type of heap, the value of parent node will always be less than or equal to the value of child node across the tree and the node with lowest value will be the root node of tree.
  • 19. Heapify  Heapify is the process of creating a heap data structure from a binary tree. It is used to create a Min-Heap or a Max-Heap.  Let the input array be Create a complete binary tree from the array
  • 20. Adding a Node to a Max-Heap  Put the new node (42) in the next available spot.  Push the new node upward, swapping with its parent until the new node satisfy the heap order property. 19 4 22 21 27 23 45 35 42
  • 21. Adding a Node to a Max-Heap  Put the new node in the next available spot.  Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 4 22 21 42 23 45 35 27
  • 22. Adding a Node to a Heap  Put the new node in the next available spot.  Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 4 22 21 35 23 45 42 27
  • 23. Adding a Node to a Max -Heap  The node move upwards until it satisfy the condition  The parent has a key that is >= new node  The process of pushing the new node upward is called reheapification upward. 19 4 22 21 35 23 45 42 27
  • 24. Deleting a Max element from Max- Heap  To remove the biggest entry, move the last node onto the root, and perform a reheapification downward. 19 4 22 21 35 23 45 42 27
  • 25. Deleting a Max element from Max- Heap  Move the last node onto the root. 19 4 22 21 35 23 27 42
  • 26. Deleting a Max element from Max- Heap  Move the last node onto the root.  Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4 22 21 35 23 27 42
  • 27. Deleting a Max element from Max- Heap  Move the last node onto the root.  Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4 22 21 35 23 42 27
  • 28. Deleting a Max element from Max- Heap  Move the last node onto the root.  Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4 22 21 27 23 42 35
  • 29. Deleting a Max element from Max- Heap  The children all have keys <= the out-of- place node, or  The node reaches the leaf.  The process of pushing the new node downward is called reheapification downward. 19 4 22 21 27 23 42 35
  • 30. Adding a Node to a Min-Heap  Put the new node (5) in the next available spot.  Push the new node upward, swapping with its parent until the new node satisfy the heap order property. 19 24 20 10 14 18 10 12 5
  • 31. Adding a Node to a Min-Heap  Put the new node in the next available spot.  Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 24 20 15 5 18 10 12 14
  • 32. Adding a Node to a Min-Heap  Put the new node in the next available spot.  Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 24 20 15 12 18 10 5 14
  • 33. Adding a Node to a Min-Heap  Put the new node in the next available spot.  Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 24 20 15 12 18 10 14 5
  • 34. Adding a Node to a Min -Heap  The node move upwards until it satisfy the condition  The parent has a key that is <= new node  The process of pushing the new node upward is called reheapification upward. 19 24 20 15 12 18 5 10 14
  • 35. Deleting a Min element from Min- Heap  To remove the biggest entry, move the last node onto the root, and perform a reheapification downward. 19 24 20 15 12 18 4 10 14
  • 36. Deleting a Min element from Min- Heap  Move the last node onto the root. 19 24 20 15 12 18 14 10
  • 37. Deleting a Min element from Min- Heap  Move the last node onto the root.  Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 24 20 15 12 18 14 10
  • 38. Deleting a Min element from Min- Heap  Move the last node onto the root.  Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 24 20 15 12 18 10 14
  • 39. Deleting a Min element from Min- Heap  Move the last node onto the root.  Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 24 20 15 14 18 10 12
  • 40. Deleting a Min element from Min- Heap  The children all have keys <= the out-of- place node, or  The node reaches the leaf.  The process of pushing the new node downward is called reheapification downward. 19 24 20 15 14 18 10 12
  • 41. Decrease-Key  To decrease the value of a certain key, we’ll use the map to locate its index. After we locate its index, we’ll change its value, and start moving it up the tree if needed. The reason we’re moving the key up the tree is that its value got reduced. Therefore, it’ll either stay in its place or move up.
  • 42. 42 Heap-Priority Queues  Supports the following operations. ◦ Insert element x. ◦ Return min element. ◦ Return and delete minimum element. ◦ Decrease key of element x to k.  Applications. ◦ Dijkstra's shortest path algorithm. ◦ Prim's MST algorithm. ◦ Event-driven simulation. ◦ Huffman encoding. ◦ Heapsort. ◦ . . .