@Before public void init() { AbstractTreeFactory factory = new LinkedTreeFactory(); PriorityQueue<TreeNode> queue = new PriorityQueue<TreeNode>(1, new TreeNode.comparator()); queue.add(factory.produceTreeNode(Color.A.getRGB(), Color.A.getAmount())); queue.add(factory.produceTreeNode(Color.B.getRGB(), Color.B.getAmount())); queue.add(factory.produceTreeNode(Color.C.getRGB(), Color.C.getAmount())); queue.add(factory.produceTreeNode(Color.D.getRGB(), Color.D.getAmount())); queue.add(factory.produceTreeNode(Color.E.getRGB(), Color.E.getAmount())); queue.add(factory.produceTreeNode(Color.F.getRGB(), Color.F.getAmount())); queue.add(factory.produceTreeNode(Color.G.getRGB(), Color.G.getAmount())); tree = factory.produceTree(queue); }
/** * Tree Layout. Color: A B C D E F G Amount: 1 2 4 6 8 9 10 * * <p>Huffman Code Tree: ------------ 0 1 --------- ---- 0 1 0 1 ------ G F E 0 1 ----- D 0 1 C * --- 0 1 B A */ @Test public void huffmanCodeTest() { HuffmanCode huffmanCode = new HuffmanCode(tree); HashMap<RGB, String> code = huffmanCode.getHuffmanCode(); assertEquals("00011", code.get(Color.A.getRGB())); assertEquals("00010", code.get(Color.B.getRGB())); assertEquals("0000", code.get(Color.C.getRGB())); assertEquals("001", code.get(Color.D.getRGB())); assertEquals("11", code.get(Color.E.getRGB())); assertEquals("10", code.get(Color.F.getRGB())); assertEquals("01", code.get(Color.G.getRGB())); }