heap - Heaps (Priority Queues) A heap is a data structure...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Heaps (Priority Queues) A heap is a data structure which supports efficient implementation of three operations: insert, findMin, and deleteMin. (Such a heap is a min-heap, to be more precise.) Two obvious implementations are based on an array (assuming the size n is known) and on a linked list. The comparisons of their relative time complexity are as follows: insert findMin deleteMin Sorted array O( n ) O(1) O(1) (from large (to move (find at end) (delete the end) to small) elements) Linked list O(1) O(1) O( n ) (append) (a pointer to Min) (reset Min pointer)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A simpler but related problem: Find the smallest and the second smallest elements in an array (of size n ): A straightforward method: Find the smallest using n –1 comparisons; swap it to the end of the array, find the next smallest in n –2 comparisons, for a total of 2 n – 3 comparisons. A method that “saves” results of some earlier comparisons: (1) Divide the array into two halves, find the smallest elements of each half, call them min1 and min2, respectively; (2) Compare min1 and min2, to find the smallest; (3) If min1 is the smallest, compare min2 with the remaining elements of the first half to determine the second smallest; similarly for the case if min2 is the smallest. The number of comparisons is ( n –1) + ( n /2 –1) = ( 3 n /2 2.
Background image of page 2
The second method can be depicted in the following figure: min1 min2 min First half of array Second half of array In the figure, a link connects a smaller element to a larger one going downward; for example, min min1 and min min2, and min1 each element in the first half of the array, similarly for min2. This organization facilitates finding the smallest and second smallest elements. H 1
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A binary tree data structure for Min-heaps: A binary tree is called a left-complete binary tree if (1) the tree is full at each level except possibly at the maximum level (depth), where the tree is full at level i means there are 2 i nodes at that level (recall the root is at level 0); and (2) at the tree’s maximum level h , if there are fewer than 2 h nodes then the missing nodes are on the right side
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/13/2011.

Page1 / 17

heap - Heaps (Priority Queues) A heap is a data structure...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online