Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Heaps & Priority Queues A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps.

Similar presentations


Presentation on theme: "1 Heaps & Priority Queues A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps."— Presentation transcript:

1 1 Heaps & Priority Queues A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient implementation of priority queues –(ii) sorting -- Heapsort.

2 2 A Heap 10 2030 40502555 5242 Any node’s key value is less than its children’s.

3 3 Sequential Representation of Trees There are three methods of representing a binary tree using array representation. 1. Using index values to represent edges: class Node { Dataelt; intleft; intright; } Node; Node[] BinaryTree[TreeSize];

4 4 Method 1: Example A D BC E G I IndexElementLeftRight 1A23 2B46 3C00 4D00 5I00 6E70 7G50

5 5 Method 2 2. Store the nodes in one of the natural traversals: class Node { StdElementelt; booleanleft; booleanright; }; Node[] BinaryTree[TreeSize];

6 6 Method 2: Example A D BC E G I IndexElementLeftRight 1ATT 2BTT 3DFF 4ETF 5GTF 6IFF 7CFF Elements stored in Pre-Order traversal

7 7 Method 3 3. Store the nodes in fixed positions: (i) root goes into first index, (ii) in general left child of tree[i] is stored in tree[2i] and right child in tree[2i+1].

8 8 Method 3: Example A D BC E G ABCDE----G- 123456789101112

9 9 Heaps Heaps are represented sequentially using the third method. Heap is a complete binary tree: shortest-path length tree with nodes on the lowest level in their leftmost positions. Max-Heap has max element as root. Min-Heap has min element as root. The elements in a heap satisfy heap conditions: for Min-Heap: key[parent] < key[left-child] or key[right-child].

10 10 Heap: An example 10 2030 40502555 5242 [1]10 [2]203040 [3]2520 [4]304050 [5]405042 [6]422530 [7]505525 [8]52 [9]554255 For the heap shown All the three arrangements satisfy min heap conditions

11 11 Constructing Heaps There are two methods of constructing heaps: –Using SiftDown operation. –Using SiftUp operation. SiftDown operation inserts a new element into the Heap from the top. SiftUp operation inserts a new element into the Heap from the bottom.

12 12 Constructing Heaps: SiftDown 50 2080 60904030 7010 [1]50 [2]80 [3]20 [4]60 [5]90 [6]40 [7]30 [8]70 [9]10 This is not a Heap But these nodes already satisfy Heap conditions.

13 13 Constructing Heaps: SiftDown 50 2080 60904030 7010 [1]50 [2]80 [3]20 [4]60 [5]90 [6]40 [7]30 [8]70 [9]10 SiftDown 60 One Swap operation needed.

14 14 Constructing Heaps: SiftDown 50 2080 10904030 7060 [1]50 [2]80 [3]20 [4]10 [5]90 [6]40 [7]30 [8]70 [9]60 After SiftDown 60

15 15 Constructing Heaps: SiftDown 50 2080 10904030 7060 [1]50 [2]80 [3]20 [4]10 [5]90 [6]40 [7]30 [8]70 [9]60 After SiftDown 20 (No Swaps) SiftDown 80

16 16 Constructing Heaps: SiftDown 50 2010 60904030 7080 [1]50 [2]10 [3]20 [4]60 [5]90 [6]40 [7]30 [8]70 [9]80 After SiftDown 80 Two Swap Operations

17 17 Constructing Heaps: SiftDown 10 2050 60904030 7080 [1]10 [2]50 [3]20 [4]60 [5]90 [6]40 [7]30 [8]70 [9]80 After SiftDown 50 Tree is a heap now.

18 18 Constructing Heaps: SiftUp Left as an exercise for the students. Hint: start with root as the only element which satisfies the heap conditions … insert new elements from the bottom one at a time and SiftUp.

19 19 ADT Heap Elements: The elements are called HeapElements. public class HeapElement { T data; Priority p; public HeapElement(T e, Priority pty) { data = e; p = pty; Other methods in Java code } Structure: The elements of the heap satisfy the heap conditions. Domain: Bounded. Type name: Heap.

20 20 ADT Heap Operations: 1.Procedure SiftUp (Heap H, int n) requires: Elements H[1],H[2],…,H[n-1] satisfy heap conditions. results: Elements H[1],H[2],…,H[n] satisfy heap conditions. 2.Procedure SiftDown (Heap H, int m,n) requires: Elements H[m+1],H[m+2],…,H[n] satisfy the heap conditions. results: Elements H[m],H[m+1],…,H[n] satisfy the heap conditions. 3.Procedure Heap (Heap H, int n) // Constructor results: Elements H[1],H[2],….H[n] satisfy the heap conditions.

21 21 ADT Heap: Implementation public class Heap { int maxsize; int size; HeapElement [] heap; public Heap(int n) { maxsize = n; size = 0; heap = (HeapElement []) Array.newInstance(HeapElement.class,n+1);}

22 22 ADT Heap: Implementation public Heap(int a[], int n) {//Creates & Initializes a Heap T x; size = n; maxsize = n; heap = (HeapElement [])Array.newInstance( HeapElement. class, n+1); for (int i = 0; i < n; ++i){ x = (T) new Object(); heap[i+1] = new HeapElement (x, new Priority(a[i])); } for (int m = size/2; m >= 1; --m) SiftDown(m); }

23 23 ADT Heap: Implementation public void Display_Heap(){ for(int i = 1; i<=size; i++){ System.out.println(heap[i].get_priority().ge t_value()); }

24 24 ADT Heap: Implementation public void SiftUp(HeapElement e){ heap[++size] = e; int i = size; while (i > 1 && heap[i/2].get_priority().get_value() > e.get_priority().get_value()) { heap[i] = heap[i/2]; i = i/2; } heap[i] = e; }

25 25 ADT Heap: Implementation public void SiftDown(int m){ int i = m, k; while ((i heap[2*i].get_priority().get_value()) || (2*i+1 heap[2*i+1].get_priority().get_value())) { if (2*i+1 heap[2*i+1].get_priority().get_value()) k = 2*i+1; else k = 2*i; swap(heap, i, k); i = k; } }

26 26 ADT Heap: Implementation private void swap (HeapElement h[], int i, int j){ HeapElement tmp; tmp = h[i]; h[i]= h[j]; h[j]=tmp; }

27 27 ADT Heap: Implementation private int min(int i, int j){ if (i<=j) return i; return j; }

28 28 HeapSort Heap can be used for sorting. Two step process: –Step 1: the data is put in a heap. –Step 2: the data are extracted from the heap in sorted order. HeapSort based on the idea that heap always has the smallest or largest element at the root.

29 29 ADT Heap: Implementation //This method extracts elements in sorted order from the heap. Heap size becomes 0. public void HeapSort(){ while (size>1){ swap(heap, 1, size); size--; SiftDown(1); } //Display the sorted elements. for(int i = 1; i<=maxsize; i++){ System.out.println(heap[i].get_priority().get_value()); }

30 30 Priority Queue as Heap Representation as a Heap public class HeapPQ { Heap pq; /** Creates a new instance of HeapPQ */ public HeapPQ() { pq = new Heap(10); }

31 31 Priority Queue as Heap public void enqueue(T e, Priority pty){ HeapElement x = new HeapElement (e, pty); pq.SiftUp(x); }

32 32 Priority Queue as Heap public T serve(Priority pty){ T e; Priority p; e = pq.heap[1].get_data(); p = pq.heap[1].get_priority(); pty.set_value(p.get_value()); pq.heap[1] = pq.heap[pq.size]; pq.size--; pq.SiftDown(1); return(e); }

33 33 Priority Queue as Heap public int length(){ return pq.size; } public boolean full(){ return false; }


Download ppt "1 Heaps & Priority Queues A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps."

Similar presentations


Ads by Google