Computer Science, asked by chiragkaushal1, 5 months ago

Write an algorithm to insert element in a heap?

Answers

Answered by ITZANNIKET
3

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 ❤️

Answered by MissCutiess
1

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

Similar questions