Inserting an element into a Max Heap.

Please explain the max heap insert algorithm.

1 Like

Insertion in max heap is done in the following manner if you’re implementing it through array:

At first, insert the element at the end of the array, then compare the value of element to its parent element if the value is greater than the value of its parent node, then swap them and repeat the above step until the heap property is achieved i.e., the key stored in each node is either greater than or equal to the keys in the node’s children and 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.

For working of the algorithm, you can refer to this link.

If you feel your question has been answered, mark it as accepted.

1 Like