CSE 326: Data Structures New 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 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 ) • 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? 0 1 1 Observations

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

0 1 1 0 Consider a subtree of a … 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: < D Proof: (By induction) 1 2 1 Say it diverges from right path at x x Base case : r=1. Tree has at least 2 -1 = 1 node Inductive step : assume true for r-1. Prove for tree with right path at least r. npl(L) ≤ D1-1 because of the path of L R 1. Right subtree: right path of r-1 nodes length D1-1 to null D 1 D2 ⇒ 2r-1-1 right subtree nodes (by induction) 2. Left subtree: also right path of length at least (prev. slide) npl(R) ≥ D -1 because every node on r-1 2 ⇒ 2r-1-1 left subtree nodes (by induction) right path is leftist

r-1 r-1 r Leftist property at x violated! 9 ⇒ Total tree size: (2 -1) + (2 -1) + 1 = 2 -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 log (N+1) nodes 2 • 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 If npl(R’) > npl(L ) leftist heap containing all elements of the two a a 1 a merge … (distinct) leftist heaps T1 and T2

merge L1 L R’ R’ L R1 1 1 T1 a a ’ R = merge(R1, T2) merge b

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

Leftest Merge Example merge ? 1 Sewing Up the Example 5 3 0 merge ? ? ? 00 7 3 6 12 1 ? 3 3 5 5 0 0 0 0 0 14 merge 7 ? ? 1 15 00 0 5 7 7 6 12 6 0 5 5 12 0 0 0 0 1 14 ? 14 0 14 0 1 3 15 0 6 6 1 6 8 ? 0 8 8 8 15 00 0 0 0 0 8 0 0 11 12 7 8 0 11 0 11 12 11 0 Ø 8 12 0 0 0 0 14 11 11 0 0 15 12 0 15 11 15 0

15 16 15 0 Sewing Up the Example Other Heap Operations

? 1 1 3 3 3 •insert ? 0 0 1 1 7 1 7 0 5 5 5 7 0 0 1 0 1 0 0 14 1 0 • deleteMin ? 8 6 14 8 6 8 6 14 0 0 0 0 0 0 11 12 11 12 11 12

0 0 15 0 15 15

17 18

Operations on Leftist Heaps

• merge with two trees of total size n: O(log n) Leftist Heaps: Summary •insertwith 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