Node:
package demo8;
public class Node implements Comparable<Node>{
int value;
Node left;
Node right;
public Node(int value) {
this.value=value;
}
@Override
public int compareTo(Node o) {
return this.value-o.value;
}
@Override
public String toString() {
return "Node [value=" + value + "]";
}
}
TestHuffmanTree:
package demo8;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestHuffmanTree {
public static void main(String[] args) {
int[] arr= {3,7,8,29,5,11,23,14};
Node node=createHuffmanTree(arr);
System.out.println(node);
}
private static Node createHuffmanTree(int[] arr) {
List<Node> nodes=new ArrayList<>();
for(int value:arr) {
nodes.add(new Node(value));
}
while(nodes.size()>1) {
Collections.sort(nodes);
Node left=nodes.get(nodes.size( )-1);
Node right=nodes.get(nodes.size()-2);
Node parent=new Node(left.value+right.value);
nodes.remove(left);
nodes.remove(right);
nodes.add(parent);
}
return nodes.get(0);
}
}