def test_is_empty(self): heap = MaxHeap() assert heap.is_empty() is True heap = MaxHeap(capacity=10) assert heap.is_empty() is True heap.push(0) assert heap.is_empty() is False heap.pop() assert heap.is_empty() is True
class PriorityQueue(QueueBase): def __init__(self): self._max_heap = MaxHeap() def get_size(self): return self._max_heap.size() def is_empty(self): return self._max_heap.is_empty() def get_front(self): return self._max_heap.find_max() def enqueue(self, e): self._max_heap.add(e) def dequeue(self): return self._max_heap.extract_max()
def initialize_cluster_centers(histogram_bins, num_of_bins, k): max_heap = MaxHeap() for bin_id in range(1, (num_of_bins) + 1): if histogram_bins['counts'][bin_id]: max_heap.heappush((bin_id, histogram_bins['values'][bin_id]), histogram_bins['counts'][bin_id]) seed_list = [] alternative_max_heap = MaxHeap() while not max_heap.is_empty(): bin = max_heap.heappop() bin_id, bin_points = bin.val should_be_added_to_seed_list = True # check if neigbouring bins don't have greater cardinality left_neighbour_idx = bin_id - 1 right_neighbour_idx = bin_id + 1 if left_neighbour_idx in histogram_bins['counts'] and histogram_bins[ 'counts'][left_neighbour_idx] > bin.priority: should_be_added_to_seed_list = False if right_neighbour_idx in histogram_bins['counts'] and histogram_bins[ 'counts'][right_neighbour_idx] > bin.priority: should_be_added_to_seed_list = False if should_be_added_to_seed_list: seed_list += [bin] else: alternative_max_heap.heappush(bin.val, bin.priority) while k > len(seed_list): bin = alternative_max_heap.heappop() seed_list += [bin] seed_centers = [] for i in range(0, k): bin = seed_list[i] bin_points = bin.val[1] seed_center = np.mean(bin_points, axis=0) seed_centers += [seed_center] return seed_centers
from max_heap import MaxHeap from index_heap import IndexMaxHeap import heapq mh = MaxHeap() n = [5, 6, 77, 111, 31, 21, 9, 1, 7, 4, 45, 5] M = MaxHeap(1000, n) for i in n: mh.push(i) while not mh.is_empty(): print(f'{mh.pop()}', end=' ') print() while not M.is_empty(): print(f'{M.pop()}', end=' ') print() print('========Index Heap Test========') imh = IndexMaxHeap() for i, x in enumerate(n): imh.push(i, x) imh.p() result = [] while not imh.is_empty(): result.append(imh.pop()) for l in (range(len(n)), n): for i in l: print(i, end='\t') print()