Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Priority Queues (Heaps) in Computing: Operations and Implementation, Schemes and Mind Maps of Object Oriented Programming

A review of priority queues or heaps, focusing on their operations, running times, and implementation using an array or vector. It covers insertion and deletion processes, traversal, and the use of STL's priority_queue.

What you will learn

  • What is a priority queue (heap) and how does it work?
  • What operations are supported in a priority queue (heap)?
  • What is the running time for inserting an element into a heap?
  • How is a heap implemented using an array or vector?
  • What is the running time for deleting the minimum element from a heap?

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 09/12/2022

ekansh
ekansh 🇺🇸

4.3

(20)

266 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNDER THE HOOD OF
PRIORITY QUEUES (HEAPS)
Problem Solving with Computers-II
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Understanding Priority Queues (Heaps) in Computing: Operations and Implementation and more Schemes and Mind Maps Object Oriented Programming in PDF only on Docsity!

UNDER THE HOOD OF

PRIORITY QUEUES (HEAPS)

Problem Solving with Computers-II

Priority Queues or Heaps (Review)

  • Clarification
    • heap , the data structure is not related to heap, the region of memory
  • What are the operations supported?
  • What are the running times?

Insert 50, then 35, then 8 6 10 40 12 32 43 47 45 41

Delete min

**- Replace the root with the rightmost node at the last level

  • “Bubble down”- swap node with one of the children until the heap**

property is restored

6 10 40

32 12 47 45 41 50 35 43

Insert into a heap

**- Insert key(x) in the first open slot at the last level of tree (going from left to right)

  • If the heap property is not violated - Done
  • Else….**

Insert the elements {12, 41, 47, 45, 32} in a min-Heap using the vector

representation of the heap

6 10 40 12 32 43 47 45 41

For a node at index i, index of the parent is int(i/2)

Value 6 10 12 40 32 43 47 45 41 Index 0 1 2 3 4 5 6 7 8 Traversing up the “tree”

Insert 8 into a heap Value 6 10 12 40 32 43 47 45 41 50 35 Index 0 1 2 3 4 5 6 7 8 9 10 After inserting 8, which node is the parent of 8? A. Node 6 B. Node 12 C. None 43 D. None - Node 8 will be the root

Delete min

**- Replace the root with the rightmost node at the last level

  • “Bubble down”- swap node with one of the children until the heap**

property is restored

6 10 40

32 12 47 45 41 50 35 43

std::priority_queue (STL’s version of heap)

A C++ priority_queue is a generic container, and can store any data type

on which an ordering can be defined: for example ints, structs (Card),

pointers etc.

#include

priority_queue pq;

Methods:

* push() //insert

* pop() //delete max priority item

* top() //get max priority item

* empty() //returns true if the priority queue is empty

  • (^) You can extract object of highest priority in O(log N)
  • (^) To determine priority: objects in a priority queue must be comparable to each other ! 13

Next lecture

* More on the STL priority_queue

* Applications of heaps and priority queues

! 14