@Test(timeout = 10000L) public void testGraphHuffman() { GraphHuffman gh = new GraphHuffman(7); int[] vertexDegrees = {10, 15, 12, 3, 4, 13, 1}; gh.buildTree(vertexDegrees); for (int i = 0; i < 7; i++) { String s = i + "\t" + gh.getCodeLength(i) + "\t" + gh.getCodeString(i) + "\t\t" + gh.getCode(i) + "\t\t" + Arrays.toString(gh.getPathInnerNodes(i)); } int[] expectedLengths = {3, 2, 2, 5, 4, 2, 5}; for (int i = 0; i < vertexDegrees.length; i++) { assertEquals(expectedLengths[i], gh.getCodeLength(i)); } Set<String> codeSet = new HashSet<>(); for (int i = 0; i < 7; i++) { String code = gh.getCodeString(i); assertFalse(codeSet.contains(code)); codeSet.add(code); } for (int i = 0; i < 7; i++) { String code = gh.getCodeString(i); for (int j = i + 1; j < 7; j++) { String codeOther = gh.getCodeString(j); if (code.length() == codeOther.length()) { assertNotEquals(code, codeOther); } else if (code.length() < codeOther.length()) { assertNotEquals(code, codeOther.substring(0, code.length())); } else { assertNotEquals(codeOther, code.substring(0, codeOther.length())); } } } }
public GraphHuffman(int nVertices) { this(nVertices, 64); }
GraphHuffman implements BinaryTree { public GraphHuffman(int nVertices) { this(nVertices, 64); } }
GraphHuffman implements BinaryTree { public GraphHuffman(int nVertices) { this(nVertices, 64); } GraphHuffman(int nVertices);  GraphHuffman(int nVertices, int maxCodeLength); }
GraphHuffman implements BinaryTree { public GraphHuffman(int nVertices) { this(nVertices, 64); } GraphHuffman(int nVertices);  GraphHuffman(int nVertices, int maxCodeLength); void buildTree(int[] vertexDegree); @Override long getCode(int vertexNum); @Override int getCodeLength(int vertexNum); @Override String getCodeString(int vertexNum); @Override int[] getPathInnerNodes(int vertexNum); }
GraphHuffman implements BinaryTree { public GraphHuffman(int nVertices) { this(nVertices, 64); } GraphHuffman(int nVertices);  GraphHuffman(int nVertices, int maxCodeLength); void buildTree(int[] vertexDegree); @Override long getCode(int vertexNum); @Override int getCodeLength(int vertexNum); @Override String getCodeString(int vertexNum); @Override int[] getPathInnerNodes(int vertexNum);  }