1 views

Original Title: Heap

Uploaded by Nirmal

- American Computer Science League (ACSL) Rules and Handbook
- Programming in C
- DATA STRUCTURE ,C,C++
- MCA 2015
- BFS and DFS
- technical Aptitude Questions eBook
- Technical Aptitude Questions eBook
- MELJUN_CORTES_JEDI Slides Data Structures Chapter05 Trees
- Testing 7
- CPE311 Trees
- Hydra UserManual
- Merge Sort
- DataStructure - Heap
- 07a1ec04 Computer Programming
- Smart Forms
- DS
- Adjusting Heaps
- Algorithms-interview-questions.pdf
- bitstream_1668140.pdf
- Segment Trees

You are on page 1of 16

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

(this creates a "hole" and the tree is no longer "complete")

with the "fartest right node" on the lowest level

of the Binary Tree

(This step makes the tree into a "complete binary tree")

Filter the replacement node UP the binary tree

else

Filter the replacement node DOWN the binary tree

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 !!!

replacement node upwards or downwards

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:

filter the replacement node up the tree

else

filter the replacement node down the tree

{

int parent;

double r; // Variable to hold deleted value

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

}

{

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 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....

}

}

}

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;

{

child1 = 2*k; // Child1 = left child of k

child2 = 2*k+1; // Child2 = right child of k

{

/* ========================================

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;

// in next iteration

}

else

{

/* -------------------------------------------------------

Child2 is smaller: swap a[k] with a[child2]

------------------------------------------------------- */

help = a[k];

a[k] = a[child2];

a[child2] = help;

// 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

{

/* -------------------------------------------------------

http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html 12/16

7/30/2017 www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/heap-delete.html

------------------------------------------------------- */

help = a[k];

a[k] = a[child1];

a[child1] = help;

// in next iteration

}

}

}

}

The test Prog file: click here

To run: java testProg2

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

- American Computer Science League (ACSL) Rules and HandbookUploaded bybellataco
- Programming in CUploaded byPriya Sharma
- DATA STRUCTURE ,C,C++Uploaded byBhanu K Prakash
- MCA 2015Uploaded byPardeep Patel
- BFS and DFSUploaded byRajendranbehappy
- technical Aptitude Questions eBookUploaded byreqtech
- Technical Aptitude Questions eBookUploaded byGayathri Suresh
- MELJUN_CORTES_JEDI Slides Data Structures Chapter05 TreesUploaded byMELJUN CORTES, MBA,MPA
- Testing 7Uploaded bydoomachaley
- CPE311 TreesUploaded byJaspher Jan Pasol
- Hydra UserManualUploaded byjose osvaldo martins
- Merge SortUploaded bysubbulakshmiv02
- DataStructure - HeapUploaded byaliyaraza1
- 07a1ec04 Computer ProgrammingUploaded byandhracolleges
- Smart FormsUploaded byjvargas85
- DSUploaded bynisha02t
- Adjusting HeapsUploaded byravikumarsid2990
- Algorithms-interview-questions.pdfUploaded bykasim
- bitstream_1668140.pdfUploaded byyekych
- Segment TreesUploaded bythextroid
- Data Structure in cUploaded byad_prakash2010
- Technical Aptitude Questions eBookUploaded bySanyam Rastogi
- 01290463Uploaded byIrtiqa Saleem
- algoUploaded byscaliwara
- gudlecture11Uploaded bysagargupta012
- jeas_0715_2273.pdfUploaded bypugal4
- Linked ListUploaded byBicky Debnath
- csent50Uploaded bysheger
- Imp Interview QqUploaded byMalli Ravi
- notes2Uploaded byBilal Hameed

- DBMS_L23Uploaded byVidushiDadu
- kdtreeUploaded byThuan Vo
- DSlabmanual (1)Uploaded byPradeep Pandey
- Splay TreesUploaded byEssirahc Lamigo
- Ch6_Binary Search TreesUploaded byrupinder18
- Discrete Math- TreeUploaded byLaksana Dewa
- C Program Day-19Uploaded byeshamu
- AVL TREEUploaded byAnand Yadav
- Chapter 4 TreesUploaded byLogaNathan BalasuBaramaniam
- Unit III part 4.pptxUploaded byJayakumar Loganathan
- AvlUploaded byKashif Amjad
- 2nd unit - 2mUploaded byPushpa Ramakrishnan Iyer
- Cs301 Fall 2009 Final TermUploaded byvuallstudymaterial
- Threaded Binary TreeUploaded byLn Amitav Biswas
- Binary Search TreeUploaded bySandeep Sahu
- L9treeUploaded bymanjeet.kamboj13
- CS301SolvedMCQsUploaded byMuhammadRizwanIslamKhan
- Heap SortUploaded byHande Koru
- AVL Trees - Horowitz Sahni CPP - Lec43Uploaded byhailos
- Assignment DiscreatemathsUploaded byParameshwar Reddy Kottapalli
- dsUploaded byRaghu Varma
- BCA2060 Exercise 5-SLMUploaded byaryanboxer786
- CSC263 Spring 2011 Assignment 2Uploaded byyellowmoog
- Elementary AlgorithmsUploaded byAditya Paliwal
- Leftist Skew Heaps.pptUploaded byRevathy
- DS GFGUploaded bychantaiah
- 17-TreeFundamentalsUploaded byRishav Kanth
- B-treeUploaded byRitwik Kumar
- M WAY TREES PDFUploaded bymush76here
- 5. Binary Search TreesUploaded byAhmed Hosni