def median_maintenance(data):
    yield data[0]
    if data[0] < data[1]:
        h_high, h_low = MinHeap([data[1]]), MaxHeap([data[0]])
    else:
        h_high, h_low = MinHeap([data[0]]), MaxHeap([data[1]])
    median = h_low.extract_max()
    h_low.insert(median)
    yield median
    for k in data[2:]:
        lower, upper = h_low.extract_max(), h_high.extract_min()
        if k <= lower:
            h_low.insert(k)
        else:
            h_high.insert(k)
        h_low.insert(lower)
        h_high.insert(upper)
        if abs(h_high.size() - h_low.size()) > 1:
            if h_high.size() > h_low.size():
                h_low.insert(h_high.extract_min())
            else:
                h_high.insert(h_low.extract_max())
        if (h_high.size() + h_low.size()) % 2 == 0 or h_low.size() > h_high.size():
            median = h_low.extract_max()
            h_low.insert(median)
            yield median
        else:
            median = h_high.extract_min()
            h_high.insert(median)
            yield median
예제 #2
0
 def test_insert_and_get_many_random_items(self):
     heap = MinHeap()
     items = random.sample(range(1000), 50)
     for index, item in enumerate(items):
         heap.insert(item)
         assert heap.size() == index + 1
         min_item = min(items[: index + 1])
         assert heap.get_min() == min_item
     assert heap.size() == len(items)
예제 #3
0
 def test_insert_and_get_many_items(self):
     heap = MinHeap()
     items = [9, 25, 86, 3, 29, 5, 55]
     for index, item in enumerate(items):
         heap.insert(item)
         assert heap.size() == index + 1
         min_item = min(items[: index + 1])
         assert heap.get_min() == min_item
     assert heap.size() == len(items)
     assert heap.items == [3, 9, 5, 25, 29, 86, 55]
예제 #4
0
 def test_insert_and_remove_many_random_items(self):
     heap = MinHeap()
     items = random.sample(range(1000), 50)
     for item in items:
         heap.insert(item)
     assert heap.size() == len(items)
     sorted_items = sorted(items)
     for index, item in enumerate(sorted_items):
         min_item = heap.remove_min()
         assert sorted_items[index] == min_item
     assert heap.size() == 0
예제 #5
0
 def test_insert_and_remove_many_items(self):
     heap = MinHeap()
     items = [9, 25, 86, 3, 29, 5, 55]
     for item in items:
         heap.insert(item)
     assert heap.size() == len(items)
     sorted_items = sorted(items)
     for index, item in enumerate(sorted_items):
         min_item = heap.remove_min()
         assert sorted_items[index] == min_item
     assert heap.size() == 0
예제 #6
0
    def test_append(self):
        mh = MinHeap()
        self.assertEqual(0, mh.size())

        mh.append(1)
        self.assertEqual(1, mh.size())

        mh.append(1)
        self.assertEqual(2, mh.size())

        mh.append(2)
        self.assertEqual(3, mh.size())
def get_trie(s):
    # get frequencies:
    frequencies = {}
    for c in s:
        if c not in frequencies:
            frequencies[c] = 1
        else:
            frequencies[c] += 1

    # build trie:
    heap = MinHeap()
    for c in frequencies:
        heap.insert(Node(frequencies[c], c))
    while heap.size() > 1:
        left = heap.extractmin()
        right = heap.extractmin()
        assert (left is not None)
        assert (right is not None)
        assert (left.frequency >= 0)
        assert (right.frequency >= 0)
        frequency = left.frequency + right.frequency
        node = Node(frequency)
        node.left = left
        node.right = right
        heap.insert(node)
    return heap.extractmin()
예제 #8
0
class HeapMedian:
    ''' solution using min-, max- heaps '''
    def __init__(self):
        self.upper = MinHeap()
        self.lower = MaxHeap()

    def add(self, i):
        assert self.lower.size() >= self.upper.size()

        if self.lower.size()==0 or\
                i <= self.lower.peek():
            self.lower.push(i)
        else:
            self.upper.push(i)

        if self.lower.size() < self.upper.size():
            self.lower.push(self.upper.pop())
        elif self.lower.size() > self.upper.size()+1:
            self.upper.push(self.lower.pop())

    def get(self):
        return self.lower.peek()
예제 #9
0
from heap import MinHeap, MaxHeap
from math import floor
import sys
#invariant: maintain min heap size = floor(n/2), median is the the max in max heap
with open('Median.txt') as f:
#with open('median10_test.txt') as f:
	a = [int(l) for l in f]
	minHeap = MinHeap([])
	maxHeap = MaxHeap([])
	medians = []
	for i in range(len(a)):
		if minHeap.size() == 0 and maxHeap.size() == 0:
			maxHeap.insert(a[i])
		else:
			if a[i] < maxHeap.top():
				maxHeap.insert(a[i])
			else:
				minHeap.insert(a[i])
			if minHeap.size() > floor((i+1)/2):
				maxHeap.insert(minHeap.extract())
			elif minHeap.size() < floor((i+1)/2):
				minHeap.insert(maxHeap.extract())
		medians.append(maxHeap.top())
	print(sum(medians)%10000)
예제 #10
0
    def find_shortest(self):
        return len(self.encoding)

    def find_longest(self):
        return len(self.encoding)


with open("huffman.txt", "r") as infile:
    infile = [int(i) for i in infile.readlines()[1:]]
    infile = list(enumerate(infile))

##infile = [(0,1), (1,5), (2,7), (3,2), (4,3)]

min_heap = MinHeap()
for i in infile:
    min_heap.insert(i[1], HCLeafNode(i[0], i[1]))

#do until only one node left, the root
while min_heap.size() > 1:
    #get two smallest nodes
    small = min_heap.pop().get_data()[0]
    small.add_prefix("0")
    two_small = min_heap.pop().get_data()[0]
    two_small.add_prefix("1")
    #merge them together
    merged = HCMiddleNode(small, two_small)

    min_heap.insert(merged.get_frequency(), merged)

tree = min_heap.pop().get_data()[0]
예제 #11
0
 def test_size_of_empty_heap(self):
     heap = MinHeap()
     assert heap.size() == 0
예제 #12
0
 def test_insert_and_get_one_item(self):
     heap = MinHeap()
     heap.insert(5)
     assert heap.size() == 1
     assert heap.get_min() == 5
     assert heap.items == [5]
예제 #13
0
 def test_insert_and_remove_one_item(self):
     heap = MinHeap()
     heap.insert(5)
     assert heap.size() == 1
     assert heap.remove_min() == 5
     assert heap.size() == 0