+ All Categories
Home > Documents > CH-8 HEAPS

CH-8 HEAPS

Date post: 02-Dec-2015
Category:
Upload: ramandeep-kaur
View: 16 times
Download: 4 times
Share this document with a friend
30
Transcript
Page 1: CH-8 HEAPS
Page 2: CH-8 HEAPS

A heap is a binary tree that satisfies the following properties:

Shape propertyOrder property

A heap that satisfies these properties is known as max heap.

Shape property Heap must be a complete binary tree.

Order property For every node in the heap, the value stored in that node is greater than or equal to the value in each of its children.

Page 3: CH-8 HEAPS

If the order property is such that for every node in the heap, the value stored in that node is less than or equal to the value in each of its children, that heap is known as min heap.

Example :

(a) Max Heap (b)Sequential representation of Max heap

Page 4: CH-8 HEAPS

Element is always deleted from the root of the heap. It creates a hole, i.e., vacant space, in the root position.

Because the heap must be complete, we fill the hole with the last (bottom) element of the heap.

Although the heap becomes complete, i.e., satisfies shape property, the order property of the heaps is violated, as the values that come from the bottom are small.

This problem is overcome by moving the elements down from the root position until either it ends up in a position where root property is satisfied or it hits the leaf node . This operation is called Reheapify downward operation

Deleting an Element From Heap

Page 5: CH-8 HEAPS

Interchange the value of the root node with the value of the child, which is the largest among its children. For example:

If the value of the left child is the largest, the root node is interchanged with its left child, and then the down heap property is repeated from its left child onward. If the right child is largest, start reheapify downward operation from that child with which the value of the root node was interchanged. This operation is recursive.

Working of Reheapify downward operation

Page 6: CH-8 HEAPS

Algorithm

ReheapifyDownward( heap, start, finish )

Heap is a linear arrayStart is the index of the element from where reheapify downward operation is to start.Finish is index of the last (bottom) element of the heap. Variable index is used to keep track of the index of the largest child.

1. If ( heap[start] is not a leaf node ) then2. Set index = index of the child with largest value3. If ( heap[start] < heap[index] ) then4. Swap heap[start] and heap[index]5. Call ReheapifyDownward( heap, index, finish ) Endif Endif6. Return

Page 7: CH-8 HEAPS

1) Assign the value of the root node to the temporary variable, which may be required for further processing.

2) Bring the last element of the heap to the root node position.

3) Reduce the size of the heap by a factor of one and

4) Apply reheapify downward operation from root node.

Deletion of an element from heap

Page 8: CH-8 HEAPS

Algorithm

DeleteElement( heap, n, item )

Here heap is the linear array with size n. This algorithm deletes the element from the root of heap and assign to item through which it is returned to the calling program. It also decreases its size by a factor of one, i.e., size becomes n−1.

1. Set item = heap[1]2. Set heap[1] = heap[n]3. Set n = n − 14. Call ReheapifyDownward( heap, 1, n )5. Return

Page 9: CH-8 HEAPS

Example: (a) Starting Heap

(b) After removing 50 and replacing with 33

(c) Reheapify downward from root node (swap 33 with 40)

Page 10: CH-8 HEAPS

Element is always inserted as last (bottom) child of the original heap.

Heap remains complete, but the order property may be violated if a larger element is inserted.

This problem is overcome by Reheapify upward operation.

If the value of the last node is greater than its parent, exchange it value with its parent, and then repeat the same process from the parent node, and so on until the order property is satisfied or we hit the root node. This operation is also recursive.

Inserting an Element Into Heap

Page 11: CH-8 HEAPS

Algorithm

ReheapifyUpward( heap, start )

Here heap is a linear array, start is the index of the element from where reheapify upward operation is to start. It use parent as the index of the parent node in the heap.

1. If heap[start] is not a root node then2. If ( heap[parent] < heap[start] ) then3. Swap heap[parent] and heap[start]4. Call ReheapifyUpward(heap, parent ) Endif Endif5. Return

Page 12: CH-8 HEAPS

1) Increase the size of the heap by a factor of one.

2) Insert the element as the last (bottom) element of the heap.

3) Apply Reheapify upward operation from last node.

Insertion of an element into heap

Page 13: CH-8 HEAPS

Algorithm

InsertElement( heap, n, item )

Here heap is a linear array with size n. This algorithm inserts an element item into the heap and also increases its size by a factor of one, i.e., size becomes n+1.

1. Set n = n + 12. Set heap[n] = item3. Call ReheapifyUpward(heap, n )4. Return

Page 14: CH-8 HEAPS

Example:

(c) Reheapify upward from last node with value 90

(d) Further reheapify upward from node with value 90

(a) Starting Heap

(b) Add value 90

Page 15: CH-8 HEAPS

Implementing a priority queue. Sorting an array using efficient technique known as heapsort.

Building a Heap Applying Heapsort

Page 16: CH-8 HEAPS

A priority queue is a structure with an interesting accessing function: Only the highestpriority element can be accessed.

Consider following situations :

A small company has only one secretary. When other employees leave work on the secretary's desk, which get job done first? The jobs are processed in order of the employee's importance in the company. Suppose, secretary has on his table work given by president and vice president, secretary completes president's work first. This example shows that the priority of each job relates to the level of the employee who initiated it.

Page 17: CH-8 HEAPS

In a telephone answering system, calls are answered in the order that they are received. That is, the highest-priority call is the one that has been waiting the longest. Thus a FIFO queue can be considered a priority queue whose highestpriority element is the one that has been queued the longest time.

Page 18: CH-8 HEAPS

General Approach:

1) From the given array, build the initial max heap.2) Interchange the root (maximum) element with the last element.3) Use reheapifydownward operation from root node to rebuild the heap of size one less than the starting size.4) Repeat steps 2 and 3 until there are no more elements.

Page 19: CH-8 HEAPS

( A) Building a Heap

(a) Unsorted array A (b) Equivalent binary tree

(c) After reheapify upwardoperation at index 3

Page 20: CH-8 HEAPS

(d) After reheapify upwardoperation at index 2

(e) After reheapify upwardoperation at index 1

Page 21: CH-8 HEAPS

Illustration of changing contents of array A

Original array A

After reheapify upward

From index 3

From index 2From index 1

Page 22: CH-8 HEAPS

Algorithm

Heapify( a, n )

Here a is the linear array with size n. This algorithm builds the max heap using the procedure described above.

1. Set index = Parent of node with index n2. Repeat step 3 For i = index to 1 in steps of −13. Call ReheapifyUpward( a, i, n ) Endif4. Return

Page 23: CH-8 HEAPS

(B) Applying Heapsort

Algorithm

HeapSort( a, n )

Here a is a linear array of size n in memory. This algorithm sorts this array in ascending order using Heap sort method.

1. Call Heapify( a, n )2. Repeat steps 3 and 4 For i = n to 2 in steps of −13. Interchange elements a[1] and a[i]4. Call Reheapifydownward( a, 1, i-1 ) Endfor5. Return

Page 24: CH-8 HEAPS

Example : Consider the following array : 10, 5, 70, 15, 12, 35, 50

Solution Illustration of effect of heapsort on array a

Page 25: CH-8 HEAPS
Page 26: CH-8 HEAPS
Page 27: CH-8 HEAPS
Page 28: CH-8 HEAPS
Page 29: CH-8 HEAPS
Page 30: CH-8 HEAPS

In the heapsort algorithm, we call function heapify() to build the initial heap, which is O(log2n) operation, and inside the loop we call utility function reheapifydownward().

The loop is executed (n−1) times, and in each iteration, the element in the root node of the heap is swapped with the last element of the reduced size heap and heap is rebuild. Swapping two elements take constant time.

A complete binary tree with n nodes has O(log2(n+1)) levels. In the worst case, if the root element is bumped down to a leaf

position, the reheapifydownward operation would perform O(log2n) swaps. So the swap plus reheapifydownward is O(log2n). Multiplying this activity by (n−1) iteration shows that the sorting loop is O(nlog2n ). Combining the heap build, which is O(n), and the sorting loop.

Complexity of heapSort is O(nlog2n).


Recommended