我对如何从预扫描中构建霍夫曼树感到有些困惑。我对标题进行了类似于here
的说明但是我不确定如何读取扫描并构建树,我的扫描看起来像这样
01a001c1b001f1e1d#(#标签用于中断循环)
这是我尝试构建的树,尽管不太正确
HuffmanNode rootTest=new HuffmanNode((byte)preOrderString.charAt(0));
Stack<HuffmanNode> s = new Stack<HuffmanNode>();
HuffmanNode current=rootTest;
root=current;
s.push(current);
for(int i=1;i<preOrderString.length();i++) {
if(preOrderString.charAt(i)=='0') {
HuffmanNode next=new HuffmanNode((byte)preOrderString.charAt(i));
if(current.left==null)
current.left=next;
else
current.right=next;
current=next;
System.out.println(current);
s.push(current);
}
if(preOrderString.charAt(i)=='1') {
i++;
if(current.left==null) {
current.left=new HuffmanNode((byte)preOrderString.charAt(i));
System.out.println("left "+current.left);
}
else if(current.right==null){
current.right=new HuffmanNode((byte)preOrderString.charAt(i));
System.out.println("right "+current.right);
}
while(!s.isEmpty()) {
current=s.pop();
}
}
}