SlideShare a Scribd company logo
1 of 12
11/05/08 Shaily Kabir,Dept. of CSE, DU 1
Heap
11/05/08 1
11/05/08 Shaily Kabir,Dept. of CSE, DU 2
Heap
• A heap is a complete binary tree except the bottom level adjusted to the left.
• The value of each node is greater than that of its two children. (Max Heap)
• The value of each node is less than that of its two children. (Min Heap)
• Height of the heap is log2n.
• Example
100
1621
14 19 17
100
1721
14 19 16
Figure: Not a Heap Figure: A Heap
11/05/08 Shaily Kabir,Dept. of CSE, DU 3
Heap Implementation
• We can use an array (due to the regular structure or completeness of binary tree).
• For a node N with location i, the following factors can be calculated.
1. Left child of N is in location (2 * i).
2. Right child of N is in location (2 * i + 1).
3. Parent of N is in location [i/2].
• Example
1 2 3 4 5 6 7 8 9
100 21 17 14 19 16
100
1721
14 19 16
Figure: Heap and Its Array Representation
11/05/08 Shaily Kabir,Dept. of CSE, DU 4
Heap Operations
(1) Insertion
(2) Delete-max or delete-min
(3) Make-heap (organize an arbitrary array as a heap)
Insertion
• Find the left-most open position and insert the new element NewElement.
• while ( NewElement > Its Parent ) exchange them.
• Example:
Figure: Item 70 is inserted.
97
88
55
48
70
97
88
55
70
48
97
88
70
55
48
11/05/08 Shaily Kabir,Dept. of CSE, DU 5
Algorithm: INSHEAP(Heap, N, Item)
1. Set N := N +1 and Ptr := N
2. Repeat steps 3 to 6 while Ptr > 1
3. Set Par := Ptr/2
4. If Item < Heap[Par], then
Set Heap[Ptr] := Item and Return.
1. Set Heap[Ptr] := Heap[Par]
2. Set Ptr := Par
3. Heap[1] := Item
4. Return.
Here,
Heap = Max heap
N = Total no. of items in the array
Item = New item to be inserted
The Complexity Analysis of Insert Operation
• Insertion takes O( log2n ) times.
• Reasons: 1) No. of operations = O( height of the heap)
2) Height of the heap: O ( log2 n )
11/05/08 Shaily Kabir,Dept. of CSE, DU 6
Delete-Max
• Replace the root node by the last node at bottom level maintaining completeness.
• While ( new root < its children ) exchange it with the greater of its children.
• Example:
50
40 23
18 30 20 21
21
40 23
18 30 20
40
21 23
18 30 20
40
30 23
18 21 20
Figure: Item 50 (root) is deleted.
11/05/08 Shaily Kabir,Dept. of CSE, DU 7
Algorithm: DELHEAP(Heap, N, Item)
1. Set Item := Heap[1] and Set Last:=Heap[N] and N:=N-1
2. Set Ptr := 1, Left := 2 and Right := 3
3. Repeat steps 5 to 6 while Right<=N
4. If Last >= Heap[Left] and Last >= Heap[Right] then
Set Tree[Ptr] := Last and Return.
5. If Heap[Right] <= Heap[Left] then
Set Heap[Ptr] := Heap[Left] and Ptr := Left.
Else Set Heap[Ptr] := Heap[Right] and Ptr := Right
6. Set Left := 2 * Ptr and Right := Left + 1
1. If Left = N and if Last < Heap[Left] then Set Heap[Ptr] := Heap[Left] and Ptr := Left.
2. Set Heap[Ptr] := Last
3. Return
11/05/08 Shaily Kabir,Dept. of CSE, DU 8
The Complexity Analysis of Delete Operation
• Deletion takes O( log2n ) times.
• Reasons: 1) No. of operations = O( height of the heap)
2) Height of the heap: O ( log2n )
11/05/08 Shaily Kabir,Dept. of CSE, DU 9
Variants of Heap
1. Binary Heap
2. Binomial Heap
3. Fibonacci Heap
4. Pairing Heap
5. Treap
6. Beap
Application of Heap
1. HeapSort
2. Priority Queue
3. Selection Algorithms
4. Graph Algorithms
11/05/08 Shaily Kabir,Dept. of CSE, DU 10
Heap Sort
• Suppose an array with N elements is given.
• The heap sort algorithm consists of the following two steps:
Step 1: Build a heap H out of the elements of A
Step 2: Repeatedly delete the root element of H
• For sorting in descending order, max heap must be created. For ascending order, min
heap must be created.
Algorithm: HeapSort(A, N)
1. Repeat for J=1 to N-1
Call INSHEAP(A, J, A[J+1])
2. Repeat while N>1
(a) Call DELHEAP(A, N, ITEM)
(b) Set A[N+1]:= ITEM
3. Exit
Here,
A = Max Heap
N = Total No. of Items in A
11/05/08 Shaily Kabir,Dept. of CSE, DU 11
Complexity of Heapsort
• Heap Sort takes O(nlog2n ) times.
Reasons:
1. BuildHeap from the initial array requires O(nlog2n).
2. Delete-Max n times requires n x O(log2n) = O(nlog2n).
11/05/08 Shaily Kabir,Dept. of CSE, DU 1211/05/08 Shaily Kabir,Dept. of CSE, DU 12
END!!!

More Related Content

What's hot

lecture 5
lecture 5lecture 5
lecture 5sajinsc
 
150970116028 2140705
150970116028 2140705150970116028 2140705
150970116028 2140705Manoj Shahu
 
Heap Data Structure
 Heap Data Structure Heap Data Structure
Heap Data StructureSaumya Som
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeksJinTaek Seo
 
DCC2014 - Fully Online Grammar Compression in Constant Space
DCC2014 - Fully Online Grammar Compression in Constant SpaceDCC2014 - Fully Online Grammar Compression in Constant Space
DCC2014 - Fully Online Grammar Compression in Constant SpaceYasuo Tabei
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1Jatin Miglani
 
CPM2013-tabei201306
CPM2013-tabei201306CPM2013-tabei201306
CPM2013-tabei201306Yasuo Tabei
 
Properties of logarithms
Properties of logarithmsProperties of logarithms
Properties of logarithmsgregcross22
 
Flyby: Improved Dense Matrix Multiplication-(Tom Vacek, Thomson Reuters)
Flyby: Improved Dense Matrix Multiplication-(Tom Vacek, Thomson Reuters)Flyby: Improved Dense Matrix Multiplication-(Tom Vacek, Thomson Reuters)
Flyby: Improved Dense Matrix Multiplication-(Tom Vacek, Thomson Reuters)Spark Summit
 
CS 151 Date time lecture
CS 151 Date time lectureCS 151 Date time lecture
CS 151 Date time lectureRudy Martinez
 

What's hot (20)

Heapsort using Heap
Heapsort using HeapHeapsort using Heap
Heapsort using Heap
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Heap sort
Heap sortHeap sort
Heap sort
 
lecture 5
lecture 5lecture 5
lecture 5
 
150970116028 2140705
150970116028 2140705150970116028 2140705
150970116028 2140705
 
Heap Data Structure
 Heap Data Structure Heap Data Structure
Heap Data Structure
 
Sortsearch
SortsearchSortsearch
Sortsearch
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 
Oral-2
Oral-2Oral-2
Oral-2
 
DCC2014 - Fully Online Grammar Compression in Constant Space
DCC2014 - Fully Online Grammar Compression in Constant SpaceDCC2014 - Fully Online Grammar Compression in Constant Space
DCC2014 - Fully Online Grammar Compression in Constant Space
 
Heapsort 1
Heapsort 1Heapsort 1
Heapsort 1
 
Linear sorting
Linear sortingLinear sorting
Linear sorting
 
IGCSE Function
 IGCSE Function IGCSE Function
IGCSE Function
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1
 
CPM2013-tabei201306
CPM2013-tabei201306CPM2013-tabei201306
CPM2013-tabei201306
 
Properties of logarithms
Properties of logarithmsProperties of logarithms
Properties of logarithms
 
Flyby: Improved Dense Matrix Multiplication-(Tom Vacek, Thomson Reuters)
Flyby: Improved Dense Matrix Multiplication-(Tom Vacek, Thomson Reuters)Flyby: Improved Dense Matrix Multiplication-(Tom Vacek, Thomson Reuters)
Flyby: Improved Dense Matrix Multiplication-(Tom Vacek, Thomson Reuters)
 
Big O Notation
Big O NotationBig O Notation
Big O Notation
 
CS 151 Date time lecture
CS 151 Date time lectureCS 151 Date time lecture
CS 151 Date time lecture
 

Viewers also liked

Report of industrial training
Report of industrial trainingReport of industrial training
Report of industrial trainingHimadri Sen Gupta
 
Fit Camp květen H-klub.cz
Fit Camp květen H-klub.czFit Camp květen H-klub.cz
Fit Camp květen H-klub.czViktor Moravec
 
Quality model ppt
Quality model pptQuality model ppt
Quality model pptDelly Win
 
Unicity "Make Life Better" - ClearStart30 helps you understand about Detox & ...
Unicity "Make Life Better" - ClearStart30 helps you understand about Detox & ...Unicity "Make Life Better" - ClearStart30 helps you understand about Detox & ...
Unicity "Make Life Better" - ClearStart30 helps you understand about Detox & ...Kheng-Kiat Toh
 
DarkTeespringProfits FB_Masterclasseespringprofits fb masterclass
DarkTeespringProfits FB_Masterclasseespringprofits fb masterclassDarkTeespringProfits FB_Masterclasseespringprofits fb masterclass
DarkTeespringProfits FB_Masterclasseespringprofits fb masterclassPhát Nguyễn
 
Ap for b.tech. (mechanical) Assignment Problem
Ap for b.tech. (mechanical) Assignment Problem Ap for b.tech. (mechanical) Assignment Problem
Ap for b.tech. (mechanical) Assignment Problem Prashant Khandelwal
 
design and analysis of composite leaf spring for light weight vehicle
 design and analysis of composite leaf spring for light weight vehicle design and analysis of composite leaf spring for light weight vehicle
design and analysis of composite leaf spring for light weight vehicleEr Deepak Sharma
 
plastic_fabrication processes
 plastic_fabrication processes plastic_fabrication processes
plastic_fabrication processesEr Deepak Sharma
 

Viewers also liked (15)

Queue
QueueQueue
Queue
 
Fitcamp en
Fitcamp enFitcamp en
Fitcamp en
 
Report of industrial training
Report of industrial trainingReport of industrial training
Report of industrial training
 
Fit Camp květen H-klub.cz
Fit Camp květen H-klub.czFit Camp květen H-klub.cz
Fit Camp květen H-klub.cz
 
Quality model ppt
Quality model pptQuality model ppt
Quality model ppt
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
14 recursion
14 recursion14 recursion
14 recursion
 
Game theory lecture
Game theory   lectureGame theory   lecture
Game theory lecture
 
Unicity "Make Life Better" - ClearStart30 helps you understand about Detox & ...
Unicity "Make Life Better" - ClearStart30 helps you understand about Detox & ...Unicity "Make Life Better" - ClearStart30 helps you understand about Detox & ...
Unicity "Make Life Better" - ClearStart30 helps you understand about Detox & ...
 
DarkTeespringProfits FB_Masterclasseespringprofits fb masterclass
DarkTeespringProfits FB_Masterclasseespringprofits fb masterclassDarkTeespringProfits FB_Masterclasseespringprofits fb masterclass
DarkTeespringProfits FB_Masterclasseespringprofits fb masterclass
 
Ap for b.tech. (mechanical) Assignment Problem
Ap for b.tech. (mechanical) Assignment Problem Ap for b.tech. (mechanical) Assignment Problem
Ap for b.tech. (mechanical) Assignment Problem
 
Tp for b.tech. (mechanical)
Tp for b.tech. (mechanical)Tp for b.tech. (mechanical)
Tp for b.tech. (mechanical)
 
Chapter 1. dpv a. lal revised
Chapter 1. dpv a. lal revisedChapter 1. dpv a. lal revised
Chapter 1. dpv a. lal revised
 
design and analysis of composite leaf spring for light weight vehicle
 design and analysis of composite leaf spring for light weight vehicle design and analysis of composite leaf spring for light weight vehicle
design and analysis of composite leaf spring for light weight vehicle
 
plastic_fabrication processes
 plastic_fabrication processes plastic_fabrication processes
plastic_fabrication processes
 

Similar to Heap

Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortReetesh Gupta
 
Ch2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxCh2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxMohammed472103
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
Array 2
Array 2Array 2
Array 2Abbott
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
Heapsortokkay
HeapsortokkayHeapsortokkay
HeapsortokkayRemesha
 
Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)Shiang-Yun Yang
 
Shell sorting
Shell sortingShell sorting
Shell sortingTUC
 
Decision Science.pdf
Decision Science.pdfDecision Science.pdf
Decision Science.pdfpandeyaman577
 

Similar to Heap (20)

Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
Lec23
Lec23Lec23
Lec23
 
Ijcm
IjcmIjcm
Ijcm
 
sparse set.pdf
sparse set.pdfsparse set.pdf
sparse set.pdf
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
 
Ch2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxCh2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptx
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Array 2
Array 2Array 2
Array 2
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
 
Lec35
Lec35Lec35
Lec35
 
Heapsortokkay
HeapsortokkayHeapsortokkay
Heapsortokkay
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 
Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)
 
Shell sorting
Shell sortingShell sorting
Shell sorting
 
Decision Science.pdf
Decision Science.pdfDecision Science.pdf
Decision Science.pdf
 

Recently uploaded

Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
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
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
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
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
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
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 

Recently uploaded (20)

Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
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
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
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 )
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
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
 
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
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
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
 

Heap

  • 1. 11/05/08 Shaily Kabir,Dept. of CSE, DU 1 Heap 11/05/08 1
  • 2. 11/05/08 Shaily Kabir,Dept. of CSE, DU 2 Heap • A heap is a complete binary tree except the bottom level adjusted to the left. • The value of each node is greater than that of its two children. (Max Heap) • The value of each node is less than that of its two children. (Min Heap) • Height of the heap is log2n. • Example 100 1621 14 19 17 100 1721 14 19 16 Figure: Not a Heap Figure: A Heap
  • 3. 11/05/08 Shaily Kabir,Dept. of CSE, DU 3 Heap Implementation • We can use an array (due to the regular structure or completeness of binary tree). • For a node N with location i, the following factors can be calculated. 1. Left child of N is in location (2 * i). 2. Right child of N is in location (2 * i + 1). 3. Parent of N is in location [i/2]. • Example 1 2 3 4 5 6 7 8 9 100 21 17 14 19 16 100 1721 14 19 16 Figure: Heap and Its Array Representation
  • 4. 11/05/08 Shaily Kabir,Dept. of CSE, DU 4 Heap Operations (1) Insertion (2) Delete-max or delete-min (3) Make-heap (organize an arbitrary array as a heap) Insertion • Find the left-most open position and insert the new element NewElement. • while ( NewElement > Its Parent ) exchange them. • Example: Figure: Item 70 is inserted. 97 88 55 48 70 97 88 55 70 48 97 88 70 55 48
  • 5. 11/05/08 Shaily Kabir,Dept. of CSE, DU 5 Algorithm: INSHEAP(Heap, N, Item) 1. Set N := N +1 and Ptr := N 2. Repeat steps 3 to 6 while Ptr > 1 3. Set Par := Ptr/2 4. If Item < Heap[Par], then Set Heap[Ptr] := Item and Return. 1. Set Heap[Ptr] := Heap[Par] 2. Set Ptr := Par 3. Heap[1] := Item 4. Return. Here, Heap = Max heap N = Total no. of items in the array Item = New item to be inserted The Complexity Analysis of Insert Operation • Insertion takes O( log2n ) times. • Reasons: 1) No. of operations = O( height of the heap) 2) Height of the heap: O ( log2 n )
  • 6. 11/05/08 Shaily Kabir,Dept. of CSE, DU 6 Delete-Max • Replace the root node by the last node at bottom level maintaining completeness. • While ( new root < its children ) exchange it with the greater of its children. • Example: 50 40 23 18 30 20 21 21 40 23 18 30 20 40 21 23 18 30 20 40 30 23 18 21 20 Figure: Item 50 (root) is deleted.
  • 7. 11/05/08 Shaily Kabir,Dept. of CSE, DU 7 Algorithm: DELHEAP(Heap, N, Item) 1. Set Item := Heap[1] and Set Last:=Heap[N] and N:=N-1 2. Set Ptr := 1, Left := 2 and Right := 3 3. Repeat steps 5 to 6 while Right<=N 4. If Last >= Heap[Left] and Last >= Heap[Right] then Set Tree[Ptr] := Last and Return. 5. If Heap[Right] <= Heap[Left] then Set Heap[Ptr] := Heap[Left] and Ptr := Left. Else Set Heap[Ptr] := Heap[Right] and Ptr := Right 6. Set Left := 2 * Ptr and Right := Left + 1 1. If Left = N and if Last < Heap[Left] then Set Heap[Ptr] := Heap[Left] and Ptr := Left. 2. Set Heap[Ptr] := Last 3. Return
  • 8. 11/05/08 Shaily Kabir,Dept. of CSE, DU 8 The Complexity Analysis of Delete Operation • Deletion takes O( log2n ) times. • Reasons: 1) No. of operations = O( height of the heap) 2) Height of the heap: O ( log2n )
  • 9. 11/05/08 Shaily Kabir,Dept. of CSE, DU 9 Variants of Heap 1. Binary Heap 2. Binomial Heap 3. Fibonacci Heap 4. Pairing Heap 5. Treap 6. Beap Application of Heap 1. HeapSort 2. Priority Queue 3. Selection Algorithms 4. Graph Algorithms
  • 10. 11/05/08 Shaily Kabir,Dept. of CSE, DU 10 Heap Sort • Suppose an array with N elements is given. • The heap sort algorithm consists of the following two steps: Step 1: Build a heap H out of the elements of A Step 2: Repeatedly delete the root element of H • For sorting in descending order, max heap must be created. For ascending order, min heap must be created. Algorithm: HeapSort(A, N) 1. Repeat for J=1 to N-1 Call INSHEAP(A, J, A[J+1]) 2. Repeat while N>1 (a) Call DELHEAP(A, N, ITEM) (b) Set A[N+1]:= ITEM 3. Exit Here, A = Max Heap N = Total No. of Items in A
  • 11. 11/05/08 Shaily Kabir,Dept. of CSE, DU 11 Complexity of Heapsort • Heap Sort takes O(nlog2n ) times. Reasons: 1. BuildHeap from the initial array requires O(nlog2n). 2. Delete-Max n times requires n x O(log2n) = O(nlog2n).
  • 12. 11/05/08 Shaily Kabir,Dept. of CSE, DU 1211/05/08 Shaily Kabir,Dept. of CSE, DU 12 END!!!