Deleting A Node (At A Specific Location) From A Heap: Problem Description: You Are Given A - For Example
Deleting A Node (At A Specific Location) From A Heap: Problem Description: You Are Given A - For Example
Deleting A Node (At A Specific Location) From A Heap: Problem Description: You Are Given A - For Example
html
Problem description:
For example:
For example: k = 2
Problem:
Delete the value a[k] from the heap (so that the resulting tree is also a heap !!!)
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 1/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
$64,000 question:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 2/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
Example:
Delete the node containing the value 5 from the following heap:
After you delete the node, the tree is not a complete binary tree:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 3/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
Step 1: replace the deleted node with the node in the "fartest right location" of the lowest level
Result:
We must fix the binary tree so that it becomes a heap again !!!
Since you have already seen the filter up algorithm --- in the heap insert algorithm --- I made the
example to show you the filter down algorithm now
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 4/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
Step 2: because the replacement node 21 is greater than its parent node (1), we must filter the
replacement node down the tree
Compare the values of the replacement node with all its children nodes in the tree:
Some child node has a smaller value: the replacement node is not in its proper
location
Swap the replacement node with the smallest of the children nodes:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 5/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
After swapping:
Repeat !
Compare the values of the replacement node (21) with all its children nodes in the
tree:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 6/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
Some child node has a smaller value: the replacement node is not in its proper
location
Swap the replacement node with the smallest of the children nodes:
After swapping:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 7/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
Repeat !
The replacement node (21) does not have any children node:
Done !!!
Warning:
Sometimes, you have to filter the replacement node up the Binary tree !!!!!
Example:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 8/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
Step 1: replace the deleted node with the "last" node in the lowest level (to make a complete
binary tree)
Result:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 9/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
In this case, the replacement node must be filtered up into the binary tree:
Result:
Conclusion:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 10/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
a[k] = a[NNodes]; // Replace deleted node with the right most leaf
// This fixes the "complete bin. tree" property
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
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 11/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
/* ========================================================
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;
Before we can perform a running time analysis, we must first understand how the algorithm works exactly...
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 13/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
Then the HeapFilterUp() or the HeapFilterDown() algorithm will migrate the replacement node
up or down into the binary tree:
and:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 14/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
Fact:
The number of times that the deleted node will migrate up or down into the binary tree is:
Example:
The maximum number of times that replacement node can move down in a binary tree:
is at most 3 times:
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.
Therefore:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 15/16
7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html
Running time( delete(n) ) = Running time heap delete in heap with n node
2h - 1 < n 2h+1 1
<===> 2h < n + 1 2h+1
<===> h < lg(n + 1) h+1
Summary:
Conclusion:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 16/16