计算Huffman树的带权路径长度WPL(C++优先队列实现)
1 构建一棵Huffman树
typedef struct HTreeNode
{
DataType data;
HTreeNode *lchild,*rchild;
int level;
}HTreeNode,*HTree;
HTree HuffmanTree(priority_queue<HTree,vector<HTree>,cmp> &q){
while (q.size()>1)
{
HTree h1=q.top();
q.pop();
HTree h2=q.top();
q.pop();
HTree h=(HTreeNode*)malloc(sizeof(HTreeNode));
h->lchild=h1;
h->rchild=h2;
h->data=h1->data+h2->data;
q.push(h);
}
return q.top();
}
2 计算Huffman树中指定结点所在的层次
int getLevel(HTree tree,HTreeNode* p){
if(!tree)
return 0;
if(tree==p){
return 1;
}
int lLevel=getLevel(tree->lchild,p);
int rLevel=getLevel(tree->rchild,p);
if (lLevel || rLevel)
{
return lLevel>rLevel? lLevel+1:rLevel+1;
}
return 0;
}
3 设置Huffman树中所有结点的层次
void setTNodeDepth(HTree &tree){
HTree t=tree;
queue<HTree> q;
q.push(tree);
while(!q.empty()){
HTree h=q.front();
q.pop();
h->level=getLevel(tree,h);
if(h->lchild)
q.push(h->lchild);
if(h->rchild)
q.push(h->rchild);
}
}
4 计算Huffman树的带权路径长度WPL
int getHuffmanTreeWPL(HTree tree){
HTree t=tree;
int HuffmanTreeWPL=0;
queue<HTree> q;
q.push(tree);
while(!q.empty()){
HTree h=q.front();
q.pop();
h->level=getLevel(tree,h);
if(!h->lchild && !h->rchild){
HuffmanTreeWPL+=(h->level*h->data);
}
if(h->lchild)
q.push(h->lchild);
if(h->rchild)
q.push(h->rchild);
}
return HuffmanTreeWPL;
}
完整代码如下所示:
#include <iostream>
#include<queue>
#include<stack>
#define N 100
#define DataType int
using namespace std;
typedef struct HTreeNode
{
DataType data;
HTreeNode *lchild,*rchild;
int level;
}HTreeNode,*HTree;
struct cmp
{
bool operator () (const HTree &a, const HTree &b)
{
return a->data>b->data;
}
};
void init(priority_queue<HTree,vector<HTree>,cmp> &q,int arr[],int len){
for(int i=0;i<len;i++){
HTree h=(HTreeNode*)malloc(sizeof(HTreeNode));
h->lchild=NULL;
h->rchild=NULL;
h->data=arr[i];
q.push(h);
}
}
void print(priority_queue<HTree,vector<HTree>,cmp> &q){
while (!q.empty())
{
HTree h=q.top();
q.pop();
cout<<h->data<<endl;
}
cout<<"--------------------------"<<endl;
}
HTree HuffmanTree(priority_queue<HTree,vector<HTree>,cmp> &q){
while (q.size()>1)
{
HTree h1=q.top();
q.pop();
HTree h2=q.top();
q.pop();
HTree h=(HTreeNode*)malloc(sizeof(HTreeNode));
h->lchild=h1;
h->rchild=h2;
h->data=h1->data+h2->data;
q.push(h);
}
return q.top();
}
int getDepth(HTree tree){
if (tree==NULL)
{
return 0;
}else{
int lDepth=getDepth(tree->lchild);
int rDepth=getDepth(tree->rchild);
return lDepth>rDepth? lDepth+1:rDepth+1;
}
}
int getLevel(HTree tree,HTreeNode* p){
if(!tree)
return 0;
if(tree==p){
return 1;
}
int lLevel=getLevel(tree->lchild,p);
int rLevel=getLevel(tree->rchild,p);
if (lLevel || rLevel)
{
return lLevel>rLevel? lLevel+1:rLevel+1;
}
return 0;
}
void setTNodeDepth(HTree &tree){
HTree t=tree;
queue<HTree> q;
q.push(tree);
while(!q.empty()){
HTree h=q.front();
q.pop();
h->level=getLevel(tree,h);
if(h->lchild)
q.push(h->lchild);
if(h->rchild)
q.push(h->rchild);
}
}
int getHuffmanTreeWPL(HTree tree){
HTree t=tree;
int HuffmanTreeWPL=0;
queue<HTree> q;
q.push(tree);
while(!q.empty()){
HTree h=q.front();
q.pop();
h->level=getLevel(tree,h);
if(!h->lchild && !h->rchild){
HuffmanTreeWPL+=(h->level*h->data);
}
if(h->lchild)
q.push(h->lchild);
if(h->rchild)
q.push(h->rchild);
}
return HuffmanTreeWPL;
}
int main(){
DataType arr[N] = {1, 8, 15, 19, 32, 17, 65, 3, 9};
int len = 9;
priority_queue<HTree,vector<HTree>,cmp> q;
init(q,arr,len);
HTree tree=HuffmanTree(q);
setTNodeDepth(tree);
cout<<getHuffmanTreeWPL(tree)<<endl;
return 0;
}
小白也是在学习数据结构,哪里学的不通透欢迎各位指正! 觉得写得不错的话,点个赞吧!
|