本文实例为大家分享了哈夫曼树 Java a, 供大家参考, 具体内容如下
패키지 붐; import java.util.arraydeque; import java.util.arraylist; import java.util.collections; import java.util.list; import java.util.queue; 클래스 노드 <t>는 비슷한 <node <t >> {private t data; 개인 INT 중량; 개인 노드 <t> 왼쪽; 개인 노드 <t> 오른쪽; public node (t data, int weight) {this.data = data; this.weight = weight; } public int compareto (node <t> 기타) {if (this.weight> else.getweight ()) {return -1; } if (this.weight <other.getweight ()) {return 1; } 반환 0; } public t getData () {반환 데이터; } public void setData (t data) {this.data = data; } public int getweight () {return weight; } public void setweight (int weight) {this.weight = weight; } public node <T> getLeft () {return Left; } public void setleft (node <t> 왼쪽) {this.left = 왼쪽; } public node <t> getright () {return right; } public void setright (노드 <t> 오른쪽) {this.right = 오른쪽; } public String toString () {return "data :"+this.data+"; weight :"+this.weight; }} public class huffuman <t> {static <t> node <t> create (list <node <t >> nodes) {while (nodes.size ()> 1) {collections.sort (nodes); 노드 <t> left = nodes.get (nodes.size () -1); 노드 <t> right = nodes.get (nodes.size () -2); 노드 <t> parent = new Node <t> (null, left.getweight ()+right.getweight ()); parent.setright (오른쪽); parent.setleft (왼쪽); 노드 .remove (왼쪽); 노드 .remove (오른쪽); 노드 .add (부모); } return nodes.get (0); } static <t> list <node <t >> 너비 (node <t> root) {list <node <t >> list = new arraylist <node <t >> (); 큐 <node <t >> queue = new Arraydeque <node <t >> (); queue.offer (루트); while (queue.size ()> 0) {node <t> out = queue.poll (); list.add (out); if (out.getleft ()! = null) {queue.offer (out.getleft ()); } if (out.getright ()! = null) {queue.offer (out.getright ()); }} 리턴 목록; } public static void main (string [] args) {// todo 자동 생성 메소드 스텁 목록 <node <문자열 >> list = new arraylist <node <string>> (); list.add (새 노드 <string> ( "a", 7)); list.add (new node <string> ( "b", 5)); list.add (new node <string> ( "c", 4)); list.add (new node <string> ( "d", 2)); 노드 <string> root = huffuman.create (list); System.out.println (huffuman.breadth (root)); // system.out.println (list); }}以上就是本文的全部内容 以上就是本文的全部内容, 希望对大家的学习有所帮助, 也希望大家多多支持武林网。