Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2000-2003 Haluk Bingöl v2.23 Data Structures and Algorithms - 03 Heaps and Priority Queues Dr. Haluk Bingöl BÜ - CmpE

Similar presentations


Presentation on theme: "© 2000-2003 Haluk Bingöl v2.23 Data Structures and Algorithms - 03 Heaps and Priority Queues Dr. Haluk Bingöl BÜ - CmpE"— Presentation transcript:

1 © 2000-2003 Haluk Bingöl v2.23 Data Structures and Algorithms - 03 Heaps and Priority Queues Dr. Haluk Bingöl BÜ - CmpE bingol@boun.edu.trmeraklisina.com BU-SWE 510 Fall 2003

2 2 © 2003 bingol meraklisina.comContent MotivationMotivation Binary HeapBinary Heap Leftist HeapsLeftist Heaps Binomial HeapsBinomial Heaps

3 3 © 2003 bingol meraklisina.com Motivation

4 4 © 2003 bingol meraklisina.com Motivation … Problem WaitingWaiting –Queue 1 4 2

5 5 © 2003 bingol meraklisina.com Motivation … Problem - At the Bank Queue at the counterQueue at the counter Give priority toGive priority to –Elderly –Pregnant –With baby –… 1 4 2

6 6 © 2003 bingol meraklisina.com Motivation … Problem - Operating System Processes waiting for processorProcesses waiting for processor Give priority toGive priority to –I/O bound –Interrups –… 1 4 2

7 7 © 2003 bingol meraklisina.com Motivation … Problem – Ho1spital Patients waiting for help in Emergency RoomPatients waiting for help in Emergency Room Give priority toGive priority to –Severely wounded –Bleading –… 1 4 2

8 8 © 2003 bingol meraklisina.com Motivation … Problem - In General Anything waiting for serviceAnything waiting for service –Printer –CPU 1 4 2

9 9 © 2003 bingol meraklisina.com Review of Queues

10 10 © 2003 bingol meraklisina.com Review Queue FIFO –first come –first serve One entry placeOne entry place One exit placeOne exit place No change in orderNo change in order No priorityNo priority public interface Queue extends Container { Object getHead(); void enqueue(Object object); Object dequeue(); } 1 4 2

11 11 © 2003 bingol meraklisina.com Review Queue … Sample operation 1 enqueque(4) 1 4 enqueque(2) 1 4 2 enqueque(3) 1 4 2 3 dequeque() 4 2 3 dequeque() 2 3 enqueque(1) 2 3 1 enqueque(1) 2 3 1 1 dequeque() 3 1 1 dequeque() 1 dequeque() 1

12 12 © 2003 bingol meraklisina.com Review Queue … Priorities - a few If there is a need for change in the orderIf there is a need for change in the order –One queue for each priority a1 a4b3 b1 b27 b3 b3c6 c4 c9 Algorithm enqueueenqueue –put in to proper queue dequeuedequeue –get from P1 first –then get from P2 –then get from P3 P1 P2 P3

13 13 © 2003 bingol meraklisina.com Review Queue … Priorities - more If there is a need for change in the orderIf there is a need for change in the order –One queue for each priority –Number of different priority categories is known a1 a4b3 b1 b27 b3 b3c6 c4 c9 P1 P2 P3 Algorithm enqueueenqueue –put in to proper queue dequeuedequeue –get from P1 first –then get from P2 –then get from P3 –… –then get from P123  7  12  41 P123......

14 14 © 2003 bingol meraklisina.com Review Queue … Priorities – too many a1 a4b3 b1 b27 b3 b3c6 c4 c9 P1 P2 P3 Algorithm enqueueenqueue –put in to proper queue dequeuedequeue –get from P1 first –then get from P2 –then get from P3 –… –then get from P123  7  12  41 P123...... If there is a need for change in the orderIf there is a need for change in the order –One queue for each priority –Number of different priority categories is unknown

15 15 © 2003 bingol meraklisina.com Priority Queues

16 16 © 2003 bingol meraklisina.com Priority Queues … Abstractions Priority queuePriority queue –A list of items –Each item has an associated priority InsertionInsertion –Arbitrary order –Arbitrary priority DeletionDeletion –Item with the lowest(highest) priority

17 17 © 2003 bingol meraklisina.com Priority Queues … Abstract Operations enqueueenqueue –Puts an object in the container findMinfindMin –Returns a reference to the smallest object in the container dequeueMindequeueMin –Removes the smallest object from the container

18 18 © 2003 bingol meraklisina.com Simple Implementations

19 19 © 2003 bingol meraklisina.com Simple Implementations … Arrays Unordered ArrayUnordered Array –Insertion at the endat the end O(1)O(1) –Deletion Search the minSearch the min Linear search O(n)Linear search O(n)

20 20 © 2003 bingol meraklisina.com Simple Implementations … Arrays Ordered ArrayOrdered Array –Insertion In the orderIn the order O(n)O(n) –Deletion At the endAt the end O(1)O(1) Unordered ArrayUnordered Array –Insertion at the endat the end O(1)O(1) –Deletion Search the minSearch the min Linear search O(n)Linear search O(n)

21 21 © 2003 bingol meraklisina.com Simple Implementations … Link Lists Unordered ArrayUnordered Array –Insertion at the endat the end O(1)O(1) –Deletion Search the minSearch the min Linear search O(n)Linear search O(n) Unordered Link ListUnordered Link List Ordered ArrayOrdered Array –Insertion In the orderIn the order O(n)O(n) –Deletion At the endAt the end O(1)O(1) Ordered Link ListOrdered Link List

22 22 © 2003 bingol meraklisina.com Simple Implementations … Binary Search Tree InsertInsert –O(log n) DeletionDeletion –O(log n) Unused propertiesUnused properties abc

23 23 © 2003 bingol meraklisina.com Priority Queues Heap

24 24 © 2003 bingol meraklisina.com Class Hierarchy Container PriorityQueueTree BinaryHeapMergeablePriorityQueue BinomialQueueLeftistHeap BinaryTreeGeneralTree BinomialTree

25 25 © 2003 bingol meraklisina.com Priority Queues … Priority Queue Interface enqueueenqueue –Puts an object in the priorityQueue findMinfindMin –Returns a reference to the smallest object in the priorityQueue dequeueMindequeueMin –Removes the smallest object from the priorityQueue public interface PriorityQueue extends Container { void enqueue(Comparable object); Comparable findMin(); Comparable dequeueMin(); }

26 26 © 2003 bingol meraklisina.com Priority Queues … Mergeable Priority Queue Interface public interface MergeablePriorityQueue extends PriorityQueue { void merge(MergeablePriorityQueue queue); }

27 27 © 2003 bingol meraklisina.com Priority Queues … Types HeapHeap Binary HeapBinary Heap Leftist HeapLeftist Heap Binomial HeapBinomial Heap

28 28 © 2003 bingol meraklisina.com Heap

29 29 © 2003 bingol meraklisina.com Review N-ary Tree Definition An N-ary tree T is a finite set of nodes with the following properties: 1.Either the set is empty, T =  ; or 2.The set consists of a root, R, and exactly N distinct N-ary trees. That is, the remaining nodes are partitioned into N  0 subsets, T 0, T 1, …, T N-1, each of which is an N-ary tree such that T = {R, T 0, T 1, …, T N-1 }. Remark The empty tree, T = , is a tree Definition The empty trees are called external nodes because they have no subtrees and therefore appear at the extremities of the tree. Conversely, the non- empty trees are called internal nodes.

30 30 © 2003 bingol meraklisina.comHeapDefinition A (Min) Heap is a tree, T = {R, T 0, T 1, …, T n-1 } T = {R, T 0, T 1, …, T n-1 } with the following properties: 1.Every subtree of T is a heap; and, 2.The root of T is less than or equal to the root of every subtree of T. That is, R  R i for all i, 0  i  n, where R i is the root of T i. RR1R1 R1R1 R1R1 …

31 31 © 2003 bingol meraklisina.comHeapDefinition A (Min) Heap is a tree, T = {R, T 0, T 1, …, T 0-1 } with the following properties: 1.Every subtree of T is a heap; and, 2.The root of T is less than or equal to the root of every subtree of T. That is, R  R i for all i, 0  i  n, where R i is the root of T i. RR1R1 R1R1 R1R1 … Observations Each node is less then all the subtrees of it. No restrictions for the relative ordering of the subtrees.

32 32 © 2003 bingol meraklisina.com Binary Heap

33 33 © 2003 bingol meraklisina.com Recap Perfect Binary Tree Definition A perfect binary tree of height h  0 is a binary tree T = {R, T L, T R } with the following properties: 1.If h = 0, T L =  and T R = . 2.If h > 0, then T L is a perfect binary tree of height h-1. T R is a perfect binary tree of height h-1. Theorem A perfect binary tree of height h has exactly 2 h+1 - 1 internal nodes. Theorem The height of a perfect binary tree with n internal nodes is log (n+1).

34 34 © 2003 bingol meraklisina.com Complete Binary Tree Definition A complete binary tree of height h  0, is a binary tree T={R, T L, T R } with the following properties: 1.If h=0, T L =  and T R = . 2.For h>0 there are two possibilities: a.T L is a perfect binary tree of height h-1 and T R is a complete binary tree of height h-1 b.T L is a complete binary tree of height h-1 and T R is a perfect binary tree of height h-2.

35 35 © 2003 bingol meraklisina.com Complete Binary Tree … 1 –1 L 2 L2 L 2 R is a complete binary tree of height 22 R is a complete binary tree of height 2 –5 L is a perfect binary tree of height 1 –5 R is a perfect binary tree of height 0 –1 R 5L5L 5R5R 2R2R

36 36 © 2003 bingol meraklisina.com Complete Binary Tree … 1 –1 L is a complete binary tree of height 3 (2a) 2 L is a perfect binary tree of height 22 L is a perfect binary tree of height 2 2 R is a complete binary tree of height 22 R is a complete binary tree of height 2 –5 L is a perfect binary tree of height 1 –5 R is a perfect binary tree of height 0 –1 R 2L2L 2R2R 1L1L

37 37 © 2003 bingol meraklisina.com Complete Binary Tree … 1 is a complete binary tree of height 4 (2b)1 is a complete binary tree of height 4 (2b) –1 L is a complete binary tree of height 3 (2a) 2 L is a perfect binary tree of height 22 L is a perfect binary tree of height 2 2 R is a complete binary tree of height 22 R is a complete binary tree of height 2 –5 L is a perfect binary tree of height 1 –5 R is a perfect binary tree of height 0 –1 R is a perfect binary tree of height 2 1L1L 1R1R

38 38 © 2003 bingol meraklisina.com Complete Binary Tree … Array representation abc b = 2a c = 2a+1 a =  b/2  a =  c/2 

39 39 © 2003 bingol meraklisina.com Complete Binary Tree … Theorem A complete binary tree of height h  0 contains at least 2 h and at most 2 h+1 - 1 nodes.

40 40 © 2003 bingol meraklisina.com Complete Binary Tree … Theorem The internal path length of a binary tree with n nodes is at least as big as the internal path length of a complete binary tree with n nodes. Definition The internal path length of a tree is simply the sum of the depths (levels) of all the internal nodes in the tree d i is the depth of the i th node n is the number of nodes

41 41 © 2003 bingol meraklisina.com Complete N-ary Tree Informally a complete tree is a tree in whicha complete tree is a tree in which –all the levels are full except for the bottom level and –the bottom level is filled from left to right.

42 42 © 2003 bingol meraklisina.com Complete N-ary Tree … Definition A complete N-ary tree of height h  0, is an N-ary tree T = {R, T 0, T 1, …, T N-1 } with the following properties 1.If h=0, T i =  for all i, 0  i < N. 2.For h>0 there exists a j, 0  j 0 there exists a j, 0  j < N such that a. T i is a perfect binary tree of height h-1 for all i, 0  i < j b. T j is a complete binary tree of height h-1 c. T i is a perfect binary tree of height h-2 for all i, j<i<N

43 43 © 2003 bingol meraklisina.com Complete N-ary Tree … Array Representation c1c1 Children of node i c 1 = N(i-1)+2 c 2 = N(i-1)+3 c 3 = N(i-1)+4 … c N = Ni+i Parent of node i  (i-1)N  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 c2c2 cNcN … i

44 44 © 2003 bingol meraklisina.com Binary Heap Implementation

45 45 © 2003 bingol meraklisina.com Binary Heap Implementation … Constructors public class BinaryHeap { … /** * Construct the binary heap. */ public BinaryHeap() { this(DEFAULT_CAPACITY); } /** * Construct the binary heap. * @param capacity the capacity of the binary heap. */ public BinaryHeap(int capacity) { currentSize = 0; array = new Comparable[capacity + 1]; } … }

46 46 © 2003 bingol meraklisina.com Binary Heap Implementation … enqueue

47 47 © 2003 bingol meraklisina.com Binary Heap Implementation … enqueue public class BinaryHeap { … /** * Insert into the priority queue, maintaining heap order. * Duplicates are allowed. * @param x the item to insert. * @exception Overflow if container is full. */ public void insert(Comparable x) throws Overflow { if (isFull()) throw new Overflow(); // Percolate up int hole = ++currentSize; for (; hole > 1 && x.compareTo(array[hole / 2]) < 0; hole /= 2) array[hole] = array[hole / 2]; array[hole] = x; } … }

48 48 © 2003 bingol meraklisina.com Binary Heap Implementation … enqueue public class BinaryHeap { … /** * Insert into the priority queue, maintaining heap order. * Duplicates are allowed. * @param x the item to insert. * @exception Overflow if container is full. */ public void insert(Comparable x) throws Overflow { if (isFull()) throw new Overflow(); // Percolate up int hole = ++currentSize; for (; hole > 1 && x.compareTo(array[hole / 2]) < 0; hole /= 2) array[hole] = array[hole / 2]; array[hole] = x; } … } weiss Fig 6.6 Fig 6.7 Worst-case: O(log n)

49 49 © 2003 bingol meraklisina.com Binary Heap Implementation … enqueue - Exacution insert 14

50 50 © 2003 bingol meraklisina.com Binary Heap Implementation … enqueue - Exacution insert 14

51 51 © 2003 bingol meraklisina.com Binary Heap Implementation … enqueue - Exacution insert 14

52 52 © 2003 bingol meraklisina.com Binary Heap Implementation … enqueue - Exacution insert 14

53 53 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue

54 54 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue public class BinaryHeap { … /** * Remove the smallest item from the priority queue. * @return the smallest item, or null, if empty. */ public Comparable deleteMin() { if (isEmpty()) return null; Comparable minItem = findMin(); array[1] = array[currentSize--]; percolateDown(1); return minItem; } … }

55 55 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue public class BinaryHeap { … /** * Remove the smallest item from the priority queue. * @return the smallest item, or null, if empty. */ public Comparable deleteMin() { if (isEmpty()) return null; Comparable minItem = findMin(); array[1] = array[currentSize--]; percolateDown(1); return minItem; } … } public class BinaryHeap { … /** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */ private void percolateDown(int hole) { int child; Comparable tmp = array[hole]; for (; hole * 2 <= currentSize; hole = child) { child = hole * 2; // left if (child != currentSize && array[child + 1].compareTo(array[child]) < 0) child++; if (array[child].compareTo(tmp) < 0) array[hole] = array[child]; else break; } array[hole] = tmp; } … }

56 56 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue public class BinaryHeap { … /** * Remove the smallest item from the priority queue. * @return the smallest item, or null, if empty. */ public Comparable deleteMin() { if (isEmpty()) return null; Comparable minItem = findMin(); array[1] = array[currentSize--]; percolateDown(1); return minItem; } … } public class BinaryHeap { … /** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */ private void percolateDown(int hole) { int child; Comparable tmp = array[hole]; for (; hole * 2 <= currentSize; hole = child) { child = hole * 2; // left if (child != currentSize && array[child + 1].compareTo(array[child]) < 0) child++; if (array[child].compareTo(tmp) < 0) array[hole] = array[child]; else break; } array[hole] = tmp; } … } Worst-case: O(log n)

57 57 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue – Execution

58 58 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue – Execution

59 59 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue – Execution

60 60 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue – Execution

61 61 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue – Execution

62 62 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue – Execution

63 63 © 2003 bingol meraklisina.com Binary Heap Implementation … buildHeap public class BinaryHeap { … /** * Establish heap order property from an arbitrary * arrangement of items. Runs in linear time. */ private void buildHeap() { for (int i = currentSize / 2; i > 0; i--) percolateDown(i); } … } public class BinaryHeap { … /** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */ private void percolateDown(int hole) { int child; Comparable tmp = array[hole]; for (; hole * 2 <= currentSize; hole = child) { child = hole * 2; // left if (child != currentSize && array[child + 1].compareTo(array[child]) < 0) child++; if (array[child].compareTo(tmp) < 0) array[hole] = array[child]; else break; } array[hole] = tmp; } … }

64 64 © 2003 bingol meraklisina.com Binary Heap Implementation … buildHeap public class BinaryHeap { … /** * Establish heap order property from an arbitrary * arrangement of items. Runs in linear time. */ private void buildHeap() { for (int i = currentSize / 2; i > 0; i--) percolateDown(i); } … } public class BinaryHeap { … /** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */ private void percolateDown(int hole) { int child; Comparable tmp = array[hole]; for (; hole * 2 <= currentSize; hole = child) { child = hole * 2; // left if (child != currentSize && array[child + 1].compareTo(array[child]) < 0) child++; if (array[child].compareTo(tmp) < 0) array[hole] = array[child]; else break; } array[hole] = tmp; } … }

65 65 © 2003 bingol meraklisina.com Binary Heap Implementation … buildHeap public class BinaryHeap { … /** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */ private void percolateDown(int hole) { int child; Comparable tmp = array[hole]; for (; hole * 2 <= currentSize; hole = child) { child = hole * 2; // left if (child != currentSize && array[child + 1].compareTo(array[child]) < 0) child++; if (array[child].compareTo(tmp) < 0) array[hole] = array[child]; else break; } array[hole] = tmp; } … } Weiss Fig 6.15 6.16 6.17 6.18

66 66 © 2003 bingol meraklisina.com Binary Heap Implementation … findMin public class BinaryHeap { … /** * Find the smallest item in the priority queue. * @return the smallest item, or null, if empty. */ public Comparable findMin() { if (isEmpty()) return null; return array[1]; } … }

67 67 © 2003 bingol meraklisina.com Binary Heap Implementation … Best Practices: Header Sample // BinaryHeap class // // CONSTRUCTION: with optional capacity (that defaults to 100) // // ******************PUBLIC OPERATIONS********************* // void insert( x ) --> Insert x // Comparable deleteMin( )--> Return and remove smallest item // Comparable findMin( ) --> Return smallest item // boolean isEmpty( ) --> Return true if empty; else false // boolean isFull( ) --> Return true if full; else false // void makeEmpty( ) --> Remove all items // ******************ERRORS******************************** // Throws Overflow if capacity exceeded /** * Implements a binary heap. * Note that all "matching" is based on the compareTo method. * @author Mark Allen Weiss */ public class BinaryHeap { … }

68 68 © 2003 bingol meraklisina.com Binary Heap Implementation … Best Practices: Test Sample Use main() for testingUse main() for testing public class BinaryHeap { … // Test program public static void main(String[] args) { int numItems = 10000; BinaryHeap h = new BinaryHeap(numItems); int i = 37; try { for (i = 37; i != 0; i = (i + 37) % numItems) h.insert(new MyInteger(i)); for (i = 1; i < numItems; i++) if (((MyInteger) (h.deleteMin())).intValue() != i) System.out.println("Oops! " + i); for (i = 37; i != 0; i = (i + 37) % numItems) h.insert(new MyInteger(i)); h.insert(new MyInteger(0)); i = 9999999; h.insert(new MyInteger(i)); for (i = 1; i <= numItems; i++) if (((MyInteger) (h.deleteMin())).intValue() != i) System.out.println("Oops! " + i + " "); } catch (Overflow e) { System.out.println("Overflow (expected)! " + i); }

69 69 © 2003 bingol meraklisina.com Binary Heap Implementation … Complexity Summary OperationComplexity enqueue O(log 2 n) dequeue builtO(n) BinaryBinary

70 70 © 2003 bingol meraklisina.com N-ary Heap Implementation … Complexity Summary OperationComplexity enqueue O(log N n) dequeue builtO(n) N-aryN-ary

71 71 © 2003 bingol meraklisina.com Leftist Trees

72 72 © 2003 bingol meraklisina.com Leftist Tree Mergeable priority queuesMergeable priority queues –Leftist heaps

73 73 © 2003 bingol meraklisina.com Leftist Trees … Null Path Definition Consider an arbitrary node x in some binary tree T. The null path of node x is the shortest path in T from x to an external node of T. The null path length of node x is the length of its null path. Definition The null path length of an empty tree is zero. The null path length of a non-empty binary tree T={R, T L, T R } is the null path length its root R.

74 74 © 2003 bingol meraklisina.com Leftist Trees … Leftist Tree Definition A leftist tree is a binary tree T with the following properties: 1.Either T =  ; or 2.T= {R, T L, T R }, where both T L and T R are leftist trees which have null path lengths d L and d L, respectively, such that d L  d L Remark A leftist tree is a tree in which the shortest path to an external node is always on the right.

75 75 © 2003 bingol meraklisina.com Leftist Trees … Length of the Right Path Theorem Consider a leftist tree T which contains n internal nodes. The path leading from the root of T downwards to the rightmost external node contains at most  log2(n+1)  nodes.

76 76 © 2003 bingol meraklisina.com Leftist Trees … Leftist Heaps A heap-ordered treeA heap-ordered tree –The positions of the keys in the tree A leftist treeA leftist tree –The shape of the tree

77 77 © 2003 bingol meraklisina.com Leftist Trees … Merging Leftist Heaps if h1 is empty, swap h1 and h2if h1 is empty, swap h1 and h2 otherwise, assume the root of h2 is larger than the root of h1:otherwise, assume the root of h2 is larger than the root of h1: recursively merge h2 with the right subheap of h1recursively merge h2 with the right subheap of h1 if the right subheap of h1 now has a larger null path length then its left subheap, swap the left and right subheapsif the right subheap of h1 now has a larger null path length then its left subheap, swap the left and right subheaps if h2 initially has the smaller root, exchange h1 and h2 and proceed as aboveif h2 initially has the smaller root, exchange h1 and h2 and proceed as above

78 78 © 2003 bingol meraklisina.com Leftist Trees … Implementation - LeftHeapNode // Basic node stored in leftist heaps // Note that this class is not accessible outside // of package DataStructures class LeftHeapNode { // Constructors LeftHeapNode(Comparable theElement) { this(theElement, null, null); } LeftHeapNode(Comparable theElement, LeftHeapNode lt, LeftHeapNode rt) { element = theElement; left = lt; right = rt; npl = 0; } // Friendly data; accessible by other package routines Comparable element; // The data in the node LeftHeapNode left; // Left child LeftHeapNode right; // Right child int npl; // null path length }

79 79 © 2003 bingol meraklisina.com Leftist Trees … Implementation – Merging public class LeftistHeap { … /** * Merge rhs into the priority queue. * rhs becomes empty. rhs must be different from this. * @param rhs the other leftist heap. */ public void merge(LeftistHeap rhs) { if (this == rhs) // Avoid aliasing problems return; root = merge(root, rhs.root); rhs.root = null; } /** * Internal static method to merge two roots. * Deals with deviant cases and calls recursive merge1. */ private static LeftHeapNode merge(LeftHeapNode h1, LeftHeapNode h2) { if (h1 == null) return h2; if (h2 == null) return h1; if (h1.element.compareTo(h2.element) < 0) return merge1(h1, h2); else return merge1(h2, h1); } … }

80 80 © 2003 bingol meraklisina.com public class LeftistHeap { … /** * Merge rhs into the priority queue. * rhs becomes empty. rhs must be different from this. * @param rhs the other leftist heap. */ public void merge(LeftistHeap rhs) { if (this == rhs) // Avoid aliasing problems return; root = merge(root, rhs.root); rhs.root = null; } /** * Internal static method to merge two roots. * Deals with deviant cases and calls recursive merge1. */ private static LeftHeapNode merge(LeftHeapNode h1, LeftHeapNode h2) { if (h1 == null) return h2; if (h2 == null) return h1; if (h1.element.compareTo(h2.element) < 0) return merge1(h1, h2); else return merge1(h2, h1); } … Leftist Trees … Implementation – Merging … /** * Internal static method to merge two roots. * Assumes trees are not empty, * and h1's root contains smallest item. */ private static LeftHeapNode merge1(LeftHeapNode h1, LeftHeapNode h2) { if (h1.left == null) // Single node h1.left = h2; // Other fields in h1 already accurate else { h1.right = merge(h1.right, h2); if (h1.left.npl < h1.right.npl) swapChildren(h1); h1.npl = h1.right.npl + 1; } return h1; } /** * Swaps t's two children. */ private static void swapChildren(LeftHeapNode t) { LeftHeapNode tmp = t.left; t.left = t.right; t.right = tmp; } … }

81 81 © 2003 bingol meraklisina.com Leftist Trees … Implementation – enqueue() public class LeftistHeap { … /** * Insert into the priority queue, * maintaining heap order. * @param x the item to insert. */ public void insert(Comparable x) { root = merge(new LeftHeapNode(x), root); } … }

82 82 © 2003 bingol meraklisina.com Leftist Trees … Implementation – findMin() public class LeftistHeap { … /** * Find the smallest item in the priority queue. * @return the smallest item, or null, if empty. */ public Comparable findMin() { if (isEmpty()) return null; return root.element; } … }

83 83 © 2003 bingol meraklisina.com Leftist Trees … Implementation – dequeueMin() public class LeftistHeap { … /** * Remove the smallest item from the priority queue. * @return the smallest item, or null, if empty. */ public Comparable deleteMin() { if (isEmpty()) return null; Comparable minItem = root.element; root = merge(root.left, root.right); return minItem; } … }

84 84 © 2003 bingol meraklisina.com Leftist Trees … Complexity Summary OperationComplexity merge O(log 2 n 1 + log 2 n 2 ) enqueue O(log 2 n) dequeue findMin O(1)

85 85 © 2003 bingol meraklisina.com Binomial Queues

86 86 © 2003 bingol meraklisina.com Binomial Queues binomial queuesbinomial queues –binomial trees –forests –merging of binomial queues

87 87 © 2003 bingol meraklisina.com Binomial Queues … Definition The binomial tree of order k  0 with root R is the tree B k defined as follows 1.If k = 0, B 0 = {R}. i.e., the binomial tree of order zero consists of a single node, R. 2.If k > 0, B k = {R, B 0, B 1, …, B k-1 }. i.e., the binomial tree of order k > 0 comprises the root R, and k binomial subtrees, B0, B1,..., B k-1.

88 88 © 2003 bingol meraklisina.com Binomial Queues … Different Views

89 89 © 2003 bingol meraklisina.com Binomial Queues … Number of Nodes Theorem The binomial tree of order k, B k, contains 2 k nodes.

90 90 © 2003 bingol meraklisina.com Binomial Queues … Height Theorem The height of the binomial tree of order k, B k, is k.

91 91 © 2003 bingol meraklisina.com Binomial Queues … Binomial Theorem Theorem The n th power of the binomial x+y for n  0 is given by where is called binomial coefficient

92 92 © 2003 bingol meraklisina.com Binomial Queues … Why Binomial ? Theorem The number of nodes at level l in B k, the binomial tree of order k, where 0 ≤ l ≤ k, is given by the binomial coefficient level 0 1 2 3 4... 1 1 1 1 2 1 1 3 3 1 14 6 4 1... k01234...

93 93 © 2003 bingol meraklisina.com Binomial trees only come in sizes that are a power of 2 How to represent arbitrary number, n, of items? Consider the binary representation of the number n: where b i  {0, 1} is the i th bit To hold n items use a forest of binomial trees: Fn = {B i : b i = 1}; Binomial Queues … Any Size ?

94 94 © 2003 bingol meraklisina.com Binomial Queues … Merging Binomial Queues Merging two binomial queues is like doing binary addition B4B4B4B4 B3B3B3B3 B1B1B1B1 B0B0B0B0 F 27 2711011 + B3B3B3B3 B1B1B1B1 F 10 +10+1010 B5B5B5B5 B2B2B2B2 B0B0B0B0 F 37 37100101

95 95 © 2003 bingol meraklisina.com Binomial Queues … Merging Binomial Queues...

96 96 © 2003 bingol meraklisina.com Debuging Tools

97 97 © 2003 bingol meraklisina.com Debuging Tools public static void main(String[] args) { int numItems = 10000; BinaryHeap h = new BinaryHeap(numItems); switch (1) { case 1 : try { h.insert(new MyInteger(4)); h.printTree(); h.insert(new MyInteger(2)); h.printTree(); h.insert(new MyInteger(6)); h.printTree(); h.insert(new MyInteger(7)); h.printTree(); h.insert(new MyInteger(1)); h.printTree(); } catch (Overflow e1) { // TODO Auto-generated catch block e1.printStackTrace(); } break; case 2 :... break; default : break; } ______binary tree______. / -4 \. ______binary tree______. / -4 \. / -2 \. ______binary tree______. / -4 \. / -2 \. / -6 \. ______binary tree______. / -7 \. / -4 \. / -2 \. / -6 \. ______binary tree______. / -7 \. / -2 \. / -4 \. / \. / -6 \.

98 98 © 2003 bingol meraklisina.com Debuging Tools public void printTree() { System.out.println("______binary tree______"); if (isEmpty()) System.out.println("Empty tree"); else printTree(1, 0); } public void printTree(String name) { System.out.println("______" + name + "______"); if (isEmpty()) System.out.println("Empty tree"); else printTree(1, 0); } private void printTree(int t, int depth) { String space = ""; for (int i = 0; i < depth; i++) { space += " "; } if (t <= currentSize) { int d = ++depth; printTree(2 * t, d); System.out.println(space + "/"); System.out.print(space + "-"); System.out.println(array[t]); System.out.println(space + "\\"); printTree(2 * t + 1, d); } else { System.out.println(space + "."); }

99 99 © 2003 bingol meraklisina.com Debuging Tools public void printTree() { System.out.println("______binary tree______"); if (isEmpty()) System.out.println("Empty tree"); else printTree(1, 0); } public void printTree(String name) { System.out.println("______" + name + "______"); if (isEmpty()) System.out.println("Empty tree"); else printTree(1, 0); } private void printTree(int t, int depth) { String space = ""; for (int i = 0; i < depth; i++) { space += " "; } if (t <= currentSize) { int d = ++depth; printTree(2 * t, d); System.out.println(space + "/"); System.out.print(space + "-"); System.out.println(array[t]); System.out.println(space + "\\"); printTree(2 * t + 1, d); } else { System.out.println(space + "."); }... int numItems = 10000; BinaryHeap h = new BinaryHeap(numItems); h.insert(new MyInteger(4)); h.insert(new MyInteger(2)); h.insert(new MyInteger(6)); h.insert(new MyInteger(7)); h.insert(new MyInteger(1)); h.printTree();... ______binary tree______. / -7 \. / -2 \. / -4 \. / \. / -6 \.

100 100 © 2003 bingol meraklisina.comSummary Binary HeapBinary Heap Leftist HeapsLeftist Heaps Binomial HeapsBinomial Heaps

101 101 © 2003 bingol meraklisina.comReferences Data Structures and Algorithms with Object-Oriented Design Patterns in Java Preiss http://www.brpreiss.com/books/opus5/Data Structures and Algorithms with Object-Oriented Design Patterns in Java Preiss http://www.brpreiss.com/books/opus5/ Data Structures and Algorithms with Object-Oriented Design Patters in C++ Preiss Wiley, 1999Data Structures and Algorithms with Object-Oriented Design Patters in C++ Preiss Wiley, 1999 Data Structures and Algorithm Analysis in Java Weiss Addison-Wesley, 1999Data Structures and Algorithm Analysis in Java Weiss Addison-Wesley, 1999


Download ppt "© 2000-2003 Haluk Bingöl v2.23 Data Structures and Algorithms - 03 Heaps and Priority Queues Dr. Haluk Bingöl BÜ - CmpE"

Similar presentations


Ads by Google