안녕하세요, 내가 pq로 만드는 모든 노드를 얻으려고합니다. 그래서 나는 huffman 트리를 만들 수 있도록 체중 측면에서 그들을 주문할 수 있고 처음 두 항목 (최소)을 제거 할 수 있습니다. 어떤 바이너리 트리의 전문 버전이 뭐하는 가장 좋은 방법은 무엇입니까? 감사합니다우선 순위 큐에 노드 삽입하기 자바
public class Main {
public void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
String inputFileName = args[0];
FileReader reader = new FileReader(inputFileName);
Scanner in = new Scanner(reader);
// read in the data and do the work here
// read a line at a time to enable newlines to be detected and allowed for
while(in.hasNext()){
CharacterMap<Character, Integer> hashMap = new CharacterMap<Character, Integer>();
char[] chars = scanner.nextLine().toLowerCase().toCharArray();
int c_count = 0;
for (Character c : chars) {
c_count += 1;
if (hashMap.containsKey(c)) {
hashMap.put(c, hashMap.get(c) + 1);
} else {
hashMap.put(c, 1);
}
}
PriorityQueue<Node> pq = new PriorityQueue<Node>(new Comparator<Node>() {
for (Map.Entry<Character, Integer> entry : hashMap.entrySet()){
Node n = new Node();
int f = entry.getValue();
String c = entry.getKey();
n.setWeight(f);
n.setCharacter(c);
n.setLeftChild(null);
n.setRightChild(null);
pq.add(n);
}
reader.close();
String outputFileName = args[1];
FileWriter writer = new FileWriter(outputFileName);
writer.write("Input file " + inputFileName + " Huffman algorithm\n\n");
// write out the results here
long end = System.currentTimeMillis();
writer.write("\nElapsed time: " + (end - start) + " milliseconds");
writer.close();
}
}
코드를 빠르게 스캔 한 것처럼 보입니다. 질문이 뭐야? – EJP
일단 노드 인스턴스 변수 중 하나의 무게로 큐를 정렬하는 방법을 모르겠다. –