给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree)。哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近。
基本术语
哈夫曼树(霍夫曼树)又称为最优树.
1、路径和路径长度
在一棵树中,从一个结点往下可以达到的孩子或孙子结点之间的通路,称为路径。通路中分支的数目称为路径长度。若规定根结点的层数为1,则从根结点到第L层结点的路径长度为L-1。
2、结点的权及带权路径长度
若将树中结点赋给一个有着某种含义的数值,则这个数值称为该结点的权。结点的带权路径长度为:从根结点到该结点之间的路径长度与该结点的权的乘积。
3、树的带权路径长度
树的带权路径长度规定为所有叶子结点的带权路径长度之和,记为WPL。
构造
假设有n个权值,则构造出的哈夫曼树有n个叶子结点。 n个权值分别设为 w1、w2、…、wn,则哈夫曼树的构造规则为:
(1) 将w1、w2、…,wn看成是有n 棵树的森林(每棵树仅有一个结点);
(2) 在森林中选出两个根结点的权值最小的树合并,作为一棵新树的左、右子树,且新树的根结点权值为其左、右子树根结点权值之和;
(3)从森林中删除选取的两棵树,并将新树加入森林;
(4)重复(2)、(3)步,直到森林中只剩一棵树为止,该树即为所求得的哈夫曼树。
以上内容来自百度百科
OK,进入正题,看代码吧,哈夫曼树利用了上篇的最小堆
HuffmanTree.h
#ifndef HUFFMAN_TREE_H
#define HUFFMAN_TREE_H
#include "MinHeap.h"
template <typename T> class HuffmanTree;
template <typename T>
class HuffmanTreeNode
{
friend class HuffmanTree<T>;
private:
T info;
HuffmanTreeNode<T> *parent;
HuffmanTreeNode<T> *left;
HuffmanTreeNode<T> *right;
public:
HuffmanTreeNode()
{
}
HuffmanTreeNode<T> * leftChild( ) const
{
return left;
}
HuffmanTreeNode<T> * rightChild( ) const
{
return right;
}
bool operator < (const HuffmanTreeNode<T> &HN)
{
return info < HN.info;
}
bool operator > (const HuffmanTreeNode<T> &HN)
{
return info > HN.info;
}
bool operator == (const HuffmanTreeNode<T> &HN)
{
return info == HN.info;
}
};
template <typename T>
class HuffmanTree
{
private:
HuffmanTreeNode<T> *root;
void deleteTree(HuffmanTreeNode<T> *root)
{
if(root)
{
deleteTree(root->leftChild());
deleteTree(root->rightChild());
delete root;
}
}
void mergeTree(HuffmanTreeNode<T> *leftChild,HuffmanTreeNode<T> *rightChild,HuffmanTreeNode<T> *parent)
{
parent->parent = nullptr;
parent->left = leftChild;
parent->right = rightChild;
parent->info = leftChild->info + rightChild->info;
leftChild->parent = rightChild->parent = parent; // 指向父节点
}
public:
//构造Huffman树,weight是存储权值的数组,n是数组长度
HuffmanTree(int weight[],int n)
{
MinHeap<HuffmanTreeNode<T>> heap(n); //定义最小值堆
HuffmanTreeNode<T> *parent,*leftChild,*rightChild;
HuffmanTreeNode<T>* nodeList = new HuffmanTreeNode<T>[n];
for(int i = 0; i < n; i++)//初始化
{
nodeList[i].info = weight[i];
nodeList[i].parent = nodeList[i].left = nodeList[i].right = nullptr;
heap.insert(nodeList[i]);//向堆中添加元素
}
for(int i = 0; i < n - 1; i++)//通过n-1次合并建立Huffman树
{
leftChild = new HuffmanTreeNode<T>;
rightChild = new HuffmanTreeNode<T>;
parent = new HuffmanTreeNode<T>;
heap.removeMin(*leftChild);//选择权值最小的结点
heap.removeMin(*rightChild);//选择权值次小的结点
mergeTree(leftChild,rightChild,parent);//合并权值最小的两棵树
heap.insert(*parent);//把parent插入到堆中去
root = parent;//建立根结点
}
delete []nodeList;
}
virtual ~HuffmanTree()//析构函数
{
deleteTree(root);
}
HuffmanTreeNode<T> * getRoot( ) const
{
return root;
}
void inOrder(HuffmanTreeNode<T> *root) //中序周游
{
if(root)
{
inOrder(root->leftChild());
cout << root->info << "\t";
inOrder(root->rightChild());
}
}
};
#endif
测试代码:
#include "HuffmanTree.h"
int main()
{
int weight[] = {6,2,3,4}; //权值
HuffmanTree<int> a(weight,4); //Huffman树
cout<< "HuffmanTree is constructed. " << endl;
cout<< "中序周游: " << endl;
a.inOrder(a.getRoot()); //中序周游
system("pause");
return 0;
}