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

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Advertisements

CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
PRIORITY QUEUES AND HEAPS Lecture 19 CS2110 Spring
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
Heaps, Heap Sort, and Priority Queues
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Priority queues CS310 – Data Structures Professor Roch Weiss, Chapter 6.9, 21 All figures marked with a chapter and section number are copyrighted © 2006.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on MinHeap: Insert Delete Converting an array.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
1 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Outline Priority Queues Binary Heaps Randomized Mergeable Heaps.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues (Heaps)
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003.
Recitation Nov. 15. HW5: Huffman Encoding/Decoding Task: –Read a text file (i.e. “message.text”) and figure out character frequencies. Don’t forget ‘\n’
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Binary Heaps Fundamental Data Structures and Algorithms Peter Lee February 4, 2003.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Binary Heaps Priority Queues
Source: Muangsin / Weiss
Binary Heaps What is a Binary Heap?
Bohyung Han CSE, POSTECH
CMSC 341 Lecture 13 Leftist Heaps
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Binary Heaps What is a Binary Heap?
Binary Heaps What is a Binary Heap?
Heaps, Heap Sort, and Priority Queues
Binary Heaps Priority Queues
CMSC 341 Lecture 14 Priority Queues & Heaps
Binary Heaps Priority Queues
CE 221 Data Structures and Algorithms
Heaps Priority Queues.
Heap code in C++ template <class eType>
Priority Queues (Heaps)
Tree.
Presentation transcript:

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

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

3 © 2003 bingol meraklisina.com Motivation

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

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 © 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 © 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 © 2003 bingol meraklisina.com Motivation … Problem - In General Anything waiting for serviceAnything waiting for service –Printer –CPU 1 4 2

9 © 2003 bingol meraklisina.com Review of Queues

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 © 2003 bingol meraklisina.com Review Queue … Sample operation 1 enqueque(4) 1 4 enqueque(2) enqueque(3) dequeque() dequeque() 2 3 enqueque(1) enqueque(1) dequeque() dequeque() 1 dequeque() 1

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 © 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 P

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 P 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 © 2003 bingol meraklisina.com Priority Queues

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 © 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 © 2003 bingol meraklisina.com Simple Implementations

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 © 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 © 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 © 2003 bingol meraklisina.com Simple Implementations … Binary Search Tree InsertInsert –O(log n) DeletionDeletion –O(log n) Unused propertiesUnused properties abc

23 © 2003 bingol meraklisina.com Priority Queues Heap

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

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 © 2003 bingol meraklisina.com Priority Queues … Mergeable Priority Queue Interface public interface MergeablePriorityQueue extends PriorityQueue { void merge(MergeablePriorityQueue queue); }

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

28 © 2003 bingol meraklisina.com Heap

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 © 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 © 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 © 2003 bingol meraklisina.com Binary Heap

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 internal nodes. Theorem The height of a perfect binary tree with n internal nodes is log (n+1).

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 © 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 © 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 © 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 © 2003 bingol meraklisina.com Complete Binary Tree … Array representation abc b = 2a c = 2a+1 a =  b/2  a =  c/2 

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 nodes.

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 © 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 © 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 © 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  c2c2 cNcN … i

44 © 2003 bingol meraklisina.com Binary Heap Implementation

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. capacity the capacity of the binary heap. */ public BinaryHeap(int capacity) { currentSize = 0; array = new Comparable[capacity + 1]; } … }

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

47 © 2003 bingol meraklisina.com Binary Heap Implementation … enqueue public class BinaryHeap { … /** * Insert into the priority queue, maintaining heap order. * Duplicates are allowed. x the item to insert. 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 © 2003 bingol meraklisina.com Binary Heap Implementation … enqueue public class BinaryHeap { … /** * Insert into the priority queue, maintaining heap order. * Duplicates are allowed. x the item to insert. 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 © 2003 bingol meraklisina.com Binary Heap Implementation … enqueue - Exacution insert 14

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

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

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

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

54 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue public class BinaryHeap { … /** * Remove the smallest item from the priority queue. 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 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue public class BinaryHeap { … /** * Remove the smallest item from the priority queue. 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. 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 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue public class BinaryHeap { … /** * Remove the smallest item from the priority queue. 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. 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 © 2003 bingol meraklisina.com Binary Heap Implementation … dequeue – Execution

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

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

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

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

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

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. 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 © 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. 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 © 2003 bingol meraklisina.com Binary Heap Implementation … buildHeap public class BinaryHeap { … /** * Internal method to percolate down in the heap. 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

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

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. Mark Allen Weiss */ public class BinaryHeap { … }

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 = ; 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 © 2003 bingol meraklisina.com Binary Heap Implementation … Complexity Summary OperationComplexity enqueue O(log 2 n) dequeue builtO(n) BinaryBinary

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

71 © 2003 bingol meraklisina.com Leftist Trees

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

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 © 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 © 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 © 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 © 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 © 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 © 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. 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 © 2003 bingol meraklisina.com public class LeftistHeap { … /** * Merge rhs into the priority queue. * rhs becomes empty. rhs must be different from this. 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 © 2003 bingol meraklisina.com Leftist Trees … Implementation – enqueue() public class LeftistHeap { … /** * Insert into the priority queue, * maintaining heap order. x the item to insert. */ public void insert(Comparable x) { root = merge(new LeftHeapNode(x), root); } … }

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

83 © 2003 bingol meraklisina.com Leftist Trees … Implementation – dequeueMin() public class LeftistHeap { … /** * Remove the smallest item from the priority queue. 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 © 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 © 2003 bingol meraklisina.com Binomial Queues

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

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 © 2003 bingol meraklisina.com Binomial Queues … Different Views

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

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

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 © 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 k

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 © 2003 bingol meraklisina.com Binomial Queues … Merging Binomial Queues Merging two binomial queues is like doing binary addition B4B4B4B4 B3B3B3B3 B1B1B1B1 B0B0B0B0 F B3B3B3B3 B1B1B1B1 F B5B5B5B5 B2B2B2B2 B0B0B0B0 F

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

96 © 2003 bingol meraklisina.com Debuging Tools

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 © 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 © 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 © 2003 bingol meraklisina.comSummary Binary HeapBinary Heap Leftist HeapsLeftist Heaps Binomial HeapsBinomial Heaps

101 © 2003 bingol meraklisina.comReferences Data Structures and Algorithms with Object-Oriented Design Patterns in Java Preiss Structures and Algorithms with Object-Oriented Design Patterns in Java Preiss 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