I am trying to implement the removal of an arbitrary element from the pyramid. That is, we are given the index of the element in the array that contains the pyramid, and we have to delete it. In theory, the algorithm is as follows: put the last element in place of the last element and start either sieving down (sift_down) or sieving up (sift_up). At first I thought that it was necessary to perform only the sieving down, then I decided to launch both sift_down (i) and sift_up (i), where i is the element that is being removed and in which place the last element was put. That's how I do about it.
heap[i] = heap[--heap_size]; // Ставим последний элемент на место удаляемого sift_down(i); // Просеивание вниз sift_up(i); // Просеивание вверх But this algorithm does not work for me on many tests (I don’t know on which ones, I just know the verdict - Wrong answer). Tell me how to properly restore the pyramid after putting the last element in place of the deleted element.
ZY I solve the problem from here (page 12, task 5).
