Java 代码 , 供大家参考 , 具体内容如下
pacote boom; importar java.util.arraydeque; importar java.util.ArrayList; importar java.util.Collections; importar java.util.list; importar java.util.queue; classe Node <T> implementa comparável <nó <T>> {Dados privados t; peso privado int; nó privado <t> esquerda; nó privado <T> certo; nó público (dados t, int peso) {this.data = data; this.weight = peso; } public int compareto (nó <T> outros) {if (this.weight> outros.getweight ()) {return -1; } if (this.weight <Other.getWeight ()) {return 1; } retornar 0; } public t getData () {retornar dados; } public void setData (t data) {this.data = data; } public int getWeight () {return Weight; } public void setweight (int weight) {this.weight = peso; } nó público <T> getLeft () {return à esquerda; } public void Setleft (nó <T> Esquerda) {this.left = esquerda; } nó público <T> getright () {return Right; } public void Setright (nó <T> Right) {this.right = Right; } public string tostring () {return "Data:"+this.data+"; peso:"+this.weight; }} classe pública Huffuman <T> {static <t> nó <T> CREATE (LIST <NODE <T>> nós) {while (sodes.size ()> 1) {collectionS.Sort (nós); Nó <T> esquerda = nós.get (modes.size ()-1); Nó <T> Right = Sodes.get (Sodes.size ()-2); Nó <T> pai = novo nó <T> (null, esquerd.getWeight ()+Rightweight ()); parent.setright (à direita); parent.setLeft (esquerda); nós.Remove (à esquerda); modes.remove (à direita); nós.add (pai); } Return nós.get (0); } static <t> list <nó <T>> largura (nó <T> root) {list <nó <T>> LIST = new ArrayList <nó <T>> (); Fila <nó <T>> fila = novo ArrayDeque <nó <T>> (); fila.offer (root); while (fileue.size ()> 0) {node <t> out = fileue.poll (); list.add (out); if (out.getLeft ()! = null) {fileue.offer (out.getLeft ()); } if (out.getright ()! = null) {fileue.offer (out.getright ()); }} Lista de retorno; } public static void main (string [] args) {// TODO Method Stub List <nó <string>> list.add (novo nó <String> ("A", 7)); list.add (novo nó <String> ("b", 5)); list.add (novo nó <String> ("c", 4)); list.add (novo nó <String> ("d", 2)); Nó <String> root = huffuman.create (list); System.out.println (huffuman.breadth (root)); // system.out.println (list); }}以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。