def test_random_additions():
    for i in range(100):
        heap = MinHeap()

        for j in range(100):
            heap.add(j)
            assert heap.is_valid()
Esempio n. 2
0
def test_min_heap():
    heap = MinHeap()
    heap.add(1)
    heap.add(2)

    value = heap.retrieve_min()
    assert value == 1
def test_add():
    for _ in range(100):
        heap = MinHeap()
        check = set()

        for _ in range(100):
            rand = randrange(0, 1000)
            heap.add(rand)
            check.add(rand)

        for num in check:
            assert heap.contains(num)
            rand = randrange(0, 1000)

            if rand not in check:
                assert not heap.contains(rand)
def minTimeEncoding(fileSizes: List[int]) -> int:
    # create a min heap and add all the sizes to it
    minHeap = MinHeap()

    for size in fileSizes:
        minHeap.add(size)

    while not minHeap.isEmpty():
        # pull the least two sizes and add it to the heap until there
        # is only one size left in the heap
        size1 = minHeap.poll()

        if minHeap.isEmpty():
            return size1

        size2 = minHeap.poll()
        mergeTime = size1 + size2
        minHeap.add(mergeTime)

    return 0
Esempio n. 5
0
    def __init__(self, graph):

        self.__tree = []
        self.__weight = 0

        heap = MinHeap()
        for i in range(graph.get_node_nums()):
            adj = graph.iter_nodes(i)
            for edge in adj:
                if edge.orgin < edge.goal:
                    heap.add(edge)

        union_find = UnionFind(graph.get_node_nums())
        while not heap.is_empty() and len(
                self.__tree) < graph.get_node_nums() - 1:

            edge = heap.pop()

            if not union_find.is_connected(edge.orgin, edge.goal):
                self.__tree.append(edge)
                union_find.union(edge.orgin, edge.goal)

        for x in self.__tree:
            self.__weight += x.weight
Esempio n. 6
0
# import heap class
from min_heap import MinHeap

# make an instance of MinHeap
min_heap = MinHeap()

# populate min_heap with descending numbers
descending_nums = [n for n in range(10001, 1, -1)]
print("ADDING!")
for el in descending_nums:
    min_heap.add(el)

print("REMOVING!")
# remove minimum until min_heap is empty
min_heap.retrieve_min()
# script.py

# import random number generator
from random import randrange
# import heap class
from min_heap import MinHeap 

# make an instance of MinHeap
min_heap = MinHeap()

# populate min_heap with descending numbers
descending_nums = [n for n in range(10001, 1, -1)]
print("ADDING!")
for el in descending_nums:
  min_heap.add(el)

print("REMOVING!")
# remove minimum until min_heap is empty
min_heap.retrieve_min()

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

# min_heap.py

class MinHeap:
  def __init__(self):
    self.heap_list = [None]
    self.count = 0

  def parent_idx(self, idx):
Esempio n. 8
0
def test_add():
    heap = MinHeap()
    heap.add(2)
    heap.add(1)

    assert heap.heap_list[1] == 1
# Terrific! We removed the minimum element with minimal disruption. Unfortunately, our heap is out of shape again with 99 sitting where the minimum element should be.
# Patience, young Heap-prentice. We will solve this in lessons to come…

# Define a .retrieve_min() method within our MinHeap class. Its only parameter is self.
# Check if our internal count is at 0…
# If it is, we have no elements to retrieve, so print a friendly “No items in heap” and return None.

# Declare the variable min, which is the element at index 1 in our internal list.

# Print the message “Removing: min from self.heap_list“.
# Then, swap the element at index 1 with the last element in the internal list.
# Remove the last element from the list, and decrement the count.

# Print the message “Last element moved to first: self.heap_list“.
# Finally, return the min variable.

# Tab over to script.py and run the test code.

# import heap class
from min_heap import MinHeap

# make an instance of MinHeap
min_heap = MinHeap()

# add elements
min_heap.add(7)
min_heap.add(12)
min_heap.add(42)

# remove minimum element
print(min_heap.retrieve_min())
Esempio n. 10
0
# Adding an Element: Heapify Up I

# The min-heap is no good if all it ever contains is None. Let’s build the functionality to add elements while maintaining the heap properties.
# Our MinHeap will abide by two principles:
# - The element at index 1 is the minimum value in the entire list.
# - Every “child” element in the list must be larger than their “parent”.
# The first element we add to the list will be the minimum because there are no other elements. We’ll tackle the trickier aspects of maintaining these principles in the coming lessons.
# For now, let’s define .add() which will allow us to add elements into the .heap_list. We’ll also start defining .heapify_up(), which will do the work of maintaining the heap properties as we add additional elements.

# Inside min_heap.py, define .add() within Heap. .add() takes two arguments: self and element.
# Give users a helpful print message like “Adding element to self.heap_list.”

# Inside of .add(), increment the internal element count, then add the element to the end of the internal list.

# Run the test code within script.py to see the element 42 added to the internal list.

# Define another method inside MinHeap: .heapify_up(). It has self as a parameter.
# Print out the message “Restoring the heap property…” within .heapify_up().

# Finish .add() by calling the .heapify_up() method we just defined.
# Run the test code in script.py.

# import heap class
from min_heap import MinHeap

min_heap = MinHeap()
print(min_heap.heap_list)

# testing out .add()
min_heap.add(42)
print(min_heap.heap_list)