C++实现哈夫曼树

如何建立哈夫曼树的,网上搜索一堆,这里就不写了,直接给代码。

1.哈夫曼树结点类:HuffmanNode.h

#ifndef HuffmanNode_h
#define HuffmanNode_h
 
template 
struct HuffmanNode {
  T weight; // 存储权值
  HuffmanNode *leftChild, *rightChild, *parent; // 左、右孩子和父结点
};
 
#endif /* HuffmanNode_h */

2.哈夫曼树最小堆:HuffmanMinHeap.h

#ifndef HuffmanMinHeap_h
#define HuffmanMinHeap_h
 
#include "HuffmanNode.h"
#include 
using namespace std;
 
const int DefaultSize = 100;
 
template 
class MinHeap {
public:
  MinHeap(); // 构造函数
  ~MinHeap(); // 析构函数
  void Insert(HuffmanNode *current); // 插入
  HuffmanNode *getMin(); // 获取最小结点
private:
  HuffmanNode *heap; // 动态数组存储最小堆
  int CurrentSize; // 目前最小堆的结点数
  void ShiftUp(int start); // 向上调整
  void ShiftDown(int start, int m); // 下滑
};
 
// 构造函数
template 
MinHeap::MinHeap() {
  heap = new HuffmanNode[DefaultSize]; // 创建堆空间
  CurrentSize = 0;
}
 
// 析构函数
template 
MinHeap::~MinHeap() {
  delete []heap; // 释放空间
}
 
// 插入
template 
void MinHeap::Insert(HuffmanNode *current) {
  if(CurrentSize == DefaultSize) {
    cout << "堆已满" << endl;
    return;
  }
  // 把current的数据复制到“数组末尾”
  heap[CurrentSize] = *current;
  // 向上调整堆
  ShiftUp(CurrentSize);
  CurrentSize++;
}
 
// 获取最小结点并在堆中删除该结点
template 
HuffmanNode *MinHeap::getMin() {
  if(CurrentSize == 0) {
    cout << "堆已空!" << endl;
    return NULL;
  }
  HuffmanNode *newNode = new HuffmanNode();
  if(newNode == NULL) {
    cerr << "存储空间分配失败!" << endl;
    exit(1);
  }
  *newNode = heap[0]; // 将最小结点的数据复制给newNode
  heap[0] = heap[CurrentSize-1]; // 用最后一个元素填补
  CurrentSize--;
  ShiftDown(0, CurrentSize-1); // 从0位置开始向下调整
  return newNode;
}
 
// 向上调整
template 
void MinHeap::ShiftUp(int start) {
  // 从start开始,直到0或者当前值大于双亲结点的值时,调整堆
  int j = start, i = (j-1)/2; // i是j的双亲
  
  HuffmanNode temp = heap[j];
  while(j > 0) {
    if(heap[i].weight <= temp.weight)
      break;
    else {
      heap[j] = heap[i];
      j = i;
      i = (j - 1) / 2;
    }
  }
  heap[j] = temp;
}
 
// 向下调整
template 
void MinHeap::ShiftDown(int start, int m) {
  int i = start, j = 2 * i + 1; // j是i的左子女
  
  HuffmanNode temp = heap[i];
  while(j <= m) {
    if(j < m && heap[j].weight > heap[j+1].weight)
      j++; // 选两个子女中较小者
    if(temp.weight <= heap[j].weight)
      break;
    else {
      heap[i] = heap[j];
      i = j;
      j = 2 * j + 1;
    }
  }
  heap[i] = temp;
}
 
 
#endif /* HuffmanMinHeap_h */

3.哈夫曼树实现:HuffmanTree.h

#ifndef HuffmanTree_h
#define HuffmanTree_h
 
 
#include "HuffmanMinHeap.h"
#include "HuffmanNode.h"
 
template 
class HuffmanTree {
public:
  HuffmanTree(); // 构造函数
  ~HuffmanTree(); // 析构函数
  void Create(T w[], int n); // 创建哈夫曼树
  void Merge(HuffmanNode *first, HuffmanNode *second, HuffmanNode *parent); // 合并
  void PreOrder(); // 前序遍历Huffman树
private:
  HuffmanNode *root; // 根结点
  void Destroy(HuffmanNode *current); // 销毁哈夫曼树
  void PreOrder(HuffmanNode *current); // 前序遍历Huffman树
};
 
// 构造函数
template 
HuffmanTree::HuffmanTree() {
  root = NULL;
}
 
// 析构函数
template 
HuffmanTree::~HuffmanTree() {
  Destroy(root); // 销毁哈夫曼树
}
 
// 销毁哈夫曼树
template 
void HuffmanTree::Destroy(HuffmanNode *current) {
  if(current != NULL) { // 不为空
    Destroy(current->leftChild); // 递归销毁左子树
    Destroy(current->rightChild); // 递归销毁右子树
    delete current; // 释放空间
    current = NULL;
  }
}
 
// 创建哈夫曼树
template 
void HuffmanTree::Create(T w[], int n) {
  int i;
  MinHeap hp; // 使用最小堆存放森林
  HuffmanNode *first, *second, *parent = NULL;
  HuffmanNode*work = new HuffmanNode();
  
  if(work == NULL) {
    cerr << "存储空间分配失败!" << endl;
    exit(1);
  }
  for(i = 0; i < n; i++) {
    work->weight = w[i];
    work->leftChild = work->rightChild = work->parent = NULL;
    hp.Insert(work); // 插入到最小堆中
  }
  for(i=0; i < n-1; i++) { // 做n-1趟,形成Huffman树
    first = hp.getMin(); // 获取权值最小的树
    second = hp.getMin(); // 获取权值次小的树
    parent = new HuffmanNode();
    if(parent == NULL) {
      cerr << "存储空间分配失败!" << endl;
      exit(1);
    }
    Merge(first, second, parent); // 合并
    hp.Insert(parent); // 重新插入到最小堆中
  }
  root = parent; // 根结点
}
// 合并
template 
void HuffmanTree::Merge(HuffmanNode *first, HuffmanNode *second, HuffmanNode *parent) {
  parent->leftChild = first; // 左子树
  parent->rightChild = second; // 右子树
  parent->weight = first->weight + second->weight; // 父结点权值
  first->parent = second->parent = parent; // 父指针
}
 
// 前序遍历Huffman树
template 
void HuffmanTree::PreOrder() {
  PreOrder(root);
}
 
// 前序遍历Huffman树
template 
void HuffmanTree::PreOrder(HuffmanNode *current) {
  if(current != NULL) {
    cout << current->weight << " "; // 访问当前结点数据
    PreOrder(current->leftChild); // 递归遍历左子树
    PreOrder(current->rightChild); // 递归遍历右子树
  }
}
#endif /* HuffmanTree_h */

4.测试:main.cpp

#include "HuffmanTree.h"
 
int main(int argc, const char * argv[]) {
  int arr[] = {7, 5, 2, 4};
  int len = sizeof(arr) / sizeof(arr[0]); // 数组长度
  HuffmanTree tree; // Huffman树的对象
  
  tree.Create(arr, len); // 创建Huffman树
  tree.PreOrder(); // 前序遍历Huffman树
  return 0;
}

测试结果:

C++实现哈夫曼树_第1张图片

C++实现哈夫曼树_第2张图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(C++实现哈夫曼树)