Docsity

Leftist Heaps: A Self-Adjusting Priority Queue Data Structure - Prof. Carleton Kingsford, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Leftist Heaps: A Self-Adjusting Priority Queue Data Structure - Prof. Carleton Kingsford and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Leftist Heaps

CMSC 420: Lecture 10

Priority Queue ADT

  • Efficiently support the following operations on a set

of keys:

findmin : return the smallest key

deletemin : return the smallest key & delete it

insert : add a new key to the set

delete : delete an arbitrary key

  • All the balanced-tree dictionary implementations

we’ve seen support these in O(log n ) time.

  • Would like to be able to do^ findmin^ faster (say O(1)).

Plane Sweep: Process points left to right:

Store points in a priority queue, ordered by their x coordinate.

Heap-Ordered Trees

  • The keys of the children of^ u^ are^ ≥^ the key( u ), for all nodes^ u.
  • (This “heap” has nothing to do with the “heap” part of computer memory.)
  • [Symmetric max-ordered version where keys are monotonically non- increasing]

Along each path keys are monotonically non-decreasing

  1. Add node as a leaf (we’ll see where later)

Heap – Insert

  1. “ sift up:” while current node is > its parent, swap them. 6

Heap – Delete( i )

  1. replace key to delete i with key j at a leaf node (we’ll see how to find a leaf soon)
  1. If i < j then sift up, moving j up the tree. If i > j then “sift down” : swap current node with smallest of children until its bigger than all of its children.
  2. need a pointer to node containing key i
  3. Delete leaf
15 21 A B C^ D^ E F

Store Heap in a Complete Tree

15 21 A B C^ D^ E F

Store Heap in a Complete Tree

2 8 3 12 9 7 10 15 21 A B C D E F

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 left( i ): 2 i if 2 i ≤ n otherwise 0 right(i): (2 i + 1) if 2 i + 1 ≤ n otherwise 0 parent(i):  i /2 if i ≥ 2 otherwise 0

Make Heap – Time Bound

There are at most n / h items at height h.

Siftdown for all height h nodes is O( h • n /

h

) time

Total time

= O(∑ h h•n /

h

) [sum of time for each height]

= O( n ∑ h ( h / 2

h

)) [factor out the n]

= O( n ) [sum bounded by const]

Heapsort – Another application of Heaps

1 2 3 4 5 6 7 8 9

Given unsorted
array of integers

end

Heapsort – Another application of Heaps

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

Given unsorted
array of integers
makeheap – O(n)
Now first position
has smallest item.
Delete last item from heap. 8 3 12 9 7 10 15

1 2 3 4 5 6 7 8 9

end end end

Heapsort – Another application of Heaps

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

Given unsorted
array of integers
makeheap – O(n)
Now first position
has smallest item.
Delete last item from heap. 8 3 12 9 7 10 15

1 2 3 4 5 6 7 8 9

end end end 8 12 9 10 15 1 2 3 4 5 6 7 8 9

end 3 7

siftdown new root key
down

Find( i )? How would you do it?

Leftist Heaps

  • Often want to merge heaps:

meld (H 1 , H 2 ): return new heap with the keys from H 1 and

H 2 , destroying heaps H 1 and H 2.

Hard to do with the complete tree implementation of

heaps above.

  • Idea: use^ imbalance^ to make melds fast.