Write an algorithm to insert element in a heap?
Answers
Inserting an element into a heap
In this article we examine the idea laying in the foundation of the heap data structure. We call it sifting, but you also may meet another terms, like "trickle", "heapify", "bubble" or "percolate".
Insertion algorithm
Now, let us phrase general algorithm to insert a new element into a heap.
Add a new element to the end of an array;
Sift up the new element, while heap property is broken. Sifting is done as following: compare node's value with parent's value. If they are in wrong order, swap them.
hope it helps you...
mark as brainliest plzz ❤️
Answer:
Insert -2 into a following heap:
Insert a new element to the end of the array:
In the general case, after insertion, heap property near the new node is broken:
To restore heap property, algorithm sifts up the new element, by swapping it with its parent:
Now heap property is broken at the root node:
Keep sifting:
Explanation:
Hope it helps