def test_BinaryHeap(): max_heap = BinaryHeap(heap_property="max") assert max_heap.extract() is None max_heap.insert(100, 100) max_heap.insert(19, 19) max_heap.insert(36, 36) max_heap.insert(17, 17) max_heap.insert(3, 3) max_heap.insert(25, 25) max_heap.insert(1, 1) max_heap.insert(2, 2) max_heap.insert(7, 7) assert str(max_heap) == \ ("[(1, 100, 100, 2), (3, 19, 19, 4), " "(5, 36, 36, 6), (7, 17, 17, 8), " "(None, 3, 3, None), (None, 25, 25, None), " "(None, 1, 1, None), (None, 2, 2, None), (None, 7, 7, None)]") expected_extracted_element = max_heap.heap[0].key assert max_heap.extract().key == expected_extracted_element expected_sorted_elements = [36, 25, 19, 17, 7, 3, 2, 1] sorted_elements = [] for _ in range(8): sorted_elements.append(max_heap.extract().key) assert expected_sorted_elements == sorted_elements elements = [ TreeNode(7, 7), TreeNode(25, 25), TreeNode(100, 100), TreeNode(1, 1), TreeNode(2, 2), TreeNode(3, 3), TreeNode(17, 17), TreeNode(19, 19), TreeNode(36, 36) ] min_heap = BinaryHeap(elements=elements, heap_property="min") expected_extracted_element = min_heap.heap[0].key assert min_heap.extract().key == expected_extracted_element expected_sorted_elements = [2, 3, 7, 17, 19, 25, 36, 100] sorted_elements = [min_heap.extract().key for _ in range(8)] assert expected_sorted_elements == sorted_elements
def heapsort(array, **kwargs): """ Implements Heapsort algorithm. Parameters ========== array: Array The array which is to be sorted. start: int The starting index of the portion which is to be sorted. Optional, by default 0 end: int The ending index of the portion which is to be sorted. Optional, by default the index of the last position filled. Examples ======== >>> from pydatastructs import OneDimensionalArray, heapsort >>> arr = OneDimensionalArray(int,[3, 2, 1]) >>> heapsort(arr) >>> [arr[0], arr[1], arr[2]] [1, 2, 3] References ========== .. [1] https://en.wikipedia.org/wiki/Heapsort Note ==== This function does not support custom comparators as is the case with other sorting functions in this file. """ from pydatastructs.trees.heaps import BinaryHeap start = kwargs.get('start', 0) end = kwargs.get('end', len(array) - 1) h = BinaryHeap(heap_property="min") for i in range(start, end + 1): if array[i] is not None: h.insert(array[i]) array[i] = None i = start while not h.is_empty: array[i] = h.extract().key i += 1 if _check_type(array, DynamicArray): array._modify(force=True)
def test_BinaryHeap(): max_heap = BinaryHeap(heap_property="max") assert raises(IndexError, lambda: max_heap.extract()) max_heap.insert(100, 100) max_heap.insert(19, 19) max_heap.insert(36, 36) max_heap.insert(17, 17) max_heap.insert(3, 3) max_heap.insert(25, 25) max_heap.insert(1, 1) max_heap.insert(2, 2) max_heap.insert(7, 7) assert str(max_heap) == \ ("[(100, 100, [1, 2]), (19, 19, [3, 4]), " "(36, 36, [5, 6]), (17, 17, [7, 8]), " "(3, 3, []), (25, 25, []), (1, 1, []), " "(2, 2, []), (7, 7, [])]") assert max_heap.extract().key == 100 expected_sorted_elements = [36, 25, 19, 17, 7, 3, 2, 1] l = max_heap.heap[0].left l = max_heap.heap[0].right sorted_elements = [] for _ in range(8): sorted_elements.append(max_heap.extract().key) assert expected_sorted_elements == sorted_elements elements = [ TreeNode(7, 7), TreeNode(25, 25), TreeNode(100, 100), TreeNode(1, 1), TreeNode(2, 2), TreeNode(3, 3), TreeNode(17, 17), TreeNode(19, 19), TreeNode(36, 36) ] min_heap = BinaryHeap(elements=DynamicOneDimensionalArray( TreeNode, 9, elements), heap_property="min") assert min_heap.extract().key == 1 expected_sorted_elements = [2, 3, 7, 17, 19, 25, 36, 100] sorted_elements = [min_heap.extract().key for _ in range(8)] assert expected_sorted_elements == sorted_elements non_TreeNode_elements = [(7, 7), TreeNode(25, 25), TreeNode(100, 100), TreeNode(1, 1), (2, 2), TreeNode(3, 3), TreeNode(17, 17), TreeNode(19, 19), TreeNode(36, 36)] assert raises( ValueError, lambda: BinaryHeap(elements=non_TreeNode_elements, heap_property='min'))