1
嗨,即時通訊嘗試找出如何遞歸搜索樹找到一個字符和二進制代碼來獲得該字符。基本上我們的目標是去查找字符的代碼,然後將其寫入文件。文件編寫器部分我可以做任何問題,但真正的問題是將二進制代碼放入一個字符串中。而即時通訊正在尋找角色。請幫忙!遞歸搜索一棵樹,得到一個字符的二進制編碼
這是遞歸方法的代碼:
public String biNum(Frequency root, String temp, String letter)
{
//String temp = "";
boolean wentLeft = false;
if(root.getString() == null && !wentLeft)
{
if(root.left.getString() == null)
{
temp = temp + "0";
return biNum(root.left, temp, letter);
}
if(root.left.getString().equals(letter))
{
return temp = temp + "0";
}
else
{
wentLeft = true;
temp = temp.substring(0, temp.length() - 1);
return temp;
}
}
if(root.getString() == null && wentLeft)
{
if(root.right.getString() == null)
{
temp = temp + "1";
return (biNum(root.right, temp, letter));
}
if(root.right.getString().equals(letter))
{
return temp = temp + "1";
}
else
{
wentLeft = false;
temp = temp.substring(0, temp.length() - 1);
return temp;
}
}
return temp;
}
,這是頻率類:
package huffman;
公共類頻率實現可比{ 私人字符串S; private int n; 公共頻率剩下; 公衆頻率權利; 私人字符串biNum; 私人字符串葉;
Frequency(String s, int n, String biNum)
{
this.s = s;
this.n = n;
this.biNum = biNum;
}
public String getString()
{
return s;
}
public int getFreq()
{
return n;
}
public void setFreq(int n)
{
this.n = n;
}
public String getLeaf()
{
return leaf;
}
public void setLeaf()
{
this.leaf = "leaf";
}
@Override
public int compareTo(Object arg0) {
Frequency other = (Frequency)arg0;
return n < other.n ? -1 : (n == other.n ? 0 : 1);
}
}