1 Heaps & Priority Queues A heap is a binary tree. A heap is best implemented in sequential...

33
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.
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    1

Transcript of 1 Heaps & Priority Queues A heap is a binary tree. A heap is best implemented in sequential...

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

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.

Page 2: 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.

2

A Heap

10

2030

40 50 25 55

52 42

Any node’s key value isless than its children’s.

Page 3: 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.

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 {Data elt;int left;int right;

} Node;Node[] BinaryTree[TreeSize];

Page 4: 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.

4

Method 1: Example

A

D

B C

E

G

I

Index Element Left Right

1 A 2 3

2 B 4 6

3 C 0 0

4 D 0 0

5 I 0 0

6 E 7 0

7 G 5 0

Page 5: 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.

5

Method 2

2. Store the nodes in one of the natural traversals:

class Node {

StdElementelt;boolean left;boolean right;

};Node[] BinaryTree[TreeSize];

Page 6: 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.

6

Method 2: Example

A

D

B C

E

G

I

Index Element Left Right

1 A T T

2 B T T

3 D F F

4 E T F

5 G T F

6 I F F

7 C F F

Elements stored in Pre-Order traversal

Page 7: 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.

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

Page 8: 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.

8

Method 3: Example

A

D

B C

E

G

A B C D E - - - - G -

1 2 3 4 5 6 7 8 9 10 11 12

Page 9: 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.

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

Page 10: 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.

10

Heap: An example

10

2030

40 50 25 55

52 42

[1] 10 10 10

[2] 20 30 40

[3] 25 20 20

[4] 30 40 50

[5] 40 50 42

[6] 42 25 30

[7] 50 55 25

[8] 52 52 52

[9] 55 42 55

For the heap shown

All the three arrangementssatisfy min heap conditions

Page 11: 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.

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.

Page 12: 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.

12

Constructing Heaps: SiftDown

50

2080

60 90 40 30

70 10

[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 nodesalready satisfyHeap conditions.

Page 13: 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.

13

Constructing Heaps: SiftDown

50

2080

60 90 40 30

70 10

[1] 50

[2] 80

[3] 20

[4] 60

[5] 90

[6] 40

[7] 30

[8] 70

[9] 10

SiftDown 60

One Swap operationneeded.

Page 14: 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.

14

Constructing Heaps: SiftDown

50

2080

10 90 40 30

70 60

[1] 50

[2] 80

[3] 20

[4] 10

[5] 90

[6] 40

[7] 30

[8] 70

[9] 60

After SiftDown 60

Page 15: 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.

15

Constructing Heaps: SiftDown

50

2080

10 90 40 30

70 60

[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

Page 16: 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.

16

Constructing Heaps: SiftDown

50

2010

60 90 40 30

70 80

[1] 50

[2] 10

[3] 20

[4] 60

[5] 90

[6] 40

[7] 30

[8] 70

[9] 80

After SiftDown 80

Two Swap Operations

Page 17: 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.

17

Constructing Heaps: SiftDown

10

2050

60 90 40 30

70 80

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

Page 18: 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.

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.

Page 19: 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.

19

ADT Heap

Elements: The elements are called HeapElements.public class HeapElement<T> { 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.

Page 20: 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.

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.

Page 21: 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.

21

ADT Heap: Implementation

public class Heap<T> { int maxsize; int size; HeapElement<T>[] heap;public Heap(int n) { maxsize = n; size = 0; heap = (HeapElement<T>[])

Array.newInstance(HeapElement.class,n+1);}

Page 22: 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.

22

ADT Heap: Implementation

public Heap(int a[], int n) {//Creates & Initializes a Heap T x;

size = n; maxsize = n;heap = (HeapElement<T>[])Array.newInstance( HeapElement.

class, n+1);for (int i = 0; i < n; ++i){

x = (T) new Object(); heap[i+1] = new HeapElement<T>(x, new Priority(a[i])); }

for (int m = size/2; m >= 1; --m) SiftDown(m);}

Page 23: 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.

23

ADT Heap: Implementation

public void Display_Heap(){ for(int i = 1; i<=size; i++){

System.out.println(heap[i].get_priority().get_value());

} }

Page 24: 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.

24

ADT Heap: Implementation

public void SiftUp(HeapElement<T> 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;

}

Page 25: 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.

25

ADT Heap: Implementation

public void SiftDown(int m){ int i = m, k; while ((i<=size/2 && heap[i].get_priority().get_value() >

heap[2*i].get_priority().get_value()) || (2*i+1<=size && heap[i].get_priority().get_value() > heap[2*i+1].get_priority().get_value())) { if (2*i+1<=size &&heap[2*i].get_priority().get_value() >

heap[2*i+1].get_priority().get_value()) k = 2*i+1; else k = 2*i; swap(heap, i, k); i = k; } }

Page 26: 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.

26

ADT Heap: Implementation

private void swap (HeapElement<T> h[], int i, int j){

HeapElement<T> tmp; tmp = h[i]; h[i]= h[j]; h[j]=tmp; }

Page 27: 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.

27

ADT Heap: Implementation

private int min(int i, int j){ if (i<=j) return i; return j; }

Page 28: 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.

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.

Page 29: 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.

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());

} }

Page 30: 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.

30

Priority Queue as Heap

Representation as a Heappublic class HeapPQ<T> { Heap<T> pq; /** Creates a new instance of HeapPQ */ public HeapPQ() { pq = new Heap(10); }

Page 31: 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.

31

Priority Queue as Heap

public void enqueue(T e, Priority pty){ HeapElement<T> x = new HeapElement<T>

(e, pty); pq.SiftUp(x);}

Page 32: 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.

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); }

Page 33: 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.

33

Priority Queue as Heap

public int length(){ return pq.size; } public boolean full(){ return false; }