29
308-203A Introduction to Computing II Lecture 10: Heaps Fall Session 2000

308-203A Introduction to Computing II Lecture 10: Heaps Fall Session 2000

Embed Size (px)

Citation preview

308-203AIntroduction to Computing II

Lecture 10: Heaps

Fall Session 2000

Motivation

Data structures supporting extraction of max elementare quite useful, for example:

• Priority queues - list of tasks to perform with priority (always take highest priority task)

• Event simulators, e.g. video games(always simulate the nearest event in the future)

Heap

Heap

Extract-Max()

Insert(object, key)

How could we do this?

• Sorted list: but recall that Insertion-Sortwas suboptimal

• Binary tree: but binary trees can becomeunbalanced and costly

• A more clever way: a special case of binary trees…

Arrays as Binary Trees

Take an array of n elements: A[1..n]

For an index i[1 .. n] define:

Parent(i) = i/2

Left-child(i) = 2iRight-child(i) = 2i + 1

Example (as array)

1 2 3 4 5 6 7 8 9 10 11 12 13 14

Example (as tree)

1

2 3

4 5 6 7

8 9 10 11 12 13 14

Facts

• These trees are always balanced

• Easy to find next leaf to add (element n+1 in the array)

• Not as flexible as trees built with pointers

The Heap Property

For any node X with parent PARENT(X):

PARENT(X).key > X.key

Compare to the Binary Search Tree Property fromlast lecture: this is a much weaker condition

Example

16

14 10

8 7 9 3

2 4 1

Heap-Insert

Heap-Insert(A[1..n], k){ A.length ++; A[n+1] = k;

j = n+1; while (j 1 and A[j] > A[PARENT(j)] {

swap(A, j, PARENT(j));j = PARENT(j);

}}

Heap-Insert: Example

16

14 10

8 7 9 3

2 4 1 15

swap

Heap-Insert: Example

16

14 10

8 9 3

2 4 1

15

swap

7

Heap-Insert: Example

16

10

8 9 3

2 4 1 7

14

15

Done (15 < 16)

Helper routine: Heapify

Given a node X, where X’s children are heaps,guarantee the heap property for the ensemble ofX and it’s children

X

Left heap Right heap

Heapify

Heapify(A[1..n], i){ l := Left(i); r := Right(i); if (A[i] > A[l] and A[i] > A[r] ) return; if (A[l] > A[r])

swap(A[i], A[l]);Heapify(A, l);

elseswap(A[i], A[l]);Heapify(A, l);

}

Heapify: example

6

14 10

8 7 9 3

2 4 1

HEAP PROPERTY VIOLATED

Heapify: example

10

8 7 9 3

2 4 1

Swap with max(14, 6, 10)

14

6

Heapify: example

10

7 9 3

2 4 1

Swap with max(8, 6, 7)

14

6

8

Heapify: example

10

7 9 3

2 4 1

Done: 6 = max(2, 6, 4)

14

6

8

Heap-Extract-Max

Heap-Extract-Max(A[1..n]){ returnValue = A[1];

A[1] = A[n]; heap.length --;

Heapify(A[1..(n-1)], 1);

return returnValue;}

Heap-Extract-Max: example

16

14 10

8 7 9 3

2 4 1

returnValue = 16

Heap-Extract-Max: example

14 10

8 7 9 3

2 4

1Replace with A[n]

Heap-Extract-Max: example

10

7 9 3

2 1

Heapify from top 14

8

4

Order of Growth

Heap-insertHeapifyHeap-Extract-Max

O( log n )O( log n )O( log n )

All three, proportional to height of tree:

Other useful operations

• Build-Heap: convert an unordered array into a heapO( n )

• Heap-Sort: sort by removing elements from the heapuntil it is empty

O( n log n )

Build-Heap

Build-Heap(A[1..n]){ for i := n/2 downto 1

Heapify(A, i);}

Heap-Sort

Heap-Sort(A[1..n]){ result = new array[1..n];

for i := n downto 1

result[i] = Heap-Extract-Max (A[1..i]);

return result;}

Any questions?