queue

package module
v0.0.0-...-4f58802 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2022 License: MIT Imports: 1 Imported by: 0

README

Queues and Heaps

Queues

  • its an ADT
  • it provides a behavior, not an implementation
  • collection of entities kept in a sequence
  • can be modified:
    • enqueue-ing - adding at the back of the queue
    • dequeue-ing - removing from the front of the queue
  • it's FIFO - first in, first out
  • Queues are used everywhere where there is a need for entities to be stored to be processed later

PriorityQueue

  • we will implement a Priority Queue
    • it's a Queue where each element has an additional "priority" property:
      • in a standard queue, elements exit the queue in the same order they arrived.
      • in a Priority Queue - the element with higher prio is returned before one with lower prio regardless of order of entry in queue.
      • we will use the Heap Data Structure to implement the Priority Queue

Heaps

  • a tree data structure which must satisfy the Heap property:

    • what is the Heap Property: it is based on the type of heap.
      • there are 2 types of Heaps: max-heaps, min-heaps.
      • max-heaps: the root node (the node in the tree without a parent) always has the highest value in the tree, and each node must have a value greater than the value of its children.
      • min-heaps: the root node (the node in the tree without a parent) always has the smallest value in the tree, and each node must have a value smaller than the value of its children.
      • when are heaps useful? When you need to repeatedly access or remove the object with the highest value (for max-heaps) or lowest value (for min-heaps).
  • then we will implement our own Priority Queue in a system that simulates an Emergency Room in a hospital, where patients are handled in the order of how severe their injury is, instead of FIFO.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EmergencyRoom

type EmergencyRoom struct {
	// contains filtered or unexported fields
}

func NewEmergencyRoom

func NewEmergencyRoom() *EmergencyRoom

func (*EmergencyRoom) AdmitPatient

func (er *EmergencyRoom) AdmitPatient(patient interface{})

AdmitPatient adds new patients in ER's priority queue

func (*EmergencyRoom) HandleNextPatient

func (er *EmergencyRoom) HandleNextPatient() *Patient

HandleNextPatient remove highest prio from the patients queue

func (*EmergencyRoom) UpdatePatientStatus

func (er *EmergencyRoom) UpdatePatientStatus(patient *Patient, newStatus SeverityStatus)

type Patient

type Patient struct {
	// contains filtered or unexported fields
}

type PatientsQueue

type PatientsQueue []*Patient

func (*PatientsQueue) Len

func (pq *PatientsQueue) Len() int

Len required by sort.Interface

func (PatientsQueue) Less

func (pq PatientsQueue) Less(a, b int) bool

Less required by the sort.Interface we flip the comparer (to greater than) because we need the comparer to sort by highest prio, not lowest

func (*PatientsQueue) Pop

func (pq *PatientsQueue) Pop() interface{}

Pop required by the heap.Interface

func (*PatientsQueue) Push

func (pq *PatientsQueue) Push(patientData interface{})

Push required by the heap.Interface

func (PatientsQueue) Swap

func (pq PatientsQueue) Swap(a, b int)

Swap required by the sort.Interface

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

type SeverityStatus

type SeverityStatus int
const (
	Mild SeverityStatus = iota
	Moderate
	Critical
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL