我如何用C重新present二进制数++(用于连接霍夫曼codeR)? [英] How do I represent binary numbers in C++ (used for Huffman encoder)?
问题描述
我写我自己的霍夫曼EN codeR ,到目前为止我已经创建哈夫曼树用minHeap弹出了两个频率最低的节点,并链接到他们一个节点,然后推新节点退一(起泡,冲洗,重复,直到只有一个节点)。
I am writing my own Huffman encoder, and so far I have created the Huffman tree by using a minHeap to pop off the two lowest frequency nodes and make a node that links to them and then pushing the new node back one (lather, rinse, repeat until only one node).
所以,现在我已经创建了树,但我需要使用此树codeS分配给每个字符。我的问题是我不知道怎么了一些二进制重新presentation存储在C ++中。我记得读取无符号的字符是一个字节的标准,但我不确定。
So now I have created the tree, but I need to use this tree to assign codes to each character. My problem is I don't know how to store the binary representation of a number in C++. I remember reading that unsigned char is the standard for a byte, but I am unsure.
我知道我必须recusively遍历树,每当我打了一个叶子结点,我必须指派任何code是目前重新presenting路径相应的字符。
I know I have to recusively traverse the tree and whenever I hit a leaf node I must assign the corresponding character whatever code is current representing the path.
下面是我到目前为止有:
Here is what I have so far:
void traverseFullTree(huffmanNode* root, unsigned char curCode, unsigned char &codeBook){
if(root->leftChild == 0 && root->rightChild == 0){ //you are at a leaf node, assign curCode to root's character
codeBook[(int)root->character] = curCode;
}else{ //root has children, recurse into them with the currentCodes updated for right and left branch
traverseFullTree(root->leftChild, **CURRENT CODE SHIFTED WITH A 0**, codeBook );
traverseFullTree(root->rightChild, **CURRENT CODE SHIFTED WITH A 1**, codeBook);
}
return 0;
}
codeBook是我的阵列有最多256个字符的codeS(为ASCII每个可能的字符)的地方,但我只打算实际分配codeS到出现值在树。
CodeBook is my array that has a place for the codes of up to 256 characters (for each possible character in ASCII), but I am only going to actually assign codes to values that appear in the tree.
我不知道这是否是我的遍历哈夫曼树的corrent方式,但这是什么,立刻似乎工作(虽然我没有测试过)。另外我怎么叫没有零或一整棵树的根的摆频功能(树的顶部)?
I am not sure if this is the corrent way to traverse my Huffman tree, but this is what immediately seems to work (though I haven't tested it). Also how do I call the traverse function of the root of the whole tree with no zeros OR ones (the very top of the tree)?
我应该使用一个字符串,而不是和追加到字符串,要么是零或1?
Should I be using a string instead and appending to the string either a zero or a 1?
推荐答案
由于计算机是二进制...在C / C ++的所有号码都已经二进制格式。
Since computers are binary ... ALL numbers in C/C++ are already in binary format.
int a = 10;
变量 A
是二进制数。
您想看看什么是位操作,运营商,如&安培; | << >方式>
What you want to look at is bit manipulation, operators such as & | << >>
.
随着霍夫曼编码,您将下将数据打包成字节数组。
With the Huffman encoding, you would pack the data down into an array of bytes.
这是因为我已经编写的C很长一段时间,所以这是一个现成的袖口伪code ...
It's been a long time since I've written C, so this is an "off-the-cuff" pseudo-code...
完全未经测试 - 但应该给你正确的想法。
Totally untested -- but should give you the right idea.
char buffer[1000]; // This is the buffer we are writing to -- calc the size out ahead of time or build it dynamically as go with malloc/ remalloc.
void set_bit(bit_position) {
int byte = bit_position / 8;
int bit = bit_position % 8;
// From http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c
byte |= 1 << bit;
}
void clear_bit(bit_position) {
int byte = bit_position / 8;
int bit = bit_position % 8;
// From http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c
bite &= ~(1 << bit);
}
// and in your loop, you'd just call these functions to set the bit number.
set_bit(0);
clear_bit(1);
这篇关于我如何用C重新present二进制数++(用于连接霍夫曼codeR)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!