def main(): alist = [3, 2, 5, 6, 9] l = Heap() l.build_heap(alist) while True: ch = int( input( "1.insert\n2.max_child\n3.exract_max\n4.print\n5.Heapify\n6.Heapsort\n7.Exit\nEnter ur choice:" )) if ch == 1: a = int(input("Entre the value:")) l.insert(a) elif ch == 2: a = int(input("Enter the index:")) print(l.items[l.maximum(a)]) elif ch == 3: print(l.extract_Max()) elif ch == 4: print(l.prin()) elif ch == 5: l.heapify() elif ch == 6: heapsort(l, l.items) elif ch == 7: break else: print('Invalid choice')
def heapsort(data): heap = Heap('maxheap') heap.build_heap(data) for i in range(len(data) - 1, 0, -1): heap.data[i], heap.data[0] = heap.data[0], heap.data[i] heap.n -= 1 heap.precolate_down(0) return heap.data
def heap_sort(items, cmp_func): sorted_array = [] if cmp_func is None: cmp_func = lambda parent, child: parent <= child heap = Heap(cmp_func) heap.build_heap(items) while heap.size > 0: sorted_array.append(heap.pop()) return sorted_array
def merge_lists(lists): ''' merge len(lists) sorted linked lists, and return the result linked list ''' for each_list in lists: each_list.append_node(ListNode(sys.maxint)) min_heap = Heap() result_list = LinkList() curr_nodes = [each_list.head for each_list in lists] curr_datas = [node.data for node in curr_nodes] # build the heap according to curr_datas min_heap.build_heap('min', curr_datas) # min_heap.heap[0] == maxint means all the lists go to end, only then, stop the while loop while min_heap.heap[0] != sys.maxint: # extract min node curr_min = min_heap.extract_node() # append to result result_list.append_node(ListNode(curr_min)) min_index = curr_datas.index(curr_min) curr_nodes[min_index] = curr_nodes[min_index].next curr_datas[min_index] = curr_nodes[min_index].data # insert the extracted node's next's data, and re-heapify min_heap.add_node(curr_datas[min_index]) return result_list
class TestHeap(unittest.TestCase): def setUp(self): self.heap = Heap([]) def init_heap(self): self.heap = Heap([1, 2, 3, 4, 5]) def test_heapify(self): self.init_heap() self.heap.heapify(0) self.assertEqual(self.heap._array, [3, 2, 1, 4, 5]) def test_build_heap1(self): self.init_heap() self.heap.build_heap() self.assertEqual(self.heap._array, [5, 4, 3, 1, 2]) def test_build_heap2(self): self.heap = Heap([1, 2, 3, 4, 5, 6]) self.heap.build_heap() self.assertEqual(self.heap._array, [6, 5, 3, 4, 2, 1]) def test_insert(self): self.init_heap() self.heap.build_heap() self.heap.insert(6) self.assertEqual(self.heap._array, [6, 4, 5, 1, 2, 3]) def test_extract_max(self): self.init_heap() self.heap.build_heap() self.heap.extract_top() self.assertEqual(self.heap._array, [4, 2, 3, 1]) def test_heap_sort(self): self.heap = Heap([1, 3, 5, 6, 3, 2, 1, 2, 5, 6, 6, 17, 19, 10]) self.heap.build_heap() self.assertEqual(self.heap.heap_sort(), [19, 17, 10, 6, 6, 6, 5, 5, 3, 3, 2, 2, 1, 1]) def test_min_heap_sort(self): self.heap = Heap([1, 3, 5, 6, 3, 2, 1, 2, 5, 6, 6, 17, 19, 10]) self.heap.build_heap(False) self.assertEqual(self.heap.heap_sort(False), [1, 1, 2, 2, 3, 3, 5, 5, 6, 6, 6, 10, 17, 19])