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

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 

Dernier (20)

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 

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.