{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

a3solutions - class pq_item Fields priority a number and...

Info icon This 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): self.L.append(x) # make space node = len(self.L)-1 # bubble up while necessary while(node > 1 and x.priority > self.L[node / 2].priority ): temp = self.L[node] self.L[node] = self.L[node / 2] self.L[node / 2] = temp node = node / 2
Image of page 1

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern