SlideShare a Scribd company logo
Class No.25  Data Structures http://ecomputernotes.com
BuildHeap The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a heap out of N keys. for( i = N/2; i > 0; i-- ) percolateDown(i); http://ecomputernotes.com
BuildHeap i = 15/2 = 7 65 19 21 14 24 26 13 15 68 16 65 21 19 26 14 16 68 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 12 15 5 70 12 i   i Why I=n/2? http://ecomputernotes.com
BuildHeap i = 15/2 = 7 65 19 21 14 24 26 13 15 12 16 65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 68 15 5 70 68 i   i http://ecomputernotes.com
BuildHeap i = 6 65 19 21 14 24 26 13 15 12 16 65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 68 15 5 70 68 i   i http://ecomputernotes.com
BuildHeap i = 5 65 5 21 14 24 26 13 15 12 16 65 21 5 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i   i http://ecomputernotes.com
BuildHeap i = 4 65 5 14 21 24 26 13 15 12 16 65 14 5 26 21 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i   i http://ecomputernotes.com
BuildHeap i = 3 65 5 14 21 24 13 26 15 12 16 65 14 5 13 21 16 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i   i http://ecomputernotes.com
BuildHeap i = 2 65 16 14 21 24 13 26 15 12 32 65 14 16 13 21 32 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 5 5 12 15 4 19 13 70 14 68 15 19 70 68 i   i http://ecomputernotes.com
BuildHeap i = 1 65 16 14 21 31 24 26 15 12 32 65 14 16 24 21 32 12 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 13 11 5 5 12 15 4 19 13 70 14 68 15 19 70 68 i   i http://ecomputernotes.com
BuildHeap Min heap 5 16 14 21 31 24 26 15 65 32 5 14 16 24 21 32 65 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 13 11 12 12 12 15 4 19 13 70 14 68 15 19 70 68 http://ecomputernotes.com
Other Heap Operations decreaseKey(p, delta) :  lowers the value of the key at position ‘p’ by the amount ‘delta’. Since this might violate the heap order, the heap must be reorganized with percolate up (in min heap) or down (in max heap). increaseKey(p, delta) :  opposite of decreaseKey. remove(p) :  removes the node at position p from the heap. This is done by first decreaseKey(p, ) and then performing deleteMin(). http://ecomputernotes.com
Heap code in C++ template <class eType> class Heap { public: Heap( int capacity = 100 ); void insert( const eType & x ); void deleteMin( eType & minItem ); const eType & getMin( ); bool isEmpty( ); bool isFull( ); int Heap<eType>::getSize( ); http://ecomputernotes.com
Heap code in C++ private: int currentSize; // Number of elements in heap eType* array; // The heap array int capacity; void percolateDown( int hole ); }; http://ecomputernotes.com
Heap code in C++ #include &quot;Heap.h“ template <class eType> Heap<eType>::Heap( int capacity ) { array = new etype[capacity + 1]; currentSize=0; } http://ecomputernotes.com
Heap code in C++ // Insert item x into the heap, maintaining heap // order. Duplicates are allowed. template <class eType> bool Heap<eType>::insert( const eType & x ) { if( isFull( ) ) { cout << &quot;insert - Heap is full.&quot; << endl; return 0; } // Percolate up int hole = ++currentSize; for(; hole > 1 && x < array[hole/2 ]; hole /= 2) array[ hole ] = array[ hole / 2 ]; array[hole] = x; } http://ecomputernotes.com
Heap code in C++ template <class eType> void Heap<eType>::deleteMin( eType & minItem ) { if( isEmpty( ) ) { cout <<  &quot; heap is empty. “ << endl ; return; } minItem = array[ 1 ]; array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); } http://ecomputernotes.com

More Related Content

PPT
Computer notes - Build Heap
ODP
C workshop day 6
DOCX
แผนการจัดการเรียนรู้ที่ ๓
TXT
Influencias admon
DOCX
Wap to implement bitwise operators
PDF
Practicas 1.4
PDF
Raphaël and You
PPTX
Device Modeling of 3INPUT COMPARATOR using PSpice
Computer notes - Build Heap
C workshop day 6
แผนการจัดการเรียนรู้ที่ ๓
Influencias admon
Wap to implement bitwise operators
Practicas 1.4
Raphaël and You
Device Modeling of 3INPUT COMPARATOR using PSpice

What's hot (14)

PPT
Tilting Google Maps and MissileLauncher
PDF
Seaborn graphing present
PDF
Exercicis selectivitat
PDF
AI&ML Conference 2019 - Deep Learning from zero to hero
PDF
Clock For My
PPT
Open Cv Tutorial Ii
PDF
TC7S08F PSpice Model (Free SPICE Model)
DOC
PDF
Advanced Topics in Haskell
PDF
Predictive Analytics Powered By Process Mining: It’s The Process, Stupid!
KEY
真っ黒Scheme
PPTX
Self Evaluation Test
Tilting Google Maps and MissileLauncher
Seaborn graphing present
Exercicis selectivitat
AI&ML Conference 2019 - Deep Learning from zero to hero
Clock For My
Open Cv Tutorial Ii
TC7S08F PSpice Model (Free SPICE Model)
Advanced Topics in Haskell
Predictive Analytics Powered By Process Mining: It’s The Process, Stupid!
真っ黒Scheme
Self Evaluation Test
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 17
PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 26
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 24
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 34
PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 10
PPT
computer notes - Data Structures - 13
PPT
computer notes - Data Structures - 15
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 9
PPT
computer notes - Data Structures - 1
computer notes - Data Structures - 20
computer notes - Data Structures - 17
computer notes - Data Structures - 7
computer notes - Data Structures - 5
computer notes - Data Structures - 29
computer notes - Data Structures - 22
computer notes - Data Structures - 26
computer notes - Data Structures - 8
computer notes - Data Structures - 11
computer notes - Data Structures - 24
computer notes - Data Structures - 31
computer notes - Data Structures - 34
computer notes - Data Structures - 32
computer notes - Data Structures - 10
computer notes - Data Structures - 13
computer notes - Data Structures - 15
computer notes - Data Structures - 18
computer notes - Data Structures - 27
computer notes - Data Structures - 9
computer notes - Data Structures - 1
Ad

Similar to computer notes - Data Structures - 25 (20)

PPT
Computer notes data structures - 9
PDF
LEC 8-DS ALGO(heaps).pdf
PPT
Computer notes - Mergesort
PPTX
(Slightly) Smarter Smart Pointers
PPT
Computer notes - Sorting
PPT
computer notes - Data Structures - 38
PPT
computer notes - Data Structures - 39
PPTX
Build Heap Step by Step
PDF
How to master C++
TXT
Saii log
PDF
The Ring programming language version 1.6 book - Part 79 of 189
PDF
Fast and accurate metrics. Is it actually possible?
PDF
ps with notes required to tackelt project
DOCX
Fatima Aliasgher Portfolio
PDF
CGo for fun and profit
PPT
computer notes - Data Structures - 16
PPTX
리눅스 드라이버 실습 #3
PDF
Everything you didn't know you needed
PDF
Creating Responsive Experiences
KEY
10 Catalyst Tips
Computer notes data structures - 9
LEC 8-DS ALGO(heaps).pdf
Computer notes - Mergesort
(Slightly) Smarter Smart Pointers
Computer notes - Sorting
computer notes - Data Structures - 38
computer notes - Data Structures - 39
Build Heap Step by Step
How to master C++
Saii log
The Ring programming language version 1.6 book - Part 79 of 189
Fast and accurate metrics. Is it actually possible?
ps with notes required to tackelt project
Fatima Aliasgher Portfolio
CGo for fun and profit
computer notes - Data Structures - 16
리눅스 드라이버 실습 #3
Everything you didn't know you needed
Creating Responsive Experiences
10 Catalyst Tips

More from ecomputernotes (18)

PPT
computer notes - Data Structures - 30
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 4
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 14
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 4
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 35
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 14
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
sap open course for s4hana steps from ECC to s4
PDF
KodekX | Application Modernization Development
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
Electronic commerce courselecture one. Pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
sap open course for s4hana steps from ECC to s4
KodekX | Application Modernization Development
Mobile App Security Testing_ A Comprehensive Guide.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology

computer notes - Data Structures - 25

  • 1. Class No.25 Data Structures http://ecomputernotes.com
  • 2. BuildHeap The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a heap out of N keys. for( i = N/2; i > 0; i-- ) percolateDown(i); http://ecomputernotes.com
  • 3. BuildHeap i = 15/2 = 7 65 19 21 14 24 26 13 15 68 16 65 21 19 26 14 16 68 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 12 15 5 70 12 i  i Why I=n/2? http://ecomputernotes.com
  • 4. BuildHeap i = 15/2 = 7 65 19 21 14 24 26 13 15 12 16 65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 68 15 5 70 68 i  i http://ecomputernotes.com
  • 5. BuildHeap i = 6 65 19 21 14 24 26 13 15 12 16 65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 68 15 5 70 68 i  i http://ecomputernotes.com
  • 6. BuildHeap i = 5 65 5 21 14 24 26 13 15 12 16 65 21 5 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i  i http://ecomputernotes.com
  • 7. BuildHeap i = 4 65 5 14 21 24 26 13 15 12 16 65 14 5 26 21 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i  i http://ecomputernotes.com
  • 8. BuildHeap i = 3 65 5 14 21 24 13 26 15 12 16 65 14 5 13 21 16 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i  i http://ecomputernotes.com
  • 9. BuildHeap i = 2 65 16 14 21 24 13 26 15 12 32 65 14 16 13 21 32 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 5 5 12 15 4 19 13 70 14 68 15 19 70 68 i  i http://ecomputernotes.com
  • 10. BuildHeap i = 1 65 16 14 21 31 24 26 15 12 32 65 14 16 24 21 32 12 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 13 11 5 5 12 15 4 19 13 70 14 68 15 19 70 68 i  i http://ecomputernotes.com
  • 11. BuildHeap Min heap 5 16 14 21 31 24 26 15 65 32 5 14 16 24 21 32 65 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 13 11 12 12 12 15 4 19 13 70 14 68 15 19 70 68 http://ecomputernotes.com
  • 12. Other Heap Operations decreaseKey(p, delta) : lowers the value of the key at position ‘p’ by the amount ‘delta’. Since this might violate the heap order, the heap must be reorganized with percolate up (in min heap) or down (in max heap). increaseKey(p, delta) : opposite of decreaseKey. remove(p) : removes the node at position p from the heap. This is done by first decreaseKey(p, ) and then performing deleteMin(). http://ecomputernotes.com
  • 13. Heap code in C++ template <class eType> class Heap { public: Heap( int capacity = 100 ); void insert( const eType & x ); void deleteMin( eType & minItem ); const eType & getMin( ); bool isEmpty( ); bool isFull( ); int Heap<eType>::getSize( ); http://ecomputernotes.com
  • 14. Heap code in C++ private: int currentSize; // Number of elements in heap eType* array; // The heap array int capacity; void percolateDown( int hole ); }; http://ecomputernotes.com
  • 15. Heap code in C++ #include &quot;Heap.h“ template <class eType> Heap<eType>::Heap( int capacity ) { array = new etype[capacity + 1]; currentSize=0; } http://ecomputernotes.com
  • 16. Heap code in C++ // Insert item x into the heap, maintaining heap // order. Duplicates are allowed. template <class eType> bool Heap<eType>::insert( const eType & x ) { if( isFull( ) ) { cout << &quot;insert - Heap is full.&quot; << endl; return 0; } // Percolate up int hole = ++currentSize; for(; hole > 1 && x < array[hole/2 ]; hole /= 2) array[ hole ] = array[ hole / 2 ]; array[hole] = x; } http://ecomputernotes.com
  • 17. Heap code in C++ template <class eType> void Heap<eType>::deleteMin( eType & minItem ) { if( isEmpty( ) ) { cout << &quot; heap is empty. “ << endl ; return; } minItem = array[ 1 ]; array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); } http://ecomputernotes.com

Editor's Notes

  • #3: Star of lecture 31
  • #7: End of lecture 30
  • #18: End of lecture 31, start of 32