Heap Flashcards
What is heap ?
A Heap is a Tree-based data structure, which satisfies the below properties:
1- A Heap is a complete tree (All levels are completely filled except possibly the last level and the last level has all keys as left as possible).
2- A Heap is either Min Heap or Max Heap. In a Min-Heap, the key at root must be minimum among all keys present in the Binary Heap. The same property must be recursively true for all nodes in the Tree. Max Heap is similar to MinHeap.
What is binary heap ?
A Binary Heap is a heap where each node can have at most two children. In other words, a Binary Heap is a complete Binary Tree satisfying the above-mentioned properties.
what is min heap. Explain with example
A min-heap is a binary heap data structure where the value of the root node is always the smallest among all nodes. It satisfies the heap property:
- For every node ( i ):
- ( A[i] \leq A[2i + 1] ) (left child)
- ( A[i] \leq A[2i + 2] ) (right child)
This property ensures that the smallest element is always at the root of the tree, which allows for efficient retrieval.
Characteristics of Min-Heap:
1. Binary Tree Representation: It is usually implemented using arrays, where:
- Parent index: ( i )
- Left child index: ( 2i + 1 )
- Right child index: ( 2i + 2 )
2. Complete Binary Tree: All levels are filled except possibly the last, which is filled from left to right.
Operations:
- Insertion: Add the element to the end and “heapify up” to maintain the min-heap property.
- Deletion (Min-Element): Replace the root with the last element, remove it, and “heapify down.”
- Peek Minimum: Always ( O(1) ), as the root contains the smallest element.
Example:
Consider the array: ([3, 5, 9, 6, 8, 20, 10, 12, 18, 9])
Min-Heap Construction:
After inserting elements step-by-step and applying the heap property, the resulting min-heap is:
3 / \ 5 9 / \ / \ 6 8 20 10 / \ 12 18
Array Representation:
The heap can also be represented as an array:
[ 3, 5, 9, 6, 8, 20, 10, 12, 18, 9 ]
Example Operations:
1. Insert 2:
- Add 2 at the end.
- “Heapify up” → Swap 2 with 6, then 2 with 3.
- Resulting heap: [ 2, 5, 3, 6, 8, 20, 10, 12, 18, 9 ].
-
Delete Min:
- Remove 3 (root), replace it with 9.
- “Heapify down” → Swap 9 with 5.
- Resulting heap: [ 5, 6, 9, 12, 8, 20, 10, 9, 18 ].
Applications:
- Priority Queues: Efficient retrieval of the smallest element.
- Heap Sort: Sorting elements in ( O(n \log n) ).
- Graph Algorithms: Prim’s and Dijkstra’s algorithms.
Example of max heap ?
How to represent binary heap ?
Since a Binary Heap is a complete Binary Tree, it can be easily represented using Arrays.
what is the index of root of binary heap is ?
0
which index returns the parent of the heap
Arr[(i-1)/2] Returns the parent node
which index returns the left child node ?
Arr[(2*i)+1] Returns the left child node
which index returns the right child node ?
Arr[(2*i)+2] Returns the right child node
How to get the max element in a max heap. approach only
Getting Maximum Element: In a Max-Heap, the maximum element is always present at the root node which is the first element in the array used to represent the Heap. So, the maximum element from a max heap can be simply obtained by returning the root node as Arr[0] in O(1) time complexity.
How to get the min element in a min heap. approach only
Getting Minimum Element: In a Min-Heap, the minimum element is always present at the root node which is the first element in the array used to represent the Heap. So, the minimum element from a minheap can be simply obtained by returning the root node as Arr[0] in O(1) time complexity.
what do you mean by Heapifying an Element
Generally, on inserting a new element onto a Heap, it does not satisfy the property of Heap as stated above on its own. The process of placing the element at the correct location so that it satisfies the Heap property is known as Heapify.
How to heapify a heap into a max heap ? approach only
Heapifying in a Max Heap: The property of Max Heap says that every node’s value must be greater than the values of its children nodes. So, to heapify a particular node swap the value of the node with the maximum value of its children nodes and continue the process until all of the nodes below it satisfies the Heap property.
What is the time complexity to heapify a tree ?
Time Complexity: The time complexity to heapify a single node is O(h), where h is equal to log(N) in a complete binary tree where N is the total number of nodes.
What is extract min in min heap ?
it means deleting or returning the root of the tree in a min heap