WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin 2011-2012 1CE222_Dr. Senem Kumova Metin.
-
Upload
angel-quinn -
Category
Documents
-
view
287 -
download
0
Transcript of WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin 2011-2012 1CE222_Dr. Senem Kumova Metin.
WEEK 3 Leftist Heaps
CE222Dr. Senem Kumova Metin
2011-2012
1CE222_Dr. Senem Kumova Metin
OUTLINE• REVIEW OF PRIORITY QUEUES AND BINARY
HEAPS
• LEFTIST HEAP– Properties– Recursive Merging– Iterative Merging
2CE222_Dr. Senem Kumova Metin
PRIORITY QUEUE• A kind of queue• Dequeue gets element with the highest priority• Priority is based on a comparable value (key) of each object
(smaller value higher priority, or higher value higher priority)
Example Applications:• printer -> print (dequeue) the shortest document first• operating system -> run (dequeue) the shortest job first• normal queue -> the first enqueued element first
3CE222_Dr. Senem Kumova Metin
PRIORITY QUEUE (HEAP)• insert (enqueue)• deleteMin (dequeue)
– smaller value higher priority– Find / save the minimum element, delete it from structure
and return it
4CE222_Dr. Senem Kumova Metin
Priority Queue:Implementation• Unsorted linked list
– insert takes O(1) time– deleteMin takes O(N) time
• Sorted linked list– insert takes O(N) time– deleteMin takes O(1) time
• Binary Tree– insert takes O(log N) time on the average– deleteMin takes O(log N) time on the average– support other operations that are not required by priority queue (for
example, findMax)– deleteMin operations make the tree unbalanced
5CE222_Dr. Senem Kumova Metin
Binary Heap ImplementationProperty 1: Structure Property• Binary tree & completely filled (bottom level is filled
from left to right) (complete binary tree)• If height is h, size between 2h (bottom level has only one
node) and 2h+1-1
Property 2: Heap Order Property(for Minimum Heap)• Any node is smaller than (or equal to) all of its children
(any subtree is a heap)• Smallest element is at the root (findMin take O(1) time)
6CE222_Dr. Senem Kumova Metin
Binary Heap Implementation
7CE222_Dr. Senem Kumova Metin
Property 1: Structure Property height= ??
Property 2: Heap Order Property(for Minimum Heap)Parent< right childParent < left child
8
Binary Heap Implementation
8CE222_Dr. Senem Kumova Metin
9
Binary Heap Implementation
9CE222_Dr. Senem Kumova Metin
10
Binary Heap Implementation
10CE222_Dr. Senem Kumova Metin
CE222_Dr. Senem Kumova Metin 11
Binary Heap Implementation
CE222_Dr. Senem Kumova Metin 12
Binary Heap Implementation
1
CE222_Dr. Senem Kumova Metin 13
2 3
4 5
Binary Heap Implementation : deleteMin
Binary Heap Implementation• insert
• worst case: takes O(log N) time, moves an element from the bottom to the top
• on average: takes a constant time (2.607 comparisons), moves an element up 1.607 levels
• deleteMin• worst case: takes O(log N) time• on average: takes O(log N) time (element that is placed
at the root is large, so it is percolated almost to the bottom )
14CE222_Dr. Senem Kumova Metin
LEFTIST HEAPS
CE222_Dr. Senem Kumova Metin 15
16
Leftist Heaps : Motivation Binary heaps perform merge operation in O(N)
time Challenge:
Build a heap that can merge in O(logN) time Use an array just like the Binary heap
Traditionally data structures support efficient merging use linked lists
Using linked lists is inefficient due to pointer operations
16CE222_Dr. Senem Kumova Metin
17
Leftist Heaps : DefinitionsA Leftist (min)Heap is a binary tree that satisfies the following conditions: If X is a node and L and R are its left and right children, then:
Property 1. Heap Order PropertyX.value ≤ L.valueX.value ≤ R.value
Property 2. Leftist Heap Property Null path length of L ≥ Null path length of R
17CE222_Dr. Senem Kumova Metin
18
Leftist Heaps : Definitions
Null Path Length (npl) of a node is the shortest distance from a node to a descendant with 0 or 1 child
If a node is null, its null path length is −1.
18CE222_Dr. Senem Kumova Metin
19
Leftist Heaps : Definitions npl(X) of any node X to be the length of the
shortest path from X to a node without two children.
npl of a node with zero or one child is 0, while npl(NULL) = -1
19CE222_Dr. Senem Kumova Metin
Example: Null Path Length
19
2027
4325
12
15
8
4
node 4 8 19 12 15 25 27 20 43
npl 1 0 1 1 0 0 0 0 0
20CE222_Dr. Senem Kumova Metin
Theorem 6.2
Theorem: A leftist tree with r nodes on the right path must have at least 2r-1 nodes.
21CE222_Dr. Senem Kumova Metin
Theorem 6.2 :Proof by Induction• r=1, at least 1 tree node is required True Induction hypothesis: Tree with r nodes on the
right path has 2r-1 nodes. For a leftist tree with r+1 nodes on the right path
then the root has a right subtree with r nodes on the right path, and a left subtree with at least r nodes on the right path (npl(left)>=npl(right))
Then each subtree has at least 2r-1 nodes The tree including the root then has
(2r-1) + (2r-1) +1=2r+1-1 nodes then, proving the theorem
22CE222_Dr. Senem Kumova Metin
Theorem 6.2 ResultFrom this theorem, it follows immediately
that a leftist tree of N nodes has a right path containing at most log(N + 1) nodes.
The general idea for the leftist heap operations is to perform all the work on the right path, which is guaranteed to be short.
23CE222_Dr. Senem Kumova Metin
Leftist Heap : OperationsThe fundamental operation on leftist heaps is merging.
– Recursive– Iterative
Insertion ? A special case of merging, since we may view an insertion as a merge of a one-node heap with a larger heap.
24CE222_Dr. Senem Kumova Metin
Leftist Heap Merge : Iterative version
Requires 2 passes on the heap:First pass: Create a new tree by merging the
right paths of both heaps in sorted order
Second pass: Perform child swaps at nodes that violate the leftist heap property.
Performs merging in O(logN) time
25CE222_Dr. Senem Kumova Metin
Leftist Heap Merge : Iterative versionExample
Heaps H1 and H2
3 8 6 7 18 3 8 6 7 18 SORT SORT 3 6 7 8 18 3 6 7 8 1826
Iterative Merge: Example
Heaps
First pass
27CE222_Dr. Senem Kumova Metin
Iterative Merge: Example
First pass
Second pass
28CE222_Dr. Senem Kumova Metin
(1) If either of the two heaps is empty, then return the other heap.
(2) Otherwise, to merge the two heaps, compare their roots, and recursively merge the heap with the larger root (H2) with the right subheap of the heap with the smaller root (H1)
(3) Make the new heap the right child of the root of H1
(4) Make the entire tree leftist by swapping the root's left and right children and updating the null path length
Leftist Heap Merge : Recursive version
29CE222_Dr. Senem Kumova Metin
Binary Heap versus Leftist Heap
CE222_Dr. Senem Kumova Metin 30
Insertion Delete Minimum
Merge
Binary Heap
O(logN) O(logN) O(N)
Leftist Heap
O(logN) O(logN) O(logN)