SlideShare une entreprise Scribd logo
1  sur  16
DATA STRUCTURES AND ALGORITHMS
LAB 10
Bianca Tesila
FILS, April 2014
OBJECTIVES
 Heap
 Heap Sort
HEAP: INTRODUCTION
 What is a heap - a data structure that:
 is stored as an array in memory
 can be represented/viewed as an almost complete
binary tree
 respects the heap property
HEAP: INTRODUCTION
 What is an almost complete binary tree - a
binary tree such that:
 all the leaves are at the bottom level or the bottom 2
levels
 all the leaves are in the leftmost possible positions
 all the levels are completely filled with nodes (except
for the bottom level)
HEAP: INTRODUCTION
The heap property:
 Max heaps
 For each node (except for the root): A[i] <= A[parent(i)]
 Conclusion: The root (A[1]) contains the largest element
 Min heaps
 For each node (except the root): A[i] >= A[parent(i)]
 Conclusion: The root (A[1]) contains the smallest element
!! Heap Sort uses max heaps
HEAP: DATA STRUCTURE
 Store the elements in an array - the advantage
of this method over using the usual pointers and
nodes is that there is no wasting of space due to
storing two pointer fields in each node.
 Start by taking the nodes level by level from the
top down, left to right. Then, store them in an
array.
HEAP: DATA STRUCTURE
 An array A[0..n] can be represented as a binary
tree:
 A[0] –root of the tree
 Parent of A[i] = parent(i) = A[(i-1)/2]
 Left child of A[i] = left(i) = A[2*i+1]
 Right child of A[i] = right(i) = A[2*(i+ 1)]
 Height of the heap: Θ(log n)
 Number of nodes from the root to the farthest leaf
HEAP: BASIC OPERATIONS
Insert a new element:
 add it as the last element in the heap
 however, the heap property may be broken
 if the added element is larger than its parent, you
need to find its correct position in the heap => filter
up
Filter up an element:
 compare it with its parent
 if the parent is smaller than it, stop
 else, swap it with the parent and continue
HEAP: BASIC OPERATIONS
Insert E in this heap
1. Place E in the next available position:
2. Filter up E:
HEAP: BASIC OPERATIONS
Delete ( extract min/max):
 simply remove A[0] (the root element)
 however, the heap is broken as it has no root element
 need to find a new root
 move the last element in the heap as the new root
 now, the heap property is lost (almost certainly) =>
need to filter down
Filter down an element:
 compare it to its children
 if it is larger than both children, stop
 else, swap it with the largest child and continue
HEAP: BASIC OPERATIONS
Remove C from this heap:
HEAP SORT
 convert the array you want to sort into a heap
 remove the root item (the smallest), readjust the
remaining items into a heap, and place the
removed item at the end of the heap (array)
 remove the new item in the root (the second
smallest), readjust the heap, and place the
removed item in the next to the last position and
so on
HEAP SORT
Example: HeapSort.pdf
HEAP
!! Exercise:
Implement the functions for returning the parent and the childs of a
given node, using the following signatures:
template <typename T>
int Heap<T>::parent(int index)
{
// TODO
}
template <typename T>
int Heap<T>::leftSubtree(int index)
{
// TODO
}
template <typename T>
int Heap<T>::rightSubtree(int index)
{
// TODO
}
!! Use heap.h
HEAP SORT
!! Exercise:
Implement the heapsort and test it for the
following array:
25, 17, 36, 2, 3, 100, 1, 19, 17
!! Use heap.h
HOMEWORK
Finish all the lab assignments.

Contenu connexe

Tendances (20)

Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Heap sort
Heap sort Heap sort
Heap sort
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Min priority queue
Min priority queueMin priority queue
Min priority queue
 
haskell_fp1
haskell_fp1haskell_fp1
haskell_fp1
 
F# array searching
F#  array searchingF#  array searching
F# array searching
 
Heaps
HeapsHeaps
Heaps
 
16 containers
16   containers16   containers
16 containers
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
Max priority queue
Max priority queueMax priority queue
Max priority queue
 
Heapsort
HeapsortHeapsort
Heapsort
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 

En vedette

Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11Bianca Teşilă
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5Bianca Teşilă
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7Bianca Teşilă
 
Data structures and algorithms lab9
Data structures and algorithms lab9Data structures and algorithms lab9
Data structures and algorithms lab9Bianca Teşilă
 
ICSE 2015 presentation @icse2014
ICSE 2015 presentation @icse2014ICSE 2015 presentation @icse2014
ICSE 2015 presentation @icse2014icse2015
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Benjamin Sach
 
Data structures and algorithms lab8
Data structures and algorithms lab8Data structures and algorithms lab8
Data structures and algorithms lab8Bianca Teşilă
 
A Quest for Subexponential Time Parameterized Algorithms for Planar-k-Path: F...
A Quest for Subexponential Time Parameterized Algorithms for Planar-k-Path: F...A Quest for Subexponential Time Parameterized Algorithms for Planar-k-Path: F...
A Quest for Subexponential Time Parameterized Algorithms for Planar-k-Path: F...cseiitgn
 
CPSC 125 ch 5sec 2
CPSC 125 ch 5sec 2CPSC 125 ch 5sec 2
CPSC 125 ch 5sec 2guest862df4e
 
CPSC 125 Ch 5 Sec 1
CPSC 125 Ch 5 Sec 1CPSC 125 Ch 5 Sec 1
CPSC 125 Ch 5 Sec 1David Wood
 
Graph isomorphism
Graph isomorphismGraph isomorphism
Graph isomorphismCore Condor
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theoryChuckie Balbuena
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 

En vedette (18)

Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
 
Maggie and deamberli graohs
Maggie and deamberli graohsMaggie and deamberli graohs
Maggie and deamberli graohs
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7
 
Data structures and algorithms lab9
Data structures and algorithms lab9Data structures and algorithms lab9
Data structures and algorithms lab9
 
ICSE 2015 presentation @icse2014
ICSE 2015 presentation @icse2014ICSE 2015 presentation @icse2014
ICSE 2015 presentation @icse2014
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)
 
Data structures and algorithms lab8
Data structures and algorithms lab8Data structures and algorithms lab8
Data structures and algorithms lab8
 
A Quest for Subexponential Time Parameterized Algorithms for Planar-k-Path: F...
A Quest for Subexponential Time Parameterized Algorithms for Planar-k-Path: F...A Quest for Subexponential Time Parameterized Algorithms for Planar-k-Path: F...
A Quest for Subexponential Time Parameterized Algorithms for Planar-k-Path: F...
 
CPSC 125 ch 5sec 2
CPSC 125 ch 5sec 2CPSC 125 ch 5sec 2
CPSC 125 ch 5sec 2
 
CPSC 125 Ch 5 Sec 1
CPSC 125 Ch 5 Sec 1CPSC 125 Ch 5 Sec 1
CPSC 125 Ch 5 Sec 1
 
Graph isomorphism
Graph isomorphismGraph isomorphism
Graph isomorphism
 
Hamiltonian path
Hamiltonian pathHamiltonian path
Hamiltonian path
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Similaire à Data structures and algorithms lab10

heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsssuser7319f8
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeksJinTaek Seo
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingTraian Rebedea
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptSudhaPatel11
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptAmitShou
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 
presentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingpresentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingBindiya syed
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptxZainiXh
 

Similaire à Data structures and algorithms lab10 (20)

Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithms
 
heapsort
 heapsort heapsort
heapsort
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
Heap tree
Heap treeHeap tree
Heap tree
 
presentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingpresentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashing
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
Leftlist Heap-1.pdf
Leftlist Heap-1.pdfLeftlist Heap-1.pdf
Leftlist Heap-1.pdf
 
Ch15 Heap
Ch15 HeapCh15 Heap
Ch15 Heap
 
Data structures notes
Data structures notesData structures notes
Data structures notes
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
 
Heapsort
HeapsortHeapsort
Heapsort
 
HeapSort
HeapSortHeapSort
HeapSort
 

Plus de Bianca Teşilă

Akka Streams - An Adobe data-intensive story
Akka Streams - An Adobe data-intensive storyAkka Streams - An Adobe data-intensive story
Akka Streams - An Adobe data-intensive storyBianca Teşilă
 
Data structures and algorithms lab6
Data structures and algorithms lab6Data structures and algorithms lab6
Data structures and algorithms lab6Bianca Teşilă
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4Bianca Teşilă
 
Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3Bianca Teşilă
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2Bianca Teşilă
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1Bianca Teşilă
 

Plus de Bianca Teşilă (6)

Akka Streams - An Adobe data-intensive story
Akka Streams - An Adobe data-intensive storyAkka Streams - An Adobe data-intensive story
Akka Streams - An Adobe data-intensive story
 
Data structures and algorithms lab6
Data structures and algorithms lab6Data structures and algorithms lab6
Data structures and algorithms lab6
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4
 
Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1
 

Dernier

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 

Dernier (20)

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 

Data structures and algorithms lab10

  • 1. DATA STRUCTURES AND ALGORITHMS LAB 10 Bianca Tesila FILS, April 2014
  • 3. HEAP: INTRODUCTION  What is a heap - a data structure that:  is stored as an array in memory  can be represented/viewed as an almost complete binary tree  respects the heap property
  • 4. HEAP: INTRODUCTION  What is an almost complete binary tree - a binary tree such that:  all the leaves are at the bottom level or the bottom 2 levels  all the leaves are in the leftmost possible positions  all the levels are completely filled with nodes (except for the bottom level)
  • 5. HEAP: INTRODUCTION The heap property:  Max heaps  For each node (except for the root): A[i] <= A[parent(i)]  Conclusion: The root (A[1]) contains the largest element  Min heaps  For each node (except the root): A[i] >= A[parent(i)]  Conclusion: The root (A[1]) contains the smallest element !! Heap Sort uses max heaps
  • 6. HEAP: DATA STRUCTURE  Store the elements in an array - the advantage of this method over using the usual pointers and nodes is that there is no wasting of space due to storing two pointer fields in each node.  Start by taking the nodes level by level from the top down, left to right. Then, store them in an array.
  • 7. HEAP: DATA STRUCTURE  An array A[0..n] can be represented as a binary tree:  A[0] –root of the tree  Parent of A[i] = parent(i) = A[(i-1)/2]  Left child of A[i] = left(i) = A[2*i+1]  Right child of A[i] = right(i) = A[2*(i+ 1)]  Height of the heap: Θ(log n)  Number of nodes from the root to the farthest leaf
  • 8. HEAP: BASIC OPERATIONS Insert a new element:  add it as the last element in the heap  however, the heap property may be broken  if the added element is larger than its parent, you need to find its correct position in the heap => filter up Filter up an element:  compare it with its parent  if the parent is smaller than it, stop  else, swap it with the parent and continue
  • 9. HEAP: BASIC OPERATIONS Insert E in this heap 1. Place E in the next available position: 2. Filter up E:
  • 10. HEAP: BASIC OPERATIONS Delete ( extract min/max):  simply remove A[0] (the root element)  however, the heap is broken as it has no root element  need to find a new root  move the last element in the heap as the new root  now, the heap property is lost (almost certainly) => need to filter down Filter down an element:  compare it to its children  if it is larger than both children, stop  else, swap it with the largest child and continue
  • 11. HEAP: BASIC OPERATIONS Remove C from this heap:
  • 12. HEAP SORT  convert the array you want to sort into a heap  remove the root item (the smallest), readjust the remaining items into a heap, and place the removed item at the end of the heap (array)  remove the new item in the root (the second smallest), readjust the heap, and place the removed item in the next to the last position and so on
  • 14. HEAP !! Exercise: Implement the functions for returning the parent and the childs of a given node, using the following signatures: template <typename T> int Heap<T>::parent(int index) { // TODO } template <typename T> int Heap<T>::leftSubtree(int index) { // TODO } template <typename T> int Heap<T>::rightSubtree(int index) { // TODO } !! Use heap.h
  • 15. HEAP SORT !! Exercise: Implement the heapsort and test it for the following array: 25, 17, 36, 2, 3, 100, 1, 19, 17 !! Use heap.h
  • 16. HOMEWORK Finish all the lab assignments.