本文整理汇总了C++中BinaryTree::generateMapping方法的典型用法代码示例。如果您正苦于以下问题:C++ BinaryTree::generateMapping方法的具体用法?C++ BinaryTree::generateMapping怎么用?C++ BinaryTree::generateMapping使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinaryTree
的用法示例。
在下文中一共展示了BinaryTree::generateMapping方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: LL
std::vector<bool>* Huffman::compress(const char* input, const size_t& size) {
encode = true;
LL* linkedlist = new LL();
unsigned int ll_size = 0;
for(unsigned int i = 0; i < size; i++) {
int* freq = linkedlist->getFreq(input[i]);
if(freq == NULL) {
linkedlist->insert(new Node(new char(input[i])));
ll_size++;
}else{
(*freq)++;
}
}
// sort linked list
// I will use bubblesort, because we will have not so many
Node* t0;
for(int j = ll_size - 1; j > 0; j--) {
for(int i = 0; i < j; i++) {
LL* ll_f = linkedlist->get(i);
LL* ll_s = linkedlist->get(i+1);
if(ll_f->tree->freq > ll_s->tree->freq) {
t0 = ll_f->tree;
ll_f->tree = ll_s->tree;
ll_s->tree = t0;
}
}
}
BinaryTree* bst = new BinaryTree(linkedlist, ll_size);
std::unordered_map<char, std::vector<bool> >* map = new std::unordered_map<char, std::vector<bool> >();
bst->generateMapping(map);
encoded = (void*)(new std::vector<bool>());
for(unsigned int i = 0; i < size; i++) {
std::vector<bool>::iterator iter = map->at(input[i]).begin();
while(iter != map->at(input[i]).end()) {
((std::vector<bool>*)encoded)->push_back(*iter);
iter++;
}
}
delete map;
this->encoding = new Huffman::Encoding();
this->encoding->data = new std::vector<char>();
this->encoding->freqs = new std::vector<int>();
for(int i = ll_size - 1; i >= 0 ; i--) {
this->encoding->data->push_back(*(linkedlist->get(i)->tree->character));
this->encoding->freqs->push_back(linkedlist->get(i)->tree->freq);
}
delete linkedlist;
delete bst;
return (std::vector<bool>*)encoded;
}