怪物猎人2g老山蟹肉质:急啊!作业题(关于哈夫曼函数的编程题)

来源:百度文库 编辑:高校问答 时间:2024/05/05 22:59:46
class LettFreq {
private char lett; // A letter
private int freq; // Frequenc

public LettFreq(int f, char l) {
freq = f;
lett = l;
}
public LettFreq(int f) {
freq = f;
}

public int weight() { return freq; }
public char letter() { return lett; };
}

class HuffTree {
private BinNode root;

public HuffTree(LettFreq val){
root = new BinNode(val);
}
public HuffTree(LettFreq val, HuffTree l, HuffTree r){
root = new BinNode(val, l.root(), r.root());
}

public BinNode root(){
return root;
}
public int weight() {
return ((LettFreq)root.element()).weight();
}

public HuffTree buildTree(MyList hufflist){
HuffTree temp1, temp2, temp3;
LettFreq tempnode;

hufflist.sort();
while(hufflist.size()>=2){
hufflist.setFirst();
temp1 = (HuffTree)hufflist.remove();
temp2 = (HuffTree)hufflist.remove();
tempnode = new LettFreq(temp1.weight() + temp2.weight());
temp3 = new HuffTree(tempnode, temp1, temp2);

for(hufflist.setFirst();hufflist.isInList();hufflist.next()){
if(temp3.weight() >= ((HuffTree)(hufflist.currValue())).weight()){
hufflist.insert(temp3);
break;
}
}
if(!hufflist.isInList())
hufflist.append(temp3);
}
hufflist.setFirst();
return (HuffTree)hufflist.remove();
}
}
修改上面的程序,使之能运行:
增加BinNode 类;
使用队列代替MyList ;
参考教材上的其它例子,增加一个HuffmanApp 类,作为程序执行的入口。

倒~~你不把题目算法详细写出来!!这么长的程序~谁有兴趣给你看啊