HuffmanTree.h
#ifndef HUFFMANTREE_H #define HUFFMANTREE_H #include#include using namespace std; struct HuffmanNode { char data; double weight; int parent,lchild,rchild; }; class HuffmanTree { private: vector hufftree; int n; public: HuffmanTree(vector &leafs); ~HuffmanTree(){} vector GetCode(int i); string Decode(vector &source); void SelectSmall(int &least,int &less,int n); }; #endif
HuffmanTree.cpp
#include#include #include #include"HuffmanTree.h" using namespace std; void HuffmanTree::SelectSmall(int &least,int &less,int n) { int i = 0; while (hufftree[i].parent!=-1 && i =least && hufftree[j].weight &leafs) { int i; int n=leafs.size(); hufftree.resize(2*n-1); for(i=0;i HuffmanTree::GetCode(int i) { vector code; int p=i; int parent=hufftree[i].parent; while(parent!=-1) { if (hufftree[parent].lchild==p) code.insert(code.begin (),0); else code.insert (code.begin (),1); p=parent; parent=hufftree[parent].parent; } return code; } string HuffmanTree::Decode(vector &source) { string target=""; int root=hufftree.size ()-1; int p=root; int i; for(i=0;i main.cpp
#include#include #include #include"HuffmanTree.h" using namespace std; void main() { vector a; HuffmanNode a1; int i,j; char c; double w; cout<<"请输入元素值(5个数据与权值):"< >c>>w; a1.data = c; a1.weight = w; a.push_back(a1); } HuffmanTree t(a); vector num; string s; for (i =0;i<5;i++) { num = t.GetCode(i); cout<<"第"< 欢迎分享,转载请注明来源:内存溢出
评论列表(0条)