Binary Heaps and Heapsort, Exams of Data Structures and Algorithms

Describe and use the Heapify (“Build Heap”) and. Heapsort algorithms. • Analyze the complexity of operations on a heap. Page 3. CPSC 259.

Typology: Exams

2022/2023

Uploaded on 03/01/2023

ekanaaa
ekanaaa 🇺🇸

4.3

(28)

268 documents

1 / 70

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPSC 259 Binary Heaps and Heapsort Page 1
CPSC 259: Data Structures and Algorithms
for Electrical Engineers
Binary Heaps and Heapsort
Textbook Reference:
Thareja first edition: Chapter 12, pages 501-506
Thareja second edition: Chapter 12, pages 361-365
Hassan Khosravi
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46

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