data structures using c++ by dr varsha patil oxford...
TRANSCRIPT
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil1
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil2
A specialized tree-based data structure known as heaps
Usage of heaps efficiently for applications such as priorityqueues
How heaps are implemented using arrays
A few more applications such as selection problem andevent simulation
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil3
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 iscompletely 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 valueof each node is greater than or equal to the key value of itschildren, or the key value of each node is lesser than orequal to the key value of its children
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil4
17
50
5
4030913
5065
70
30
5
7
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
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil5
17
50
4030913
5065
70
30
5
Fig 12.4 Binary Trees with no Heap
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil6
Min-heap
Max-heap
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil7
Data
all >= Dataall >= Data
Fig 12.5 :Structure of min-heap
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil8
In min-heap, the key value of each node is lesser than orequal to the key value of its children
In addition, every path from root to leaf should be sortedin ascending order
9710
52
1
Fig 12.6 An example of a min heap
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil9
A max-heap is where the key value of a node is greaterthan the key values in all of its sub trees
In general, whenever the term ‘heap’ is used by itself
Data
all <= Dataall <= Data
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil10
326
48
9
Fig 12.7 An example of a max-heap
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil11
326
48
9
Fig 12.7 An example of a max-heap
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil12
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 rightsub tree is completely full of height h − 2, or
its left sub tree is completely full of height h − 1 and its rightsub 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
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil13
A binary tree has the heap property if :
it is empty or
the key in the root is larger than either children andboth sub trees have the heap property
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil14
326
48
9
Fig 12.8 Sample Heap
Implementation of Heap
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil15
In this array,
1. parent of index ith node is at index (i − 1)/2
2. left child of index ith node is at index 2 X i + 1
3. right child of index ith node is at index 2 X i + 2
Data 9 8 4 6 2 3
Index 0 1 2 3 4 5 6 7
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil16
68 46 22 35 02 13 09
0913235
2246
68
A Heap Tree
Representation of heap by using array
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil17
Declare create() : Heap
Insert(Heap,Data) : Heap
Deletemaxval(Heap) : Heap
ReheapDown(Heap, Child) : Heap
ReheapUp(Heap, Root) : Heap
End
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil18
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
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil19
The Reheap Up operations repair the structure so that it isa heap by lifting the last element up the tree until thatelement reaches a proper position in the tree
ReheapUp
ReheapUp Operation
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil20
To restore the heap, we need an operation that will sinkthe root down until the heap ordering property is satisfiedand thus the operation ReheapDown comes into action
ReheapUDown Operation
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil21
312327
4332
21
Original Tree, not a Heap
Example
41
2624
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil22
Root 21 moved down (right)
312327
2132
43
41
2624
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil23
Moved down again yielding a heap
312327
32
43
21
2624
41
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil24
Insert into heap—A new key can be inserted into a heap. Initially, anew key is inserted by locating the first empty leaf location in anarray, and the ReheapUp operation places it in a proper location inthe heap
Delete into heap—The key can be deleted from heap and it is theroot value. After deletion, the heap without root is repaired byReheapDown operation
The last node key is placed at root and then ReheapDown operationplaces it at proper location
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil25
Organize the entire collection of data elements as a binary treestored in an array indexed from 1 to n, where for any node atindex i, its two children, if exist, will be stored at index 2 X i + 1and 2 X i + 2
the top part in which the data elements are in they Divide thebinary tree into two parts: r original order and the bottom partin which the data elements are in their heap order, where eachnode is in higher order than its children, if any
Start the bottom part with the half of the array, which containsonly leaf nodes. Of course, it is in heap order, because the leafnodes have no child
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil26
Move the last node from the top part to the bottom part,compare its order with its children, and swap its locationwith its highest order child if its order is lower than anychild
Repeat the comparison and swapping to ensurethe bottom part is in heap order again with this new nodeadded
Repeat step 4 until the top part is empty. At this time, thebottom part becomes a complete heap tree
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil27
Heaps are used commonly in the following operations:
Selection algorithm
Scheduling and prioritizing (priority queue)
Sorting
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil28
For the solution to the problem of determining the kthelement, we can create the heap and delete k − 1 elementsfrom it, leaving the desired element at the root.
So the selection of the kth element will be very easy as it isthe root of the heap
For this, we can easily implement the algorithm of theselection problem using heap creation and heap deletionoperations
This problem can also be solved in O(nlogn) time usingpriority queues
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil29
The heap is usually defined so that only the largestelement (that is, the root) is removed at a time.
This makes the heap useful for scheduling andprioritizing
In fact, one of the two main uses of the heap is as a prioriyqueue, which helps systems decide what to do next
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil30
Applications of priority queues where heaps areimplemented are as follows:
CPU scheduling
I/O scheduling
Process scheduling.
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil31
Other than as a priority queue, the heap has one otherimportant usage: heap sort
Heap sort is one of the fastest sorting algorithms,achieving speed as that of the quicksort and merge sortalgorithms
The advantages of heap sort are that it does not userecursion, and it is efficient for any data order
There is no worse-case scenario in case of heap sort
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil32
The steps for building heap sort are as follows:
Build the heap tree
Start Delete Heap operations, storing each deletedelement at the end of the heap array
After performing step 2, the order of the elements will beopposite to the order in the heap tree
Hence, if we want the elements to be sorted in ascendingorder, we need to build the heap tree in
Descending order—the greatest element will have thehighest priority
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil33
Note that we use only one array, treating its partsdifferently
When building the heap tree, a part of the array will beconsidered as the heap, and the rest part will be theoriginal array
When sorting, a part of the array will be the heap, and therest part will be the sorted array
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil34
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 anyother node; moreover, if the children of the root arenumbered from left to right by k − 1, k − 2, …, 0, the child iis the root of a subtree
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil35
A binomial heap H is a set of binomial trees that satisfiesthe 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 onebinomial tree in H whose root has degree k
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil36
The node of a binomial heap can be represented by five tuples as shown in fig
1. Parent—Pointing to parent node
2. Key—Key value, that is, data
3. Degree—Degree of each node, that is, the number of children it has
4. 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
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil37
Parent
Key
Degree
Child Sibling
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil38
Null
11
1
Null
Null
2
2
13
1
26
0
Null Null
19
0
Null Null
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil39
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
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil40
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
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil41
An example of the Fibonacci heap consisting of 5 min-heap-ordered trees and 15 nodes
9
23
3615
12
30
18
5
2
40
25
33
2124
19
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil42
Fibonacci heap can be represented using the Fibonacci heap nodes
The representation of such a node is shown in Figure
Parent
Key
Degree
Mark
Left Child Right
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil43
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
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil44
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
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil45
A complete or nearly complete binary tree where each node is greateror equal to its decedents with each subtree satisfying this property iscalled 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 binarytree. It is easy to maintain fixed relationship between a node and itschildren
Among many applications of heap, the key ones are the following:priority queue, sorting, and selection
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil46
Priority queue is implemented using heap by maintaining itsrelationship 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, thelargest element is to be picked up for processing known as selectionalgorithm
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil47