Binary..... heaps


A priority queue is implemented as a Max-Heap.Initially , it has 5 elements.The level-order traversal of the heap is:10,8,5,3,2.Two new elements 1 and 7 are inserted into the heap in that order.The level order traversal of the heap after the insertion of the elements is:


ans) 10,8,7,3,2,1,5

explanation -> after insertion of 1 there will be no need to modify the tree as it has maintained its heap properties but after insertion of 7, “heapify” needs to be performed to make the tree balanced.

heapify -> swapping inserted element with its parent if the parent is less than child.(for Max heap)


After insertion of 1,1 will be the child of 5 in the new heap tree.But,after insertion of 7,as 7 is greater than 5 and it is as max heap tree,7 and 5 swap their positions and so,7 be the parent of 1 and 5.