It is the one type of data structure where we are going to build a tree with some restrictions.
There are two types of restrictions
1. Every node's value should be minimum than it's child's value.
2. Every node's value should be maximum than it's child's value .
Generally it used for sorting called heapsort.
Tree where every node's value is minimum than it's child's value that tree is called minheap. We can maintain it by implementing function which will insert element at the place where it's child's value will be maximum than it's value.
Ex. suppose we have to add 3,2,1 into heap
then first we will add 3 at root.
now 2 is less than root item i.e. 3.
so replace them and same for 1 also.
3 3 2 1
___| ___|___ ___|___
| | | | |
2 3 1 3 2
Tree where every node's value is maximum than it's child's value that tree is called maxheap. We can maintain it by implementing function which will insert element at the place where it's child's value will be minimum than it's value.
Ex. suppose we have to add 1,2,3 into heap
then first we will add 1 at root.
now 2 is high than root item i.e. 1.
so replace them and same for 3 also.
1 1 2 3
___| ___|___ ___|___
| | | | |
2 1 3 1 2
For this please check in both folder i.e. minheap and maxheap
I am Rohit Pawar Want to know more about me, click below