def isHeap(heap, option): index = 1 while index <= heap.size(): if 2 * index <= heap.size(): if (option and heap.getArray()[index] > heap.getArray()[2 * index]) or (not option and heap.getArray()[index] < heap.getArray()[2 * index]): return False if 2 * index + 1 <= heap.size(): if (option and heap.getArray()[index] > heap.getArray()[2 * index + 1]) or (not option and heap.getArray()[index] < heap.getArray()[2 * index + 1]): print heap.getArray()[index], " ", heap.getArray()[2 * index + 1] return False index += 1 return True
def isHeap(heap, option): index = 1 while index <= heap.size(): if 2 * index <= heap.size(): if (option and heap.getArray()[index] > heap.getArray()[2 * index] ) or (not option and heap.getArray()[index] < heap.getArray()[2 * index]): return False if 2 * index + 1 <= heap.size(): if (option and heap.getArray()[index] > heap.getArray()[2 * index + 1] ) or (not option and heap.getArray()[index] < heap.getArray()[2 * index + 1]): print heap.getArray()[index], " ", heap.getArray()[2 * index + 1] return False index += 1 return True
def test_simple(self): h = [] self.assertEqual(0, heap.size(h)) heap.insert(h, 100) self.assertEqual(1, heap.size(h)) self.assertEqual(100, heap.top(h)) heap.insert(h, 10) self.assertEqual(2, heap.size(h)) self.assertEqual(100, heap.top(h)) heap.insert(h, 200) self.assertEqual(3, heap.size(h)) self.assertEqual(200, heap.top(h)) self.assertEqual(200, heap.remove_top(h)) self.assertEqual(2, heap.size(h)) self.assertEqual(100, heap.remove_top(h)) self.assertEqual(1, heap.size(h)) self.assertEqual(10, heap.remove_top(h)) self.assertEqual(0, heap.size(h))