SlideShare a Scribd company logo
1
Submitted
by
Mrs.R.Vishnupriya.,
Assistant Professor of IT
E.M.G Yadava Womens College.,
 In many applications, we need a scheduler
A program that decides which job to run next
 Often the scheduler a simple FIFO queue
As in bank tellers, DMV, grocery stores etc
 But often a more sophisticated policy needed
Routers or switches use priorities on data packets
File transfers vs. streaming video latency requirement
Processors use job priorities
 Priority Queue is a more refined form of such a
scheduler.
2
 A set of elements with priorities, or keys
 Basic operations:
 insert (element)
 element = deleteMin (or deleteMax)
 No find operation!
 Sometimes also:
 increaseKey (element, amount)
 decreaseKey (element, amount)
 remove (element)
 newQueue = union (oldQueue1, oldQueue2)
3
 Unordered linked list
 insert is O(1), deleteMin is O(n)
 Ordered linked list
 deleteMin is O(1), insert is O(n)
 Balanced binary search tree
 insert, deleteMin are O(log n)
 increaseKey, decreaseKey, remove are O(log n)
 union is O(n)
 Most implementations are based on heaps . . .
4
 Tree Structure: A complete binary tree
 One element per node
 Only vacancies are at the bottom, to the right
 Tree filled level by level.
 Such a tree with n nodes has height O(log n)

5
 Heap Property
One element per node
key(parent) < key(child) at all nodes everywhere
Therefore, min key is at the root
Which of the following has the heap property?
6
 percolateUp
 used for decreaseKey, insert
percolateUp (e):
while key(e) < key(parent(e))
swap e with its parent
7
 percolateDown
 used for increaseKey, deleteMin
percolateDown (e):
while key(e) > key(some child of e)
swap e with its smallest child
8
 Must know where the element is; no find!
 DecreaseKey
key(element) = key(element) – amount
percolateUp (element)
 IncreaseKey
key(element) = key(element) + amount
percolateDown (element)
9
 add element as a new leaf
 (in a binary heap, new leaf goes at end of array)
 percolateUp (element)
 O( tree height ) = O(log n) for binary heap
10
 Insert 14: add new leaf, then percolateUp
 Finish the insert operation on this example.
11
 element to be returned is at the root
 to delete it from heap:
 swap root with some leaf
 (in a binary heap, the last leaf in the array)
 percolateDown (new root)
 O( tree height ) = O(log n) for binary heap
12
 deleteMin. Hole at the root.
 Put last element in it, percolateDown.
13
 Heap best visualized as a tree, but easier to
implement as an array
 Index arithmetic to compute positions of parent
and children, instead of pointers.
14
 Array implementation
15
1
2 3
4 5 6 7
8 9
parent = child/2
Lchild = 2·parent
Rchild = 2·parent+1
 A naïve method for sorting with a heap.
 O(N log N)
 Improvement: Build the whole heap at once
 Start with the array in arbitrary order
 Then fix it with the following routine
16
for (int i=0; i<n; i++)
H.insert(a[i]);
for (int i=0; i<n; i++)
H.deleteMin(x);
a[i] = x;
template <class Comparable>
BinaryHeap<Comparable>::buildHeap( )
for (int i=currentSize/2; i>0; i--)
percolateDown(i);
17
Fix the bottom level
Fix the next to bottom level
Fix the top level
 For each i, the cost is the height of the subtree at i
 For perfect binary trees of height h, sum:
18
15
11
3
13
10
6 1
14 18
2
8
9
5
4
17
7
12 16
 insert: O(log n)
 deleteMin: O(log n)
 increaseKey: O(log n)
 decreaseKey: O(log n)
 remove: O(log n)
 buildHeap: O(n)
 advantage: simple array representation, no pointers
 disadvantage: union is still O(n)
19
 Heap Sort
 Graph algorithms (Shortest Paths, MST)
 Event driven simulation
 Tracking top K items in a stream of data
 d-ary Heaps:
 Insert O(logd n)
 deleteMin O(d logd n)
 Optimize value of d for insert/deleteMin
20
 Binary Heaps great for insert and deleteMin but do not
support merge operation
 Leftist Heap is a priority queue data structure that also
supports merge of two heaps in O(log n) time.
 Leftist heaps introduce an elegant idea even if you never
use merging.
 There are several ways to define the height of a node.
 In order to achieve their merge property, leftist heaps use
NPL (null path length), a seemingly arbitrary definition,
whose intuition will become clear later.
21
 NPL(X) : length of shortest path from X to a null pointer
 Leftist heap : heap-ordered binary tree in which
NPL(leftchild(X)) >= NPLl(rightchild(X)) for every node X.
 Therefore, npl(X) = length of the right path from X
 also, NPL(root)  log(N+1)
 proof: show by induction that
NPL(root) = r implies tree has at least 2r
- 1 nodes
22
 NPL(X) : length of shortest path from X to a null pointer
 Two examples. Which one is a valid Leftist Heap?
23
 NPL(root)  log(N+1)
proof: show by induction that
NPL(root) = r implies tree has at least 2r
- 1 nodes
The key operation in Leftist Heaps is Merge.
Given two leftist heaps, H1 and H2, merge them into a
single leftist heap in O(log n) time.
24
 Let H1 and H2 be two heaps to be merged
Assume root key of H1 <= root key of H2
Recursively merge H2 with right child of H1, and make the
result the new right child of H1
Swap the left and right children of H1 to restore the leftist
property, if necessary
25
 Result of merging H2 with right child of H1
26
 Make the result the new right child of H1
27
 Because of leftist violation at root, swap the children
 This is the final outcome
28
 Insert: create a single node heap, and merge
deleteMin: delete root, and merge the children
Each operation takes O(log n) because root’s NPL bound
29
 insert: merge with a new 1-node heap
 deleteMin: delete root, merge the two subtrees
 All in worst-case O(log n) time
30
Merge(t1, t2)
if t1.empty() then return t2;
if t2.empty() then return t1;
if (t1.key > t2.key) then swap(t1, t2);
t1.right = Merge(t1.right, t2);
if npl(t1.right) > npl(t1.left) then
swap(t1.left, t1.right);
npl(t1) = npl(t1.right) + 1;
return t1
 skew heaps
 like leftist heaps, but no balance condition
 always swap children of root after merge
 amortized (not per-operation) time bounds
 binomial queues
 binomial queue = collection of heap-ordered
“binomial trees”, each with size a power of 2
 merge looks just like adding integers in base 2
 very flexible set of operations
 Fibonacci heaps
 variation of binomial queues
 Decrease Key runs in O(1) amortized time,
other operations in O(log n) amortized time
31
Ad

More Related Content

Similar to Heaps in Data Structure binary tree concepts (20)

ESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdf
ESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdf
ESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdf
LusArajo20
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
chidabdu
 
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
Traian Rebedea
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
RohitkumarYadav80
 
RProgrammingassignmenthPPT_05.07.24.pptx
RProgrammingassignmenthPPT_05.07.24.pptxRProgrammingassignmenthPPT_05.07.24.pptx
RProgrammingassignmenthPPT_05.07.24.pptx
R Programming Assignment Help
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
Sorting2
Sorting2Sorting2
Sorting2
Saurabh Mishra
 
lecture 11
lecture 11lecture 11
lecture 11
sajinsc
 
Heap tree
Heap treeHeap tree
Heap tree
JananiJ19
 
Q
QQ
Q
guest9b2176
 
Heaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesHeaps & Adaptable priority Queues
Heaps & Adaptable priority Queues
Priyanka Rana
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-deques
Rishabh Jindal
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
multimedia9
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
chidabdu
 
Review session2
Review session2Review session2
Review session2
NEEDY12345
 
Soft Heaps
Soft HeapsSoft Heaps
Soft Heaps
⌨️ Andrey Goder
 
Heaps
HeapsHeaps
Heaps
Hafiz Atif Amin
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
GauravPandey43518
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
Abhishek Tirkey
 
lecture 12
lecture 12lecture 12
lecture 12
sajinsc
 
ESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdf
ESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdf
ESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdfESINF05-Heaps.pdf
LusArajo20
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
chidabdu
 
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
Traian Rebedea
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
lecture 11
lecture 11lecture 11
lecture 11
sajinsc
 
Heaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesHeaps & Adaptable priority Queues
Heaps & Adaptable priority Queues
Priyanka Rana
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-deques
Rishabh Jindal
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
multimedia9
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
chidabdu
 
Review session2
Review session2Review session2
Review session2
NEEDY12345
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
GauravPandey43518
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
Abhishek Tirkey
 
lecture 12
lecture 12lecture 12
lecture 12
sajinsc
 

More from Rvishnupriya2 (11)

Data Structures and Algorithms are function in various method
Data Structures and Algorithms are function in various methodData Structures and Algorithms are function in various method
Data Structures and Algorithms are function in various method
Rvishnupriya2
 
Programming in C and Decision Making Branching
Programming in C and Decision Making BranchingProgramming in C and Decision Making Branching
Programming in C and Decision Making Branching
Rvishnupriya2
 
Advanced Software Engineering.ppt
Advanced Software Engineering.pptAdvanced Software Engineering.ppt
Advanced Software Engineering.ppt
Rvishnupriya2
 
Cluster Analysis.pptx
Cluster Analysis.pptxCluster Analysis.pptx
Cluster Analysis.pptx
Rvishnupriya2
 
Data Mining.ppt
Data Mining.pptData Mining.ppt
Data Mining.ppt
Rvishnupriya2
 
Data Mining Concepts and Techniques.ppt
Data Mining Concepts and Techniques.pptData Mining Concepts and Techniques.ppt
Data Mining Concepts and Techniques.ppt
Rvishnupriya2
 
Data Mining Concepts and Techniques.ppt
Data Mining Concepts and Techniques.pptData Mining Concepts and Techniques.ppt
Data Mining Concepts and Techniques.ppt
Rvishnupriya2
 
Rdbms
Rdbms Rdbms
Rdbms
Rvishnupriya2
 
Programming in C
Programming in CProgramming in C
Programming in C
Rvishnupriya2
 
Computer architecture
Computer architectureComputer architecture
Computer architecture
Rvishnupriya2
 
Computer organization
Computer organizationComputer organization
Computer organization
Rvishnupriya2
 
Data Structures and Algorithms are function in various method
Data Structures and Algorithms are function in various methodData Structures and Algorithms are function in various method
Data Structures and Algorithms are function in various method
Rvishnupriya2
 
Programming in C and Decision Making Branching
Programming in C and Decision Making BranchingProgramming in C and Decision Making Branching
Programming in C and Decision Making Branching
Rvishnupriya2
 
Advanced Software Engineering.ppt
Advanced Software Engineering.pptAdvanced Software Engineering.ppt
Advanced Software Engineering.ppt
Rvishnupriya2
 
Cluster Analysis.pptx
Cluster Analysis.pptxCluster Analysis.pptx
Cluster Analysis.pptx
Rvishnupriya2
 
Data Mining Concepts and Techniques.ppt
Data Mining Concepts and Techniques.pptData Mining Concepts and Techniques.ppt
Data Mining Concepts and Techniques.ppt
Rvishnupriya2
 
Data Mining Concepts and Techniques.ppt
Data Mining Concepts and Techniques.pptData Mining Concepts and Techniques.ppt
Data Mining Concepts and Techniques.ppt
Rvishnupriya2
 
Computer architecture
Computer architectureComputer architecture
Computer architecture
Rvishnupriya2
 
Computer organization
Computer organizationComputer organization
Computer organization
Rvishnupriya2
 
Ad

Recently uploaded (20)

Process Mining as Enabler for Digital Transformations
Process Mining as Enabler for Digital TransformationsProcess Mining as Enabler for Digital Transformations
Process Mining as Enabler for Digital Transformations
Process mining Evangelist
 
Dynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics DynamicsDynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics Dynamics
heyoubro69
 
Introduction to systems thinking tools_Eng.pdf
Introduction to systems thinking tools_Eng.pdfIntroduction to systems thinking tools_Eng.pdf
Introduction to systems thinking tools_Eng.pdf
AbdurahmanAbd
 
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdfZ14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Fariborz Seyedloo
 
Automation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success storyAutomation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success story
Process mining Evangelist
 
RAG Chatbot using AWS Bedrock and Streamlit Framework
RAG Chatbot using AWS Bedrock and Streamlit FrameworkRAG Chatbot using AWS Bedrock and Streamlit Framework
RAG Chatbot using AWS Bedrock and Streamlit Framework
apanneer
 
AWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptxAWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptx
bharatkumarbhojwani
 
report (maam dona subject).pptxhsgwiswhs
report (maam dona subject).pptxhsgwiswhsreport (maam dona subject).pptxhsgwiswhs
report (maam dona subject).pptxhsgwiswhs
AngelPinedaTaguinod
 
L1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptxL1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptx
38NoopurPatel
 
Lagos School of Programming Final Project Updated.pdf
Lagos School of Programming Final Project Updated.pdfLagos School of Programming Final Project Updated.pdf
Lagos School of Programming Final Project Updated.pdf
benuju2016
 
Understanding Complex Development Processes
Understanding Complex Development ProcessesUnderstanding Complex Development Processes
Understanding Complex Development Processes
Process mining Evangelist
 
Language Learning App Data Research by Globibo [2025]
Language Learning App Data Research by Globibo [2025]Language Learning App Data Research by Globibo [2025]
Language Learning App Data Research by Globibo [2025]
globibo
 
real illuminati Uganda agent 0782561496/0756664682
real illuminati Uganda agent 0782561496/0756664682real illuminati Uganda agent 0782561496/0756664682
real illuminati Uganda agent 0782561496/0756664682
way to join real illuminati Agent In Kampala Call/WhatsApp+256782561496/0756664682
 
Automated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptxAutomated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptx
handrymaharjan23
 
hersh's midterm project.pdf music retail and distribution
hersh's midterm project.pdf music retail and distributionhersh's midterm project.pdf music retail and distribution
hersh's midterm project.pdf music retail and distribution
hershtara1
 
HershAggregator (2).pdf musicretaildistribution
HershAggregator (2).pdf musicretaildistributionHershAggregator (2).pdf musicretaildistribution
HershAggregator (2).pdf musicretaildistribution
hershtara1
 
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
muhammed84essa
 
AI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptxAI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptx
AyeshaJalil6
 
Mining a Global Trade Process with Data Science - Microsoft
Mining a Global Trade Process with Data Science - MicrosoftMining a Global Trade Process with Data Science - Microsoft
Mining a Global Trade Process with Data Science - Microsoft
Process mining Evangelist
 
Controlling Financial Processes at a Municipality
Controlling Financial Processes at a MunicipalityControlling Financial Processes at a Municipality
Controlling Financial Processes at a Municipality
Process mining Evangelist
 
Process Mining as Enabler for Digital Transformations
Process Mining as Enabler for Digital TransformationsProcess Mining as Enabler for Digital Transformations
Process Mining as Enabler for Digital Transformations
Process mining Evangelist
 
Dynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics DynamicsDynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics Dynamics
heyoubro69
 
Introduction to systems thinking tools_Eng.pdf
Introduction to systems thinking tools_Eng.pdfIntroduction to systems thinking tools_Eng.pdf
Introduction to systems thinking tools_Eng.pdf
AbdurahmanAbd
 
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdfZ14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Fariborz Seyedloo
 
Automation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success storyAutomation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success story
Process mining Evangelist
 
RAG Chatbot using AWS Bedrock and Streamlit Framework
RAG Chatbot using AWS Bedrock and Streamlit FrameworkRAG Chatbot using AWS Bedrock and Streamlit Framework
RAG Chatbot using AWS Bedrock and Streamlit Framework
apanneer
 
AWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptxAWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptx
bharatkumarbhojwani
 
report (maam dona subject).pptxhsgwiswhs
report (maam dona subject).pptxhsgwiswhsreport (maam dona subject).pptxhsgwiswhs
report (maam dona subject).pptxhsgwiswhs
AngelPinedaTaguinod
 
L1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptxL1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptx
38NoopurPatel
 
Lagos School of Programming Final Project Updated.pdf
Lagos School of Programming Final Project Updated.pdfLagos School of Programming Final Project Updated.pdf
Lagos School of Programming Final Project Updated.pdf
benuju2016
 
Language Learning App Data Research by Globibo [2025]
Language Learning App Data Research by Globibo [2025]Language Learning App Data Research by Globibo [2025]
Language Learning App Data Research by Globibo [2025]
globibo
 
Automated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptxAutomated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptx
handrymaharjan23
 
hersh's midterm project.pdf music retail and distribution
hersh's midterm project.pdf music retail and distributionhersh's midterm project.pdf music retail and distribution
hersh's midterm project.pdf music retail and distribution
hershtara1
 
HershAggregator (2).pdf musicretaildistribution
HershAggregator (2).pdf musicretaildistributionHershAggregator (2).pdf musicretaildistribution
HershAggregator (2).pdf musicretaildistribution
hershtara1
 
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
muhammed84essa
 
AI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptxAI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptx
AyeshaJalil6
 
Mining a Global Trade Process with Data Science - Microsoft
Mining a Global Trade Process with Data Science - MicrosoftMining a Global Trade Process with Data Science - Microsoft
Mining a Global Trade Process with Data Science - Microsoft
Process mining Evangelist
 
Controlling Financial Processes at a Municipality
Controlling Financial Processes at a MunicipalityControlling Financial Processes at a Municipality
Controlling Financial Processes at a Municipality
Process mining Evangelist
 
Ad

Heaps in Data Structure binary tree concepts

  • 2.  In many applications, we need a scheduler A program that decides which job to run next  Often the scheduler a simple FIFO queue As in bank tellers, DMV, grocery stores etc  But often a more sophisticated policy needed Routers or switches use priorities on data packets File transfers vs. streaming video latency requirement Processors use job priorities  Priority Queue is a more refined form of such a scheduler. 2
  • 3.  A set of elements with priorities, or keys  Basic operations:  insert (element)  element = deleteMin (or deleteMax)  No find operation!  Sometimes also:  increaseKey (element, amount)  decreaseKey (element, amount)  remove (element)  newQueue = union (oldQueue1, oldQueue2) 3
  • 4.  Unordered linked list  insert is O(1), deleteMin is O(n)  Ordered linked list  deleteMin is O(1), insert is O(n)  Balanced binary search tree  insert, deleteMin are O(log n)  increaseKey, decreaseKey, remove are O(log n)  union is O(n)  Most implementations are based on heaps . . . 4
  • 5.  Tree Structure: A complete binary tree  One element per node  Only vacancies are at the bottom, to the right  Tree filled level by level.  Such a tree with n nodes has height O(log n)  5
  • 6.  Heap Property One element per node key(parent) < key(child) at all nodes everywhere Therefore, min key is at the root Which of the following has the heap property? 6
  • 7.  percolateUp  used for decreaseKey, insert percolateUp (e): while key(e) < key(parent(e)) swap e with its parent 7
  • 8.  percolateDown  used for increaseKey, deleteMin percolateDown (e): while key(e) > key(some child of e) swap e with its smallest child 8
  • 9.  Must know where the element is; no find!  DecreaseKey key(element) = key(element) – amount percolateUp (element)  IncreaseKey key(element) = key(element) + amount percolateDown (element) 9
  • 10.  add element as a new leaf  (in a binary heap, new leaf goes at end of array)  percolateUp (element)  O( tree height ) = O(log n) for binary heap 10
  • 11.  Insert 14: add new leaf, then percolateUp  Finish the insert operation on this example. 11
  • 12.  element to be returned is at the root  to delete it from heap:  swap root with some leaf  (in a binary heap, the last leaf in the array)  percolateDown (new root)  O( tree height ) = O(log n) for binary heap 12
  • 13.  deleteMin. Hole at the root.  Put last element in it, percolateDown. 13
  • 14.  Heap best visualized as a tree, but easier to implement as an array  Index arithmetic to compute positions of parent and children, instead of pointers. 14
  • 15.  Array implementation 15 1 2 3 4 5 6 7 8 9 parent = child/2 Lchild = 2·parent Rchild = 2·parent+1
  • 16.  A naïve method for sorting with a heap.  O(N log N)  Improvement: Build the whole heap at once  Start with the array in arbitrary order  Then fix it with the following routine 16 for (int i=0; i<n; i++) H.insert(a[i]); for (int i=0; i<n; i++) H.deleteMin(x); a[i] = x; template <class Comparable> BinaryHeap<Comparable>::buildHeap( ) for (int i=currentSize/2; i>0; i--) percolateDown(i);
  • 17. 17 Fix the bottom level Fix the next to bottom level Fix the top level
  • 18.  For each i, the cost is the height of the subtree at i  For perfect binary trees of height h, sum: 18 15 11 3 13 10 6 1 14 18 2 8 9 5 4 17 7 12 16
  • 19.  insert: O(log n)  deleteMin: O(log n)  increaseKey: O(log n)  decreaseKey: O(log n)  remove: O(log n)  buildHeap: O(n)  advantage: simple array representation, no pointers  disadvantage: union is still O(n) 19
  • 20.  Heap Sort  Graph algorithms (Shortest Paths, MST)  Event driven simulation  Tracking top K items in a stream of data  d-ary Heaps:  Insert O(logd n)  deleteMin O(d logd n)  Optimize value of d for insert/deleteMin 20
  • 21.  Binary Heaps great for insert and deleteMin but do not support merge operation  Leftist Heap is a priority queue data structure that also supports merge of two heaps in O(log n) time.  Leftist heaps introduce an elegant idea even if you never use merging.  There are several ways to define the height of a node.  In order to achieve their merge property, leftist heaps use NPL (null path length), a seemingly arbitrary definition, whose intuition will become clear later. 21
  • 22.  NPL(X) : length of shortest path from X to a null pointer  Leftist heap : heap-ordered binary tree in which NPL(leftchild(X)) >= NPLl(rightchild(X)) for every node X.  Therefore, npl(X) = length of the right path from X  also, NPL(root)  log(N+1)  proof: show by induction that NPL(root) = r implies tree has at least 2r - 1 nodes 22
  • 23.  NPL(X) : length of shortest path from X to a null pointer  Two examples. Which one is a valid Leftist Heap? 23
  • 24.  NPL(root)  log(N+1) proof: show by induction that NPL(root) = r implies tree has at least 2r - 1 nodes The key operation in Leftist Heaps is Merge. Given two leftist heaps, H1 and H2, merge them into a single leftist heap in O(log n) time. 24
  • 25.  Let H1 and H2 be two heaps to be merged Assume root key of H1 <= root key of H2 Recursively merge H2 with right child of H1, and make the result the new right child of H1 Swap the left and right children of H1 to restore the leftist property, if necessary 25
  • 26.  Result of merging H2 with right child of H1 26
  • 27.  Make the result the new right child of H1 27
  • 28.  Because of leftist violation at root, swap the children  This is the final outcome 28
  • 29.  Insert: create a single node heap, and merge deleteMin: delete root, and merge the children Each operation takes O(log n) because root’s NPL bound 29
  • 30.  insert: merge with a new 1-node heap  deleteMin: delete root, merge the two subtrees  All in worst-case O(log n) time 30 Merge(t1, t2) if t1.empty() then return t2; if t2.empty() then return t1; if (t1.key > t2.key) then swap(t1, t2); t1.right = Merge(t1.right, t2); if npl(t1.right) > npl(t1.left) then swap(t1.left, t1.right); npl(t1) = npl(t1.right) + 1; return t1
  • 31.  skew heaps  like leftist heaps, but no balance condition  always swap children of root after merge  amortized (not per-operation) time bounds  binomial queues  binomial queue = collection of heap-ordered “binomial trees”, each with size a power of 2  merge looks just like adding integers in base 2  very flexible set of operations  Fibonacci heaps  variation of binomial queues  Decrease Key runs in O(1) amortized time, other operations in O(log n) amortized time 31
  翻译: