Пример #1
0
class PriorityQueue(object):
    '''
    Priority Q based on Heaps. You can set min heap of max heap. Default is Max Heap.
    1) Iterating yields (item, priority) tuples
    '''
    
    def __init__(self, reverse_priority = False):
        '''
        creates a priority queue such that priority 10 is processed first than priority 2. If you want
        to reverse this set reverse_priority so that item with priority 2 is processed first 
        '''
        self._heap = Heap(min_heap =  reverse_priority)#create the heap as needed. Default Max heap
    
    def add_item(self, item = None, priority = None):
        if item == None:
            return #we are not wasting time
        
        self._heap.add_item(PriorityQueueEntry(item = item, priority = priority))
    
    def get(self):
        priority_q_entry = self._heap.get()
        if priority_q_entry == None:
            return None
        return priority_q_entry.item
    
    def __len__(self):
        return self._heap.heap_size
    
    def __iter__(self):
        for priority_q_entry in self._heap:
            yield (priority_q_entry.item, priority_q_entry.priority)
Пример #2
0
class MaxHeap_TestCase_1000_Pops_With_RandomSet_Of_1000_Elements(
        unittest.TestCase):
    """
    Create only. No heap modification. Small predictable set of elements.
    """
    def setUp(self):
        self._heap = Heap(min_heap=False)

    def test_heap_status_on_push_pop(self):
        self.assertFalse(self._heap.is_min_heap, 'heap must be a max heap.')

        for i in range(1000):
            self._heap.add_item(randint(0, 1000))
            self.assertTrue(
                assert_max_heap_property(self._heap._get_heap_as_list()),
                'heap property must be satisfied.')

        #pop all
        for i in range(1000):
            self._heap.get()
            self.assertTrue(
                assert_max_heap_property(self._heap._get_heap_as_list()),
                'heap property must be satisfied.')

    def tearDown(self):
        unittest.TestCase.tearDown(self)
Пример #3
0
class MaxHeap_TestCase_Create_With_One_Element(unittest.TestCase):
    """
    Create only. No heap modification. Only one element.
    """
    def setUp(self):
        self._heap = Heap(min_heap=False)
        self._heap.add_item(3)

    def test_heap_status(self):
        self.assertFalse(self._heap.is_min_heap, 'heap must be a max heap.')
        self.assertTrue(
            assert_max_heap_property(self._heap._get_heap_as_list()),
            'heap property must be satisfied.')
        self.assertTrue([None, 3] == self._heap._get_heap_as_list(),
                        'heap contents are not same as expected.')

    def tearDown(self):
        unittest.TestCase.tearDown(self)
Пример #4
0
'''
Created on Oct 25, 2015

@author: hari
'''
from __future__ import print_function
from builtins import str
from pycoils.trees.heap import Heap

if __name__ == '__main__':
    # A max heap. for a min heap use minHeap = True (is default)
    max_heap = Heap(min_heap = False)
    
    elements = [0, 4, 3, 2, 5, 1, 6]
    print('Adding elements to heap: %s' % elements)
    for element in elements:
        max_heap.add_item(element)
        
    print('Getting element from max heap')
    element = max_heap.get()
    print('Top element from heap is %s' % str(element))
    
    print('Iterating through heap') 
    for element in max_heap:
        print('heap popped %s' % str(element))