a3solutions

A3solutions - class pq_item Fields priority a number and item the value assigned this priority def_init(self priority item self.priority = priority

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
class pq_item: """ Fields: priority - a number and item - the value assigned this priority """ def __init__(self, priority, item): self.priority = priority self.item = item def __str__(self): return "<" + str(self.priority) + "," + str(self.item) + ">" class pq_heap: """ Field: L - a list representing a heap of prioritized items Note: L[0] is not used. Valid indices in L are between 1 and the number of items in the priority queue (inclusive). """ ## __init__: pq_heap -> pq_heap ## Produces an empty priority queue def __init__(self): self.L = ["not used"] ##__str__: pq_heap -> string ## Produces a string representing the list of prioritized items. def __str__(self): s = "" for i in self.L[1:]: s = s + str(i) + "," # inefficient if L large return "[" + s[0:len(s)-1] + "]" ## add: pq_heap pq_item -> None ## Consumes a priority queue and adds pq_item x to it. ## Assumes no existing item in the priority queue ## has the same priority as x. def add(self, x):
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/27/2011 for the course CS 234 taught by Professor Baranoski during the Fall '09 term at Waterloo.

Page1 / 3

A3solutions - class pq_item Fields priority a number and item the value assigned this priority def_init(self priority item self.priority = priority

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online