import java.util.HashMap; import java.util.Map; public class TreeNode implements Comparable { private TreeNode leftChild = null; private TreeNode rightChild = null; private TreeNode parent = null; private String value = null; private int frequency = 0; TreeNode() { this.leftChild = null; this.rightChild = null; this.parent = null; this.value = null; this.frequency = 0; } TreeNode(TreeNode a, TreeNode b) { frequency = a.getFrequency() + b.getFrequency(); leftChild = a; rightChild = b; a.setParent(this); b.setParent(this); } TreeNode(String v, int f) { value = v; frequency = f; } private void addToMap(String prefix, Map h) { if (value != null) { h.put(value, prefix); } else { leftChild.addToMap(prefix + '0', h); rightChild.addToMap(prefix + '1', h); } } public Map getMap() { HashMap h = new HashMap<>(); addToMap("", h); return h; } public void setLeftChild(TreeNode child) { this.leftChild = child; } public void setRightChild(TreeNode child) { this.rightChild = child; } public void setParent(TreeNode parent) { this.parent = parent; } public void setValue(String value) { this.value = value; } public void setFrequency(int frequency) { this.frequency = frequency; } public TreeNode getLeftChild() { return this.leftChild; } public TreeNode getRightChild() { return this.rightChild; } public TreeNode getParent() { return this.parent; } public String getValue() { return this.value; } public int getFrequency() { return this.frequency; } @Override public int compareTo(TreeNode o) { return new Integer(frequency).compareTo(o.getFrequency()); } }