CSE3144 Β· Advanced Data Structures Β· Jul–Nov Semester 2026 Β· Lecture 20 of 36 Β· CO CSE3144.3

Pairing Heaps & Double-Ended Priority Queues

Two structures for the two things Lecture 19 left unresolved: a heap simple enough to actually deploy (not just cite in a proof), and a queue that answers "give me the smallest" and "give me the largest" from the same collection.

Dr. Manu ShrivastavaCourse Instructor Β· Consultation Fri 2–5 PM, LHC 308F
~110 minutesSession outcome: implement advanced heap and DEPQ operations
L20 Β· 00 β€” Agenda ~116 min

Today, in two parts

Part A: Why pairing heaps?

00–06

Fibonacci heaps are the theoretical champion nobody deploys. Pairing heaps are what people actually use.

Structure: one tree, a sibling list, no bookkeeping fields

06–11

No parent, degree, or mark field anywhere β€” a clever pointer trick replaces all three.

MELD: the one primitive everything is built from

11–14

Compare two roots, link the smaller under the larger. O(1). Always.

Advantages & limitations

14–19

Faster in practice than Fibonacci heaps β€” with a famous open problem sitting inside its analysis.

Animation: INSERT

19–24

Five inserts, watching the root change or stay put each time.

Animation: DECREASE-KEY

24–29

Detach using the sibling-list trick, no parent pointer needed, then meld back in.

Animation: REMOVE-MIN, two-pass scheme

29–44

Five orphaned subtrees, paired left to right, then combined right to left.

Complexity β€” and an honest open problem

44–48

Most bounds are proven. One famous one isn't, even today.

Part B: Why double-ended priority queues?

48–54

External quicksort needs both "smallest so far" and "largest so far" from one live structure.

Generic methods: dual, total, and leaf correspondence

54–59

Bolt a min-heap and a max-heap together β€” three ways to do it, with a real trade-off.

Interval heaps: the custom, elegant answer

59–65

One tree, every node holds an interval β€” a min-heap and a max-heap, embedded in the same shape.

Animation: building an interval heap

65–71

Eight inserts from empty β€” and why every one of them fits without bubbling.

Animation: INSERT, all four cases

71–88

Fits directly, bubbles via the min side, bubbles via the max side, fills an odd slot.

Animation: REMOVE-MIN

88–98

Promote from the last node, then reinsert it down through the embedded min-heap.

Advantages & limitations

98–102

Half the space of a dual structure, at the cost of trickier bookkeeping per node.

Complexity & where this runs

102–109

External sorting, and any system that needs both ends of a priority order live.

Recap & what's next

109–116

Lecture 21: Comparative Study of Heap Structures.

CSE3144 β€” Lecture 20
PART A

Pairing Heaps

A single unordered tree, a sibling list, and one primitive β€” MELD β€” that every other operation reduces to.

L20 Β· 01 β€” Why pairing heaps? ~6 min

The theoretical champion nobody deploys

Real-life example β€” the same router, a different engineer

Lecture 19 ended with a caveat: Fibonacci heaps win the asymptotic argument for Dijkstra's algorithm, but the parent/child/left/right/degree/mark bookkeeping per node, plus cascading-cut logic, carries real constant-factor cost. An engineer actually shipping a routing engine or a game-AI pathfinder needs something that wins in practice, at the graph sizes that really show up β€” not just in the limit as n → ∞.

What experiments actually show

Pairing heaps were invented specifically to answer this. Experimentally, they are consistently faster than Fibonacci heaps in practice, simpler to implement correctly, carry smaller runtime overhead per operation, and use less memory per node β€” no degree field, no mark bit, no parent pointer at all.

The trade-off, stated honestly up front

Pairing heaps give up something for this simplicity: several of their amortized bounds β€” most notably DECREASE-KEY β€” are not fully proven to match Fibonacci heaps' O(1). They are conjectured to be very fast and observed to be fast, but the tightest known proofs land somewhere between the conjecture and Fibonacci heaps' guarantee. We will be precise about exactly which bounds are solid and which are open when we reach the complexity slide.

CSE3144 β€” Lecture 20 Β· Why pairing heaps
L20 Β· 02 β€” Structure ~5 min

One tree. A sibling list. No parent, degree, or mark field anywhere.

What a pairing heap actually is
  • A pairing heap is a single min-heap-ordered tree β€” not a forest like binomial or Fibonacci heaps. Every node's key is ≤ every key in its subtree.
  • Children are unordered and held in a doubly-linked sibling list (not circular, unlike Fibonacci heaps' root/child lists).
  • Each node stores exactly three pointers: child (its first child), left and right (siblings) β€” plus its key. That is the entire node.
The trick that removes the parent pointer

A node x is the first child in its sibling list exactly when x.left.child = x. So instead of wasting a slot on a separate parent pointer, the left pointer of a first child is simply repurposed to point at the parent β€” and every other sibling's left points at its left neighbour as usual.

This one trick is what makes DECREASE-KEY and REMOVE cheap without any cascading-cut bookkeeping: you can always find "where does this node detach from" in O(1), using only left/right β€” no degree or mark field is ever needed.

CSE3144 β€” Lecture 20 Β· Structure
L20 Β· 03 β€” MELD (compare-link) ~3 min

Compare two roots. The larger tree becomes a child of the smaller. O(1).

The one primitive

Given two pairing-heap trees, MELD compares their roots. The tree rooted at the larger key becomes the new leftmost child of the tree rooted at the smaller key β€” one pointer rewire, no traversal of either tree's interior. Every other operation in this lecture β€” INSERT, DECREASE-KEY, REMOVE-MIN, REMOVE β€” is built entirely out of calls to MELD.

Worked example

Tree 1: root 5, child [9]. Tree 2: root 3, children [8, 6].

5
9
3
8
6
meld these two trees
3
5
9
8
6
5 > 3, so tree(5) becomes 3's new leftmost child
CSE3144 β€” Lecture 20 Β· MELD
L20 Β· 04 β€” Advantages & limitations ~5 min

Fast in practice, simple to build β€” with one famous asterisk

Advantages
  • Every core operation reduces to MELD, an O(1) actual-cost primitive β€” the whole implementation is short and hard to get subtly wrong.
  • Smaller node footprint than Fibonacci heaps: 3 pointers + a key (4 fields total), versus 4 pointers + a key + degree + mark (7 fields total).
  • Experimentally faster than Fibonacci heaps across realistic workloads β€” better cache behaviour, fewer pointer chases per operation.
  • INSERT and MELD are O(1) actual cost β€” both are single MELD calls.
Limitations
  • REMOVE-MIN needs a good multi-way-meld strategy. Melding the orphaned children left-to-right in a naive chain gives Θ(n) amortized cost β€” genuinely bad. The two-pass and multipass schemes fix this to O(log n) amortized, but you must implement one of them correctly.
  • DECREASE-KEY's tight amortized bound is a famous open problem. It is conjectured to be O(1) amortized (matching Fibonacci heaps) and performs like it in practice, but the best proven upper bound is more subtle, and a matching lower bound rules out a naive O(1) proof via the obvious potential function.
  • Worst-case degree and height are both Θ(n) for a single operation sequence (e.g. inserting keys in sorted order) β€” exactly like a Fibonacci heap tree can look, before any consolidation.
CSE3144 β€” Lecture 20 Β· Trade-offs
L20 Β· 05 β€” Animation: INSERT ~5 min

Create a 1-node tree, MELD it in β€” that's the whole operation

Building a pairing heap with five inserts: 15, 9, 20, 6, 12. Watch the root change twice (whenever the new key is smaller) and stay put three times (whenever it isn't).

Interactive β€” INSERT 15, 9, 20, 6, 12
CSE3144 β€” Lecture 20 Β· INSERT
L20 Β· 06 β€” Animation: DECREASE-KEY ~5 min

Detach using the sibling list, update the key, MELD back in

Starting from INSERT's result: root 6, children [12, 9(→[20, 15])]. We'll decrease node 20 to 2. Since there is no parent pointer, the ONLY way to check for a violation is to just always detach and re-meld.

Interactive β€” DECREASE-KEY(20, to 2)
CSE3144 β€” Lecture 20 Β· DECREASE-KEY
L20 Β· 07 β€” Animation: REMOVE-MIN, two-pass scheme ~15 min

Five orphaned subtrees, paired left to right, combined right to left

A fresh tree built by inserting 1, 3, 5, 7, 9, 11 in increasing order β€” the worst case for degree, exactly as noted in the trade-offs slide. Result: root 1, children [11, 9, 7, 5, 3], all leaves. Watch every pairing and every combine β€” nothing is skipped.

Interactive β€” REMOVE-MIN(H)
Why not just meld left to right?

The "bad way": currentTree = compareLink(currentTree, nextTree) repeated left to right. If you alternate n/2 inserts then n/2 remove-mins in the worst order, the total remove-min cost becomes (n/2−1)+…+2+1+0 = Θ(n²) β€” if insert is O(1) amortized, that forces remove-min to be Θ(n) amortized. Genuinely bad.

The multipass alternative

Put all orphaned subtrees in a FIFO queue; repeatedly dequeue 2, meld them, enqueue the result; stop at 1 tree. Same O(log n) amortized bound as two-pass, but two-pass shows better observed performance in practice β€” which is why it's the one we animated in full.

CSE3144 β€” Lecture 20 Β· REMOVE-MIN
L20 Β· 08 β€” Complexity β€” and an honest open problem ~4 min

Most bounds are proven. One famous one isn't.

OperationActual costAmortized cost
MELDO(1)O(1)
INSERTO(1)O(1)
FIND-MINO(1)O(1)
DECREASE-KEYO(1)conjectured O(1); best proven bound is looser β€” a genuine open problem
REMOVE-MIN (two-pass or multipass)O(degree of root)O(log n) β€” proven
REMOVE (arbitrary node)O(n) worst caseO(log n) β€” proven, same machinery as REMOVE-MIN

n = number of nodes at the time of the operation. Verified today: INSERT and DECREASE-KEY both cost a small constant number of pointer rewires regardless of heap size; REMOVE-MIN's two-pass scheme on a 5-child root took exactly 4 melds to finish (3 in pass 1, 1 in pass 2) β€” matching ⌈log₂ 5⌉-ish work, not the n² blow-up of the naive approach.

CSE3144 β€” Lecture 20 Β· Pairing heap complexity
PART B

Double-Ended Priority Queues

One collection, two questions always answerable: what's the smallest, and what's the largest β€” right now.

L20 Β· 09 β€” Why double-ended priority queues? ~6 min

Sometimes you need BOTH ends of the priority order, live

Definition

A double-ended priority queue (DEPQ) supports: isEmpty(), size(), getMin(), getMax(), put(x), removeMin(), removeMax(). A regular priority queue only ever exposes one end; a DEPQ exposes both, simultaneously, at all times.

Real-life example β€” external quicksort

Quicksort partitions into Left (≤ pivot), Middle (the pivot), Right (≥ pivot). When the data is too big for memory, an external quicksort makes the Middle group as large as possible using a DEPQ: fill the DEPQ from disk; for every further element, if it's ≤ the DEPQ's current min, ship it straight to Left; if it's ≥ the current max, ship it to Right; otherwise it belongs in the middle β€” remove either the current min or max to make room, and let the DEPQ hold onto the new element instead. This needs both ends live, continuously β€” exactly the DEPQ contract.

CSE3144 β€” Lecture 20 Β· Why DEPQs
L20 Β· 10 β€” Generic methods for DEPQs ~5 min

Bolt a min-heap and a max-heap together β€” three ways to do it

MethodIdeaTrade-off
Dual structureMaintain a full min-PQ and a full max-PQ of every element, with a correspondence pointer linking each element's copy in one to its copy in the other.Simplest to implement; each element effectively stored twice β€” most space.
Total correspondenceSplit elements roughly in half between a min-PQ and a max-PQ; pair every min-PQ element with a distinct max-PQ element (a,b) where priority(a) ≤ priority(b). One buffered element if the count is odd.Half the space of dual structure; noticeably more complex algorithms.
Leaf correspondenceSame halved split, but only leaf elements of the min-PQ and max-PQ are required to be paired β€” internal nodes need no pairing.Same space savings as total correspondence, but generally the fastest of the three in practice.

Any of these, layered on a PQ structure that also supports an efficient remove(theNode) β€” a binary heap, a pairing heap, or a height-biased leftist tree β€” gives put/removeMin/removeMax in O(log n) (amortized, for pairing heaps) and everything else in O(1). But there's a more elegant, purpose-built option next.

CSE3144 β€” Lecture 20 Β· Generic DEPQ methods
L20 Β· 11 β€” Interval heaps ~6 min

One tree. Every node holds an interval, not a single key.

Definition

An interval heap is a complete binary tree in which every node (except possibly the last) holds two elements, a ≤ b β€” representing the closed interval [a,b]. The rule: every child's interval is contained in its parent's interval. If the last node holds a single element c, then a ≤ c ≤ b for its parent's interval [a,b].

10,90
20,70
25,50
30,80
a valid 4-node interval heap β€” every child's interval fits inside its parent's
Two heaps, hiding in plain sight
  • All the left endpoints, read together, form a valid min-heap.
  • All the right endpoints, read together, form a valid max-heap.
  • The root's left endpoint is always the overall minimum; the root's right endpoint is always the overall maximum. getMin/getMax are O(1) β€” just read the root.
  • Stored compactly in an array exactly like an ordinary heap, just with two slots per position. Height is Θ(log n) for n elements.
CSE3144 β€” Lecture 20 Β· Interval heap definition
L20 Β· 11a β€” Animation: building an interval heap ~6 min

Eight inserts, from empty, to the heap every later slide reuses

Every example so far started from a heap already fully formed: root [10,90], left [20,70], right [30,80], left-left [25,50]. Here it is built from nothing, one INSERT at a time β€” and every single value happens to fit its parent's containment on arrival, so watch for what a bubble-free build actually looks like before L20·12 shows you what happens when one doesn't.

Interactive — INSERT 10, 90, 20, 70, 30, 80, 25, 50
Where each new node goes

Same shape rule as a binary heap: if the element count is currently odd, a half-full node already exists β€” the new value fills its free second slot. If the count is even, every node is full, so a brand-new node is created at the next position in the complete tree (left to right, level by level).

Why nothing bubbles here

Each pair of values (10&90, then 20&70, then 30&80, then 25&50) was inserted back to back, right after its node was created β€” and each pair already sits inside its parent's interval the moment it arrives. That is a deliberately convenient order. L20·12 reuses this exact heap and inserts 15, 100, and 65 into it β€” values that do not fit so conveniently, forcing the bubbling you have not seen yet.

CSE3144 β€” Lecture 20 Β· Building an interval heap
L20 Β· 12 β€” Animation: INSERT, all four cases ~17 min

Fits directly, bubbles via the min side, bubbles via the max side, or fills an odd slot

Starting heap: root [10,90], left [20,70], right [30,80], left-left [25,50] β€” 4 nodes, 8 elements (verified valid: every child's interval sits inside its parent's). Each case below starts fresh from this same heap, with a new node A about to be added as left's second child.

Interactive β€” four independent single-element inserts, one flagship chain
CSE3144 β€” Lecture 20 Β· Interval heap INSERT
L20 Β· 13 β€” Animation: REMOVE-MIN ~10 min

Take the root's left endpoint, promote from the last node, reinsert down

Continuing from the "bubbles via the min side" case: root [10,90], left [15,70], node A [20] (last node, single element). removeMin() must remove 10 and restore a valid interval heap using only these 9 elements.

Interactive β€” removeMin()
CSE3144 β€” Lecture 20 Β· Interval heap REMOVE-MIN
L20 Β· 14 β€” Advantages & limitations ~4 min

Half the space of a dual structure β€” for some extra bookkeeping per node

Advantages
  • Every element stored exactly once β€” no correspondence pointers, no duplicated storage, unlike the dual-structure method.
  • O(1) getMin and getMax β€” both just read the root.
  • O(log n) put/removeMin/removeMax β€” a single tree, a single array, ordinary heap-style bookkeeping.
  • Widely regarded as the simplest and most efficient of the heap-based DEPQ adaptations β€” simpler than min-max heaps, twin heaps, deaps, or diamond deques.
Limitations
  • Every node carries two elements, and every insert/remove must maintain the a ≤ b invariant and the containment property simultaneously β€” more bookkeeping per step than a plain single-value heap node.
  • Odd-count edge cases (a single-element last node) need special-casing throughout insert and remove.
  • Removing an arbitrary interior element (not just min or max) is possible in O(log n) but is a genuinely fiddlier procedure than an ordinary heap's remove(theNode).
CSE3144 β€” Lecture 20 Β· Interval heap trade-offs
L20 Β· 15 β€” Complexity & where this runs ~7 min

O(1) for reading either end, O(log n) for changing the collection

OperationCost (interval heap)
isEmpty(), size(), getMin(), getMax()O(1)
put(x), removeMin(), removeMax()O(log n)
Initializing an n-element interval heap from scratchO(n)
External sorting (today's example)

External quicksort's middle group, kept maximal via a live DEPQ β€” exactly the interval heap's textbook application.

SLA / QoS monitoring

A system tracking both "worst current latency" and "best current latency" across live requests needs both ends of a priority order simultaneously, updated in real time.

Order statistics / range filtering

Interval heaps also answer "which points lie outside [a,b]?" (the complementary range search problem) in O(k) time for k reported points β€” a nice bonus of the same structure.

CSE3144 β€” Lecture 20 Β· DEPQ complexity & applications
L20 Β· 16 β€” Recap & what's next ~7 min

A practical heap, and a heap that answers from both ends

Lecture 21 β€” next

Comparative Study of Heap Structures

Binary, Binomial, Fibonacci, and Pairing heaps, side by side β€” when to reach for which one, mirroring Lecture 15's tree comparison.

Homework β€” bring to Lecture 21
  • Build a pairing heap by inserting 20, 4, 15, 8, 30 in that order. Trace REMOVE-MIN using the two-pass scheme, showing every meld.
  • On paper, show why melding the orphaned children in a single left-to-right chain (the "bad way") can cost Θ(n) for one remove-min, using a small concrete example.
  • Insert the 8 elements 5, 40, 12, 35, 18, 28, 22, 30 into an empty interval heap, two at a time (filling each node's two slots as you go). Show the final tree and verify the containment property at every node.
  • In 3–4 sentences: why does a DEPQ built from a plain binary heap need a "correspondence pointer" scheme at all β€” what breaks if you just keep a separate min-heap and max-heap with no links between them?
CSE3144 β€” Lecture 20

Questions?

Dr. Manu Shrivastava β€” LHC 308F β€” Friday 2:00–5:00 PM

Next: Lecture 21 β€” Comparative Study of Heap Structures.