递归搜索树以获得字符的二进制编码

发布于 2024-09-10 21:10:58 字数 1937 浏览 6 评论 0原文

您好,我正在尝试找出如何递归搜索树以查找字符和二进制代码以找到该字符。基本上,目标是找到角色的代码,然后将其写入文件。文件编写器部分我可以做没有问题,但真正的问题是将二进制代码放入字符串中。当我在寻找这个角色时。请帮忙!

这是递归方法的代码:

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;


}

这是Frequency 类:

package huffman;

public classFrequency Implements Comparable { 私有字符串; 私有 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);

}

}

Hi im trying to figure out how to recursively search a tree to find a character and the binary code to get to that character. basically the goal is to go find the code for the character and then write it to a file. the file writer part i can do no problem but the real problem is putting the binary code into a string. while im searching for the character. please help!

this is the code for the recursive method:

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;


}

and this is the Frequency class:

package huffman;

public class Frequency implements Comparable {
private String s;
private int n;
public Frequency left;
public Frequency right;
private String biNum;
private String leaf;

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);

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无悔心 2024-09-17 21:10:58

在您的更新版本中,我认为您应该重新检查 return biNum(root.left, temp, letter);。具体来说,如果整个树的根节点有一个不是叶子的左子节点(因此 root.left.getString() == null),但您寻找的值是从整棵树的根节点的右子节点。

例如,考虑这棵树:

          26
         /  \
        /    \
       /      \
      11      15
     /  \    /  \
    /    B  A    \
   6     5  6     9
  / \            / \
 D   \          C  sp
 3    3         4  5
     / \
    E   F
    2   1

并跟踪您的函数在查找字母 C 时将遵循的步骤。

也许您应该考虑遍历整个树(并构建 1 和 0 的模式)而不查找任何特定字母,但是当找到叶节点时采取特定操作?

In your updated version, I think you should re-examine return biNum(root.left, temp, letter);. Specifically, what happens if the root node of the entire tree has a left child which is not a leaf (and thus root.left.getString() == null) but the value you seek descends from the right child of the root node of the entire tree.

Consider this tree, for example:

          26
         /  \
        /    \
       /      \
      11      15
     /  \    /  \
    /    B  A    \
   6     5  6     9
  / \            / \
 D   \          C  sp
 3    3         4  5
     / \
    E   F
    2   1

and trace the steps your function will follow looking for the letter C.

Perhaps you should consider traversing the entire tree (and building up the pattern of 1s and 0s as you go) without looking for any specific letter but taking a particular action when you find a leaf node?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文