Exemple #1
0
 def test_peek_at_smallest(self):
     numbers = [11, 322, 3, 199, 29, 7, 1, 18, 76, 4, 2, 47, 123]
     h = MinHeap(numbers)
     self.assertEqual(h.peek(), 1)
     self.assertEqual(len(h), len(numbers))
     self.assertEqual(len(numbers), 13)
     i = MinHeap(BIG_NUMBERS)
     self.assertEqual(i.peek(), 46)
     self.assertEqual(len(i), len(BIG_NUMBERS))
Exemple #2
0
 def test_key_function(self):
     fruits = ['Watermelon', 'blueberry', 'lime', 'Lemon', 'pear', 'loquat']
     fruits_heap = MinHeap(fruits, key=str.lower)
     self.assertEqual(fruits_heap.peek(), 'blueberry')
     fruits_heap.push('Apple')
     fruits_heap.push('jujube')
     self.assertEqual(fruits_heap.pop(), 'Apple')
     self.assertEqual(fruits_heap.pop(), 'blueberry')
     self.assertEqual(fruits_heap.pop(), 'jujube')
     self.assertEqual(fruits_heap.pop(), 'Lemon')
Exemple #3
0
 def test_push_onto_heap(self):
     numbers = [11, 322, 3, 199, 29, 7, 1, 18, 76, 4, 2, 47, 123]
     i = MinHeap(BIG_NUMBERS)
     i.push(17)
     self.assertEqual(i.peek(), 17)
     i.push(24)
     self.assertEqual(i.pop(), 17)
     self.assertEqual(i.pop(), 24)
     self.assertEqual(i.pop(), 46)
     h = MinHeap(numbers)
     h.push(6)
     self.assertEqual(len(h), len(numbers)+1)
     self.assertEqual(h.pop(), 1)
     self.assertEqual(h.pop(), 2)
     self.assertEqual(h.pop(), 3)
     self.assertEqual(h.pop(), 4)
     self.assertEqual(h.pop(), 6)
Exemple #4
0
class MinHeapTest(unittest.TestCase):

	@classmethod
	def setUpClass(self):
		self.H = MinHeap()
		self.data = [('A',7),('B',3),('C',4),('D',1),('E',6),('F',5),('G',2)]

	@classmethod
	def tearDownClass(self):
		self.H = None

	def check_assertions(self,heap_val,data_val,size_val):
		self.assertEqual(self.H.heap,heap_val,'Non-Empty Heap')
		self.assertEqual(self.H.data,data_val,'Non-Empty Data')
		self.assertEqual(self.H.size,size_val,'Non-Zero Size')

	def test_empty_heap_1_func(self):
		self.H.emptyHeap()
		self.check_assertions([],{},0)

	def test_empty_heap_2_pop(self):
		self.assertRaises(Exception,self.H.pop)

	def test_empty_heap_3_find(self):
		self.assertRaises(Exception,self.H.find,'A')

	def test_empty_heap_4_peek(self):
		self.assertRaises(Exception,self.H.peek)

	def test_empty_heap_5_heapify(self):
		self.H.heapify()
		self.check_assertions([],{},0)

	def test_empty_heap_6_update(self):
		self.assertRaises(Exception,self.H.update,'A',3)

	def test_single_entry_heap_1_push(self):
		self.H.emptyHeap()
		self.H.push('A',3)
		self.check_assertions([('A',[3])],{'A':[[3],0]},1)

	def test_single_entry_heap_2_find(self):
		self.assertEqual(self.H.find('A'),3)
		self.assertRaises(Exception,self.H.find,'B')

	def test_single_entry_heap_3_peek(self):
		self.assertEqual(self.H.peek(),('A',3))

	def test_single_entry_heap_4_heapify(self):
		self.H.heapify()
		self.check_assertions([('A',[3])],{'A':[[3],0]},1)

	def test_single_entry_heap_5_update(self):
		self.H.update('A',7)
		self.check_assertions([('A',[7])],{'A':[[7],0]},1)

	def test_single_entry_heap_6_pop(self):
		self.assertEqual(self.H.pop(),('A',7))
		self.check_assertions([],{},0)

	def check_heap_order_property(self):
		N = self.H.size
		for k in range(N):
			x,y = self.H.heap[k]
			self.assertEqual(self.H.data[x],[y,k])
			childOne,childTwo,parent = 2*k+1,2*k+2,(k-1)//2
			if parent >= 0: 
				p = self.H.heap[parent]
				self.assertGreater(y,p[1])
				self.assertEqual(self.H.data[p[0]],[p[1],parent])
			if childOne < N:
				c = self.H.heap[childOne]
				self.assertLess(y,c[1])
				self.assertEqual(self.H.data[c[0]],[c[1],childOne])
			if childTwo < N:
				c = self.H.heap[childTwo]
				self.assertLess(y,c[1])
				self.assertEqual(self.H.data[c[0]],[c[1],childTwo])


	def test_multiple_entry_heap_1_push(self):
		self.H.emptyHeap()
		for k,v in self.data:
			self.H.push(k,v)
			self.check_heap_order_property()

	def test_multiple_entry_heap_2_peek(self):
		self.assertEqual(self.H.peek(),('D',1))

	def test_multiple_entry_heap_3_find(self):
		for k,v in self.data:
			self.assertEqual(self.H.find(k),v)

	def test_multiple_entry_heap_4_heapify(self):
		values = self.H.data.values()
		self.H.heapify()
		self.assertEqual(values,self.H.data.values())

	def test_multiple_entry_heap_5_update(self):
		self.H.update('G',8)
		self.check_heap_order_property()
		self.H.update('E',0)
		self.check_heap_order_property()
		self.H.update('E',9)
		self.check_heap_order_property()
		self.H.update('F',2)
		self.check_heap_order_property()
		self.H.update('G',2)
		self.H.update('E',6)
		self.H.update('F',5)
		self.check_heap_order_property()

	def test_multiple_entry_heap_6_pop(self):	
		heap_sorted = []
		while not self.H.isEmpty():
			heap_sorted.append(self.H.pop())
		self.assertEqual(heap_sorted,sorted(self.data,key=lambda (x,y): y))
		self.check_assertions([],{},0)
Exemple #5
0
 def test_peek(self):
     item = {"id":1, "price":1.0}
     heap = MinHeap()
     heap.push(item)
     self.assertEqual(heap.peek(), item)
Exemple #6
0
class TestMinHeap(unittest.TestCase):
    def setUp(self):
        self.h = MinHeap(elements=[2, 4, 5, 12, 13, 6, 10])

    def test_parent_index(self):
        self.assertLess(self.h._parent_index(0), 0)
        self.assertEqual(self.h._parent_index(1), 0)
        self.assertEqual(self.h._parent_index(2), 0)
        self.assertEqual(self.h._parent_index(4), 1)
        self.assertEqual(self.h._parent_index(6), 2)

    def test_left_child_index(self):
        self.assertGreaterEqual(self.h._left_child_index(3), len(self.h))
        self.assertGreaterEqual(self.h._left_child_index(5), len(self.h))
        self.assertEqual(self.h._left_child_index(0), 1)
        self.assertEqual(self.h._left_child_index(1), 3)
        self.assertEqual(self.h._left_child_index(2), 5)

    def test_right_child_index(self):
        self.assertGreaterEqual(self.h._right_child_index(3), len(self.h))
        self.assertGreaterEqual(self.h._right_child_index(6), len(self.h))
        self.assertEqual(self.h._right_child_index(0), 2)
        self.assertEqual(self.h._right_child_index(1), 4)
        self.assertEqual(self.h._right_child_index(2), 6)

    def test_is_empty(self):
        self.assertFalse(self.h.is_empty())
        self.assertTrue(MinHeap().is_empty())

    def test_peek(self):
        self.assertEqual(self.h.peek(), self.h.h[0])
        self.assertIsNone(MinHeap().peek())

    def test_heapify_init(self):
        elements = [2, 4, 5, 6, 10, 12, 13]
        h = MinHeap(elements)
        self.assertEqual(h.h, [2, 4, 5, 6, 10, 12, 13])

    def test_heapify_after_init(self):
        elements = [2, 4, 5, 6, 10, 12, 13]
        h = MinHeap()
        self.assertEqual(h.h, [])
        h._heapify(elements)
        self.assertEqual(h.h, [2, 4, 5, 6, 10, 12, 13])

    def test_insert_1(self):
        self.assertEqual(self.h.insert(1), 0)
        self.assertEqual(self.h.h, [1, 2, 5, 4, 13, 6, 10, 12])

    def test_insert_3(self):
        self.assertEqual(self.h.insert(3), 1)
        self.assertEqual(self.h.h, [2, 3, 5, 4, 13, 6, 10, 12])

    def test_insert_10(self):
        self.assertEqual(self.h.insert(9), 3)
        self.assertEqual(self.h.h, [2, 4, 5, 9, 13, 6, 10, 12])

    def test_insert_15(self):
        self.assertEqual(self.h.insert(15), 7)
        self.assertEqual(self.h.h, [2, 4, 5, 12, 13, 6, 10, 15])

    def test_update_min(self):
        self.assertTrue(9 not in self.h)
        self.assertTrue(10 in self.h)
        self.h.update_min(9, 10)
        self.assertTrue(9 in self.h)
        self.assertTrue(10 not in self.h)
        self.assertEqual(self.h.h, [2, 4, 5, 12, 13, 6, 9])

    def test_pop_empty(self):
        h = MinHeap()
        self.assertIsNone(h.pop())
        self.assertEqual(h.h, [])