1、 friend class MyHuffmanTree;public: char charater;/元素值(字符) int weight;/权值(频率) string code;/Huffman编码 HTNode *left;/左孩子 HTNode *right;/右孩子 HTNode (char c=0, int w=0) charater=c; weight=w; left=right=NULL; bool operator (HTNode & target) if( weight target.weight ) return true; else return false; /oper
2、ator (HTNode * target) target-weight ) friend ostream & operator (ostream &cout, HTNode * target) coutcharatertweight:weightcoderight & NULL=target-left) void HuffmanCode(HTNode *root) /以前序周游的思想编码 root-code=;/root的Huffman编码 stack aStack;/存储未访问元素的栈 HTNode * temp =root; aStack.push(NULL);/栈底监视哨 while(
3、temp) if(NULL != temp-right ) temp-right-code=temp-code + 1 aStack.push(temp-right); if( NULL !left )left-0 temp=temp-left; else/左子树访问完毕,转向访问右子树 temp=aStack.top(); aStack.pop(); /while /Huffman编码函数 MyHuffmanTree(HTNode * r) root=r; HuffmanCode(root); /构造函数 MyHuffmanTree()DeleteTree(root); /析构函数 void
4、 DeleteTree(HTNode *root) if(NULL!=root) DeleteTree(root-left); delete root; /销毁树函数 void InOrder(HTNode * root) InOrder(root- visit(root); /中序周游 HTNode * Root() return root;/返回跟结点函数main文件:#include HuffmanTree.hMinHeap.hfstreamint main () int f128=0;/频率表 char c;/读入字符 /打开文件 ifstream fin(input.txt); if
5、(!fin) coutc; fc+; MinHeap heap(128); HTNode * p;/建堆用指针 HTNode *first , * second , *pa; for(int i=0;iweight+second-weight); pa-left=first;right=second; heap.insert(pa); pa=heap.min(); MyHuffmanTree mht(pa); mht.InOrder(mht.Root();最小堆MinHeap.h文件:template class MinHeap T * heapArray ; /存放堆数据的数组 int Cu
6、rrentSize ; /当前堆中的元素数目 int MaxSize ; /最大元素数目 void swap( int pos_x, int pos_y ) /交换位置xy的元素的函数 if( pos_x0 | pos_y0 ) return ; T temp ; temp=heapArraypos_x; heapArraypos_x=heapArraypos_y; heapArraypos_y=temp; MinHeap( const int size ) /构造函数,size为堆的最大元素数目 if( size= CurrentSize/2) & ( pos CurrentSize );
7、int leftchild( int pos ) /返回左孩子位置的函数 return ( 2 * pos + 1 ); int rightchild( int pos )/返回右孩子位置的函数 return ( 2 * pos + 2 ); int Parent( int pos ) /返回父结点位置的函数 return ( (pos-1) / 2 ); int Pos( T value ) /找出值为value的元素在堆中位置的函数 iMaxSize; i+) if( heapArrayi = value ) return i ; return -1 ;/pos bool insert(
8、const T & newNode )/向堆中插入新元素newNode /判断堆是否已满 if ( CurrentSize = MaxSize )/堆已满堆已满 return false; /堆未满 heapArrayCurrentSize= newNode ;/新元素newNode放到堆的末尾 SiftUp( CurrentSize ); /向上调整 CurrentSize + ; /堆的当前元素数加1 return true ; /insert bool remove( int pos)/删除给定下标的元素 /判断下标是否越界 if ( (pos = CurrentSize) ) retu
9、rn false ; /下标未越界 heapArraypos=heapArray-CurrentSize;/用最后的元素值代替被删除的元素 if( pos = 0 ) SiftDown(pos); if ( heapArrayParent(pos) heapArraypos ) SiftUp(pos); else SiftDown(pos); /remove T & min() /返回最小值元素 return heapArray0 ; void SiftUp( int pos ) /从pos开始向上调整 int temppos=pos; T temp = heapArraytemppos; w
10、hile( (temppos0) & (heapArrayParent(temppos) temp) ) heapArraytemppos=heapArrayParent(temppos); temppos=Parent(temppos); heapArraytemppos=temp ; /SiftUp void SiftDown ( int pos )/从pos开始向下调整 int i = pos ; /标识父结点 int j = leftchild(i);/用于记录关键值较小的子结点 T temp = heapArrayi;/记录父结点元素值 while( j CurrentSize) i
11、f( (j heapArrayj+1) )/若有右子结点且小于左子结点 j+; /j指向小的右子结点 if ( temp heapArrayj )/若父结点的值大于子结点的值则交换 heapArrayi=heapArrayj; i=j; j=leftchild(j); else break ; /父结点 1) SiftDown(0); return heapArrayCurrentSize; /else /RemoveMin int GetCurrentSize() return CurrentSize ;程序运行结果:输入:2 4 6 8 10输出: 权:1 3 5 7 9三、总结和分析经过
12、这次程序设计,我深刻的认识到,编写的程序不但要拿来使用,还要给别人查看,以便代码的维护。所以代码编写的风格尽量规范,清晰。代码较为冗余,可读性较差,可以多添加一些提示语句以及注释。在这次课程设计中我又进一步地了解了数据结构中算法的核心思想的重要性,懂得了一个程序地好坏关键在于算法是否优秀,一个好的优秀的算法可以使我们的程序更加完善,安全性更高以及有更高的效率。这次设计中我发现了自己的许多不足,更深入的了解了数据结构的思想,明白了理论知识和实际操作编写程序是密不可分的。同时,也明白了同学之间团结合作的重要性。指导老师评语:成绩评定: 指导老师签名:基地指导老师签名:年 月 日教研室意见教研室主任签章学院意见分管院长签章
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1