Heaps, Heapsort and Priority Queues - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Heaps, Heapsort and Priority Queues

Description:

A binary tree satisfying heap property: data in ... Trickle down to readjust heap. HeapSort ... Readjust tree (move last key to root, trickle down) in O(log n) ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 11
Provided by: scie350
Category:

less

Transcript and Presenter's Notes

Title: Heaps, Heapsort and Priority Queues


1
Lecture 8
  • Heaps, Heapsort and Priority Queues

2
Heap
  • A binary tree satisfying heap property data in
    parent larger than data in children. Examples.
  • Implemented efficiently as an array fast access
    time. Space n. Access O(1).

3
Heap
  • Data at parent is larger than data at children
  • Tree is binary, balanced, filled left

4
Operations on Heaps
  • Insert/remove element from heap trickle up/down.
  • Time height of tree O(log n)

5
Delete Max
  • Remove root, replace with last element.

6
  • Trickle down to readjust heap

7
HeapSort
  • Heapsort Variation on Selection Sort, with
    maximum computed efficiently at each step using a
    heap.
  • Step 1 create a heap. In O(n) time.
  • Then iterate as in Selection Sort. Remove root
    (max). Readjust tree (move last key to root,
    trickle down) in O(log n). Continue until all are
    removed (in sorted order).
  • Time O(n log n) because removing/readjusting
    takes log n.

8
Analysis
  • Create a heap insert an element O(log n). Total
    O(n log n).
  • Analysis can be improved to O(n) time (read
    handout, ch. 7.3) start with elements in the
    arary, heapify for each element. Use nr of
    elements at height h, lt n/2h1
  • Total time S h n/2h1 O(n)

9
Applications Priority Queues
  • Priority Queues data structure to allow for
    efficient finding of the minimum/maximum, after
    initial preprocessing, and maintain it in O(log
    n) time. Done with heaps.
  • Used in Operating Systems, to maintain priority
    queues of processes.

10
Homework
  • From handout, ex. 7.1-1,2,3,4,5,6.
  • 7.2-1,2
  • 7.3-1,3
  • 7.4-1,2,3.
  • 7.5-1,2
  • Extra credit 7.5-6
Write a Comment
User Comments (0)
About PowerShow.com