Presentation is loading. Please wait.

Presentation is loading. Please wait.

WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin 2011-2012 1CE222_Dr. Senem Kumova Metin.

Similar presentations


Presentation on theme: "WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin 2011-2012 1CE222_Dr. Senem Kumova Metin."— Presentation transcript:

1 WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin 2011-2012 1CE222_Dr. Senem Kumova Metin

2 OUTLINE REVIEW OF PRIORITY QUEUES AND BINARY HEAPS LEFTIST HEAP – Properties – Recursive Merging – Iterative Merging 2CE222_Dr. Senem Kumova Metin

3 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

4 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

5 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

6 Binary Heap Implementation Property 1: Structure Property Binary tree & completely filled (bottom level is filled from left to right) (complete binary tree) If height is h, size between 2 h (bottom level has only one node) and 2 h+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

7 Binary Heap Implementation 7CE222_Dr. Senem Kumova Metin Property 1: Structure Property height= ?? Property 2: Heap Order Property(for Minimum Heap) Parent< right child Parent < left child

8 8 Binary Heap Implementation 8CE222_Dr. Senem Kumova Metin

9 9 Binary Heap Implementation 9CE222_Dr. Senem Kumova Metin

10 10 Binary Heap Implementation 10CE222_Dr. Senem Kumova Metin

11 11 Binary Heap Implementation

12 CE222_Dr. Senem Kumova Metin12 Binary Heap Implementation 1

13 CE222_Dr. Senem Kumova Metin13 23 4 5 Binary Heap Implementation : deleteMin

14 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

15 LEFTIST HEAPS CE222_Dr. Senem Kumova Metin15

16 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 17 Leftist Heaps : Definitions A 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 Property X.value ≤ L.value X.value ≤ R.value Property 2. Leftist Heap Property Null path length of L ≥ Null path length of R 17CE222_Dr. Senem Kumova Metin

18 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 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

20 Example: Null Path Length 19 2027 43 25 12 15 8 4 node4 8 19 12 15 25 27 20 43 npl1 0 1 1 0 0 0 0 0 20CE222_Dr. Senem Kumova Metin

21 Theorem 6.2 Theorem: A leftist tree with r nodes on the right path must have at least 2 r -1 nodes. 21CE222_Dr. Senem Kumova Metin

22 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 2 r -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 2 r -1 nodes The tree including the root then has (2 r -1) + (2 r -1) +1=2 r+1 -1 nodes then, proving the theorem 22CE222_Dr. Senem Kumova Metin

23 Theorem 6.2 Result From 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

24 Leftist Heap : Operations The 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

25 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

26 Leftist Heap Merge : Iterative version Example Heaps H1 and H2 3 8 6 7 18  SORT  3 6 7 8 18 26

27 Iterative Merge: Example Heaps First pass 27CE222_Dr. Senem Kumova Metin

28 Iterative Merge: Example First pass Second pass 28CE222_Dr. Senem Kumova Metin

29 (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

30 Binary Heap versus Leftist Heap CE222_Dr. Senem Kumova Metin30 InsertionDelete Minimum Merge Binary Heap O(logN) O(N) Leftist Heap O(logN)


Download ppt "WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin 2011-2012 1CE222_Dr. Senem Kumova Metin."

Similar presentations


Ads by Google