本文实例为大家分享了哈夫曼树 java 代码 , 供大家参考 具体内容如下
boom del paquete; import java.util.arraydeque; import java.util.arrayList; import java.util.collections; import java.util.list; import java.util.queue; El nodo de clase <T> implementa comparable <nodo <T>> {datos privados t; Peso privado int; nodo privado <t> izquierda; nodo privado <t> derecho; nodo público (t data, int weight) {this.data = data; this.weight = peso; } public int Compareto (nodo <t> otro) {if (this.weight> other.getweight ()) {return -1; } if (this.weight <other.getWeight ()) {return 1; } return 0; } public t getData () {return data; } public void setData (t data) {this.data = data; } public int getPeight () {return peso; } public void setweight (int weight) {this.weight = weight; } nodo público <t> getleft () {return a la izquierda; } public void setleft (nodo <t> izquierda) {this.left = izquierda; } nodo público <t> getRight () {return Right; } public void setRight (nodo <t> derecho) {this.right = right; } public String toString () {return "Data:"+this.data+"; peso:"+this.weight; }} clase pública Huffuman <T> {static <t> nodo <t> create (list <node <t>> nodos) {while (nodo.size ()> 1) {colección.sort (nodos); Nodo <t> left = nodo.get (nodo.size ()-1); Nodo <t> right = nodo.get (nodo.size ()-2); Nodo <t> parent = nuevo nodo <t> (null, left.getweight ()+right.getWeight ()); parent.setRight (derecho); parent.setleft (izquierda); nodos.remove (izquierda); nodos.remove (derecha); nodos.add (padre); } return nodos.get (0); } static <t> list <node <t>> amplitud (nodo <t> root) {list <node <t>> list = new ArrayList <node <t> (); Cola <node <t>> queue = new ArrayDeque <node <t>> (); cola.offer (raíz); while (queue.size ()> 0) {nodo <t> out = queue.poll (); list.add (out); if (out.getleft ()! = null) {queue.offer (out.getleft ()); } if (out.getRight ()! = null) {queue.offer (out.getRight ()); }} lista de retorno; } public static void main (string [] args) {// TODO Método Generado Autorgenerado Lista de botes <nodo <string>> list = new ArrayList <node <String> (); list.Add (nuevo nodo <string> ("A", 7)); list.Add (nuevo nodo <string> ("b", 5)); list.Add (nuevo nodo <String> ("C", 4)); list.Add (nuevo nodo <String> ("D", 2)); Nodo <String> root = huffuman.create (lista); System.out.println (huffuman.breadth (raíz)); // System.out.println (lista); }}以上就是本文的全部内容 , 希望对大家的学习有所帮助 也希望大家多多支持武林网。 也希望大家多多支持武林网。