Vision Gate 2023 Pds Class 22 Binary Heap1701874364337
Vision Gate 2023 Pds Class 22 Binary Heap1701874364337
Binary Heap
Guidelines to Attend Live Class
✓ Pen, Notebook and Calculator are must while attending the online class.
✓ Hold chat while attending the class. We will allow you to ask and put your questions in the comment
box.
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
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
❑ 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 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
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
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