Upload
keyan-monadjem
View
252
Download
1
Embed Size (px)
DESCRIPTION
Slideshow on the basics of leftisit skew heaps.
Citation preview
Leftist HeapsText Read Weiss, 23.1 (Skew Heaps)Leftist HeapDefinition of null path length Definition of leftist heapBuilding a Leftist HeapSequence of inserts Re-heapification if leftist heap property is violated
MotivationA binary heap provides O(log n) inserts and O(log n) deletes but suffers from O(n log n) mergesA leftist heap offers O(log n) inserts and O(log n) deletes and O(log n) mergesNote, however, leftist heap inserts and deletes are more expensive than Binary Heap inserts and deletes
DefinitionA Leftist (min)Heap is a binary tree that satisfies the follow-ing conditions. If X is a node and L and R are its left and right children, then: X.value L.value X.value R.value null path length of L null path length of R
where the null path length of a node is the shortest distance between from that node to a descendant with 0 or 1 child.If a node is null, its null path length is 1.
Example: Null Path Length1920274325121584node4 8 19 12 15 25 27 20 43npl1 0 1 1 0 0 0 0 0
Example: Null Path Length1920274325121584node4 8 19 12 15 25 27 20 43npl1 0 1 1 0 0 0 0 001100000What are the npls of the children of 20 and the right child of 27?What is the npl of the right child of 8?
Example: Null Path Length1920274325121584node4 8 19 12 15 25 27 20 43npl1 0 1 1 0 0 0 0 0node 4 violates leftist heap property fix by swapping children01100000
Leftist Heap4node4 8 19 12 15 25 27 20 43npl1 0 1 1 0 0 0 0 0
Merging Leftist Heaps425121581920274367814Consider two leftist heaps Task: merge them into a single leftist heap
Merging Leftist Heaps425121581920274367814 First, instantiate a Stack
Merging Leftist Heaps425121581920274367814 Compare root nodesmerge(x,y)
xy
Merging Leftist Heaps425121581920274367814Remember smaller value
4xy
Merging Leftist Heaps425121581920274367814Repeat the process with the right child of the smaller value
4xy
Merging Leftist Heaps425121581920274367814Remember smaller value
6 4xy
Merging Leftist Heaps425121581920274367814Repeat the process with the right child of the smaller value
6 4xy
Merging Leftist Heaps425121581920274367814Remember smaller value
7 6 4xy
Merging Leftist Heaps425121581920274367814Repeat the process with the right child of the smaller value
7 6 4xynull
Merging Leftist Heaps425121581920274367814Because one of the arguments is null, return the other argument
7 6 4x8
Merging Leftist Heaps4251215192027436814Make 8 the right child of 7
7 6 4x887Refers to node 8
Merging Leftist Heaps4251215192027436814Make 7 leftist (by swapping children)
7 6 4887Refers to node 8
Merging Leftist Heaps4251215192027436814Return node 7
6 4787Refers to node 8
Merging Leftist Heaps4251215192027436814Make 7 the right child of 6 (which it already is)
6 4787Refers to node 8
Merging Leftist Heaps4251215192027436814Make 6 leftist (it already is)
6 4787Refers to node 8
Merging Leftist Heaps4251215192027436814Return node 6
4687Refers to node 8
Merging Leftist Heaps4251215192027436814Make 6 the right child of 4
4687
Merging Leftist Heaps4251215192027436814Make 4 leftist (it already is)
4687
Final Leftist Heap4251215192027436814Return node 4
487 Verify that the tree is heap Verify that the heap is leftist
AnalysisHeight of a leftist heap O(log n)Maximum number of values stored in Stack 2 * O(log n) O(log n)Total cost of merge O(log n)
Inserts and DeletesTo insert a node into a leftist heap, merge the leftist heap with the nodeAfter deleting root X from a leftist heap, merge its left and right subheapsIn summary, there is only one operation, a merge.
Skew HeapsText Read Weiss, 6.7Skew HeapNo need for null path length Definition of skew heapBuilding a Skew HeapSequence of inserts Swap children at every merge step
MotivationSimplify leftist heap by not maintaining null path lengthsswapping children at every merge step
DefinitionA Skew (min)Heap is a binary tree that satisfies the follow-ing conditions. If X is a node and L and R are its left and right children, then: X.value L.value X.value R.value
A Skew (max)Heap is a binary tree that satisfies the follow-ing conditions. If X is a node and L and R are its left and right children, then: X.value L.value X.value R.value
Merging Skew Heaps425121581920274367814Consider two skew heaps Task: merge them into a single skew heap
Merging Skew Heaps425121581920274367814 First, instantiate a Stack
Merging Skew Heaps425121581920274367814 Compare root nodesmerge(x,y)
xy
Merging Skew Heaps425121581920274367814Remember smaller value
4xy
Merging Skew Heaps425121581920274367814Repeat the process with the right child of the smaller value
4xy
Merging Skew Heaps425121581920274367814Remember smaller value
6 4xy
Merging Skew Heaps425121581920274367814Repeat the process with the right child of the smaller value
6 4xy
Merging Skew Heaps425121581920274367814Remember smaller value
7 6 4xy
Merging Skew Heaps425121581920274367814Repeat the process with the right child of the smaller value
7 6 4xynull
Merging Skew Heaps425121581920274367814Because one of the arguments is null, return the other argument
7 6 4x8
Merging Skew Heaps4251215192027436814Make 8 the right child of 7
7 6 4x887Refers to node 8
Merging Skew Heaps4251215192027436814Swap children of node 7
7 6 4887Refers to node 8
Merging Skew Heaps4251215192027436814Return node 7
6 4787Refers to node 8
Merging Skew Heaps4251215192027436814Make 7 the right child of 6 (which it already is)
6 4787Refers to node 8
Merging Skew Heaps4251215192027436814Swap children of node 6
6 4787Refers to node 8
Merging Skew Heaps419202743Return node 6
46Refers to node 8251215681487
Merging Skew HeapsMake 6 the right child of 4
46419202743251215681487
Merging Skew HeapsSwap children of node 4
46419202743251215681487
Final Skew HeapReturn node 4
4 Verify that the tree is heap Verify that the heap is skew419202743251215681487
AnalysisHeight of a skew heap O(log n)Maximum number of values stored in Stack 2 * O(log n) O(log n)Total cost of merge O(log n)
Inserts and DeletesTo insert a node into a skew heap, merge the leftist heap with the nodeAfter deleting root X from a skew heap, merge its left and right subheapsIn summary, there is only one operation, a merge.