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

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 lab11
Bianca Teşilă
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
Bianca Teşilă
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7
Bianca Teşilă
 
Data structures and algorithms lab9
Data structures and algorithms lab9Data structures and algorithms lab9
Data structures and algorithms lab9
Bianca Teşilă
 
ICSE 2015 presentation @icse2014
ICSE 2015 presentation @icse2014ICSE 2015 presentation @icse2014
ICSE 2015 presentation @icse2014
icse2015
 
Data structures and algorithms lab8
Data structures and algorithms lab8Data structures and algorithms lab8
Data structures and algorithms lab8
Bianca Teşilă
 
CPSC 125 ch 5sec 2
CPSC 125 ch 5sec 2CPSC 125 ch 5sec 2
CPSC 125 ch 5sec 2
guest862df4e
 
CPSC 125 Ch 5 Sec 1
CPSC 125 Ch 5 Sec 1CPSC 125 Ch 5 Sec 1
CPSC 125 Ch 5 Sec 1
David Wood
 
Graph isomorphism
Graph isomorphismGraph isomorphism
Graph isomorphism
Core Condor
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
Chuckie Balbuena
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
Tech_MX
 

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 (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
AmitShou
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
chidabdu
 
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
Bindiya syed
 

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
 
Heaps & its operation -Max Heap, Min Heap
Heaps & its operation -Max Heap, Min  HeapHeaps & its operation -Max Heap, Min  Heap
Heaps & its operation -Max Heap, Min Heap
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
 
Lecture 5_ Sorting and order statistics.pptx
Lecture 5_ Sorting and order statistics.pptxLecture 5_ Sorting and order statistics.pptx
Lecture 5_ Sorting and order statistics.pptx
 

Mais de Bianca Teşilă

Data structures and algorithms lab6
Data structures and algorithms lab6Data structures and algorithms lab6
Data structures and algorithms lab6
Bianca Teşilă
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4
Bianca Teşilă
 
Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3
Bianca Teşilă
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2
Bianca Teşilă
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1
Bianca 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

Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
FIDO Alliance
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
panagenda
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
UK Journal
 

Último (20)

Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 

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.