示例#1
0
class MaxHeap_TestCase_Iterator_Element(unittest.TestCase):
    """
    Create only. No heap modification. No Elements.
    """
    def setUp(self):
        self._heap = Heap(min_heap=False)
        self._sortedListofElements = [40, 20, 10, 9, 4]
        self._heap.add_items([10, 9, 4, 20, 40])

    def test_heap_iterator(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.')
        # Iterate through the heap and make sure the elements are present
        list_index = 0
        for element in self._heap:
            self.assertEqual(
                element, self._sortedListofElements[list_index],
                'heap contents did not match up when iterating over a known list of elements.'
            )
            list_index = list_index + 1

    def tearDown(self):
        unittest.TestCase.tearDown(self)
示例#2
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)
示例#3
0
class MaxHeap_TestCase_1_Pop_With_Predictable_SmallSet_Of_Elements(
        unittest.TestCase):
    """
    Create only. No heap modification. Small predictable set of elements.
    """
    def setUp(self):
        self._heap = Heap(min_heap=False)
        self._heap.add_items([3, 1, 2])

    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, 1, 2] == self._heap._get_heap_as_list(),
                        'heap contents are not same as expected.')
        #Pop
        self._heap.get()
        #
        self.assertTrue(
            assert_max_heap_property(self._heap._get_heap_as_list()),
            'heap property must be satisfied.')
        self.assertTrue([None, 2, 1] == self._heap._get_heap_as_list(),
                        'heap contents are not same as expected.')

    def tearDown(self):
        unittest.TestCase.tearDown(self)
示例#4
0
class MaxHeap_TestCase_Create_With_No_Element(unittest.TestCase):
    """
    Create only. No heap modification. No Elements.
    """
    def setUp(self):
        self._heap = Heap(min_heap=False)

    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] == self._heap._get_heap_as_list(),
                        'heap contents are not same as expected.')

    def tearDown(self):
        unittest.TestCase.tearDown(self)
示例#5
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)
示例#6
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))
    
      
示例#7
0
 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
示例#8
0
 def setUp(self):
     self._heap = Heap(min_heap=False)
     self._heap.add_items([3, 1, 2])
示例#9
0
 def setUp(self):
     self._heap = Heap(min_heap=False)
     self._heap.add_item(3)
示例#10
0
 def setUp(self):
     self._heap = Heap(min_heap=False)
示例#11
0
 def setUp(self):
     self._heap = Heap(min_heap=False)
     self._sortedListofElements = [40, 20, 10, 9, 4]
     self._heap.add_items([10, 9, 4, 20, 40])