예제 #1
0
class TestMinBinaryHeap(unittest.TestCase):
    def setUp(self):
        self.keys = [2,3,4]
        self.values = ["b", "d", "c"]
        self.heap = MinBinaryHeap(self.keys, self.values)

    def insert(self):
        self.heap.insert(1, "a")

    def pop(self):
        self.heap.pop()

    def test_ensure_on_min_heap(self):
        heap = MinBinaryHeap(self.keys, self.values)
        self.assertEqual(self.heap._keys, [None] + self.keys)
        self.assertEqual(self.heap._values, [None] + self.values)

    def test_ensure_on_improper_min_heap(self):
        heap = MinBinaryHeap([3,2,4], ["d", "b", "c"])
        self.assertEqual(self.heap._keys, [None] + self.keys)
        self.assertEqual(self.heap._values, [None] + self.values)

    def test_insert(self):
        self.insert()
        self.assertEqual(self.heap._keys, [None, 1,2,4,3])
        self.assertEqual(self.heap._values, [None, "a", "b", "c", "d"])

    def test_pop(self):
        self.insert()
        self.assertEqual(self.heap._keys, [None, 1,2,4,3])
        self.assertEqual(self.heap._values, [None, "a", "b", "c", "d"])

        self.pop()
        self.assertEqual(self.heap._keys, [None, 2, 3, 4])
        self.assertEqual(self.heap._values, [None, "b", "d", "c"])
예제 #2
0
class PriorityQueue:
  
    def __init__(self):
        self.queue = MinBinaryHeap()
    
    # O(log n)
    def enqueue(self, item):
        self.queue.insert(item)
  
    # O(log n)
    def dequeue(self):
        return self.queue.del_min()
  
    def is_empty(self):
        return self.queue.is_empty()
  
    def size(self):
        return self.queue.size
예제 #3
0
 def setUp(self):
     self.keys = [2,3,4]
     self.values = ["b", "d", "c"]
     self.heap = MinBinaryHeap(self.keys, self.values)
예제 #4
0
    def size(self):
        return len(self.stack)
    
    def is_empty(self):
        return len(self.stack) == 0
    
    # allow us to compare Stacks
    # assume other is a Stack
    def __lt__(self, other):
        # has to return True or False
        if self.size() < other.size():
            return True
        else:
            return False
        
    # __gt__ for >
    # __ge__ for >=
    # __eq__ for ==
    # __ne__ for !=

heap = MinBinaryHeap()
heap.insert(Stack())
heap.insert(Stack())

stack1 = Stack()
stack1.push(1)
stack1.push(2)
stack2 = Stack()
stack2.push(3)
print(stack1 < stack2)
print(stack1 > stack2)
예제 #5
0
 def __init__(self):
     self.queue = MinBinaryHeap()