Lab 14, Task 3 -------------- 1. At the end of the previous task, we had the following heap: +----+ | 12 | +----+ ____/ \____ / \ +----+ +----+ | 8 | | 6 | +----+ +----+ / \ / \ +----+ +----+ +----+ +----+ | 3 | | 7 | | 4 | | 5 | +----+ +----+ +----+ +----+ The corresponding array looks like this: {12, 8, 6, 3, 7, 4, 5} 2. Remove 12. Place 5 at the root and sift down. +----+ | 5 | +----+ ____/ \____ / \ +----+ +----+ | 8 | | 6 | +----+ +----+ / \ / +----+ +----+ +----+ | 3 | | 7 | | 4 | +----+ +----+ +----+ The 8 moves up. +----+ | 8 | +----+ ____/ \____ / \ +----+ +----+ | 5 | | 6 | +----+ +----+ / \ / +----+ +----+ +----+ | 3 | | 7 | | 4 | +----+ +----+ +----+ The 7 moves up, and the 5 goes where the 7 used to be. +----+ | 8 | +----+ ____/ \____ / \ +----+ +----+ | 7 | | 6 | +----+ +----+ / \ / +----+ +----+ +----+ | 3 | | 5 | | 4 | +----+ +----+ +----+ The removed item (the 12) is put in the last position of the array, which now looks like this: {8, 7, 6, 3, 5, 4, 12} 2. Remove 8. Place 4 at the root and sift down. +----+ | 4 | +----+ ____/ \____ / \ +----+ +----+ | 7 | | 6 | +----+ +----+ / \ +----+ +----+ | 3 | | 5 | +----+ +----+ The 7 moves up. +----+ | 7 | +----+ ____/ \____ / \ +----+ +----+ | 4 | | 6 | +----+ +----+ / \ +----+ +----+ | 3 | | 5 | +----+ +----+ The 5 moves up, and the 4 goes where the 5 used to be. +----+ | 7 | +----+ ____/ \____ / \ +----+ +----+ | 5 | | 6 | +----+ +----+ / \ +----+ +----+ | 3 | | 4 | +----+ +----+ The removed item (the 8) is put in the second-to-last position of the array, which now looks like this: {7, 5, 6, 3, 4, 8, 12} 3. Remove 7. Place 4 at the root and sift. +----+ | 4 | +----+ ____/ \____ / \ +----+ +----+ | 5 | | 6 | +----+ +----+ / +----+ | 3 | +----+ The 6 moves up, and the 4 goes where the 6 used to be: +----+ | 6 | +----+ ____/ \____ / \ +----+ +----+ | 5 | | 4 | +----+ +----+ / +----+ | 3 | +----+ The removed item (the 7) is put in the third-to-last position of the array, which now looks like this: {6, 5, 4, 3, 7, 8, 12} 4. Remove 6. Place 3 at the root and sift. +----+ | 3 | +----+ ____/ \____ / \ +----+ +----+ | 5 | | 4 | +----+ +----+ The 5 moves up, and the 3 goes where the 5 used to be. +----+ | 5 | +----+ ____/ \____ / \ +----+ +----+ | 3 | | 4 | +----+ +----+ The removed item (the 6) is put in the fourth-to-last position of the array, which now looks like this: {5, 3, 4, 6, 7, 8, 12} 5. Remove 5. Place 4 at the root and sift, but it doesn't move. +----+ | 4 | +----+ ____/ / +----+ | 3 | +----+ The removed item (the 5) is put in the fifth-to-last position of the array, which now looks like this: {4, 3, 5, 6, 7, 8, 12} 6. Remove 4. Place 3 at the root, and we're done, because there is only one item left in the heap. The removed item (the 4) is put in position 1 of the array, which is now fully sorted: {3, 4, 5, 6, 7, 8, 12}