+/EE360C
algorithms
git clone https://git.julia.blue/+/EE360C
381a8ab
master
1import java .util .HashMap ; 2import java .util .Map ; 3 4public class TreeNode implements Comparable <TreeNode > { 5private TreeNode leftChild =null ; 6private TreeNode rightChild =null ; 7private TreeNode parent =null ; 8private String value =null ; 9private int frequency =0 ; 10 11TreeNode () { 12this .leftChild =null ; 13this .rightChild =null ; 14this .parent =null ; 15this .value =null ; 16this .frequency =0 ; 17 } 18 19TreeNode (TreeNode a ,TreeNode b ) { 20frequency =a .getFrequency () +b .getFrequency (); 21leftChild =a ; 22rightChild =b ; 23a .setParent (this ); 24b .setParent (this ); 25 } 26 27TreeNode (String v ,int f ) { 28value =v ; 29frequency =f ; 30 } 31 32private void addToMap (String prefix ,Map <String ,String >h ) { 33if (value !=null ) { 34h .put (value ,prefix ); 35 }else { 36leftChild .addToMap (prefix +'0' ,h ); 37rightChild .addToMap (prefix +'1' ,h ); 38 } 39 } 40 41public Map <String ,String >getMap () { 42HashMap <String ,String >h =new HashMap <>(); 43addToMap ("" ,h ); 44return h ; 45 } 46 47public void setLeftChild (TreeNode child ) {this .leftChild =child ; } 48 49public void setRightChild (TreeNode child ) {this .rightChild =child ; } 50 51public void setParent (TreeNode parent ) {this .parent =parent ; } 52 53public void setValue (String value ) {this .value =value ; } 54 55public void setFrequency (int frequency ) {this .frequency =frequency ; } 56 57public TreeNode getLeftChild () {return this .leftChild ; } 58 59public TreeNode getRightChild () {return this .rightChild ; } 60 61public TreeNode getParent () {return this .parent ; } 62 63public String getValue () {return this .value ; } 64 65public int getFrequency () {return this .frequency ; } 66 67@ Override 68public int compareTo (TreeNode o ) { 69return new Integer (frequency ).compareTo (o .getFrequency ()); 70 } 71}