SlideShare a Scribd company logo
1 of 52
Initializing A Max Heap 
11 
8 
4 
7 
3 
6 7 
8 9 
10 
1 
5 
2 
input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Initializing A Max Heap 
11 
8 
4 
7 
3 
6 7 
8 9 
10 
1 
5 
2 
Start at rightmost array position that has a child. 
Index is n/2.
Initializing A Max Heap 
8 
4 
7 
6 7 
8 9 
Move to next lower array position. 
3 
10 
1 
5 
11 
2
Initializing A Max Heap 
8 
4 
7 
6 7 
8 9 
3 
10 
1 
5 
11 
2
Initializing A Max Heap 
8 
9 
7 
6 7 
8 4 
3 
10 
1 
5 
11 
2
Initializing A Max Heap 
8 
9 
7 
6 7 
8 4 
3 
10 
1 
5 
11 
2
Initializing A Max Heap 
8 
9 
7 
6 3 
8 4 
7 
10 
1 
5 
11 
2
Initializing A Max Heap 
8 
9 
7 
6 3 
8 4 
7 
10 
1 
5 
11 
2
Initializing A Max Heap 
8 
9 
7 
6 3 
8 4 
7 
10 
1 
5 
11 
Find a home for 2.
Initializing A Max Heap 
8 
9 
7 
6 3 
8 4 
7 
75 
1 
11 
10 
Find a home for 2.
Initializing A Max Heap 
8 
9 
7 
6 3 
8 4 
7 
72 
1 
11 
10 
5 
Done, move to next lower array position.
Initializing A Max Heap 
8 
9 
7 
6 3 
8 4 
7 
72 
1 
11 
10 
5 
Find home for 1.
Initializing A Max Heap 
11 
8 
9 
7 
6 3 
8 4 
7 
72 
10 
5 
Find home for 1.
Initializing A Max Heap 
8 
9 
7 
6 3 
8 4 
7 
72 
11 
10 
5 
Find home for 1.
Initializing A Max Heap 
8 
9 
7 
6 3 
8 4 
7 
72 
11 
10 
5 
Find home for 1.
Initializing A Max Heap 
8 
9 
7 
6 3 
8 4 
7 
72 
11 
10 
5 
Done. 
1
Time Complexity 
7 8 
6 3 
4 
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).
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 
2 
2 1 
2 1 1 
0 0 0 0 
0 0 
0 0 
0 
0 
1 1 1
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 
2 
2 1 
2 1 1 
0 0 0 0 
0 0 
0 0 
0 
0 
1 1 1
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 
2 
2 1 
2 1 1 
0 0 0 0 
0 0 
0 0 
0 
0 
1 1 1 
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 
2 
2 1 
2 1 1 
0 0 0 0 
0 0 
0 0 
0 
0 
1 1 1 
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 
2 
4 3 
6 8 5 
8 6 9
Some Min Leftist Tree Operations 
put() 
remove() 
meld() 
initialize() 
put() and remove() use meld().
Put Operation 
put(7) 
2 
4 3 
6 8 5 
8 6 9
Put Operation 
put(7) 
2 
4 3 
6 8 5 
8 6 9 
Create a single node min leftist tree. 7
Put Operation 
put(7) 
2 
4 3 
6 8 5 
8 6 9 
Create a single node min leftist tree. 
Meld the two min leftist trees. 
7
Remove Min 
2 
4 3 
6 8 5 
8 6 9
Remove Min 
2 
4 3 
6 8 5 
8 6 9 
Remove the root.
Remove Min 
2 
4 3 
6 8 5 
8 6 9 
Remove the root. 
Meld the two subtrees.
Meld Two Min Leftist Trees 
4 3 
6 8 5 
8 6 9 
6 
Traverse only the rightmost paths so as to get 
logarithmic performance.
Meld Two Min Leftist Trees 
4 3 
6 8 5 
8 6 9 
6 
Meld right subtree of tree with smaller root and 
all of other tree.
Meld Two Min Leftist Trees 
4 3 
6 8 5 
8 6 9 
6 
Meld right subtree of tree with smaller root and all of 
other tree.
Meld Two Min Leftist Trees 
6 8 
8 6 
4 6 
Meld right subtree of tree with smaller root and all of 
other tree.
Meld Two Min Leftist Trees 
8 6 
Meld right subtree of tree with smaller root and all of 
other tree. 
Right subtree of 6 is empty. So, result of melding right 
subtree of tree with smaller root and other tree is the 
other tree.
Meld Two Min Leftist Trees 
8 6 
Make melded subtree right subtree of smaller root. 
6 
8 
Swap left and right subtree if s(left) < s(right). 
6 
8
Meld Two Min Leftist Trees 
6 6 
8 6 
4 
6 
4 6 
8 
8 6 8 
Make melded subtree right subtree of smaller root. 
Swap left and right subtree if s(left) < s(right).
Meld Two Min Leftist Trees 
9 
5 
3 
4 
6 6 
8 6 
8 
Make melded subtree right subtree of smaller root. 
Swap left and right subtree if s(left) < s(right).
Meld Two Min Leftist Trees 
9 
5 
3 
6 6 
8 6 
4 
8
Initializing In O(n) Time 
• create n single node min leftist trees 
and place them in a FIFO queue 
• repeatedly remove two min leftist trees 
from the FIFO queue, meld them, and 
put the resulting min leftist tree into the 
FIFO queue 
• the process terminates when only 1 min 
leftist tree remains in the FIFO queue 
• analysis is the same as for heap 
initialization

More Related Content

What's hot

Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...
Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...
Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...Databricks
 
Heap Hand note
Heap Hand noteHeap Hand note
Heap Hand noteAbdur Rouf
 
maXbox Regular Expression in PI
maXbox Regular Expression in PImaXbox Regular Expression in PI
maXbox Regular Expression in PIMax Kleiner
 
The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212Mahmoud Samir Fayed
 
Work-stealing Tree Data Structure
Work-stealing Tree Data StructureWork-stealing Tree Data Structure
Work-stealing Tree Data StructureAleksandar Prokopec
 
PyCon Ukraine 2017: Operational Transformation
PyCon Ukraine 2017: Operational Transformation PyCon Ukraine 2017: Operational Transformation
PyCon Ukraine 2017: Operational Transformation Max Klymyshyn
 
Ds0601 stack
Ds0601 stackDs0601 stack
Ds0601 stackvirajrana
 
Chart parsing with features
Chart parsing with featuresChart parsing with features
Chart parsing with featuresSRah Sanei
 
Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.Rakib Hossain
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort Amit Kundu
 
lecture 4
lecture 4lecture 4
lecture 4sajinsc
 

What's hot (20)

Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...
Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...
Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
 
Heap Hand note
Heap Hand noteHeap Hand note
Heap Hand note
 
04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort
 
04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort
 
Heap sort
Heap sort Heap sort
Heap sort
 
heapsort_bydinesh
heapsort_bydineshheapsort_bydinesh
heapsort_bydinesh
 
maXbox Regular Expression in PI
maXbox Regular Expression in PImaXbox Regular Expression in PI
maXbox Regular Expression in PI
 
The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212
 
Work-stealing Tree Data Structure
Work-stealing Tree Data StructureWork-stealing Tree Data Structure
Work-stealing Tree Data Structure
 
Lec10
Lec10Lec10
Lec10
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
PyCon Ukraine 2017: Operational Transformation
PyCon Ukraine 2017: Operational Transformation PyCon Ukraine 2017: Operational Transformation
PyCon Ukraine 2017: Operational Transformation
 
Lec9
Lec9Lec9
Lec9
 
Ds0601 stack
Ds0601 stackDs0601 stack
Ds0601 stack
 
Chart parsing with features
Chart parsing with featuresChart parsing with features
Chart parsing with features
 
Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.
 
Ctrie Data Structure
Ctrie Data StructureCtrie Data Structure
Ctrie Data Structure
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
lecture 4
lecture 4lecture 4
lecture 4
 

Viewers also liked

Viewers also liked (8)

Tutorial 3 (b tree min heap)
Tutorial 3 (b tree min heap)Tutorial 3 (b tree min heap)
Tutorial 3 (b tree min heap)
 
Heap tree
Heap treeHeap tree
Heap tree
 
Heapsort
HeapsortHeapsort
Heapsort
 
Data Structure (Heap Sort)
Data Structure (Heap Sort)Data Structure (Heap Sort)
Data Structure (Heap Sort)
 
Heapsort using Heap
Heapsort using HeapHeapsort using Heap
Heapsort using Heap
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil
 
Heap sort
Heap sort Heap sort
Heap sort
 

Similar to Lec24

CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfssuser034ce1
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docxfredharris32
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
Sample Standard Deviation - Desk Work 8
Sample Standard Deviation - Desk Work 8Sample Standard Deviation - Desk Work 8
Sample Standard Deviation - Desk Work 8ccooking
 
Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Getachew Ganfur
 
Heapsortokkay
HeapsortokkayHeapsortokkay
HeapsortokkayRemesha
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treeszukun
 
computer notes - Data Structures - 2
computer notes - Data Structures - 2computer notes - Data Structures - 2
computer notes - Data Structures - 2ecomputernotes
 
presentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingpresentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingBindiya syed
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...BhumikaBiyani1
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree Krish_ver2
 

Similar to Lec24 (20)

CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Lec34
Lec34Lec34
Lec34
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Sample Standard Deviation - Desk Work 8
Sample Standard Deviation - Desk Work 8Sample Standard Deviation - Desk Work 8
Sample Standard Deviation - Desk Work 8
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Heapify algorithm
Heapify algorithmHeapify algorithm
Heapify algorithm
 
Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
 
Splay tree
Splay treeSplay tree
Splay tree
 
Leftlist Heap-1.pdf
Leftlist Heap-1.pdfLeftlist Heap-1.pdf
Leftlist Heap-1.pdf
 
Heapsortokkay
HeapsortokkayHeapsortokkay
Heapsortokkay
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure trees
 
computer notes - Data Structures - 2
computer notes - Data Structures - 2computer notes - Data Structures - 2
computer notes - Data Structures - 2
 
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
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec21
Lec21Lec21
Lec21
 
Lec20
Lec20Lec20
Lec20
 
Lec19
Lec19Lec19
Lec19
 
Lec18
Lec18Lec18
Lec18
 
Lec17
Lec17Lec17
Lec17
 

Lec24

  • 1. Initializing A Max Heap 11 8 4 7 3 6 7 8 9 10 1 5 2 input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  • 2. Initializing A Max Heap 11 8 4 7 3 6 7 8 9 10 1 5 2 Start at rightmost array position that has a child. Index is n/2.
  • 3. Initializing A Max Heap 8 4 7 6 7 8 9 Move to next lower array position. 3 10 1 5 11 2
  • 4. Initializing A Max Heap 8 4 7 6 7 8 9 3 10 1 5 11 2
  • 5. Initializing A Max Heap 8 9 7 6 7 8 4 3 10 1 5 11 2
  • 6. Initializing A Max Heap 8 9 7 6 7 8 4 3 10 1 5 11 2
  • 7. Initializing A Max Heap 8 9 7 6 3 8 4 7 10 1 5 11 2
  • 8. Initializing A Max Heap 8 9 7 6 3 8 4 7 10 1 5 11 2
  • 9. Initializing A Max Heap 8 9 7 6 3 8 4 7 10 1 5 11 Find a home for 2.
  • 10. Initializing A Max Heap 8 9 7 6 3 8 4 7 75 1 11 10 Find a home for 2.
  • 11. Initializing A Max Heap 8 9 7 6 3 8 4 7 72 1 11 10 5 Done, move to next lower array position.
  • 12. Initializing A Max Heap 8 9 7 6 3 8 4 7 72 1 11 10 5 Find home for 1.
  • 13. Initializing A Max Heap 11 8 9 7 6 3 8 4 7 72 10 5 Find home for 1.
  • 14. Initializing A Max Heap 8 9 7 6 3 8 4 7 72 11 10 5 Find home for 1.
  • 15. Initializing A Max Heap 8 9 7 6 3 8 4 7 72 11 10 5 Find home for 1.
  • 16. Initializing A Max Heap 8 9 7 6 3 8 4 7 72 11 10 5 Done. 1
  • 17. Time Complexity 7 8 6 3 4 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).
  • 18. 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).
  • 19. 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.
  • 20. 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.
  • 22. An Extended Binary Tree number of external nodes is n+1
  • 23. 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.
  • 25. s() Values Example 2 2 1 2 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1
  • 26. Properties Of s() If x is an external node, then s(x) = 0. Otherwise, s(x) = min {s(leftChild(x)), s(rightChild(x))} + 1
  • 27. 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))
  • 28. A Leftist Tree 2 2 1 2 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1
  • 29. 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).
  • 30. A Leftist Tree 2 2 1 2 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 Length of rightmost path is 2.
  • 31. 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)
  • 32. A Leftist Tree 2 2 1 2 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 Levels 1 and 2 have no external nodes.
  • 33. 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.
  • 34. 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.
  • 35. A Min Leftist Tree 2 4 3 6 8 5 8 6 9
  • 36. Some Min Leftist Tree Operations put() remove() meld() initialize() put() and remove() use meld().
  • 37. Put Operation put(7) 2 4 3 6 8 5 8 6 9
  • 38. Put Operation put(7) 2 4 3 6 8 5 8 6 9 Create a single node min leftist tree. 7
  • 39. Put Operation put(7) 2 4 3 6 8 5 8 6 9 Create a single node min leftist tree. Meld the two min leftist trees. 7
  • 40. Remove Min 2 4 3 6 8 5 8 6 9
  • 41. Remove Min 2 4 3 6 8 5 8 6 9 Remove the root.
  • 42. Remove Min 2 4 3 6 8 5 8 6 9 Remove the root. Meld the two subtrees.
  • 43. Meld Two Min Leftist Trees 4 3 6 8 5 8 6 9 6 Traverse only the rightmost paths so as to get logarithmic performance.
  • 44. Meld Two Min Leftist Trees 4 3 6 8 5 8 6 9 6 Meld right subtree of tree with smaller root and all of other tree.
  • 45. Meld Two Min Leftist Trees 4 3 6 8 5 8 6 9 6 Meld right subtree of tree with smaller root and all of other tree.
  • 46. Meld Two Min Leftist Trees 6 8 8 6 4 6 Meld right subtree of tree with smaller root and all of other tree.
  • 47. Meld Two Min Leftist Trees 8 6 Meld right subtree of tree with smaller root and all of other tree. Right subtree of 6 is empty. So, result of melding right subtree of tree with smaller root and other tree is the other tree.
  • 48. Meld Two Min Leftist Trees 8 6 Make melded subtree right subtree of smaller root. 6 8 Swap left and right subtree if s(left) < s(right). 6 8
  • 49. Meld Two Min Leftist Trees 6 6 8 6 4 6 4 6 8 8 6 8 Make melded subtree right subtree of smaller root. Swap left and right subtree if s(left) < s(right).
  • 50. Meld Two Min Leftist Trees 9 5 3 4 6 6 8 6 8 Make melded subtree right subtree of smaller root. Swap left and right subtree if s(left) < s(right).
  • 51. Meld Two Min Leftist Trees 9 5 3 6 6 8 6 4 8
  • 52. Initializing In O(n) Time • create n single node min leftist trees and place them in a FIFO queue • repeatedly remove two min leftist trees from the FIFO queue, meld them, and put the resulting min leftist tree into the FIFO queue • the process terminates when only 1 min leftist tree remains in the FIFO queue • analysis is the same as for heap initialization

Editor's Notes

  1. s() values may be computed easily using a postorder traversal.