Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

05 Leftist Heaps

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CSE 326: Data Structures New Heap Operation: Merge

Given two heaps, merge them into one heap


Priority Queues: – first attempt: insert each element of the smaller
Leftist Heaps heap into the larger.
runtime:
Brian Curless
Spring 2008 – second attempt: concatenate binary heaps’
arrays and run buildHeap.
runtime:
1 2

Definition: Null Path Length


Leftist Heaps null path length (npl) of a node x = the number of nodes between x
and a null in its subtree
OR
Idea: npl(x) = min distance to a descendant with 0 or 1 children
Focus all heap maintenance work in one • npl(null) = -1
small part of the heap • npl(leaf) = 0
• npl(single-child node) = 0
Leftist heaps:
1. Binary trees
2. Most nodes are on the left
3. All the merging work is done on the right Equivalent definition:

3 npl(x) = 1 + min{npl(left(x)), npl(right(x))} 4


Definition: Null Path Length Leftist Heap Properties
Another useful definition: • Order property
– parent’s priority value is ≤ to childrens’ priority
npl(x) is the height of the largest perfect binary tree that is both
values
itself rooted at x and contained within the subtree rooted at x.
– result: minimum element is at the root
– (Same as binary heap)
• Structure property
– For every node x, npl(left(x)) ≥ npl(right(x))
– result: tree is at least as “heavy” on the left as the
right
(Terminology: we will say a leftist heap’s tree is a leftist
5 tree.) 6

2 Are These Leftist?


1 1 0
Observations
0 1 0 0 0 0

1
Are leftist trees always…
0 0 0 0
– complete?
0 0 1
– balanced?
2 0 0 0

0
1 1 0
Consider a subtree of a leftist tree…
0
1 0 0 0 0 – is it leftist?
0
0 0 0

7
Right Path in a Leftist Tree is Short (#1) Right Path in a Leftist Tree is Short (#2)
Claim: The right path (path from root to rightmost Claim: If the right path has r nodes, then the tree has
leaf) is as short as any in the tree. at least 2r-1 nodes.
Proof: (By contradiction)
Pick a shorter path: D1 < D2
Proof: (By induction)
Say it diverges from right path at x x Base case : r=1. Tree has at least 21-1 = 1 node
Inductive step : assume true for r-1. Prove for tree with right
npl(L) ≤ D1-1 because of the path of path at least r.
L R 1. Right subtree: right path of r-1 nodes
length D1-1 to null D1 D2 ⇒ 2r-1-1 right subtree nodes (by induction)
2. Left subtree: also right path of length at least r-1 (prev. slide)
npl(R) ≥ D2-1 because every node on
⇒ 2r-1-1 left subtree nodes (by induction)
right path is leftist

Leftist property at x violated! 9 ⇒ Total tree size: (2r-1-1) + (2r-1-1) + 1 = 2r-1 10

Why do we have the leftist property? Merge two heaps (basic idea)
Because it guarantees that: • Put the root with smaller value as the new
• the right path is really short compared to root.
the number of nodes in the tree • Hang its left subtree on the left.
• A leftist tree of N nodes, has a right path of • Recursively merge its right subtree and the
other tree.
at most log2(N+1) nodes
• Before returning from recursion:
– Update npl of merged root.
Idea – perform all work on the right path – Swap left and right subtrees just below root, if
needed, to keep leftist property of merged
11
result. 12
Merging Two Leftist Heaps Merge Continued
Recursive calls to merge(T1,T2): returns one
leftist heap containing all elements of the two a a If npl(R’) > npl(L1) a
merge

(distinct) leftist heaps T1 and T2
merge L1 R1 L1 R’ R’ L1
T1 a a R’ = merge(R1, T2)
merge b

L1 R1 L1
a<b R1 L2 R2
T2 b b
Note special case: merge(null, T) = merge(T, null) = T
L2 R2 L2 R2 13 runtime: 14

Leftest Merge Example


merge
1 3
? Sewing Up the Example
5
0 merge ? ? ?
0 0 7 3
6 12 1 ? 3 3
5 5
0 0 0 0
0 14 0 0 merge 7 ? 7 ? 7 1
15 6 12
0 5 5 5
6 0
12 0
0 0 0
1 14 ? 14 0 14 0 1
3 15 0 6 8 6 1
6 8
? 8
8 15 0
0 0 0 0 0 0
8 0 0
7 8 0 11 0 11 12 11 12
11 0 Ø 8 12
0
14 11 0 11 0 0 15 0
12 11 0 0 15 0
15
15 16
15 0
Sewing Up the Example Other Heap Operations
? 1 1
3 3 3
0 0 1
• insert ?
1 0
7 1 7 5 5 7
5
0 0 1 0 1 0
14
8
1
6
0 14 8 6 8 6 14
0
• deleteMin ?
0 0 0 0
0 0
11 12 11 12 11 12

15 0 15 0 15 0

17 18

Operations on Leftist Heaps


• merge with two trees of total size n: O(log n) Leftist Heaps: Summary
• insert with heap size n: O(log n)
– pretend node is a size 1 leftist heap Good
– insert by merging original heap with one node heap •
merge

• deleteMin with heap size n: O(log n)
– remove and return root Bad
– merge left and right subtrees •

merge
19 20

You might also like