This is an easy and small guide to minimum binary heap. This post does not have the entire code mentioned (although there is a link to the code in the end). The motive is to make you familiar with the concepts and the procedure.
It is recommended to try writing the code on your own.
What is a minimum binary heap?
A binary tree which follows:
What operations can you perform on a minimum binary heap?
Cascade up
Cascading up means taking up the inserted element to its correct position such that none of the properties of a minimum binary heap are violated. This is also known as bubble-up, percolate-up, sift-up, trickle-up or heapify-up.
For inserting an element in a minimum binary heap, all you got to do is follow these two rules:
Delete
For deleting an element from a minimum binary heap:
It is recommended to try writing the code on your own.
What is a minimum binary heap?
A binary tree which follows:
- Heap property : Every parent node should be smaller than its children.
- Shape property : The binary heap should be a complete binary tree.
What operations can you perform on a minimum binary heap?
- Insert : Insert an element in the minimum binary heap. {O(log(n))}
- Delete : Delete an element from the minimum binary heap. {O(log(n))}
- Extract-min : Extract the minimum element from the minimum binary heap. {O(1)}
Cascade up
Cascading up means taking up the inserted element to its correct position such that none of the properties of a minimum binary heap are violated. This is also known as bubble-up, percolate-up, sift-up, trickle-up or heapify-up.
- If the element is smaller than its parent, swap the parent and the child.
- Continue the process until the element is placed at right position.
For inserting an element in a minimum binary heap, all you got to do is follow these two rules:
- Insert the element in the end of the tree.
- Cascade up the element until it is smaller than its parent.
GIF by: algorithms.tutorialhorizon.com
Cascade down
Cascading down is just the opposite of cascade up. This is also known as bubble-down, percolate-down, sift-down, trickle down or heapify-down.
- Check which of the children of the element is smaller.
- If the element is greater than the smaller child, swap the element and the child.
- Continue the process such that the heap follows the two properties.
For deleting an element from a minimum binary heap:
- Find out the index 'i' of the element.
- Swap the element with the last node 'n' in the tree.
- Delete the last element of the tree.
- Cascade down the node 'n' (now at index 'i') until it is greater than its children.
GIF by: algorithms.tutorialhorizon.com
Extract-min
We're always maintaining the two properties of minimum binary heap at the time of insertion as well as deletion. This leaves us to an O(1) solution for extracting the minimum element of the heap.
Guesses?
Yes! Just return the root of the heap.
Guesses?
Yes! Just return the root of the heap.
But, what if you wish to extract the minimum element one after the other?
- Save the root of the tree 'r'.
- Swap the root with the last element of the tree.
- Delete the last element of the tree.
- Perform Cascade down on now root.
- Return 'r'.
Uses
- Implementing priority queue
- Heap Sort
- Implementing Dijkstra's algorithm in O(E*log(V))
That is all about minimum binary heap. Similarly, you can do maximum binary heap.
Looking for the entire code? See here for C++ solution.
Looking for the entire code? See here for C++ solution.
No comments:
Post a Comment