
Heap before deleting the value a[2] = 5 

Heap after the deletion... 
$64,000 question:

1, Delete a node from the array (this creates a "hole" and the tree is no longer "complete") 2. Replace the deletion node with the "fartest right node" on the lowest level of the Binary Tree (This step makes the tree into a "complete binary tree") 3. Heapify (fix the heap): if ( value in replacement node < its parent node ) Filter the replacement node UP the binary tree else Filter the replacement node DOWN the binary tree 


Example:

if ( replacement node < its parent node ) filter the replacement node up the tree else filter the replacement node down the tree 
public double remove( int k ) { int parent; double r; // Variable to hold deleted value r = a[k]; // Save return value a[k] = a[NNodes]; // Replace deleted node with the right most leaf // This fixes the "complete bin. tree" property NNodes; // One less node in heap.... parent = k/2; /* ======================================================= Filter a[k] up or down depending on the result of: a[k] <==> a[k's parent] ======================================================= */ if ( k == 1 /* k is root */  a[parent] < a[k] ) HeapFilterDown(k); // Move the node a[k] DOWN the tree else HeapFilterUp(k); // Move the node a[k] UP the tree return r; // Return deleted value... } 
(We have already discussed the HeapFilterUp() algorithm .... )
HeapFilterDown( k ) // Filter node a[k] down to its proper place { while ( a[k] has at least 1 child node ) { child1 = 2*k; // left child of a[k] child2 = 2*k + 1; // right child of a[k] if ( a[k] has 2 childred nodes ) { if ( a[k] has smallest value among {a[k], a[child1], a[child2]} ) break; // a[k] in proper place, done else { /* ========================================= Replace a[k] with the smaller child node ========================================= */ if ( a[child1] < a[child2] ) { swap ( a[k], a[child1] ); k = child1; // Continue check.... } else { swap ( a[k], a[child2] ); k = child2; // Continue check... } } } else // a[k] must have only 1 child node { if ( a[k] has smallest value among {a[k], a[child1]} ) break; // a[k] in proper place, done else { swap ( a[k], a[child1] ); k = child1; // Continue check.... } } } 
/* ======================================================== HeapFilterDown(k): Filters the node a[k] down the heap ======================================================== */ void HeapFilterDown( int k ) { int child1, child2; // Indices of the children nodes of k double help; while ( 2*k <= NNodes ) // Does k have any child node ? { child1 = 2*k; // Child1 = left child of k child2 = 2*k+1; // Child2 = right child of k if ( child2 <= NNodes ) // If true, then k has 2 children nodes ! { /* ======================================== Node k has 2 children nodes.... Find the min. of 3 nodes !!! ======================================== */ if ( a[k] < a[child1] && a[k] < a[child2] ) { /*  Node k has the smallest value ! Node k is in correct location... It's a heap. Stop...  */ break; // STOP, it's a heap now } else { /* =================================================== Swap a[k] with the smaller of its 2 children nodes =================================================== */ if ( a[child1] < a[child2] ) { /*  Child1 is smaller: swap a[k] with a[child1]  */ help = a[k]; a[k] = a[child1]; a[child1] = help; k = child1; // Replacement node is a[child1] // in next iteration } else { /*  Child2 is smaller: swap a[k] with a[child2]  */ help = a[k]; a[k] = a[child2]; a[child2] = help; k = child2; // Replacement node is a[child2] // in next iteration } } } else { /* ======================================== Node k only has a left child node Find the min. of 2 nodes !!! ======================================== */ if ( a[k] < a[child1] ) { /*  Node k is in correct location... It's a heap. Stop...  */ break; } else { /*  Child1 is smaller: swap a[k] with a[child1]  */ help = a[k]; a[k] = a[child1]; a[child1] = help; k = child1; // Replacement node is a[child1] // in next iteration } } } } 
How to run the program:



Example:

The same argument can be apply to show that the maximum number of times that a nodes can move up the tree is at most the height of the tree.
Running time( delete(n) ) = Running time heap delete in heap with n node = height of a heap of n nodes 
Let: n = # nodes in heap of height h 2^{h}  1 < n ≤ 2^{h+1} − 1 <===> 2^{h} < n + 1 ≤ 2^{h+1} <===> h < lg(n + 1) ≤ h+1 ===> height of heap ~= lg(n + 1) 

