0% found this document useful (0 votes)
200 views10 pages

Heaps

A binary heap is a complete binary tree structure where the key at each node is either greater than or equal to (for a max heap) or less than or equal to (for a min heap) its children. Elements can be inserted randomly in O(log n) time by adding to the bottom and bubbling up, and the maximum/minimum element can be removed from the root in O(log n) time by replacing with the last element and bubbling it down. Binary heaps enable efficient priority queue implementations with O(log n) time for enqueue and dequeue.

Uploaded by

Anton Erone Pius
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
200 views10 pages

Heaps

A binary heap is a complete binary tree structure where the key at each node is either greater than or equal to (for a max heap) or less than or equal to (for a min heap) its children. Elements can be inserted randomly in O(log n) time by adding to the bottom and bubbling up, and the maximum/minimum element can be removed from the root in O(log n) time by replacing with the last element and bubbling it down. Binary heaps enable efficient priority queue implementations with O(log n) time for enqueue and dequeue.

Uploaded by

Anton Erone Pius
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Heaps

• A heap is a specialized tree-based data structure.


• There are several variants of heaps which are the
prototypical implementations of priority queues.
• We have already discussed priority queues.
• Heaps are also crucial in several efficient graph algorithms.
BINARY HEAPS
• A binary heap is a complete binary tree in which every node
satisfies the heap property which states that:
– If B is a child of A, then key(A) ≥ key(B)
• This implies that elements at every node will be either greater
than or equal to the element at its left and right child.
• Thus, the root node has the highest key value in the heap.
• Such a heap is commonly known as a max-heap.
• Alternatively, elements at every node will be either less than or
equal to the element at its left and right child.
• Thus, the root has the lowest key value.
• Such a heap is called a min-heap.
• Figure 12.1 shows a binary min heap and a binary max heap.
• The properties of binary heaps are given as follows:
– Since a heap is defined as a complete binary tree, all its
elements can be stored sequentially in an array. It follows
the same rules as that of a complete binary tree. That is, if
an element is at position i in the array, then its left child is
stored at position 2i and its right child at position 2i+1.
Conversely, an element at position i has its parent stored at
position i/2.
– Being a complete binary tree, all the levels of the tree
except the last level are completely filled.
– The height of a binary tree is given as log2n, where n is the
number of elements.
– Heaps (also known as partially ordered trees) are a very
popular data structure for implementing priority queues.
• A binary heap is a useful data structure in which elements can be
added randomly but only the element with the highest value is
removed in case of max heap and lowest value in case of min
heap.
• A binary tree is an efficient data structure, but a binary heap is
more space efficient and simpler.
Inserting a New Element in a Binary Heap

• Consider a max heap H with n elements.


• Inserting a new value into the heap is done in the following
two steps:
1. Add the new value at the bottom of H in such a way that H is still
a complete binary tree but not necessarily a heap.
2. Let the new value rise to its appropriate place in H so that H now
becomes a heap as well.
• To do this, compare the new value with its parent to check
if they are in the correct order.
• If they are, then the procedure halts, else the new value
and its parent’s value are swapped and Step 2 is repeated.
Build a max heap H from the given set
of numbers: 45, 36, 54, 27, 63, 72, 61,
and 18. Also draw the memory
representation of the heap.
Inserting a New Element in a Binary
Heap
• After discussing the concept behind
inserting a new value in the heap, let
us now look at the algorithm to do so
as shown in Fig. 12.7.
• We assume that H with n elements is
stored in array HEAP.
• VAL has to be inserted in HEAP.
• The location of VAL as it rises in the
heap is given by POS, and PAR denotes
the location of the parent of VAL.
• Note that this algorithm inserts a
single value in the heap.
• In order to build a heap, use this
algorithm in a loop.
Deleting an Element from a Binary
Heap
• After discussing the concept
behind deleting the root element
from the heap, let us look at the
algorithm given in Fig. 12.10.
• We assume that heap H with n
elements is stored using a
sequential array called HEAP.
• LAST is the last element in the
heap and PTR, LEFT, and RIGHT
denote the position of LAST and
its left and right children
respectively as it moves down
the heap.
Applications of Binary Heaps
• Binary heaps are mainly applied for
1. Sorting an array using heapsort algorithm.
2. Implementing priority queues.
Binary Heap Implementation of
Priority Queues

• A priority queue is similar to a queue in which an item is dequeued (or removed) from
the front.
• However, unlike a regular queue, in a priority queue the logical order of elements is
determined by their priority.
• While the higher priority elements are added at the front of the queue, elements with
lower priority are added at the rear.
• Conceptually, we can think of a priority queue as a bag of priorities shown in Fig.
12.11.
• In this bag you can insert any priority but you can take out one with the highest value.
• Though we can easily implement priority queues using a linear array, but we should
first consider the time required to insert an element in the array and then sort it.
• We need O(n) time to insert an element and at least O(n log n) time to sort the array.
• Therefore, a better way to implement a priority queue is by using a binary heap which
allows both enqueue and dequeue of elements in O(log n) time.

You might also like