sec heaps and priority queues routine is finished if the value of is greater than that of its parentthen the two elements swap positions from herethe process of comparing to its (currentparent continues until reaches its correct position each call to insert takes th(log ntime in the worst casebecause the value being inserted can move at most the distance from the bottom of the tree to the top of the tree thusthe time to insert values into the heapif we insert them one at timewill be th( log nin the worst case if all values are available at the beginning of the building processwe can build the heap faster than just inserting the values into the heap one by one consider figure ( )which shows one series of exchanges that will result in heap note that this figure shows the input in its logical form as complete binary treebut you should realize that these values are physically stored in an array all exchanges are between node and one of its children the heap is formed as result of this exchange process the array for the right-hand tree of figure (awould appear as follows figure (bshows an alternate series of exchanges that also forms heapbut much more efficiently from this exampleit is clear that the heap for any given set of numbers is not uniqueand we see that some rearrangements of the input values require fewer exchanges than others to build the heap sohow do we pick the best rearrangementone good algorithm stems from induction suppose that the left and right subtrees of the root are already heapsand is the name of the element at the root this situation is illustrated by figure in this case there are two possibilities ( has value greater than or equal to its two children in this caseconstruction is complete ( has value less than one or both of its children in this caser should be exchanged with the child that has greater value the result will be heapexcept that might still be less than one or both of its (newchildren in this casewe simply continue the process of "pushing downr until it reaches level where it is greater than its childrenor is leaf node this process is implemented by the private method siftdown of the heap class the siftdown operation is illustrated by figure this approach assumes that the subtrees are already heapssuggesting that complete algorithm can be obtained by visiting the nodes in some order such that the children of node are visited before the node itself one simple way to do this is simply to work from the high index of the array to the low index actuallythe build process need not visit the leaf nodes (they can never move down because they are already at the bottom)so the building algorithm can start in the middle of the