how to insert new value into max heap and apply max_delete to heap
<p><em>Problem 1</em></p>

<pre><code>                     98
                    /  \
                   /    \
                 67      89
                / \     /  \
               /   \   /    \
             38   42  54    89
            / \
           /   \
          17   25
</code></pre>

<p>i want to insert 97 into max heap [98,67,89,38,42,54,89,17,25] (represented in list).</p>

<p>As per me, resulting heap is [98,97,89,38,67,54,89,17,25,42]</p>

<pre><code>                     98
                    /  \
                   /    \
                 97      89
                / \     /  \
               /   \   /    \
             38    67  54    89
            / \     |
           /   \    |
          17   25   42
</code></pre>

<p><em>Problem 2</em> </p>

<p>i want to apply delete_max() twice to the heap [100,97,93,38,67,54,93,17,25,42]. </p>

<pre><code>                    100
                    /  \
                   /    \
                 97      93
                / \     /  \
               /   \   /    \
             38    67  54    93
            / \     |
           /   \    |
          17   25   42
</code></pre>

<p>As per me heap after two deletemax operations,resulting heap is [93,67,93,38,42,54,25,17]</p>

<pre><code>                     93
                    /  \
                   /    \
                 67      93
                / \     /  \
               /   \   /    \
             38    42  54    25
            /     
           /      
          17      
</code></pre>

<p><strong>I want to conform, is I am doing insertion and max_delete correctly for heap and above answer is correct?</strong>
If not correct, then please guide me. </p>

<p>Your answers look correct. Let's take a closer look at why.</p>

<p>In the first case, you have the heap:</p>

<pre><code>[98,67,89,38,42,54,89,17,25]
</code></pre>

<p>You want to insert 97. So you add it to the end and then bubble it up:</p>

<pre><code>[98,67,89,38,42,54,89,17,25,97]
</code></pre>

<p>You compare 97 with its parent (42). Since 97 is greater, you swap them:</p>

<pre><code>[98,67,89,38,97,54,89,17,25,42]
</code></pre>

<p>Then compare 97 with its parent again. This time the parent is 67, so you have to swap again.</p>

<pre><code>[98,97,89,38,67,54,89,17,25,42]
</code></pre>

<p>Comparing one more time, you see that the parent (98) is greater than the item you inserted, so you're done.</p>

<p>Now, given the heap <code>[100,97,93,38,67,54,93,17,25,42]</code>, you want to remove the two highest items. The rule for delete_max is replace the root with the last item on the heap, and then sift it down. So you have:</p>

<pre><code> [42,97,93,38,67,54,93,17,25]
</code></pre>

<p>42 is smaller than its children, so you swap it with the largest child:</p>

<pre><code> [97,42,93,38,67,54,93,17,25]
</code></pre>

<p>It's greater than 38, but smaller than 67, so you swap again:</p>

<pre><code> [97,67,93,38,42,54,93,17,25]
</code></pre>

<p>And 42 is now at the leaf level so there's nothing more to do. That's the first item removed. Now to remove the second. Move 25 to the root:</p>

<pre><code> [25,67,93,38,42,54,93,17]
</code></pre>

<p>And sifting down:</p>

<pre><code>[93,67,25,38,42,54,93,17]  // swapped with 93
[93,67,93,38,42,54,25,17]  // swapped with 93 again
</code></pre>

python python-3.x heap heapsort binary-heap