Learn R Programming

collections (version 0.1.6)

PriorityQueue: Priority Queue

Description

The PriorityQueue class creates a priority queue (a.k.a heap).

Usage

PriorityQueue

Format

An object of class R6ClassGenerator of length 24.

Usage

PriorityQueue$new(items = NULL, priorities = NULL)
PriorityQueue$push(item, priority = 0)
PriorityQueue$pop()
PriorityQueue$clear()
PriorityQueue$size()
PriorityQueue$as_list()

Argument

  • items: initialization list

  • priorities: a vector of priorities of the same length of items

  • item: any R object

  • priority: a real number, item with larger priority pops first

Examples

Run this code
# NOT RUN {
q <- PriorityQueue$new()
q$push("not_urgent")
q$push("urgent", priority = 2)
q$push("not_as_urgent", priority = 1)
q$pop()  # urgent
q$pop()  # not_as_urgent
q$pop()  # not_urgent

q <- PriorityQueue$new(list("not_urgent", "urgent"), c(0, 2))
q$push("not_as_urgent", 1)$push("not_urgent2")
# }

Run the code above in your browser using DataLab