SlideShare a Scribd company logo
1 of 67
Heaps & Priority Queues
Overview ,[object Object],[object Object],[object Object],[object Object],[object Object]
Complete  Binary Tree ,[object Object],[object Object],[object Object]
Array-Based  Representation of a Binary Tree Bob Mike Sam Tom Pam Joe Sue Ann Jane Mary root 0 free 10
Left Child  &  Right Child  in a  Complete,  Array-Based Binary Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Left Child  &  Right Child  in a  Complete,  Array-Based Binary Tree ,[object Object],[object Object],Bob Mike Sam Tom Pam Joe Sue Ann Jane Mary
Parent  in a  Complete, Array-Based Binary Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Parent  in a  Complete , Array-Based Binary Tree ,[object Object],Bob Mike Sam Tom Pam Joe Sue Ann Jane Mary
Array-Based Representation of a  Complete  Binary Tree ,[object Object],[object Object]
Array-Based Representation of a Complete  Binary Tree Bob Mike Sam Tom Pam Joe Sue Ann Jane Mary
Heaps ,[object Object],[object Object],[object Object],[object Object],[object Object]
Array-Based Representation of a Heap Mary Mike Sam Ann Tom Pam Sue Joe Bob Jane 3 2 1 5 4 7 6 9 8 10
Array-Based Representation of a Heap ,[object Object],[object Object],[object Object],[object Object]
The ADT Priority Queue ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
PriorityQ:  Array-Based Implementation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
PriorityQ:  Array-Based Implementation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
PriorityQ:  Public Member Function Definitions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
PriorityQ:  Retrieve & Delete ,[object Object],[object Object],[object Object],[object Object],[object Object]
PriorityQ:  Public Member Function Definition ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Semiheap ,[object Object],10 5 10 40 30 15 50 35 20 55 60 45 25 55 45 25
Rebuilding a Heap:  Basic Idea ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Retrieve & Delete:  Example ,[object Object],[object Object],5 10 40 30 15 50 35 20 55 45 60 25 move 25 to here
Rebuilding a Heap:  Example (Cont’d.) ,[object Object],[object Object],15 50 35 20 5 10 40 30 55 45 25 swap 25 with the item in this node
Rebuilding a Heap:  Example (Cont’d.) ,[object Object],[object Object],15 50 35 20 5 10 40 30 25 45 55 swap 25 with the item in this node
Rebuilding a Heap:  Example (Cont’d.) ,[object Object],[object Object],15 25 35 20 5 10 40 30 50 45 55 swap 25 with the item in this node
Rebuilding a Heap:  Example (Cont’d.) ,[object Object],[object Object],[object Object],15 40 35 20 5 10 25 30 50 45 55
PriorityQ:  Private Member Function Definition ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
PriorityQ  Insert :  Basic Idea ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
PriorityQ Insert:  Example ,[object Object],[object Object],15 40 35 20 5 10 25 30 50 45 55 47
PriorityQ Insert:  Example (Cont’d.) ,[object Object],15 40 35 20 5 10 25 30 50 45 55 47
PriorityQ Insert:  Example (Cont’d.) ,[object Object],15 40 47 20 5 10 25 30 50 45 55 35
PriorityQ Insert:  Example (Cont’d.) ,[object Object],[object Object],15 40 45 20 5 10 25 30 50 47 55 35
PriorityQ:  Public Member Function Definition ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Heap-Based PriorityQ:  Efficiency ,[object Object],[object Object],[object Object],[object Object],[object Object]
Heapsort:  Basic Idea ,[object Object],[object Object],[object Object],[object Object]
Transform an Array Into a Heap: Basic Idea ,[object Object],[object Object],[object Object],[object Object]
Transform an Array Into a Heap:  Example ,[object Object],[object Object],[object Object],7 2 4 10 9 3 5 6 5 4 3 2 1 0 4 2 7 5 3 6 7 6 9 10 rebuildHeap
Transform an Array Into a Heap:  Example ,[object Object],[object Object],rebuildHeap 9 2 4 10 7 3 5 6 5 4 3 2 1 0 4 2 9 5 3 6 7 6 7 10
Transform an Array Into a Heap:  Example ,[object Object],[object Object],rebuildHeap 9 2 4 5 7 3 10 6 5 4 3 2 1 0 4 2 9 10 3 6 7 6 7 5
Transform an Array Into a Heap:  Example ,[object Object],[object Object],rebuildHeap 3 2 4 5 7 9 10 6 5 4 3 2 1 0 4 2 3 10 9 6 7 6 7 5
Transform an Array Into a Heap:  Example ,[object Object],[object Object],[object Object],rebuildHeap 7 2 4 5 3 9 10 6 5 4 3 2 1 0 4 2 7 10 9 6 7 6 3 5
Transform an Array Into a Heap:  Example ,[object Object],[object Object],7 2 4 5 3 9 6 10 5 4 3 2 1 0 4 2 7 6 9 10 7 6 3 5
Transform an Array Into a Heap (Cont’d.) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Transform an Array Into a Heap:  C++ ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Rebuild a Heap:  C++ ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Transform a Heap Into a Sorted Array ,[object Object],[object Object],[object Object]
Transform a Heap Into a Sorted Array: Basic Idea ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],[object Object],7 2 4 5 3 9 6 10 4 2 7 6 9 10 5 4 3 2 1 0 7 6 3 5 a[ ]: Heap
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],[object Object],7 2 4 5 9 6 3 4 2 7 6 9 3 5 4 3 2 1 0 7 6 10 5 a[ ]: Semiheap Sorted rebuildHeap
Transform a Heap Into a Sorted Array:  Example ,[object Object],7 2 4 5 3 6 9 4 2 7 6 3 9 5 4 3 2 1 0 7 6 10 5 a[ ]: Becoming a Heap Sorted rebuildHeap
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],3 2 4 5 7 6 9 4 2 3 6 7 9 5 4 3 2 1 0 7 6 10 5 a[ ]: Heap Sorted
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],[object Object],3 2 4 7 6 5 4 2 3 6 7 5 5 4 3 2 1 0 7 6 10 9 a[ ]: Semiheap Sorted rebuildHeap
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],[object Object],3 2 4 5 6 7 4 2 3 6 5 7 5 4 3 2 1 0 7 6 10 9 a[ ]: Heap Sorted rebuildHeap
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],[object Object],3 2 5 6 4 7 2 3 6 5 4 5 4 3 2 1 0 7 6 10 9 a[ ]: Semiheap Sorted rebuildHeap
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],3 2 5 4 6 7 2 3 4 5 6 5 4 3 2 1 0 7 6 10 9 a[ ]: Heap Sorted
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],[object Object],3 5 4 2 7 6 3 4 5 2 5 4 3 2 1 0 7 6 10 9 a[ ]: Semiheap Sorted rebuildHeap
Transform a Heap Into a Sorted Array:  Example ,[object Object],Becoming a Heap 3 2 4 5 7 6 3 4 2 5 5 4 3 2 1 0 7 6 10 9 a[ ]: Sorted rebuildHeap
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],2 3 4 5 7 6 2 4 3 5 5 4 3 2 1 0 7 6 10 9 a[ ]: Heap Sorted
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],[object Object],3 4 2 7 6 5 4 3 2 5 4 3 2 1 0 7 6 10 9 a[ ]: Semiheap Sorted rebuildHeap
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],3 2 4 7 6 5 2 3 4 5 4 3 2 1 0 7 6 10 9 a[ ]: Heap Sorted
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],[object Object],3 2 7 6 5 4 3 2 5 4 3 2 1 0 7 6 10 9 a[ ]: Semiheap Sorted rebuildHeap
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],2 3 7 6 5 4 2 3 5 4 3 2 1 0 7 6 10 9 a[ ]: Heap Sorted
Transform a Heap Into a Sorted Array:  Example ,[object Object],[object Object],[object Object],2 7 6 5 4 3 2 5 4 3 2 1 0 7 6 10 9 a[ ]: Heap Sorted
Transform a Heap Into a Sorted Array:  Example ,[object Object],7 6 5 4 3 2 5 4 3 2 1 0 7 6 10 9 a[ ]: Sorted
Heapsort:  C++ ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Heapsort:  Efficiency ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Growth Rates for Selected Sorting Algorithms †   According to Knuth, the  average growth rate  of Insertion sort is about 0.9 times that of Selection sort and about 0.4 times that of Bubble Sort.  Also, the  average growth rate  of Quicksort is about 0.74 times that of Mergesort and about 0.5 times that of Heapsort.

More Related Content

What's hot

Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Treekhabbab_h
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)Home
 
strassen matrix multiplication algorithm
strassen matrix multiplication algorithmstrassen matrix multiplication algorithm
strassen matrix multiplication algorithmevil eye
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxAlbin562191
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSAjunnubabu
 

What's hot (20)

Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Red black tree
Red black treeRed black tree
Red black tree
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Heap sort
Heap sortHeap sort
Heap sort
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
strassen matrix multiplication algorithm
strassen matrix multiplication algorithmstrassen matrix multiplication algorithm
strassen matrix multiplication algorithm
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
B trees dbms
B trees dbmsB trees dbms
B trees dbms
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Network flow problems
Network flow problemsNetwork flow problems
Network flow problems
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Heaps
HeapsHeaps
Heaps
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Hash tables
Hash tablesHash tables
Hash tables
 

Viewers also liked (20)

Priority queues
Priority queuesPriority queues
Priority queues
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Priority queue
Priority queuePriority queue
Priority queue
 
computer notes - Priority queue
computer notes -  Priority queuecomputer notes -  Priority queue
computer notes - Priority queue
 
Heaps
HeapsHeaps
Heaps
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
 
23 priority queue
23 priority queue23 priority queue
23 priority queue
 
Heap sort
Heap sort Heap sort
Heap sort
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Heap sort
Heap sort Heap sort
Heap sort
 
Queue
QueueQueue
Queue
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Heapsort
HeapsortHeapsort
Heapsort
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Maps
MapsMaps
Maps
 

Similar to Heaps & priority queues

Unit-5 Advanced tree zxcppt
Unit-5 Advanced tree                     zxcpptUnit-5 Advanced tree                     zxcppt
Unit-5 Advanced tree zxcpptDhruvilSTATUS
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10Bianca Teşilă
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeksJinTaek Seo
 
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptxLesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptxCrisannJasmin
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of HeapMeghaj Mallick
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonLifna C.S
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-dequesRishabh Jindal
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms CourseHunt
 
CS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfCS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfssuser034ce1
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 

Similar to Heaps & priority queues (20)

Unit-5 Advanced tree zxcppt
Unit-5 Advanced tree                     zxcpptUnit-5 Advanced tree                     zxcppt
Unit-5 Advanced tree zxcppt
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 
Heaps
HeapsHeaps
Heaps
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptxLesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
 
Heap tree
Heap treeHeap tree
Heap tree
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
Ch15 Heap
Ch15 HeapCh15 Heap
Ch15 Heap
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-deques
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
CS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfCS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdf
 
Heapsort
HeapsortHeapsort
Heapsort
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

More from Pedro Hugo Valencia Morales (10)

Árboles como Estructura de Datos
Árboles como Estructura de DatosÁrboles como Estructura de Datos
Árboles como Estructura de Datos
 
C++ io manipulation
C++ io manipulationC++ io manipulation
C++ io manipulation
 
Colas de prioridad
Colas de prioridadColas de prioridad
Colas de prioridad
 
Tema4 programación generica
Tema4   programación genericaTema4   programación generica
Tema4 programación generica
 
Arboles03
Arboles03Arboles03
Arboles03
 
Arboles02
Arboles02Arboles02
Arboles02
 
Arboles01
Arboles01Arboles01
Arboles01
 
Arquitectura ssdd
Arquitectura ssddArquitectura ssdd
Arquitectura ssdd
 
Cap02 modelos1
Cap02 modelos1Cap02 modelos1
Cap02 modelos1
 
Chapter 1 slides
Chapter 1 slidesChapter 1 slides
Chapter 1 slides
 

Heaps & priority queues

  • 2.
  • 3.
  • 4. Array-Based Representation of a Binary Tree Bob Mike Sam Tom Pam Joe Sue Ann Jane Mary root 0 free 10
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. Array-Based Representation of a Complete Binary Tree Bob Mike Sam Tom Pam Joe Sue Ann Jane Mary
  • 11.
  • 12. Array-Based Representation of a Heap Mary Mike Sam Ann Tom Pam Sue Joe Bob Jane 3 2 1 5 4 7 6 9 8 10
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67. Growth Rates for Selected Sorting Algorithms † According to Knuth, the average growth rate of Insertion sort is about 0.9 times that of Selection sort and about 0.4 times that of Bubble Sort. Also, the average growth rate of Quicksort is about 0.74 times that of Mergesort and about 0.5 times that of Heapsort.