public class HuffmanTree{ // name of node (-> char represented by node) public int name; // weight of huffmann-tree public int weight = 0; // right subtree public HuffmanTree right = null; // left subtree public HuffmanTree left = null; /** * To construct a new empty HT (subtrees are null and weight is 0). */ public HuffmanTree() { } /** * To construct a leaf of the HT with given name (subtrees are empty HTs). * * @param name * name of leaf */ public HuffmanTree(int name, int weight) { this.name = name; this.weight = weight; right = new HuffmanTree(); left = new HuffmanTree(); } /** * To construct a new HT with 2 given subtrees. * * @param left * left subtree * @param right * right subtree */ public HuffmanTree(HuffmanTree left, HuffmanTree right) { // set left subtree this.left = left; // set right subtree this.right = right; // update weight (sum of weights of both subtrees) this.weight = left.weight + right.weight; } /** * To check if HT is empty or not. * * @return true, if both subtrees are null */ public boolean isEmpty() { return left == null && right == null; } /** * To check if HT is a leaf. * * @return true, if actual HT is non-empty, but both subtrees are */ public boolean isLeaf() { return !isEmpty() && left.isEmpty() && right.isEmpty(); } public BitString[] encode(BitString[] codeTable, BitString path) { if (isLeaf()) { codeTable[name] = path; return null; } right.encode(codeTable, path.clone().append(true)); left.encode(codeTable, path.clone().append(false)); return codeTable; }}