def test_huffnode_eq_repr(self):
     huff1 = HuffmanNode('c', 5, None, None)
     huff2 = HuffmanNode('d', 5, None, None)
     self.assertNotEqual(huff1, huff2)
     huff2.char = 'c'
     self.assertEqual(huff1, huff2)
     self.assertEqual(repr(huff1), "HuffmanNode(c, 5, None, None)")
    def test_eq(self):
        """ Tests whether two nodes are equal"""
        node1 = HuffmanNode(4)
        node2 = HuffmanNode(1)
        node3 = HuffmanNode(4)

        self.assertFalse(node1 == node2)
        self.assertTrue(node1 == node3)
 def test_repr(self):
     """ Tests how the class represents itself"""
     node1 = HuffmanNode(1)
     node2 = HuffmanNode(4)
     node3 = HuffmanNode(12, None, node1, node2)
     expected = "Huffman Node(freq: 12, char: None, left: " \
                "Huffman Node(freq: 1, char: None, left: None, right: " \
                "None), right: Huffman Node(freq: 4, char: None, " \
                "left: None, right: None))"
     self.assertEqual(expected, repr(node3))
示例#4
0
    def _unpack_data(data):
        """
        :param data:    binary data as read from a file or pulled directly out
                        of a certificate extension. Data should be compressed
                        with huffman coding as described for v3 entitlement
                        certificates
        :type  data:    binary string
        :return:        tuple: (list of HuffmanNode instances not yet in a
                        tree, binary string of leftover bits that were not
                        part of the zlib-compressed word list
        :rtype:         tuple(list, binary string)
        """
        decompress = zlib.decompressobj()
        decompressed_data = decompress.decompress(data)
        # ordered list of words that will be composed into a huffman tree
        words = decompressed_data.split('\0')

        # enumerate() would be better here, but lacks a 'start' arg in 2.4
        weighted_words = zip(itertools.count(1), words)
        # huffman nodes, without having put them in a tree. These will all be
        # leaves in the tree.
        nodes = [
            HuffmanNode(weight, value) for weight, value in weighted_words
        ]
        return nodes, decompress.unused_data
示例#5
0
    def __init__(self, data):
        """
        Uncompresses data into a tree that can be traversed for matching paths

        :param data:    binary data as read from a file or pulled directly out
                        of a certificate extension. Data should be compressed
                        with huffman coding as described for v3 entitlement
                        certificates
        :type  data:    binary string
        """
        word_leaves, unused_bits = self._unpack_data(data)
        HuffmanNode.build_tree(word_leaves)
        word_dict = dict((node.code, node.value) for node in word_leaves)
        bitstream = GhettoBitStream(unused_bits)
        path_leaves = self._generate_path_leaves(bitstream)
        HuffmanNode.build_tree(path_leaves)
        path_dict = dict((node.code, node) for node in path_leaves)
        self.path_tree = self._generate_path_tree(path_dict, path_leaves, word_dict, bitstream)
示例#6
0
def create_huff_tree(list_of_freqs):
    """returns the root node of a Huffman Tree
    """
    huff_tree = []
    for idx in range(len(list_of_freqs)):
        if list_of_freqs[idx] != 0:
            huff_tree.append(HuffmanNode(list_of_freqs[idx], chr(idx)))
    huff_arr = MinPQ()
    huff_arr.heapify(huff_tree)
    while huff_arr.num_items > 1:
        node1 = huff_arr.del_min()
        node2 = huff_arr.del_min()
        freq = node1.freq + node2.freq
        char = min(node1.char, node2.char)
        new_huff_node = HuffmanNode(freq, char)
        new_huff_node.left = node1
        new_huff_node.right = node2
        huff_arr.insert(new_huff_node)
    return huff_arr.min()
示例#7
0
    def __init__(self, data):
        """
        Uncompresses data into a tree that can be traversed for matching paths

        :param data:    binary data as read from a file or pulled directly out
                        of a certificate extension. Data should be compressed
                        with huffman coding as described for v3 entitlement
                        certificates
        :type  data:    binary string
        """
        word_leaves, unused_bits = self._unpack_data(data)
        HuffmanNode.build_tree(word_leaves)
        word_dict = dict((node.code, node.value) for node in word_leaves)
        bitstream = GhettoBitStream(unused_bits)
        path_leaves = self._generate_path_leaves(bitstream)
        HuffmanNode.build_tree(path_leaves)
        path_dict = dict((node.code, node) for node in path_leaves)
        self.path_tree = self._generate_path_tree(path_dict, path_leaves,
                                                  word_dict, bitstream)
示例#8
0
def create_huff_tree(list_of_freqs):
    """Creates a huffman tree given a list of character frequencies.
    Arguments:
        list_of_freqs (list): List of character frequencies from cnt_freq()
    Returns:
        The root node of the huffman tree created from the list of frequencies
    """
    pq_arr = []
    for char, freq in enumerate(list_of_freqs):
        if freq > 0:
            pq_arr.append(HuffmanNode(chr(char), freq, None, None))
    min_pq = MinPQ(pq_arr)
    while min_pq.size() > 1:
        left = min_pq.del_min()
        right = min_pq.del_min()
        min_char = min(left.char, right.char)
        parent_node = HuffmanNode(min_char, left.freq + right.freq, left, right)
        min_pq.insert(parent_node)
    return min_pq.min()
def create_huff_tree(list_of_freqs):
    """creates the root node of a Huffman Tree using a list of freqencies
    Args:
        list_of_freqs (list): list of size 256 integers with the frequencies of characters in file
    Returns:
        HuffmanNode: the root of Huffman Tree
    """
    heap_arr = []
    for i in range(len(list_of_freqs)):
        if list_of_freqs[i] != 0:
            heap_arr.append(HuffmanNode(list_of_freqs[i], chr(i)))
    min_heap = MinPQ(heap_arr)
    while min_heap.num_items > 1:
        huff_node1 = min_heap.del_min()
        huff_node2 = min_heap.del_min()
        new_huff_node = HuffmanNode(huff_node1.freq + huff_node2.freq,
                                    min(huff_node1.char, huff_node2.char))
        new_huff_node.left, new_huff_node.right = huff_node1, huff_node2
        min_heap.insert(new_huff_node)
    return min_heap.arr[0]
示例#10
0
def create_huff_tree(list_of_freqs):
    """builds a huffman tree
    Args:
        list_of_freqs(list): list of frequencies from cnt_freq
    Returns:
        HuffmanNode: root node of the huffman tree
    """
    min_huff = MinPQ()  # min pq of huffman nodes
    # for i in range(len(list_of_freqs)):  # method 2: insert all huffman nodes
    for i, item in enumerate(list_of_freqs):
        if list_of_freqs[i] != 0:
            min_huff.insert(HuffmanNode(list_of_freqs[i], chr(i)))
    while min_huff.num_items != 1:  # until a root node
        low = min_huff.del_min()  # HuffmanNode
        low_2 = min_huff.del_min()  # HuffmanNode
        if ord(low.char) < ord(low_2.char):
            char = low.char
        else:
            char = low_2.char
        new_huff = HuffmanNode(low.freq + low_2.freq, char, low, low_2)
        min_huff.insert(new_huff)
    return min_huff.del_min()  # pop out root node
示例#11
0
def create_huff_tree(list_of_freqs):
    """ Create a Huffman Tree from list of character frequencies
        Args:
            list_of_freqs(list): list with length of 256 characters
        Returns:
            MinPQ: the root node of the Min Priority Queue constructed from
                   the Huffman Nodes
    """

    priority_queue_list = []
    for each in range(len(list_of_freqs)):
        if list_of_freqs[each] > 0:
            char = chr(each)
            freq = list_of_freqs[each]
            node = HuffmanNode(freq, char)
            priority_queue_list.append(node)

    priority_queue = MinPQ(priority_queue_list)

    if priority_queue.size() == 0:
        return None

    while priority_queue.size() > 1:
        # Retrieves the two smallest occurring Nodes from the queue
        node_1 = priority_queue.del_min()
        node_2 = priority_queue.del_min()

        # Determine the representation of the min chr representation
        min_chr = min(node_1.char, node_2.char)

        # Sum the different frequencies, create new Node, and add to queue
        sum_freq = node_1.freq + node_2.freq
        new_node = HuffmanNode(sum_freq, min_chr, node_1, node_2)
        priority_queue.insert(new_node)

    return priority_queue.del_min()
示例#12
0
    def _generate_path_leaves(cls, bitstream):
        """
        Given the remaining bits after decompressing the word list, this
        generates HummanNode objects to represent each node (besides root)
        that will end up in the path tree.

        :param bitstream:   stream of bits remaining after decompressing the
                            word list
        :type  bitstream:   rhsm.bitstream.GhettoBitStream
        :return:            list of HuffmanNode objects that can be used to
                            build a path tree
        :rtype:             list of HuffmanNode objects
        """
        node_count = cls._get_node_count(bitstream)
        nodes = []
        # make leaves for a huffman tree and exclude the root node of the path
        # tree, because we don't need a reference code for that.
        for weight in range(1, node_count):
            node = HuffmanNode(weight, {})
            nodes.append(node)
        return nodes
示例#13
0
    """
    list_of_freqs = [0] * 256
    with open(filename) as file:
        for line in file:
            for char in line:
                list_of_freqs[ord(char)] += 1
    return list_of_freqs

def create_huff_tree(list_of_freqs):
    """returns the root node of a Huffman Tree
    """
    tree = MinPQ()
<<<<<<< HEAD
    for i in range(len(list_of_freqs)):
        if list_of_freqs[i] != 0:
            new = HuffmanNode(list_of_freqs[i], chr(i))
            # tree.insert(new)
            hufflist.append(new)
    hufflist.sort()
    # print(hufflist)
    tree = MinPQ(hufflist)
    # print('TREE AFTER INSERTION:\n', tree)
=======
    for idx, freq in enumerate(list_of_freqs):
        if freq != 0:
            new = HuffmanNode(freq, chr(idx))
            tree.insert(new)
>>>>>>> 1c1c70a6eb7b91e2d3714eb49057c9513665ab0d
    while tree.num_items > 1:
        left = tree.del_min()
        right = tree.del_min()