PROGRAMMING & DATA STRUCTURES
Binary Heap
Guidelines to Attend Live Class
✓ Pen, Notebook and Calculator are must while attending the online class.
✓ Regularity and Punctuality is necessary.
✓ Hold chat while attending the class. We will allow you to ask and put your questions in the comment
box.
✓ Attempt Quizzes Daily as per the schedule.
✓ Strictly follow the day-wise study plan.
Binary Heap
Last Class – Quick Revision
Binary Heap
Important Formulae in Trees
Binary Heap
Important Formulae in Trees
Binary Heap
Binary Heap
❑ A binary heap is a heap data structure that takes the form of a binary tree.
❑ Binary heaps are a common way of implementing priority queues.
❑ The binary heap was introduced by J. W. J. Williams in 1964.
Binary Heap
Binary Heap
A binary heap is defined as a binary tree with two additional constraints:
❑ Shape property: a binary heap is a complete binary tree; that is, all levels of the tree,
except possibly the last one (deepest) are fully filled, and, if the last level of the tree is
not complete, the nodes of that level are filled from left to right.
❑ Heap property: the key stored in each node is either greater than or equal to (≥) or less
than or equal to (≤) the keys in the node's children.
❑ Heaps where the parent key is greater than or equal to (≥) the child keys are called
max-heaps; those where it is less than or equal to (≤) are called min-heaps.
Binary Heap
Max - Heap
❑ In this heap, the key value of a node is greater than or equal to the key value of the
highest child.
❑ Hence, H[Parent(i)] ≥ H[i]
Binary Heap
Min - Heap
❑ In mean-heap, the key value of a node is lesser than or equal to the key value of the
lowest child.
❑ Hence, H[Parent(i)] ≤ H[i]
Binary Heap
Array Representation
❑ A complete binary tree can be represented by an array, storing its elements using level
order traversal.
❑ Let us consider a heap (as shown below) which will be represented by an array H.
Index 0 1 2 3 4 5 6 7 8 ...
elements 70 30 50 12 20 35 25 4 8 ...
Binary Heap
Array Representation
❑ If a parent at index ‘i’, then it’s left child at index 2* i and right child at index (2 * i +1).
❑ Indexing from 1 would be more understandable.
Index 0 1 2 3 4 5 6 7 8 ...
elements 70 30 50 12 20 35 25 4 8 ...
Binary Heap
Building Max Heap
❑ First, we have to insert the element in such a way that the property of the complete
binary tree must be maintained.
❑ Secondly, the value of the parent node should be greater than the either of its child.
Binary Heap
Example Construct a Max heap with elements inserted in the order:
43, 28, 67, 54, 76, 64, 80, 17, 90.
Binary Heap
Insertion into Max-Heap
1. Insert the new item at the end of the heap.
2. Compare the newly inserted item with its parent.
3. If the parent is larger, stop. If the parent is smaller, swap the item with its parent.
4. Repeat step 2 until the parent is larger or equal to the item.
Binary Heap
Example Assume a Max heap with elements in the level order 75, 54, 62, 43, 30, 57. Now,
Insert 92, Insert 70, Insert 84 and Insert 45.
Binary Heap
Deletion / Find Max from Max - Heap
1. Store the first time in the tree in some temporary variable.
2. Replace the first node with the item in the last node.
3. Check the first node with its children nodes.
• If the left child is larger, we swap it with the left child.
• If the right node is larger, we swap it with the right node.
4. Repeat step 3 until the parent node is larger than the left and right child node.
5. Return the maximum value (stored in the temporary variable).
Binary Heap
Example Assume a Max heap with elements in the level order
75, 54, 62, 43, 30, 57, 60, 27, 41, 18, 23, 44.
Now, Perform 3 Delete Operations and return the resultant tree elements.
Binary Heap
Q1 A Priority-Queue is implemented as a Max-Heap. Initially, it has 5 elements. The level-
order traversal of the heap is given below:
10, 8, 5, 3, 2
Two new elements ”1‘ and ”7‘ are inserted in the heap in that order. The level-order
traversal of the heap after the insertion of the elements is:
GATE 2005
A 10, 8, 7, 5, 3, 2, 1 B 10, 8, 7, 2, 3, 1, 5
C 10, 8, 7, 1, 2, 3, 5 D 10, 8, 7, 3, 2, 1, 5
Binary Heap
Q2 Which of the following sequences of array elements forms a heap?
GATE 2006
A {23, 17, 14, 6, 13, 10, 1, 12, 7, 5} B {23, 17, 14, 6, 13, 10, 1, 5, 7, 12}
C {23, 17, 14, 7, 13, 10, 1, 5, 6, 12} D {23, 17, 14, 7, 13, 10, 1, 12, 5, 7}
Binary Heap
Q3 A max-heap is a heap where the value of each parent is greater than or equal to the value
of its children. Which of the following is a max-heap ?
GATE 2011
A B
C D