SlideShare a Scribd company logo
1
Operations on Heaps
• Maintain/Restore the max-heap property
– MAX-HEAPIFY
• Create a max-heap from an unordered array
– BUILD-MAX-HEAP
• Sort an array in place
– HEAPSORT
• Priority queues
2
Maintaining the Heap Property
• Suppose a node is smaller than a child
– Left and Right subtrees of i are max-heaps
• To eliminate the violation:
– Exchange with larger child
– Move down the tree
– Continue until node is not smaller than children
3
Example
MAX-HEAPIFY(A, 2, 10)
A[2] violates the heap property
A[2]  A[4]
A[4] violates the heap property
A[4]  A[9]
Heap property restored
4
Maintaining the Heap Property
• Assumptions:
– Left and Right
subtrees of i
are max-heaps
– A[i] may be
smaller than its
children
Alg: MAX-HEAPIFY(A, i, n)
1. l ← LEFT(i)
2. r ← RIGHT(i)
3. if l ≤ n and A[l] > A[i]
4. then largest ←l
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] ↔ A[largest]
10. MAX-HEAPIFY(A, largest, n)
Initializing A Max Heap
input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
8
4
7
6 7
8 9
3
7
10
1
11
5
2
Initializing A Max Heap
Start at rightmost array position that has a child.
8
4
7
6 7
8 9
3
7
10
1
11
5
2
Index is n/2.
Initializing A Max Heap
Move to next lower array position.
8
4
7
6 7
8 9
3
7
10
1
5
11
2
Initializing A Max Heap
8
4
7
6 7
8 9
3
7
10
1
5
11
2
Initializing A Max Heap
8
9
7
6 7
8 4
3
7
10
1
5
11
2
Initializing A Max Heap
8
9
7
6 7
8 4
3
7
10
1
5
11
2
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
10
1
5
11
2
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
10
1
5
11
2
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
10
1
5
11
Find a home for 2.
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
5
1
11
Find a home for 2.
10
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
1
11
Done, move to next lower array position.
10
5
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
1
11
10
5
Find home for 1.
11
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
10
5
Find home for 1.
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
11
10
5
Find home for 1.
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
11
10
5
Find home for 1.
Initializing A Max Heap
8
9
7
6 3
8 4
7
7
2
11
10
5
Done.
1
Time Complexity
8
7
6 3
4
7
7
10
11
5
2
9
8
1
Height of heap = h.
Number of subtrees with root at level j is <= 2 j-1.
Time for each subtree is O(h-j+1).
Complexity
Time for level j subtrees is <= 2j-1(h-j+1) = t(j).
Total time is t(1) + t(2) + … + t(h-1) = O(n).
23
Example: A 4 1 3 2 16 9 10 14 8 7
Heap Sort
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
26
Alg: HEAPSORT(A)
1. BUILD-MAX-HEAP(A)
2. for i ← length[A] downto 2
3. do exchange A[1] ↔ A[i]
4. MAX-HEAPIFY(A, 1, i - 1)
Running time: O(nlgn) --- Can be
shown to be Θ(nlgn)
O(n)
O(lgn)
n-1 times
27
The ADT Priority Queue
• A priority queue is an ADT in which items are
ordered by a priority value. The item with the
highest priority is always the next to be removed
from the queue. (Highest Priority In, First Out:
HPIFO) or (Best In, First Out: BIFO)
• Supported operations include:
– Create an empty priority queue
– Destroy a priority queue
– Determine whether a priority queue is empty
– Insert a new item into a priority queue
– Retrieve, and then delete from the priority queue the item
with the highest priority value
• We shall implement a priority queue using a heap.
Priority Queue using Max Heap
30 12 18 8 14 41 3 39
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
Priority Queue using Min Heap
30 12 18 8 14 41 3 39
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
32
Summary
• We can perform the following operations on
heaps:
– MAX-HEAPIFY O(lgn)
– BUILD-MAX-HEAP O(n)
– HEAP-SORT O(nlgn)
– MAX-HEAP-INSERT O(lgn)
– HEAP-EXTRACT-MAX O(lgn)
– HEAP-INCREASE-KEY O(lgn)
– HEAP-MAXIMUM O(1)
Average
O(lgn)
Leftist Trees
Linked binary tree.
Can do everything a heap can do and in the
same asymptotic complexity.
Can meld two leftist tree priority queues in
O(log n) time.
Extended Binary Trees
Start with any binary tree and add an
external node wherever there is an
empty subtree.
Result is an extended binary tree.
A Binary Tree
An Extended Binary Tree
number of external nodes is n+1
The Function s()
For any node x in an extended binary tree,
let s(x) be the length of a shortest path
from x to an external node in the subtree
rooted at x.
s() Values Example
s() Values Example
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
Properties of s()
If x is an external node, then s(x) = 0.
Otherwise,
s(x) = min {s(leftChild(x)),
s(rightChild(x))} + 1
Height Biased Leftist Trees
A binary tree is a (height biased) leftist tree
iff for every internal node x,
s(leftChild(x)) >= s(rightChild(x))
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
Leftist Trees--Property 1
In a leftist tree, the rightmost path is a
shortest root to external node path and
the length of this path is s(root).
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
Length of rightmost path is 2.
Leftist Trees—Property 2
The number of internal nodes is at least
2s(root) - 1
Because levels 1 through s(root) have no
external nodes.
So, s(root) <= log(n+1)
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
Levels 1 and 2 have no external nodes.
Leftist Trees—Property 3
Length of rightmost path is O(log n), where
n is the number of nodes in a leftist tree.
Follows from Properties 1 and 2.
Leftist Trees As Priority Queues
Min leftist tree … leftist tree that is a min tree.
Used as a min priority queue.
Max leftist tree … leftist tree that is a max tree.
Used as a max priority queue.
A Min Leftist Tree
8 6 9
6 8 5
4 3
2
Some Min Leftist Tree Operations
put()
remove()
meld()
initialize()
put() and remove() use meld().
51
Weight-Biased Leftist Tree
(WBLT)
• Let the weight, w(x), of node x to be the number of internal
nodes in the subtree with root x
• If x is an external node, w(x) = 0
• If x is an internal node, its weight is one more than the sum
of the weights of its children
• A binary tree is a weight-biased leftist tree (WBLT) iff at
every internal node, the w value of the left child is greater
than or equal to the w value of the right child
52
Extended Binary Tree
Figure 12.6 s and w values
When drawing two max HBLTs that are to be
melded, we will always draw the one with larger
root value on the left.
Ties are broken arbitrarily. Because of this
convention, the root of the left HBLT always
becomes the root of the final HBLT.
Also, we will shade the nodes of the HBLT on the
right.
54
Melding max HBLTs
Figure 12.7 Melding (combining) max HBLTs
To create a max HBLT
• 7, 1, 9, 11 and 2
To create a max HBLT
Initializing in O(n) Time
• create n single node max (min) leftist trees
and place them in a FIFO queue
• repeatedly remove two max (min) leftist
trees from the FIFO queue, meld them, and
put the resulting max (min) leftist tree into
the FIFO queue
• the process terminates when only 1 max
(min) leftist tree remains in the FIFO queue
• analysis is the same as for heap initialization
Complexity
• Insertion - O(log n)
• Deletion - O(log n)
• Find max – O(1)
• Melding - O(log n)
• Initialization - O(n)
Ad

Recommended

Lec24
Lec24
Nikhil Chilwant
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptx
Israr63
 
Heap Sort sorting algorithm analysis of algorithm.pptx
Heap Sort sorting algorithm analysis of algorithm.pptx
SiddhantRaut20
 
Cis435 week05
Cis435 week05
ashish bansal
 
CS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdf
ssuser034ce1
 
Heaps
Heaps
Hafiz Atif Amin
 
Heapsort ppt
Heapsort ppt
Mariam Saeed
 
Algorithms - "heap sort"
Algorithms - "heap sort"
Ra'Fat Al-Msie'deen
 
Heap_data_structures_in_data_steruc.pptx
Heap_data_structures_in_data_steruc.pptx
FabulousOneKarthik
 
Heapsortokkay
Heapsortokkay
Remesha
 
Lec23
Lec23
Nikhil Chilwant
 
Heap and heapsort
Heap and heapsort
Amit Kumar Rathi
 
Heap tree
Heap tree
JananiJ19
 
Heap_Sort1.pptx
Heap_Sort1.pptx
sandeep54552
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
Reetesh Gupta
 
CSE680-06HeapSort.ppt
CSE680-06HeapSort.ppt
AmitShou
 
Heapsort quick sort
Heapsort quick sort
Dr Sandeep Kumar Poonia
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Unit 3.ppt
Unit 3.ppt
JITTAYASHWANTHREDDY
 
Heap Hand note
Heap Hand note
Abdur Rouf
 
4 heapsort pq
4 heapsort pq
hasan Mohammad
 
Unit III Heaps.ppt
Unit III Heaps.ppt
RohitkumarYadav80
 
Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational material
AymericTaylor
 
heapsort_bydinesh
heapsort_bydinesh
Dinesh Kumar
 
heap Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Heap Sort Algorithm
Heap Sort Algorithm
Musaddiq Khan
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
ssuser034ce1
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
MouDhara1
 
CSN221_Lec_27 Computer Architecture and Microprocessor
CSN221_Lec_27 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_26 Computer Architecture and Microprocessor
CSN221_Lec_26 Computer Architecture and Microprocessor
ssuser034ce1
 

More Related Content

Similar to CS-102 BT_24_3_14 Binary Tree Lectures.pdf (20)

Heap_data_structures_in_data_steruc.pptx
Heap_data_structures_in_data_steruc.pptx
FabulousOneKarthik
 
Heapsortokkay
Heapsortokkay
Remesha
 
Lec23
Lec23
Nikhil Chilwant
 
Heap and heapsort
Heap and heapsort
Amit Kumar Rathi
 
Heap tree
Heap tree
JananiJ19
 
Heap_Sort1.pptx
Heap_Sort1.pptx
sandeep54552
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
Reetesh Gupta
 
CSE680-06HeapSort.ppt
CSE680-06HeapSort.ppt
AmitShou
 
Heapsort quick sort
Heapsort quick sort
Dr Sandeep Kumar Poonia
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Unit 3.ppt
Unit 3.ppt
JITTAYASHWANTHREDDY
 
Heap Hand note
Heap Hand note
Abdur Rouf
 
4 heapsort pq
4 heapsort pq
hasan Mohammad
 
Unit III Heaps.ppt
Unit III Heaps.ppt
RohitkumarYadav80
 
Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational material
AymericTaylor
 
heapsort_bydinesh
heapsort_bydinesh
Dinesh Kumar
 
heap Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Heap Sort Algorithm
Heap Sort Algorithm
Musaddiq Khan
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
ssuser034ce1
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
MouDhara1
 

More from ssuser034ce1 (20)

CSN221_Lec_27 Computer Architecture and Microprocessor
CSN221_Lec_27 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_26 Computer Architecture and Microprocessor
CSN221_Lec_26 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_25 Computer Architecture and Microprocessor
CSN221_Lec_25 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_36 Computer Architecture and Microprocessor
CSN221_Lec_36 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_35 Computer Architecture and Microprocessor
CSN221_Lec_35 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_34 Computer Architecture and Microprocessor
CSN221_Lec_34 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_17.pdf Multi Cycle Datapath Design
CSN221_Lec_17.pdf Multi Cycle Datapath Design
ssuser034ce1
 
CSN221_Lec_16.pdf MIPS ISA and Datapath design
CSN221_Lec_16.pdf MIPS ISA and Datapath design
ssuser034ce1
 
CSN221_Lec_15.pdf MIPS ISA and Datapath design
CSN221_Lec_15.pdf MIPS ISA and Datapath design
ssuser034ce1
 
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
ssuser034ce1
 
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
ssuser034ce1
 
CSN221_Lec_4.pdf Computer Organization & Architecture
CSN221_Lec_4.pdf Computer Organization & Architecture
ssuser034ce1
 
CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdf
ssuser034ce1
 
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdf
ssuser034ce1
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
ssuser034ce1
 
CS-102 DS-class04a Lectures DS Class.pdf
CS-102 DS-class04a Lectures DS Class.pdf
ssuser034ce1
 
CS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdf
ssuser034ce1
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
ssuser034ce1
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
ssuser034ce1
 
CSN221_Lec_27 Computer Architecture and Microprocessor
CSN221_Lec_27 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_26 Computer Architecture and Microprocessor
CSN221_Lec_26 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_25 Computer Architecture and Microprocessor
CSN221_Lec_25 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_36 Computer Architecture and Microprocessor
CSN221_Lec_36 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_35 Computer Architecture and Microprocessor
CSN221_Lec_35 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_34 Computer Architecture and Microprocessor
CSN221_Lec_34 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_17.pdf Multi Cycle Datapath Design
CSN221_Lec_17.pdf Multi Cycle Datapath Design
ssuser034ce1
 
CSN221_Lec_16.pdf MIPS ISA and Datapath design
CSN221_Lec_16.pdf MIPS ISA and Datapath design
ssuser034ce1
 
CSN221_Lec_15.pdf MIPS ISA and Datapath design
CSN221_Lec_15.pdf MIPS ISA and Datapath design
ssuser034ce1
 
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
ssuser034ce1
 
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
ssuser034ce1
 
CSN221_Lec_4.pdf Computer Organization & Architecture
CSN221_Lec_4.pdf Computer Organization & Architecture
ssuser034ce1
 
CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdf
ssuser034ce1
 
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdf
ssuser034ce1
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
ssuser034ce1
 
CS-102 DS-class04a Lectures DS Class.pdf
CS-102 DS-class04a Lectures DS Class.pdf
ssuser034ce1
 
CS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdf
ssuser034ce1
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
ssuser034ce1
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
ssuser034ce1
 
Ad

Recently uploaded (20)

DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
Introduction to sensing and Week-1.pptx
Introduction to sensing and Week-1.pptx
KNaveenKumarECE
 
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
hosseinihamid192023
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Mark Billinghurst
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
retina_biometrics ruet rajshahi bangdesh.pptx
retina_biometrics ruet rajshahi bangdesh.pptx
MdRakibulIslam697135
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
دراسة حاله لقرية تقع في جنوب غرب السودان
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Complete guidance book of Asp.Net Web API
Complete guidance book of Asp.Net Web API
Shabista Imam
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
Introduction to sensing and Week-1.pptx
Introduction to sensing and Week-1.pptx
KNaveenKumarECE
 
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
hosseinihamid192023
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Mark Billinghurst
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
retina_biometrics ruet rajshahi bangdesh.pptx
retina_biometrics ruet rajshahi bangdesh.pptx
MdRakibulIslam697135
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
دراسة حاله لقرية تقع في جنوب غرب السودان
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Ad

CS-102 BT_24_3_14 Binary Tree Lectures.pdf

  • 1. 1 Operations on Heaps • Maintain/Restore the max-heap property – MAX-HEAPIFY • Create a max-heap from an unordered array – BUILD-MAX-HEAP • Sort an array in place – HEAPSORT • Priority queues
  • 2. 2 Maintaining the Heap Property • Suppose a node is smaller than a child – Left and Right subtrees of i are max-heaps • To eliminate the violation: – Exchange with larger child – Move down the tree – Continue until node is not smaller than children
  • 3. 3 Example MAX-HEAPIFY(A, 2, 10) A[2] violates the heap property A[2]  A[4] A[4] violates the heap property A[4]  A[9] Heap property restored
  • 4. 4 Maintaining the Heap Property • Assumptions: – Left and Right subtrees of i are max-heaps – A[i] may be smaller than its children Alg: MAX-HEAPIFY(A, i, n) 1. l ← LEFT(i) 2. r ← RIGHT(i) 3. if l ≤ n and A[l] > A[i] 4. then largest ←l 5. else largest ←i 6. if r ≤ n and A[r] > A[largest] 7. then largest ←r 8. if largest  i 9. then exchange A[i] ↔ A[largest] 10. MAX-HEAPIFY(A, largest, n)
  • 5. Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 8 4 7 6 7 8 9 3 7 10 1 11 5 2
  • 6. Initializing A Max Heap Start at rightmost array position that has a child. 8 4 7 6 7 8 9 3 7 10 1 11 5 2 Index is n/2.
  • 7. Initializing A Max Heap Move to next lower array position. 8 4 7 6 7 8 9 3 7 10 1 5 11 2
  • 8. Initializing A Max Heap 8 4 7 6 7 8 9 3 7 10 1 5 11 2
  • 9. Initializing A Max Heap 8 9 7 6 7 8 4 3 7 10 1 5 11 2
  • 10. Initializing A Max Heap 8 9 7 6 7 8 4 3 7 10 1 5 11 2
  • 11. Initializing A Max Heap 8 9 7 6 3 8 4 7 7 10 1 5 11 2
  • 12. Initializing A Max Heap 8 9 7 6 3 8 4 7 7 10 1 5 11 2
  • 13. Initializing A Max Heap 8 9 7 6 3 8 4 7 7 10 1 5 11 Find a home for 2.
  • 14. Initializing A Max Heap 8 9 7 6 3 8 4 7 7 5 1 11 Find a home for 2. 10
  • 15. Initializing A Max Heap 8 9 7 6 3 8 4 7 7 2 1 11 Done, move to next lower array position. 10 5
  • 16. Initializing A Max Heap 8 9 7 6 3 8 4 7 7 2 1 11 10 5 Find home for 1.
  • 17. 11 Initializing A Max Heap 8 9 7 6 3 8 4 7 7 2 10 5 Find home for 1.
  • 18. Initializing A Max Heap 8 9 7 6 3 8 4 7 7 2 11 10 5 Find home for 1.
  • 19. Initializing A Max Heap 8 9 7 6 3 8 4 7 7 2 11 10 5 Find home for 1.
  • 20. Initializing A Max Heap 8 9 7 6 3 8 4 7 7 2 11 10 5 Done. 1
  • 21. Time Complexity 8 7 6 3 4 7 7 10 11 5 2 9 8 1 Height of heap = h. Number of subtrees with root at level j is <= 2 j-1. Time for each subtree is O(h-j+1).
  • 22. Complexity Time for level j subtrees is <= 2j-1(h-j+1) = t(j). Total time is t(1) + t(2) + … + t(h-1) = O(n).
  • 23. 23 Example: A 4 1 3 2 16 9 10 14 8 7 Heap Sort
  • 26. 26 Alg: HEAPSORT(A) 1. BUILD-MAX-HEAP(A) 2. for i ← length[A] downto 2 3. do exchange A[1] ↔ A[i] 4. MAX-HEAPIFY(A, 1, i - 1) Running time: O(nlgn) --- Can be shown to be Θ(nlgn) O(n) O(lgn) n-1 times
  • 27. 27 The ADT Priority Queue • A priority queue is an ADT in which items are ordered by a priority value. The item with the highest priority is always the next to be removed from the queue. (Highest Priority In, First Out: HPIFO) or (Best In, First Out: BIFO) • Supported operations include: – Create an empty priority queue – Destroy a priority queue – Determine whether a priority queue is empty – Insert a new item into a priority queue – Retrieve, and then delete from the priority queue the item with the highest priority value • We shall implement a priority queue using a heap.
  • 28. Priority Queue using Max Heap 30 12 18 8 14 41 3 39
  • 30. Priority Queue using Min Heap 30 12 18 8 14 41 3 39
  • 32. 32 Summary • We can perform the following operations on heaps: – MAX-HEAPIFY O(lgn) – BUILD-MAX-HEAP O(n) – HEAP-SORT O(nlgn) – MAX-HEAP-INSERT O(lgn) – HEAP-EXTRACT-MAX O(lgn) – HEAP-INCREASE-KEY O(lgn) – HEAP-MAXIMUM O(1) Average O(lgn)
  • 33. Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity. Can meld two leftist tree priority queues in O(log n) time.
  • 34. Extended Binary Trees Start with any binary tree and add an external node wherever there is an empty subtree. Result is an extended binary tree.
  • 36. An Extended Binary Tree number of external nodes is n+1
  • 37. The Function s() For any node x in an extended binary tree, let s(x) be the length of a shortest path from x to an external node in the subtree rooted at x.
  • 39. s() Values Example 0 0 0 0 0 0 0 0 0 0 1 1 1 2 1 1 2 1 2
  • 40. Properties of s() If x is an external node, then s(x) = 0. Otherwise, s(x) = min {s(leftChild(x)), s(rightChild(x))} + 1
  • 41. Height Biased Leftist Trees A binary tree is a (height biased) leftist tree iff for every internal node x, s(leftChild(x)) >= s(rightChild(x))
  • 42. A Leftist Tree 0 0 0 0 0 0 0 0 0 0 1 1 1 2 1 1 2 1 2
  • 43. Leftist Trees--Property 1 In a leftist tree, the rightmost path is a shortest root to external node path and the length of this path is s(root).
  • 44. A Leftist Tree 0 0 0 0 0 0 0 0 0 0 1 1 1 2 1 1 2 1 2 Length of rightmost path is 2.
  • 45. Leftist Trees—Property 2 The number of internal nodes is at least 2s(root) - 1 Because levels 1 through s(root) have no external nodes. So, s(root) <= log(n+1)
  • 46. A Leftist Tree 0 0 0 0 0 0 0 0 0 0 1 1 1 2 1 1 2 1 2 Levels 1 and 2 have no external nodes.
  • 47. Leftist Trees—Property 3 Length of rightmost path is O(log n), where n is the number of nodes in a leftist tree. Follows from Properties 1 and 2.
  • 48. Leftist Trees As Priority Queues Min leftist tree … leftist tree that is a min tree. Used as a min priority queue. Max leftist tree … leftist tree that is a max tree. Used as a max priority queue.
  • 49. A Min Leftist Tree 8 6 9 6 8 5 4 3 2
  • 50. Some Min Leftist Tree Operations put() remove() meld() initialize() put() and remove() use meld().
  • 51. 51 Weight-Biased Leftist Tree (WBLT) • Let the weight, w(x), of node x to be the number of internal nodes in the subtree with root x • If x is an external node, w(x) = 0 • If x is an internal node, its weight is one more than the sum of the weights of its children • A binary tree is a weight-biased leftist tree (WBLT) iff at every internal node, the w value of the left child is greater than or equal to the w value of the right child
  • 52. 52 Extended Binary Tree Figure 12.6 s and w values
  • 53. When drawing two max HBLTs that are to be melded, we will always draw the one with larger root value on the left. Ties are broken arbitrarily. Because of this convention, the root of the left HBLT always becomes the root of the final HBLT. Also, we will shade the nodes of the HBLT on the right.
  • 54. 54 Melding max HBLTs Figure 12.7 Melding (combining) max HBLTs
  • 55. To create a max HBLT • 7, 1, 9, 11 and 2
  • 56. To create a max HBLT
  • 57. Initializing in O(n) Time • create n single node max (min) leftist trees and place them in a FIFO queue • repeatedly remove two max (min) leftist trees from the FIFO queue, meld them, and put the resulting max (min) leftist tree into the FIFO queue • the process terminates when only 1 max (min) leftist tree remains in the FIFO queue • analysis is the same as for heap initialization
  • 58. Complexity • Insertion - O(log n) • Deletion - O(log n) • Find max – O(1) • Melding - O(log n) • Initialization - O(n)