当前位置: 首页>>代码示例>>C++>>正文


C++ BinaryTree::generateMapping方法代码示例

本文整理汇总了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;
}
开发者ID:realm01,项目名称:huffman-cpp,代码行数:62,代码来源:huffman.cpp


注:本文中的BinaryTree::generateMapping方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。