Download presentation
Presentation is loading. Please wait.
1
Source: Muangsin / Weiss1 Priority Queue (Heap) 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 -> dequeue the first enqueued element first
2
Source: Muangsin / Weiss2 Priority Queue (Heap) Operations insert (enqueue) deleteMin (dequeue) –smaller value higher priority –find, return, and remove the minimum element Priority Queue insertdeleteMin
3
Source: Muangsin / Weiss3 Implementation using Linked List 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
4
Source: Muangsin / Weiss4 Implementation using Binary Search Tree insert takes O(log N) time deleteMin takes O(log N) time support other operations that are not required by priority queue (for example, findMax) deleteMin operations make the tree unbalanced
5
Source: Muangsin / Weiss5 Terminology: full tree completely filled (bottom level is filled from left to right size between 2 h (bottom level has only one node) and 2 h+1 -1 A C G F B E J D HI
6
Source: Muangsin / Weiss6 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) 1313 1616 6868 1919 2121 3131 3232 2424 6565 2626
7
Source: Muangsin / Weiss7 Insert 1313 1616 6868 1919 2121 3131 3232 2424 6565 2626 Create a hole in the next available location Move the hole up (swap with its parent) until data can be placed in the hole without violating the heap order property (called percolate up) 1313 1616 6868 1919 2121 3232 2424 6565 2626 3131
8
Source: Muangsin / Weiss8 Insert 1313 1616 6868 1919 2121 3131 3232 2424 6565 2626 1313 1616 6868 1919 2121 3232 2424 6565 2626 3131 Percolate Up -> move the place to put 14 up (move its parent down) until its parent <= 14 insert 14
9
Source: Muangsin / Weiss9 Insert 1313 1616 6868 1919 2121 3131 3232 2424 6565 2626 1313 1616 6868 1919 2121 3131 3232 2424 6565 2626 1414
10
Source: Muangsin / Weiss10 deleteMin Create a hole at the root Move the hole down (swap with the smaller one of its children) until the last element of the heap can be placed in the hole without violating the heap order property (called percolate down) 1313 1616 6868 1919 2121 3131 3232 1919 6565 2626 1414 1616 6868 1919 2121 3232 1919 6565 2626 1414 31
11
Source: Muangsin / Weiss11 deleteMin 1313 1616 6868 1919 2121 3131 3232 1919 6565 2626 1414 1616 6868 1919 2121 3232 1919 6565 2626 1414 31 Percolate Down -> move the place to put 31 down (move its smaller child up) until its children >= 31
12
Source: Muangsin / Weiss12 deleteMin 1616 6868 1919 2121 3232 1919 6565 2626 1414 31 1616 6868 1919 2121 3232 1919 6565 2626 1414
13
Source: Muangsin / Weiss13 deleteMin 1616 6868 1919 2121 3232 1919 6565 1414 31 2626 1616 6868 1919 2121 3232 1919 6565 2626 1414 3131
14
Source: Muangsin / Weiss14 Running Time 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)
15
Source: Muangsin / Weiss15 Array Implementation of Binary Heap A C G F B E J D HI ABCDEFGHI J 012345678910111213 left child is in position 2i right child is in position (2i+1) parent is in position i/2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.