save the root value and copy the last heap value to the root value self _elements[ self _count - self _elements[ self _elementsself _count sift the root value down the tree self _siftdown sift the value at the ndx element up the tree def _siftupselfndx )if ndx parent ndx / if self _elements[ndxself _elements[parenttmp self _elements[ndxself _elements[ndxself _elements[parentself _elements[parenttmp self _siftupparent swap elements sift the value at the ndx element down the tree def _siftdownselfndx )left ndx right ndx determine which node contains the larger value largest ndx if left self _elements[largestlargest left elif right self _elements[largest]largest right if the largest value is not in the current node (ndx)swap it with the largest value and repeat the process if largest !ndx swapself _elements[ndx]self _elements[largest_siftdownlargest the first step when adding new item to heap is to link new leaf node in the rightmost position on the lowest level in the array implementationthis will always be the next position following the last heap item in the array after inserting the new item into the array (lines - it has to be sifted up the tree to find its correct position figure illustrates the modifications to the heap and the storage array when adding to the sample heap to extract the maximum value from max-heapwe first have to copy and save the value in the root nodewhich we know is in index position nextthe root value has to be replaced with the value from the leaf node that is in the rightmost position on the lowest level of the tree in the array implementationthat leaf node will always be the last item of the heap stored in linear order within the array after copying the last heap item to the root node (lines - )the new value in the root node has to be sifted down the tree to find its correct position the implementation of the sift-down operation is straightforward after determining the indices of the nodes left and right childwe determine which of the three values is largerthe value in the nodethe value in the node' left childor the value in the node' right child if one of the two children contains value greater than or equal to the value in the node( it has to be swapped with the