SlideShare a Scribd company logo
5
Most read
6
Most read
13
Most read
Heap (Max/ Min /Heapify)
Prepared By:
Mr. Zeeshan Mubeen (Senior Lecturer, RSCI)
Unit No. 10
What is a Heap?
• A Heap is a special Tree-based data structure in which the tree
is a complete binary tree. It’s a complete tree (All levels are
completely filled except possibly the last level and the last
level has all keys as left as possible).
• Generally, Heaps can be of two types:
• Max Heap
• Min Heap
Max Heap:
• In a Max-Heap the key present at the root node must be
greatest among the keys present at all of it’s children. The
same property must be recursively true for all sub-trees in that
Binary Tree.
Min Heap:
• In a Min-Heap the key present at the root node must be
minimum among the keys present at all of it’s children. The
same property must be recursively true for all sub-trees in that
Binary Tree.
Traversal Method:
• The traversal method use to achieve Array representation is
Level Order
Arr[(i-1)/2]
Returns the parent
node
Arr[(2*i)+1]
Returns the left child
node
Arr[(2*i)+2]
Returns the right child
node
Below table shows indexes of other nodes for the ith
node, i.e., Arr[i]:
Build Max Heap
• Build Max-Heap : Using MAX-HEAPIFY() we can construct a
max-heap by starting with the last node that has children
(which occurs at A.length/2 the elements the array A.
BUILD-MAX-HEAP(A)
A.heapsize = A.length
for i = A.length/2 downto 0
MAX-HEAPIFY(A,i)
Max-Heapify:
• Max-Heapify : Given a tree that is a heap except for node
i,Max-Heapify function arranges node i and it’s subtrees to
satisfy the heap property.
Build max Heap:
Build max Heap….
Build max Heap….
Build max Heap….
Heap Sort:
Heap Sort….
Heap Sort….
Heap Sort….
Heap Sort….
Heap Sort….
Heap Sort….
Heap Sort….
How Heap Sort Works?
1. Since the tree satisfies Max-Heap property, then the largest
item is stored at the root node.
2. Swap: Remove the root element and put at the end of the
array (nth position) Put the last item of the tree (heap) at
the vacant place.
3. Remove: Reduce the size of the heap by 1.
4. Heapify: Heapify the root element again so that we have the
highest element at root.
5. The process is repeated until all the items of the list are
sorted.

More Related Content

PPTX
Heaps & its operation -Max Heap, Min Heap
PPTX
heapsort
PPTX
Heap sort
PDF
Heap, Types of Heap, Insertion and Deletion
PPTX
Binary Heap Tree, Data Structure
PPTX
05 heap 20161110_jintaeks
PPTX
HEAP SORT .pptx
PPT
Heapsort ppt
Heaps & its operation -Max Heap, Min Heap
heapsort
Heap sort
Heap, Types of Heap, Insertion and Deletion
Binary Heap Tree, Data Structure
05 heap 20161110_jintaeks
HEAP SORT .pptx
Heapsort ppt

Similar to Max Heap, Min Heap, Heapify, Heap sort, Build max Heap (20)

PDF
Heap and heapsort
PPTX
Algorithms - "heap sort"
PPTX
Lecture 07 - HeapSort.pptx
PPT
Cis435 week05
PDF
Heap Hand note
PPTX
Lecture 3 - Data Structure File Organization
PDF
Heap Tree.pdf
PPTX
Heap_data_structures_in_data_steruc.pptx
PPTX
heap Sort Algorithm
PPTX
Array implementation & Construction of Heap
PPTX
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
PPT
Analysis of Algorithms-Heapsort
PPTX
Heapify algorithm
PPTX
Heap Sort sorting algorithm analysis of algorithm.pptx
PDF
Heapsort quick sort
PPT
Heap Sort || Heapify Method || Build Max Heap Algorithm
PPT
PPTX
Heap_Sort1.pptx
PDF
Binary heap in data structures algorithms.pdf
Heap and heapsort
Algorithms - "heap sort"
Lecture 07 - HeapSort.pptx
Cis435 week05
Heap Hand note
Lecture 3 - Data Structure File Organization
Heap Tree.pdf
Heap_data_structures_in_data_steruc.pptx
heap Sort Algorithm
Array implementation & Construction of Heap
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
Analysis of Algorithms-Heapsort
Heapify algorithm
Heap Sort sorting algorithm analysis of algorithm.pptx
Heapsort quick sort
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap_Sort1.pptx
Binary heap in data structures algorithms.pdf
Ad

Recently uploaded (20)

PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
Introduction to machine learning and Linear Models
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PDF
Business Analytics and business intelligence.pdf
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
Computer network topology notes for revision
PPT
ISS -ESG Data flows What is ESG and HowHow
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
Business Ppt On Nestle.pptx huunnnhhgfvu
Introduction to machine learning and Linear Models
climate analysis of Dhaka ,Banglades.pptx
1_Introduction to advance data techniques.pptx
Reliability_Chapter_ presentation 1221.5784
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
Acceptance and paychological effects of mandatory extra coach I classes.pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
Clinical guidelines as a resource for EBP(1).pdf
Miokarditis (Inflamasi pada Otot Jantung)
Business Analytics and business intelligence.pdf
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Computer network topology notes for revision
ISS -ESG Data flows What is ESG and HowHow
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Introduction-to-Cloud-ComputingFinal.pptx
Data_Analytics_and_PowerBI_Presentation.pptx
Ad

Max Heap, Min Heap, Heapify, Heap sort, Build max Heap

  • 1. Heap (Max/ Min /Heapify) Prepared By: Mr. Zeeshan Mubeen (Senior Lecturer, RSCI) Unit No. 10
  • 2. What is a Heap? • A Heap is a special Tree-based data structure in which the tree is a complete binary tree. It’s a complete tree (All levels are completely filled except possibly the last level and the last level has all keys as left as possible). • Generally, Heaps can be of two types: • Max Heap • Min Heap
  • 3. Max Heap: • In a Max-Heap the key present at the root node must be greatest among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree.
  • 4. Min Heap: • In a Min-Heap the key present at the root node must be minimum among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree.
  • 5. Traversal Method: • The traversal method use to achieve Array representation is Level Order Arr[(i-1)/2] Returns the parent node Arr[(2*i)+1] Returns the left child node Arr[(2*i)+2] Returns the right child node Below table shows indexes of other nodes for the ith node, i.e., Arr[i]:
  • 6. Build Max Heap • Build Max-Heap : Using MAX-HEAPIFY() we can construct a max-heap by starting with the last node that has children (which occurs at A.length/2 the elements the array A. BUILD-MAX-HEAP(A) A.heapsize = A.length for i = A.length/2 downto 0 MAX-HEAPIFY(A,i)
  • 7. Max-Heapify: • Max-Heapify : Given a tree that is a heap except for node i,Max-Heapify function arranges node i and it’s subtrees to satisfy the heap property.
  • 20. How Heap Sort Works? 1. Since the tree satisfies Max-Heap property, then the largest item is stored at the root node. 2. Swap: Remove the root element and put at the end of the array (nth position) Put the last item of the tree (heap) at the vacant place. 3. Remove: Reduce the size of the heap by 1. 4. Heapify: Heapify the root element again so that we have the highest element at root. 5. The process is repeated until all the items of the list are sorted.