lecture-15

Trickle up the new root a if items search key is

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // start with lei child if(child < size) { int rightChild = child + 1; if(rightChild < size && items[rightChild] > items[child]) child = rightChild; // set child to larger right child if(items[root] < items[child]) { swap(item[root], item[child]); heapRebuild(child); size = 5 } items = 9 5 8 4 2 4 } 0 CPSC 223  ­ ­ Fall 2010 1 2 3 4 5 6 30 15 10/21/10 Array-Based Heap Implementation Cost of deletion •  How many comparisons and swaps? •  Worst case is when we trickle down to a leaf node –  There are O(log n) such trickle-down steps … why? •  At each step we have –  2 (item) comparisons and 1 swap –  Therefore, each trickle-down step is O(1) •  Deletion is worst case O(log n) CPSC 223  ­ ­ Fall 2010 3...
View Full Document

Ask a homework question - tutors are online