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

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
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuplesMalligaarjunanN
 
presentation-130909130658- (1).pdf
presentation-130909130658- (1).pdfpresentation-130909130658- (1).pdf
presentation-130909130658- (1).pdfMalligaarjunanN
 

More from MalligaarjunanN (20)

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
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
 
X02PredCalculus.ppt
X02PredCalculus.pptX02PredCalculus.ppt
X02PredCalculus.ppt
 
presentation-130909130658- (1).pdf
presentation-130909130658- (1).pdfpresentation-130909130658- (1).pdf
presentation-130909130658- (1).pdf
 
Presentation (5)-1.pptx
Presentation (5)-1.pptxPresentation (5)-1.pptx
Presentation (5)-1.pptx
 

Recently uploaded

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
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
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
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
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
 
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
 
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
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
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
 
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
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 

Recently uploaded (20)

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
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
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
 
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
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
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
 
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
 
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
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
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 )
 
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
 
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
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
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
 

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