Java 我们可以试着解码哈夫曼密码吗
给定一组字符及其对应的哈夫曼编码字符串。我们可以试着解码它们吗 我有下面的课程来说明我的方法。我确实在网上尝试了一些测试案例,但我对我的发现并不完全满意 这是我在互联网“Geeksforgeks”上找到的一个测试用例,它对应的编码字符串在main方法中作为搜索函数的参数给出。这个测试用例似乎运行良好。有人能解释为什么我们可以或为什么我们不能使用试镜吗Java 我们可以试着解码哈夫曼密码吗,java,data-structures,decoding,huffman-code,Java,Data Structures,Decoding,Huffman Code,给定一组字符及其对应的哈夫曼编码字符串。我们可以试着解码它们吗 我有下面的课程来说明我的方法。我确实在网上尝试了一些测试案例,但我对我的发现并不完全满意 这是我在互联网“Geeksforgeks”上找到的一个测试用例,它对应的编码字符串在main方法中作为搜索函数的参数给出。这个测试用例似乎运行良好。有人能解释为什么我们可以或为什么我们不能使用试镜吗 public class HuffmanDecode { static class Code { Character c;
public class HuffmanDecode {
static class Code {
Character c;
Code[] children;
boolean isEnd;
public Code(){
this.c = null;
this.children = new Code[2];
this.isEnd = false;
for(int i = 0 ; i < 2; i++){
children[i] = null;
}
}
}
static Code root;
static StringBuilder str = new StringBuilder();
public static void buildTree(String input, Code current, char ch){
for(int i = 0 ; i < input.length() ; i++){
char curr = input.charAt(i);
int index = curr - '0';
if(current.children[index] == null){
current.children[index] = new Code();
}
current = current.children[index];
}
current.isEnd = true;
current.c = ch;
}
public static String search(String input, Code current){
for(int i = 0 ; i < input.length(); i++){
char curr = input.charAt(i);
int index = curr - '0';
if(current!=null && current.isEnd){
str.append(current.c);
current = root;
i--;
}
else if(current.children[index]!=null && !current.isEnd){
current = current.children[index];
}
}
if(current!=null && current.isEnd)str.append(current.c);
return str.toString();
}
public static void main(String[] args) {
HuffmanDecode obj = new HuffmanDecode();
HashMap<Character, String> map = new HashMap<>();
root = new Code();
map.put('e',"10");
map.put('f',"1100");
map.put('g',"011");
map.put('k',"00");
map.put('o',"010");
map.put('r',"1101");
map.put('s',"111");
map.forEach((key, value)->{
obj.buildTree(value,root,key);
});
search("01110100011111000101101011101000111",root);
System.out.println(str.toString());
}
}
公共类HuffmanDecode{
静态类代码{
字符c;
代码[]儿童;
布尔isEnd;
公共代码(){
这个.c=null;
this.children=新代码[2];
this.isEnd=false;
对于(int i=0;i<2;i++){
children[i]=null;
}
}
}
静态代码根;
静态StringBuilder str=新StringBuilder();
公共静态void构建树(字符串输入、当前代码、字符){
对于(int i=0;i{
对象构建树(值、根、键);
});
搜索(“01110101111000101101011101000111”,根);
System.out.println(str.toString());
}
}
是的,可以使用Trie将字符编码和解码为位表示,前提是它是一个二叉树。没有二进制结构的trie将不可解码,因为在通过trie中每个节点的潜在字符值进行解析时,可能会有一些字符最终无法访问,因为它们与trie结构中较高的节点表示的字符共享前缀。例如,如果a B用代码001表示,a C用代码001111表示,则解码算法无法到达表示字母C的节点,因为它将在到达父值B时返回。这将导致无法解码包含字母C的任何语句,因此,使非二进制trie在编码或解码一组哈夫曼编码字符时无效。然而,给定一个二进制trie,表示一个字符的每个节点都将表示为trie中的一个叶值,这意味着每个编码字符都将有一个“前缀码”确保其父节点中没有字符表示,从而确保解码算法可以实现字符表示。是的,可以使用Trie将字符编码和解码为位表示,前提是它是一个二叉树。没有二进制结构的trie将不可解码,因为在通过trie中每个节点的潜在字符值进行解析时,可能会有一些字符最终无法访问,因为它们与trie结构中较高的节点表示的字符共享前缀。例如,如果a B用代码001表示,a C用代码001111表示,则解码算法无法到达表示字母C的节点,因为它将在到达父值B时返回。这将导致无法解码包含字母C的任何语句,因此,使非二进制trie在编码或解码一组哈夫曼编码字符时无效。然而,给定一个二进制trie,表示一个字符的每个节点都将表示为trie中的一个叶值,这意味着每个编码字符都将有一个“前缀码”确保其父节点中没有字符表示,从而确保解码算法可以实现字符表示。这是trie的理想用例。你有什么问题?我只是想确认我的方法是否适用于所有提供给定输入格式的测试用例。这是trie的理想用例。你有什么问题?我只是想确认我的方法是否适用于为输入提供给定格式的所有测试用例。