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

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
 
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
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
 

More from MalligaarjunanN

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 .pdfMalligaarjunanN
 
Microprocessor and microcontroller record.pdf
Microprocessor and microcontroller record.pdfMicroprocessor and microcontroller record.pdf
Microprocessor and microcontroller record.pdfMalligaarjunanN
 
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.pdfMalligaarjunanN
 
8089 microprocessor with diagram and analytical
8089 microprocessor with diagram and analytical8089 microprocessor with diagram and analytical
8089 microprocessor with diagram and analyticalMalligaarjunanN
 
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.pptxMalligaarjunanN
 
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).pptxMalligaarjunanN
 
Technical English grammar and tenses.pptx
Technical English grammar and tenses.pptxTechnical English grammar and tenses.pptx
Technical English grammar and tenses.pptxMalligaarjunanN
 
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.pptxMalligaarjunanN
 
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.pptxMalligaarjunanN
 
C programming DOC-20230723-WA0001..pptx
C programming  DOC-20230723-WA0001..pptxC programming  DOC-20230723-WA0001..pptx
C programming DOC-20230723-WA0001..pptxMalligaarjunanN
 
Chemistry fluorescent topic chemistry.pptx
Chemistry fluorescent topic  chemistry.pptxChemistry fluorescent topic  chemistry.pptx
Chemistry fluorescent topic chemistry.pptxMalligaarjunanN
 
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.pptxMalligaarjunanN
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxMalligaarjunanN
 
Python programming file handling mhhk.pptx
Python programming file handling mhhk.pptxPython programming file handling mhhk.pptx
Python programming file handling mhhk.pptxMalligaarjunanN
 
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.pptxMalligaarjunanN
 
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.pptxMalligaarjunanN
 
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.pptxMalligaarjunanN
 
Computer organisation and architecture .
Computer organisation and architecture .Computer organisation and architecture .
Computer organisation and architecture .MalligaarjunanN
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and commentMalligaarjunanN
 
pythoncommentsandvariables-231016105804-9a780b91 (1).pptx
pythoncommentsandvariables-231016105804-9a780b91 (1).pptxpythoncommentsandvariables-231016105804-9a780b91 (1).pptx
pythoncommentsandvariables-231016105804-9a780b91 (1).pptxMalligaarjunanN
 

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

Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 

Recently uploaded (20)

Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 

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. ◦ . . .