我想将霍夫曼代码保存到文件中。我怎样才能做到这一点?我将霍夫曼代码保存到字符串中,但生成的文件的大小比原始文件大。
我编写了一个程序将一篇文章编码为霍夫曼代码并输出一个代码表。
时:000 日:1011 电子:100 左:11 时:01 回复:1010 字:001 总位数:27 编码代码:000100111101001011010111011
我想编写一个程序,将文件作为输入并对其进行解码。
但我不知道如何重建它。
我的问题是如何重建哈夫曼树?
令 X 为实线上 n 个区间的集合。如果 X 中的每个区间至少包含 P 中的一个点,我们就说一组 P 点刺穿 X 。描述并分析一种有效的算法来计算刺穿 X 的最小点集。假设您的输入由两个数组 XL [1 .. n] 和 XR[1..n] 组成,分别代表 X 中区间的左端点和右端点。
有什么建议从哪里开始以及如何解决吗?贪心算法?霍夫曼的?
我需要为一个大学项目制作一棵哈夫曼树,但我真的很困惑它是如何工作的。我实现了哈夫曼树的编码部分,但它始终与http://huffman.ooz.ie/不同。
一个人向另一个人编码可能会有所不同,但正确吗?
在https://www.rfc-editor.org/rfc/rfc1951
Note that in the "deflate" format, the Huffman codes for the
various alphabets must not exceed certain maximum code lengths.
Run Code Online (Sandbox Code Playgroud)
最大代码长度定义为 15。
当霍夫曼码长度超过15时会发生什么?
来自https://cs.stackexchange.com/questions/75542/maximum-size-of-huffman-codes-for-an-alphabet-containing-256-letters 256 个符号字母表的最大可能代码大小是 256 位。考虑以下情况:最频繁的符号的频率为 1/2,下一个最频繁的符号的频率为 1/4,然后是 1/8
因此,在文字/长度字母表中,最大霍夫曼代码长度为 285-1=284,但在 zlib 中,最大代码长度为 15。
我正在尝试使用霍夫曼编码为一组符号创建最佳编码。然而,对编码施加了约束,使得没有编码包含字符串“00”。
例如,编码“A”=“0”和“B”=“10”将不满足约束,因为字符串“BA”编码为“100”,其中包含“00”子字符串。
这意味着代码字也不能包含字符串“00”。例如,编码“A”=“1”、B=“00”和C=“01”将不满足约束,因为编码“B”总是会导致“00”出现在编码中。
我尝试修改维基百科上找到的霍夫曼编码算法:
还有一种情况是队列中只剩下两个节点都是非叶节点。我不知道如何解决这个问题。否则,我相信这会创建一个满足约束的编码,但我不确定它是否是最优的,并且我想确定它是最优的。
使霍夫曼树脱水的最佳方法是什么,通过脱水我的意思是给霍夫曼树,以及每片叶子中的字符,您如何才能有效地存储此树的结构并随后对其进行重构。
采取下面的树:
---------------garbage------
-------------/-------\------
------------A-------garbage-
--------------------/-----\-
-------------------B-------C-
Run Code Online (Sandbox Code Playgroud)
一个想法可能是将符号存储在每个级别,然后使用此信息来重构树。在这种情况下:A1B2C2。因此,我如何首先获得关卡,并将每个关卡与角色相关联。
我是Haskell的新手,我正在尝试创建一个霍夫曼树,直到最后我都无法弄明白.
我对树的定义如下: data HuffTree = Node Int HuffTree HuffTree | Leaf (Int, Char)
到目前为止,我有一个函数insTree :: HuffTree -> HuffTree -> HuffTree,在树中插入带有子树的Node并返回新树.一个函数makePair :: HuffTree -> HuffTree -> HuffTree,它接受两棵树,并使一个新的树具有原始两棵树的子树,并且值为前两棵树中的值的总和.以及value :: HuffTree -> Int从每个节点返回值的函数.
我的问题是makeHuffTree :: [(Int, Char)] -> HuffTree看起来像这样的功能:
makeHuffTree :: [(Int, Char)] -> HuffTree
makeHuffTree lst = merge leafList
where
leafList = map (\ ((x,c)) -> Leaf (x,c)) lst
merge [] = []
merge [t] = [t]
merge (t1 : t2 : tree) = …Run Code Online (Sandbox Code Playgroud) 我不确定我将如何攻击我的霍夫曼树的穿越.树是正确的,我只是很难确定如何以一种好的方式遍历它.出于某种原因,我的遍历方法没有结果......
更新:清理代码,使其更加面向对象
节点类:
public class Node
{
public int frekvens; //Frequency
public char tegn; //Symbol
public Node venstre; //Left child
public Node høyre; //Right child
public string s; //result string
public string resultat;
public Node (char c) // Node constructor containing symbol.
{
frekvens = 1;
tegn = c;
}
public Node (int f, Node venstre, Node høyre) // Node Constructor containing frequency and children
{
frekvens = f;
this.venstre = venstre;
this.høyre = høyre;
}
public Node (Node node) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用霍夫曼编码实现文件压缩.目前,我正在将标题写为压缩文件的第一行,然后编写编码的二进制字符串(即具有二进制编码值的字符串).
但是,不是减小文件大小,我的文件大小正在增加,就像'a'这样的每个字符一样,我正在编写相应的二进制文件,例如01010001需要更多的空间.
如何以缩小空间的方式将其写入文件?
这是我的代码
public void write( String aWord ) {
counter++;
String content;
byte[] contentInBytes;
//Write header before writing file contents
if ( counter == 1 )
{
//content gets the header in String format from the tree
content = myTree.myHeader;
contentInBytes = content.getBytes();
try {
fileOutputStream.write(contentInBytes);
fileOutputStream.write(System.getProperty("line.separator").getBytes());
} catch (IOException e) {
System.err.println(e);
}
}
//content gets the encoded binary in String format from the tree
content = myTree.writeMe(aWord);
contentInBytes = content.getBytes();
try {
fileOutputStream.write(contentInBytes);
fileOutputStream.write(System.getProperty("line.separator").getBytes());
} catch …Run Code Online (Sandbox Code Playgroud)