UJS: Priority Queues

In [1]:
# make a node
# make a Priority Queue class
# use them to answer Pat's question

Making the Priority Queue

The Skeleton

In [60]:
class Node:
    def __init__(self, val, priority):
        self.val = val
        self.priority = priority
        
#     def __str__(self):
#         return [self.val, self.priority]
#         return str(self.val)

class PriorityQueue:
    def __init__(self):
        self.values = []
        
    def insert(self, val, priority):
        new_node = Node(val, priority)
        self.values.append(new_node)
        self.bubbleUp()
        
    def extractMin(self):
        min_val = self.values.pop()
        self.sinkDown()
        
    def bubbleUp(self):
        print('bubbling up')
        
    def sinkDown(self):
        print('sink down')
        
    def display(self):
#         print(self.values)
        for i in self.values:
            print(i.val)
In [61]:
myPQ = PriorityQueue()
myPQ.insert('walk cats', 2)
bubbling up
In [62]:
myPQ.insert('walk turtle', 3)
myPQ.insert('brush teeth', 1)
bubbling up
bubbling up
In [63]:
myPQ.display()
walk cats
walk turtle
brush teeth
In [ ]:
 
In [ ]: