博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
哈夫曼树
阅读量:6568 次
发布时间:2019-06-24

本文共 5262 字,大约阅读时间需要 17 分钟。

BinTreeNode.h

 

template
class BinaryTree;template
void Huffman(Type *, int, BinaryTree
&);template
class BinTreeNode{public: friend class BinaryTree
; friend void Huffman
(Type *, int, BinaryTree
&); BinTreeNode():m_pleft(NULL),m_pright(NULL){} BinTreeNode(Type item,BinTreeNode
*left=NULL,BinTreeNode
*right=NULL) :m_data(item),m_pleft(left),m_pright(right){} void Destroy(){ //destroy the tree with the root of the node if(this!=NULL){ this->m_pleft->Destroy(); this->m_pright->Destroy(); delete this; } } Type GetData(){ return m_data; } BinTreeNode
*Copy(const BinTreeNode
*copy); //copy the nodeprivate: BinTreeNode
*m_pleft,*m_pright; Type m_data;};template
BinTreeNode
* BinTreeNode
::Copy(const BinTreeNode
*copy){ if(copy==NULL){ return NULL; } BinTreeNode
*temp=new BinTreeNode
(copy->m_data); temp->m_pleft=Copy(copy->m_pleft); temp->m_pright=Copy(copy->m_pright); return temp;}
BinaryTree.h
#include "BinTreeNode.h"template
void Huffman(Type *, int, BinaryTree
&);template
class BinaryTree{public: BinaryTree(BinaryTree
&bt1, BinaryTree
&bt2){ m_proot = new BinTreeNode
(bt1.m_proot->m_data + bt2.m_proot->m_data, bt1.m_proot, bt2.m_proot); } BinaryTree(Type item){ m_proot = new BinTreeNode
(item); } BinaryTree(const BinaryTree
©){ this->m_proot = copy.m_proot; } BinaryTree(){ m_proot = NULL; } void Destroy(){ m_proot->Destroy(); } ~BinaryTree(){// m_proot->Destroy(); } BinaryTree
& operator=(BinaryTree
copy); //evaluate node friend void Huffman
(Type *, int, BinaryTree
&); friend bool operator <
(BinaryTree
&l, BinaryTree
& r); friend bool operator >
(BinaryTree
&l, BinaryTree
& r); friend bool operator <=
(BinaryTree
&l, BinaryTree
& r); friend ostream& operator<<
(ostream& ,BinaryTree
&); //output the dataprivate: BinTreeNode
*m_proot; void Print(BinTreeNode
*start,int n=0); //print the tree with the root of start};template
bool operator <(BinaryTree
&l, BinaryTree
&r){ return l.m_proot->GetData() < r.m_proot->GetData();}template
bool operator >(BinaryTree
&l, BinaryTree
&r){ return l.m_proot->GetData() > r.m_proot->GetData();}template
bool operator <=(BinaryTree
&l, BinaryTree
&r){ return l.m_proot->GetData() <= r.m_proot->GetData();}template
void BinaryTree
::Print(BinTreeNode
*start, int n){ if(start==NULL){ for(int i=0;i
m_pright,n+1); //print the right subtree for(int i=0;i
=0){ cout<
m_data<<"--->"<
m_pleft,n+1); //print the left subtree}template
ostream& operator<<(ostream& os,BinaryTree
& out){ out.Print(out.m_proot); return os;}template
BinaryTree
& BinaryTree
::operator=(BinaryTree
copy){ m_proot=m_proot->Copy(copy.m_proot); return *this;}
MinHeap.h
template
class MinHeap{public: MinHeap(Type heap[],int n); //initialize heap by a array ~MinHeap(){ delete[] m_pheap; }public: bool Insert(const Type item); bool DeleteMin(Type &first);private: void Adjust(const int start, const int end); //adjust the elements from start to endprivate: const int m_nMaxSize; Type *m_pheap; int m_ncurrentsize;};template
void MinHeap
::Adjust(const int start, const int end){ int i = start,j = i*2+1; Type temp=m_pheap[i]; while(j <= end){ if(j
m_pheap[j+1]){ j++; } if(temp <= m_pheap[j]){ break; } else{ m_pheap[i] = m_pheap[j]; i = j; j = 2*i+1; } } m_pheap[i] = temp;}template
MinHeap
::MinHeap(Type heap[], int n):m_nMaxSize(n){ m_pheap = new Type[m_nMaxSize]; for(int i=0; i
=0){ Adjust(pos, n-1); pos--; }}template
bool MinHeap
::DeleteMin(Type &first){ first = m_pheap[0]; m_pheap[0] = m_pheap[m_ncurrentsize-1]; m_ncurrentsize--; Adjust(0, m_ncurrentsize-1); return 1;}template
bool MinHeap
::Insert(const Type item){ if(m_ncurrentsize == m_nMaxSize){ cerr<<"Heap Full!"<
0){ if(m_pheap[i] <= temp){ break; } else{ m_pheap[j] = m_pheap[i]; j = i; i = (j-1)/2; } } m_pheap[j] = temp; m_ncurrentsize++; return 1;}
Huffman.h
#include "BinaryTree.h"#include "MinHeap.h"template
void Huffman(Type *elements, int n, BinaryTree
&tree){ BinaryTree
first, second; BinaryTree
node[20]; for (int i=0; i
(elements[i]); } MinHeap
> heap(node, n); for (int i=0; i
GetData() == second.m_proot->GetData()){ tree = *(new BinaryTree
(second, first)); } else { tree = *(new BinaryTree
(first, second)); } heap.Insert(tree); }}
Test.cpp
#include 
using namespace std;#include "Huffman.h"int main(){ BinaryTree
tree; int init[10]={3,6,0,2,8,4,9,1,5,7}; Huffman(init,10,tree); cout << tree; tree.Destroy(); return 0;}
==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/04/08/2438225.html,如需转载请自行联系原作者
你可能感兴趣的文章
《Node学习指南》一2.2 REPL的优势:更好地理解表层之下的JavaScript
查看>>
《Spark Cookbook 中文版》一1.8 使用Tachyon作为堆外存储层
查看>>
运维改革探索(一):用多层级监控实现可视化运维
查看>>
震精 - 数据库还能这样玩 - 三十六计 (下)
查看>>
最右App:架构演进之路
查看>>
React Native和Android整合详解
查看>>
spring-data-elasticsearch api
查看>>
应用迁云之镜像迁移-(3)工具介绍
查看>>
《C语言及程序设计》实践参考——输出这样的整数
查看>>
iOS中分段控制器与UIScrollView结合使用
查看>>
高识别、大流量服务、模型优化的印刷文字识别服务
查看>>
Swoole PHP高性能编程
查看>>
给飞驰的法拉利换引擎 - 谈边做业务边做架构重构(3)
查看>>
使用httpwebrequest Post数据到网站【转】
查看>>
CentOS 7 安装 JDK
查看>>
android自定义view无法预览
查看>>
深入讲解数据库中User和Schema的关系
查看>>
【转】SQL行列转换
查看>>
[转载]技术发展瓶颈的突破
查看>>
Linux(1)——在Linux下安装Nodejs(详细教程,包会),并成功创建一个简单的服务器...
查看>>