Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "308-203A Introduction to Computing II Lecture 10: Heaps Fall Session 2000."— Presentation transcript:

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

2 Motivation Data structures supporting extraction of max element are 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)

3 Heap Extract-Max() Insert(object, key)

4 How could we do this? Sorted list: but recall that Insertion-Sort was suboptimal Binary tree: but binary trees can become unbalanced and costly A more clever way: a special case of binary trees…

5 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) = 2i Right-child(i) = 2i + 1

6 Example (as array) 1234567891011121314

7 Example (as tree) 1 23 4567 891011121314

8 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

9 The Heap Property For any node X with parent PARENT(X): PARENT(X).key > X.key Compare to the Binary Search Tree Property from last lecture: this is a much weaker condition

10 Example 16 1410 8793 241

11 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); }

12 Heap-Insert: Example 16 1410 8793 24115 swap

13 Heap-Insert: Example 16 1410 893 241 15 swap 7

14 Heap-Insert: Example 16 10 893 2417 14 15 Done (15 < 16)

15 Helper routine: Heapify Given a node X, where X’s children are heaps, guarantee the heap property for the ensemble of X and it’s children X Left heapRight heap

16 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); else swap(A[i], A[l]); Heapify(A, l); }

17 Heapify: example 6 1410 8793 241 HEAP PROPERTY VIOLATED

18 Heapify: example 10 8793 241 Swap with max(14, 6, 10) 14 6

19 Heapify: example 10 793 241 Swap with max(8, 6, 7) 14 6 8

20 Heapify: example 10 793 241 Done: 6 = max(2, 6, 4) 14 6 8

21 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; }

22 Heap-Extract-Max: example 16 1410 8793 241 returnValue = 16

23 Heap-Extract-Max: example 1410 8793 24 1 Replace with A[n]

24 Heap-Extract-Max: example 10 793 21 Heapify from top 14 8 4

25 Order of Growth Heap-insert Heapify Heap-Extract-Max O( log n ) All three, proportional to height of tree:

26 Other useful operations Build-Heap: convert an unordered array into a heap O( n ) Heap-Sort: sort by removing elements from the heap until it is empty O( n log n )

27 Build-Heap Build-Heap(A[1..n]) { for i :=  n/2  downto 1 Heapify(A, i); }

28 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; }

29 Any questions?


Download ppt "308-203A Introduction to Computing II Lecture 10: Heaps Fall Session 2000."

Similar presentations


Ads by Google