heap

12
11/05/08 Shaily Kabir,Dept. of CSE, DU 1 Heap 11/05/08 1

Upload: himadri-sen-gupta

Post on 16-Apr-2017

90 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 1

Heap

11/05/08 1

Page 2: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 2

Heap• A heap is a complete binary tree except the bottom level adjusted to the left.

• The value of each node is greater than that of its two children. (Max Heap)

• The value of each node is less than that of its two children. (Min Heap)

• Height of the heap is log2n.

• Example

100

1621

14 19 17

100

1721

14 19 16

Figure: Not a Heap Figure: A Heap

Page 3: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 3

Heap Implementation

• We can use an array (due to the regular structure or completeness of binary tree).

• For a node N with location i, the following factors can be calculated.

1. Left child of N is in location (2 * i).

2. Right child of N is in location (2 * i + 1).

3. Parent of N is in location [i/2].

• Example

1 2 3 4 5 6 7 8 9

100 21 17 14 19 16

100

1721

14 19 16

Figure: Heap and Its Array Representation

Page 4: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 4

Heap Operations

(1) Insertion(2) Delete-max or delete-min(3) Make-heap (organize an arbitrary array as a heap)

Insertion

• Find the left-most open position and insert the new element NewElement.

• while ( NewElement > Its Parent ) exchange them.

• Example:

Figure: Item 70 is inserted.

97

8855

48

70

97

8855

70

48

97

8870

55

48

Page 5: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 5

Algorithm: INSHEAP(Heap, N, Item) 1. Set N := N +1 and Ptr := N

2. Repeat steps 3 to 6 while Ptr > 1

3. Set Par := Ptr/2

4. If Item < Heap[Par], then

Set Heap[Ptr] := Item and Return.

1. Set Heap[Ptr] := Heap[Par]

2. Set Ptr := Par

3. Heap[1] := Item

4. Return.

Here,

Heap = Max heap

N = Total no. of items in the array

Item = New item to be inserted

The Complexity Analysis of Insert Operation

• Insertion takes O( log2n ) times.

• Reasons: 1) No. of operations = O( height of the heap)

2) Height of the heap: O ( log2 n )

Page 6: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 6

Delete-Max• Replace the root node by the last node at bottom level maintaining completeness.

• While ( new root < its children ) exchange it with the greater of its children.

• Example:

50

40 23

18 30 20 21

21

40 23

18 30 20

40

21 23

18 30 20

40

30 23

18 21 20

Figure: Item 50 (root) is deleted.

Page 7: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 7

Algorithm: DELHEAP(Heap, N, Item)

1. Set Item := Heap[1] and Set Last:=Heap[N] and N:=N-1

2. Set Ptr := 1, Left := 2 and Right := 3

3. Repeat steps 5 to 6 while Right<=N

4. If Last >= Heap[Left] and Last >= Heap[Right] then

Set Tree[Ptr] := Last and Return.

5. If Heap[Right] <= Heap[Left] then

Set Heap[Ptr] := Heap[Left] and Ptr := Left.

Else Set Heap[Ptr] := Heap[Right] and Ptr := Right

6. Set Left := 2 * Ptr and Right := Left + 1

1. If Left = N and if Last < Heap[Left] then Set Heap[Ptr] := Heap[Left] and Ptr := Left.

2. Set Heap[Ptr] := Last

3. Return

Page 8: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 8

The Complexity Analysis of Delete Operation

• Deletion takes O( log2n ) times.

• Reasons: 1) No. of operations = O( height of the heap)

2) Height of the heap: O ( log2n )

Page 9: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 9

Variants of Heap

1. Binary Heap

2. Binomial Heap

3. Fibonacci Heap

4. Pairing Heap

5. Treap

6. Beap

Application of Heap

1. HeapSort

2. Priority Queue

3. Selection Algorithms

4. Graph Algorithms

Page 10: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 10

Heap Sort• Suppose an array with N elements is given.

• The heap sort algorithm consists of the following two steps:

Step 1: Build a heap H out of the elements of A

Step 2: Repeatedly delete the root element of H

• For sorting in descending order, max heap must be created. For ascending order, min

heap must be created.

Algorithm: HeapSort(A, N)

1. Repeat for J=1 to N-1

Call INSHEAP(A, J, A[J+1])

2. Repeat while N>1

(a) Call DELHEAP(A, N, ITEM)

(b) Set A[N+1]:= ITEM

3. Exit

Here,

A = Max Heap

N = Total No. of Items in A

Page 11: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 11

Complexity of Heapsort

• Heap Sort takes O(nlog2n ) times. Reasons:

1. BuildHeap from the initial array requires O(nlog2n).

2. Delete-Max n times requires n x O(log2n) = O(nlog2n).

Page 12: Heap

11/05/08 Shaily Kabir,Dept. of CSE, DU 1211/05/08 Shaily Kabir,Dept. of CSE, DU 12

END!!!