12. Heaps - Data Structures using C++ by Varsha Patil

47
Oxford University Press © 2012 Data Structures Using C++ by Dr Varsha Patil 1 12. HEAPS

Transcript of 12. Heaps - Data Structures using C++ by Varsha Patil

Page 1: 12. Heaps - Data Structures using C++ by Varsha Patil

1Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

12. HEAPS

Page 2: 12. Heaps - Data Structures using C++ by Varsha Patil

2Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Objectives A specialized tree-based data structure known

as heaps

Usage of heaps efficiently for applications such as priority queues

How heaps are implemented using arrays

A few more applications such as selection problem and event simulation

Page 3: 12. Heaps - Data Structures using C++ by Varsha Patil

3Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Basic Concepts Definition of heaps: A heap is a binary tree having the following

properties:

It is a complete binary tree, that is, each level of the tree is completely filled, except at the bottom level

At this level, it is filled from left to right

It satisfies the heap-order property, that is, the key value of each node is greater than or equal to the key value of its children, or the key value of each node is lesser than or equal to the key value of its children

Page 4: 12. Heaps - Data Structures using C++ by Varsha Patil

4Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

17

50

5

4030913

5065

70

30

57

10

Fig 12.1 Heap with height three

Fig 12.1 Heap with height two

Fig 12.1 Heap with height one

Fig 12.1 Fig 12.2 Fig 12.3

Page 5: 12. Heaps - Data Structures using C++ by Varsha Patil

5Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

17

50

4030913

5065

70

30

5

Fig 12.4 Binary Trees with no Heap

Page 6: 12. Heaps - Data Structures using C++ by Varsha Patil

6Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Types of Heap Min-heap

Max-heap

Page 7: 12. Heaps - Data Structures using C++ by Varsha Patil

7Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Min-Heap Data

all >= Data

all >= Data

Fig 12.5 :Structure of min-heap

Page 8: 12. Heaps - Data Structures using C++ by Varsha Patil

8Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Min-Heap In min-heap, the key value of each node is

lesser than or equal to the key value of its children

In addition, every path from root to leaf should be sorted in ascending order

9710

52

1

Fig 12.6 An example of a min heap

Page 9: 12. Heaps - Data Structures using C++ by Varsha Patil

9Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Max-Heap A max-heap is where the key value of a node is

greater than the key values in all of its sub trees

In general, whenever the term ‘heap’ is used by itself

Data

all <= Data

all <= Data

Page 10: 12. Heaps - Data Structures using C++ by Varsha Patil

10Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

326

48

9

Fig 12.7 An example of a max-heap

Page 11: 12. Heaps - Data Structures using C++ by Varsha Patil

11Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

326

48

9

Fig 12.7 An example of a max-heap

Page 12: 12. Heaps - Data Structures using C++ by Varsha Patil

12Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Structure Property A binary tree is complete if it is of height h and

has 2h+1 − 1 nodes. A binary tree of height h is complete if

it is empty, or its left sub tree is complete of height h − 1 and

its right sub tree is completely full of height h − 2, or

its left sub tree is completely full of height h − 1 and its right sub tree is complete of height h − 1.

A complete tree is filled from the left when all the leaves are on

the same level or two adjacent ones

all nodes at the lowest level are as far to the left as possible

Page 13: 12. Heaps - Data Structures using C++ by Varsha Patil

13Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Heap-order Property A binary tree has the heap property if :

it is empty or

the key in the root is larger than either children and both sub trees have the heap property

Page 14: 12. Heaps - Data Structures using C++ by Varsha Patil

14Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

326

48

9

Fig 12.8 Sample Heap

Implementation of Heap

Page 15: 12. Heaps - Data Structures using C++ by Varsha Patil

15Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

In this array,1. parent of index ith node is at index (i − 1)/22. left child of index ith node is at index 2 X i + 13. right child of index ith node is at index 2 X i + 2

Data 9 8 4 6 2 3Index

0 1 2 3 4 5 6 7

Page 16: 12. Heaps - Data Structures using C++ by Varsha Patil

16Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

68 46 22 35 02 13 09

0913235

2246

68

A Heap Tree

Representation of heap by using array

Page 17: 12. Heaps - Data Structures using C++ by Varsha Patil

17Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Heaps as Abstract Data Type

Declare create() : Heap

Insert(Heap,Data) : Heap

Deletemaxval(Heap) : Heap

ReheapDown(Heap, Child) : Heap

ReheapUp(Heap, Root) : Heap

End

Page 18: 12. Heaps - Data Structures using C++ by Varsha Patil

18Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Operations on Heaps Create—To create an empty heap to which ‘root’

points

Insert—To insert an element into the heap

Delete—To delete max (or min) element from the heap

ReheapUp—To rebuild heap when we use the insert() function

ReheapDown—To build heap when we use the delete() function

Page 19: 12. Heaps - Data Structures using C++ by Varsha Patil

19Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

ReheapUp The Reheap Up operations repair the structure

so that it is a heap by lifting the last element up the tree until that element reaches a proper position in the tree

ReheapUp

ReheapUp Operation

Page 20: 12. Heaps - Data Structures using C++ by Varsha Patil

20Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

ReheapDown To restore the heap, we need an operation that

will sink the root down until the heap ordering property is satisfied and thus the operation ReheapDown comes into action

ReheapUDown Operation

Page 21: 12. Heaps - Data Structures using C++ by Varsha Patil

21Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

312327

4332

21

Original Tree, not a Heap

Example

41

2624

Page 22: 12. Heaps - Data Structures using C++ by Varsha Patil

22Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Root 21 moved down (right)

312327

2132

43

41

2624

Page 23: 12. Heaps - Data Structures using C++ by Varsha Patil

23Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Moved down again yielding a heap

312327

32

43

21

2624

41

Page 24: 12. Heaps - Data Structures using C++ by Varsha Patil

24Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Insert into heap—A new key can be inserted into a heap. Initially, a new key is inserted by locating the first empty leaf location in an array, and the ReheapUp operation places it in a proper location in the heap

Delete into heap—The key can be deleted from heap and it is the root value. After deletion, the heap without root is repaired by ReheapDown operation

The last node key is placed at root and then ReheapDown operation places it at proper location

Page 25: 12. Heaps - Data Structures using C++ by Varsha Patil

25Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

The steps for creating heaps are as follows:

Organize the entire collection of data elements as a binary tree stored in an array indexed from 1 to n, where for any node at index i, its two children, if exist, will be stored at index 2 X i + 1 and 2 X i + 2

the top part in which the data elements are in they Divide the binary tree into two parts: r original order and the bottom part in which the data elements are in their heap order, where each node is in higher order than its children, if any

Start the bottom part with the half of the array, which contains only leaf nodes. Of course, it is in heap order, because the leaf nodes have no child

Page 26: 12. Heaps - Data Structures using C++ by Varsha Patil

26Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

The steps for creating heaps are as follows:

Move the last node from the top part to the bottom part, compare its order with its children, and swap its location with its highest order child if its order is lower than any child

Repeat the comparison and swapping to ensure the bottom part is in heap order again with this new node added

Repeat step 4 until the top part is empty. At this time, the bottom part becomes a complete heap tree

Page 27: 12. Heaps - Data Structures using C++ by Varsha Patil

27Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Heap Applications Heaps are used commonly in the following

operations:

Selection algorithm

Scheduling and prioritizing (priority queue)

Sorting

Page 28: 12. Heaps - Data Structures using C++ by Varsha Patil

28Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Selection Problem For the solution to the problem of determining

the kth element, we can create the heap and delete k − 1 elements from it, leaving the desired element at the root.

So the selection of the kth element will be very easy as it is the root of the heap

For this, we can easily implement the algorithm of the selection problem using heap creation and heap deletion operations

This problem can also be solved in O(nlogn) time using priority queues

Page 29: 12. Heaps - Data Structures using C++ by Varsha Patil

29Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Scheduling and prioritizing (priority

queue) The heap is usually defined so that only the

largest element (that is, the root) is removed at a time.

This makes the heap useful for scheduling and prioritizing

In fact, one of the two main uses of the heap is as a prioriy queue, which helps systems decide what to do next

Page 30: 12. Heaps - Data Structures using C++ by Varsha Patil

30Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Applications of priority queues where heaps are implemented are as follows:

CPU scheduling

I/O scheduling

Process scheduling.

Page 31: 12. Heaps - Data Structures using C++ by Varsha Patil

31Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Sorting Other than as a priority queue, the heap has one

other important usage: heap sort

Heap sort is one of the fastest sorting algorithms, achieving speed as that of the quicksort and merge sort algorithms

The advantages of heap sort are that it does not use recursion, and it is efficient for any data order

There is no worse-case scenario in case of heap sort

Page 32: 12. Heaps - Data Structures using C++ by Varsha Patil

32Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Heap Sort The steps for building heap sort are as follows:

Build the heap tree Start Delete Heap operations, storing

each deleted element at the end of the heap array

After performing step 2, the order of the elements will be opposite to the order in the heap tree

Hence, if we want the elements to be sorted in ascending order, we need to build the heap tree in

Descending order—the greatest element will have the highest priority

Page 33: 12. Heaps - Data Structures using C++ by Varsha Patil

33Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Note that we use only one array, treating its parts differently

When building the heap tree, a part of the array will be considered as the heap, and the rest part will be the original array

When sorting, a part of the array will be the heap, and the rest part will be the sorted array

Page 34: 12. Heaps - Data Structures using C++ by Varsha Patil

34Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Binomial Trees and Heap

Binomial tree is an ordered tree defined recursively

For the binomial tree Bk, There are 2k nodes The height of the tree is k There are exactly (ki) nodes at depth i for i = 0,

1, …, k The root has degree k, which is greater than

that of any other node; moreover, if the children of the root are numbered from left to right by k − 1, k − 2, …, 0, the child i is the root of a subtree

Page 35: 12. Heaps - Data Structures using C++ by Varsha Patil

35Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Binomial Heap A binomial heap H is a set of binomial trees that

satisfies the following binomial heap properties

Each binomial tree in H follows the min-heap property. We say that each such tree is minheap- ordered

For any non-negative integer k, there is utmost one binomial tree in H whose root has degree k

Page 36: 12. Heaps - Data Structures using C++ by Varsha Patil

36Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Representation of Binomial Heap

The node of a binomial heap can be represented by five tuples as shown in fig

1. Parent—Pointing to parent node2. Key—Key value, that is, data3. Degree—Degree of each node, that is, the number of children it has4. Child—Pointing to any of its child node (mostly pointing to its leftmost child)5. Siblings—Pointing to sibling node, that is, used to maintain the singly-circular lists of siblings

Page 37: 12. Heaps - Data Structures using C++ by Varsha Patil

37Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Fig. Representation of a node of binomial

heap

Parent

KeyDegree

Child Sibling

Page 38: 12. Heaps - Data Structures using C++ by Varsha Patil

38Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Fig. Representation of binomial heap of Fig. “A”

using five-tuple nodeNull111

Null

Null22

131

260

Null

Null

190

Null

Null

Page 39: 12. Heaps - Data Structures using C++ by Varsha Patil

39Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Operations on Binomial Heaps

There are various operations of binomial heaps CreateBHeap—Creates an empty binomial heap, that

is, simply allocates and returns an object H, where head[H] = null

FindMinimumKey—Returns a pointer to the node with the minimum key in an n-node binomial heap H

UnitingTwoBHeap—Takes the union of the two binomial heaps.

InsertNode—Inserts node into binomial heap H ExtractMinimumKeyNode—Extracts the node with

minimum key from binomial heap H and returns the pointer to the extracted node

DecreaseKey—Decreases the key of a node in a binomial heap H to a new value k

DeleteKey—Deletes the specified key from binomial heap H

Page 40: 12. Heaps - Data Structures using C++ by Varsha Patil

40Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Fibonacci Heap Like binomial heap, Fibonacci heap is a

collection of min-heap-ordered trees

The trees in a Fibonacci are not constrained to be binomial trees

The roots of all the trees in Fibonacci heap are linked together using left and right pointers into circular doubly-linked list called root list of the Fibonacci heap

Page 41: 12. Heaps - Data Structures using C++ by Varsha Patil

41Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Fibonacci Heap An example of the Fibonacci heap consisting of

5 min-heap-ordered trees and 15 nodes

9

23

3615

12

30

185

2

40

25

33

2124

19

Page 42: 12. Heaps - Data Structures using C++ by Varsha Patil

42Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Representation of Fibonacci Heap

Fibonacci heap can be represented using the Fibonacci heap nodes

The representation of such a node is shown in Figure

ParentKey

DegreeMark

Left Child Right

Page 43: 12. Heaps - Data Structures using C++ by Varsha Patil

43Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

The node of Fibonacci heap can be represented

by seven tuples Parent—Pointing to parent node Key—Key value, that is, data Degree—Degree of each node, that is, the number of

children it has Child—Pointing to any of its child node (mostly

pointing to its leftmost child) Mark—The Boolean-valued field indicates whether

the node has lost a child since the last time the node was made the child of another node. The newly created nodes are,unmarked (i.e., default value is false)

Left—Pointing to the left sibling node, that is, used to maintain the doubly circular lists of siblings

Right—Pointing to the right sibling node, that is, used to maintain the doubly circular lists of siblings

Page 44: 12. Heaps - Data Structures using C++ by Varsha Patil

44Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Operations on Fibonacci Heaps

CreateHeap—Creates an empty Fibonacci Heap, that is, simply allocates and returns an object H, where min[H]=null

FindMinimumKey—Returns a min[H], that is, pointer to the node with the minimum key in an n-node Fibonacci heap H

UnitingTwoFHeap—Takes the union of the two Fibonacci heaps

InsertNode—Inserts node into Fibonacci heap H ExtractMinimumKeyNode—Extracts the node with

minimum key from Fibonacci heap H and returns the pointer to the extracted node

DecreaseKey—Decreases the key of a node in a Fibonacci heap H to a new value k

DeleteKey—Deletes the specified key from Fibonacci heap H

Page 45: 12. Heaps - Data Structures using C++ by Varsha Patil

45Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Summary A complete or nearly complete binary tree where each

node is greater or equal to its decedents with each subtree satisfying this property is called as heap

The basic operations on heap are as follows: insert, delete, ReheapUp, and ReheapDown

Heap can be implemented using an array as it is a complete binary tree. It is easy to maintain fixed relationship between a node and its children

Among many applications of heap, the key ones are the following: priority queue, sorting, and selection

Page 46: 12. Heaps - Data Structures using C++ by Varsha Patil

46Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Priority queue is implemented using heap by maintaining its relationship of element with other members in a list

One of the popular sorting techniques is heap sort that uses heap

The popularly heap is used in application where at each stage, the largest element is to be picked up for processing known as selection algorithm

Summary

Page 47: 12. Heaps - Data Structures using C++ by Varsha Patil

47Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

END Of

Chapter 12….!