SlideShare uma empresa Scribd logo
1 de 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.

Mais conteúdo relacionado

Mais procurados (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
 

Destaque

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
 

Destaque (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais 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ă
 

Mais 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
 

Último

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
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
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
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
 

Último (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 

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.