Docsity

Binary Heaps and Heapsort, Exams of Data Structures and Algorithms

Partial preview of the text

Download Binary Heaps and Heapsort and more Exams Data Structures and Algorithms in PDF only on Docsity!

CPSC 259: Data Structures and Algorithms

for Electrical Engineers

Binary Heaps and Heapsort

Textbook Reference: Thareja first edition: Chapter 12, pages 501- Thareja second edition: Chapter 12, pages 361- Hassan Khosravi

Learning Goals

  • Provide examples of appropriate applications for priority queues and heaps.
  • Implement and manipulate a heap using an array as the underlying data structure.
  • Describe and use the Heapify (“Build Heap”) and Heapsort algorithms.
  • Analyze the complexity of operations on a heap.

CPSC 259 Binary Trees Page 4 Abstract Data Types Data Structures Stack Queue Circular Array Array Linked list Tools Asymptotic Analysis

CPSC 259 Journey

Recursion Algorithms Dictionary Binary Search Tree Pointers Dynamic Memory Alloca2on Priority Queue Binary Heap Heapsort

Priority Queues

  • Let’s say we have the following tasks. 2 - Water plants 5 - Order cleaning supplies 1 - Clean coffee maker 3 - Empty trash 9 - Fix overflowing sink 2 - Shampoo carpets 4 - Replace light bulb 1 - Remove pencil sharpener shavings We are interested in finding the task with the highest priority quickly.

Priority Queue ADT

  • Priority Queue operations
    • create
    • destroy
    • insert
    • deleteMin
    • isEmpty
  • Priority Queue property: for two elements in the queue, x and y , if x has a lower priority value than y , x will be deleted before y. F(7) E(5) D(100) A(4) B(6) insert deleteMin G(9) C(3)

Heaps and Priority Queues

  • A priority queue is an abstract data type (ADT) that maintains a bag of items. - What is the difference between a set and a bag? - A set does not contain duplicates.
  • Two or more distinct items in a priority queue can have the same priority. If all items have the same priority, you might think a priority queue should behave like a queue, but it may not. We’ll see why, shortly.
  • A binary heap can implement a priority queue, efficiently.

Naïve Priority Q Data Structures

  • Let’s use an unsorted list (could be implemented with either an Array or Linked List)
  • Running time of insert?

a. O(1)

b. O(lg n)

c. O(n)

d. O(n lg n)

e. Something else

Naïve Priority Q Data Structures

  • Let’s use an unsorted list (could be implemented with either an Array or Linked List)
  • Running time of insert?

a. O(1)

b. O(lg n)

c. O(n)

d. O(n lg n)

e. Something else

Naïve Priority Q Data Structures

  • Let’s use an unsorted list (could be implemented with either an Array or Linked List)
  • Running time of deleteMin?

a. O(1)

b. O(lg n)

c. O(n)

d. O(n lg n)

e. Something else

Naïve Priority Q Data Structures

  • Let’s use a sorted list (could be implemented with either an Array or Linked List)
  • Running time of insert?

a. O(1)

b. O(lg n)

c. O(n)

d. O(n lg n)

e. Something else

Naïve Priority Q Data Structures

  • Let’s use a sorted list (could be implemented with either an Array or Linked List)
  • Running time of deleteMin?

a. O(1)

b. O(lg n)

c. O(n)

d. O(n lg n)

e. Something else

Naïve Priority Q Data Structures

  • Let’s use a sorted list (could be implemented with either an Array or Linked List)
  • Running time of deleteMin?

a. O(1)

b. O(lg n)

c. O(n)

d. O(n lg n)

e. Something else

Naïve Priority Q Data Structures

  • Let’s use a Binary Search Tree (could be implemented with either an Array or Linked List)
  • Worst case running time of insert?

a. O(1)

b. O(lg n)

c. O(n)

d. O(n lg n)

e. Something else

Naïve Priority Q Data Structures

  • Let’s use a Binary Search Tree (could be implemented with either an Array or Linked List)
  • Worst case running time of deleteMin?

a. O(1)

b. O(lg n)

c. O(n)

d. O(n lg n)

e. Something else