Skip to content

Instantly share code, notes, and snippets.

@Mizzlr
Created June 19, 2016 15:12
Show Gist options
  • Save Mizzlr/107c7d7fb20e087bcfa6bffbe3924a19 to your computer and use it in GitHub Desktop.
Save Mizzlr/107c7d7fb20e087bcfa6bffbe3924a19 to your computer and use it in GitHub Desktop.
import argparse, re, math, sys, time, os
class HuffmanNode:
"""
Node Object that supports Merging operation as in the
Huffman Encoding Algorithm.
self.alphabet: list of characters at a node which share
a common ancestors in the Huffman Tree.
self.count: number of times the element in the list of
characters have occured. Used for statistical purpose
while Merging
Example:
>>> import huffman.HuffmanNode as HN
>>> hn1 = HN(['A','B'],100)
>>> hn2 = HN(['C'],200)
>>> hn3 = hn1.merge(hn2)
>>> hn3
HuffmanNode(['A', 'B', 'C']|300)
"""
def __init__(self, alphabet,count):
"Create an HuffmanNode() Object"
self.alphabet = alphabet
self.count = count
def merge(self, other):
"""
Merge two HuffmanNode() and return the newly created
HuffmanNode() which has all the element of the two nodes
self and other, with count being sum of their counts.
"""
alphabet = self.alphabet + other.alphabet
count = self.count + other.count
return HuffmanNode(alphabet,count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment