哈夫曼树

简介:
BinTreeNode.h

 

template<typename Type> class BinaryTree;

template<typename Type> void Huffman(Type *, int, BinaryTree<Type> &);

template<typename Type> class BinTreeNode{
public:
	friend class BinaryTree<Type>;
    friend void Huffman<Type>(Type *, int, BinaryTree<Type> &);
	BinTreeNode():m_pleft(NULL),m_pright(NULL){}
	BinTreeNode(Type item,BinTreeNode<Type> *left=NULL,BinTreeNode<Type> *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<Type> *Copy(const BinTreeNode<Type> *copy);	//copy the node

private:
	BinTreeNode<Type> *m_pleft,*m_pright;
	Type m_data;
};

template<typename Type> BinTreeNode<Type>* BinTreeNode<Type>::Copy(const BinTreeNode<Type> *copy){
	if(copy==NULL){
		return NULL;
	}

	BinTreeNode<Type> *temp=new BinTreeNode<Type>(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<typename Type> void Huffman(Type *, int, BinaryTree<Type> &);

template<typename Type> class BinaryTree{
public:
    
    BinaryTree(BinaryTree<Type> &bt1, BinaryTree<Type> &bt2){
        m_proot = new BinTreeNode<Type>(bt1.m_proot->m_data 
            + bt2.m_proot->m_data, bt1.m_proot, bt2.m_proot);
    }
    BinaryTree(Type item){
        m_proot = new BinTreeNode<Type>(item);
    }
    BinaryTree(const BinaryTree<Type> &copy){
        this->m_proot = copy.m_proot;
    }
    BinaryTree(){
        m_proot = NULL;
    }
    void Destroy(){
        m_proot->Destroy();
    }
    ~BinaryTree(){
//        m_proot->Destroy();
    }
    
    BinaryTree<Type>& operator=(BinaryTree<Type> copy);	//evaluate node
    friend void Huffman<Type>(Type *, int, BinaryTree<Type> &);
    friend bool operator < <Type>(BinaryTree<Type> &l, BinaryTree<Type> & r);
    friend bool operator > <Type>(BinaryTree<Type> &l, BinaryTree<Type> & r);
    friend bool operator <= <Type>(BinaryTree<Type> &l, BinaryTree<Type> & r);
    friend ostream& operator<< <Type>(ostream& ,BinaryTree<Type>&);	//output the data
private:
	BinTreeNode<Type> *m_proot;
    void Print(BinTreeNode<Type> *start,int n=0);	//print the tree with the root of start
};

template<typename Type> bool operator <(BinaryTree<Type> &l, BinaryTree<Type> &r){
    return l.m_proot->GetData() < r.m_proot->GetData();
}

template<typename Type> bool operator >(BinaryTree<Type> &l, BinaryTree<Type> &r){
    return l.m_proot->GetData() > r.m_proot->GetData();
}

template<typename Type> bool operator <=(BinaryTree<Type> &l, BinaryTree<Type> &r){
    return l.m_proot->GetData() <= r.m_proot->GetData();
}


template<typename Type> void BinaryTree<Type>::Print(BinTreeNode<Type> *start, int n){
	if(start==NULL){
		for(int i=0;i<n;i++){
			cout<<"     ";
		}
		cout<<"NULL"<<endl;
		return;
	}
	Print(start->m_pright,n+1);	//print the right subtree
	for(int i=0;i<n;i++){	//print blanks with the height of the node
		cout<<"     ";
	}
	if(n>=0){
		cout<<start->m_data<<"--->"<<endl;//print the node
	}
	Print(start->m_pleft,n+1);	//print the left subtree
}

template<typename Type> ostream& operator<<(ostream& os,BinaryTree<Type>& out){
	out.Print(out.m_proot);
	return os;
}

template<typename Type> BinaryTree<Type>& BinaryTree<Type>::operator=(BinaryTree<Type> copy){
	m_proot=m_proot->Copy(copy.m_proot);
    return *this;
}
MinHeap.h
template<typename Type> 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 end


private:
	const int m_nMaxSize;	
	Type *m_pheap;
	int m_ncurrentsize;
};

template<typename Type> void MinHeap<Type>::Adjust(const int start, const int end){
	int i = start,j = i*2+1;
	Type temp=m_pheap[i];
	while(j <= end){
		if(j<end && m_pheap[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<typename Type> MinHeap<Type>::MinHeap(Type heap[], int n):m_nMaxSize(n){
	m_pheap = new Type[m_nMaxSize];
	for(int i=0; i<n; i++){
		m_pheap[i] = heap[i];
	}
	m_ncurrentsize = n;
	int pos=(n-2)/2;	//Find the last tree which has more than one element;
	while(pos>=0){
		Adjust(pos, n-1);
		pos--;
	}
}

template<typename Type> bool MinHeap<Type>::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<typename Type> bool MinHeap<Type>::Insert(const Type item){
	if(m_ncurrentsize == m_nMaxSize){
		cerr<<"Heap Full!"<<endl;
		return 0;
	}
	m_pheap[m_ncurrentsize] = item;
	int j = m_ncurrentsize, i = (j-1)/2;
	Type temp = m_pheap[j];
	while(j > 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<typename Type> void Huffman(Type *elements, int n, BinaryTree<Type> &tree){
    BinaryTree<Type> first, second;
    BinaryTree<Type> node[20];
    for (int i=0; i<n; i++){
        node[i].m_proot = new BinTreeNode<Type>(elements[i]);
    }
    MinHeap<BinaryTree<Type> > heap(node, n);

    for (int i=0; i<n-1; i++){
        heap.DeleteMin(first);
        heap.DeleteMin(second);
        
        //using the first and the second minimize element create new tree
        if (first.m_proot->GetData() == second.m_proot->GetData()){
            tree = *(new BinaryTree<Type>(second, first));
        }
        else {
            tree = *(new BinaryTree<Type>(first, second));
        }

        heap.Insert(tree);
    }
}
Test.cpp
#include <iostream>

using namespace std;

#include "Huffman.h"

int main(){
    BinaryTree<int> tree;
    int init[10]={3,6,0,2,8,4,9,1,5,7};
    Huffman(init,10,tree);
    cout << tree;
    tree.Destroy();
    return 0;
}
目录
相关文章
|
7月前
|
机器学习/深度学习 数据可视化 网络架构
RT-DETR改进策略【SPPF】| NeuralPS-2022 Focal Modulation : 使用焦点调制模块优化空间金字塔池化SPPF
RT-DETR改进策略【SPPF】| NeuralPS-2022 Focal Modulation : 使用焦点调制模块优化空间金字塔池化SPPF
150 18
RT-DETR改进策略【SPPF】| NeuralPS-2022 Focal Modulation : 使用焦点调制模块优化空间金字塔池化SPPF
|
6月前
|
监控 JavaScript 前端开发
MutationObserver详解+案例——深入理解 JavaScript 中的 MutationObserver:原理与实战案例
MutationObserver 是一个非常强大的 API,提供了一种高效、灵活的方式来监听和响应 DOM 变化。它解决了传统 DOM 事件监听器的诸多局限性,通过异步、批量的方式处理 DOM 变化,大大提高了性能和效率。在实际开发中,合理使用 MutationObserver 可以帮助我们更好地控制 DOM 操作,提高代码的健壮性和可维护性。 只有锻炼思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
MutationObserver详解+案例——深入理解 JavaScript 中的 MutationObserver:原理与实战案例
|
8月前
|
供应链 数据可视化 搜索推荐
商业模式画布BMC入门指南:模块、实操与工具
2分钟了解什么是商业模式画布BMC,哪些工具可以绘制。
813 11
商业模式画布BMC入门指南:模块、实操与工具
|
10月前
|
API 索引
Elasticsearch实时搜索
【11月更文挑战第2天】
229 1
|
11月前
|
文字识别 数据可视化 前端开发
《智能文档处理“百宝箱”:数字化时代文档处理的必备利器》
在数字化时代,文档处理面临工具选择难、调试耗时、内容复杂和校对困难等问题。合合信息推出智能文档处理“百宝箱”,包含文档解析测评工具、可视化文档解析前端和向量化模型,助力开发者高效解决这些问题。这些工具广泛应用于企业办公、金融、教育和医疗等行业,提升文档处理的效率和准确性。
258 1
|
存储 弹性计算 运维
|
Linux 虚拟化
麒麟系统开发笔记(一):国产麒麟系统搭建开发环境之虚拟机安装
麒麟系统开发笔记(一):国产麒麟系统搭建开发环境之虚拟机安装
麒麟系统开发笔记(一):国产麒麟系统搭建开发环境之虚拟机安装
|
人工智能 自然语言处理 供应链
阿里云联合伙伴发起“物流智能联盟”
物流行业内首个专注于大模型应用研究与实践的联盟“物流智能联盟”在杭州成立,旨在加速大模型在物流领域落地,用AI助力物流行业增效降本和业务创新。该联盟由阿里云、菜鸟、高德地图、中远海运、东航物流、圆通速递、申通快递、中通快递、德邦快递、G7易流、地上铁、浙江大学智能交通研究所等在2024数智物流峰会上共同成立。
|
物联网 芯片 开发者
低功耗技术在智能硬件上的应用
随着芯片技术的不断发展,CPU的主频越来越高,随之而来的高功耗及发热等问题也日益显现出来,因此低功耗设计也成为了智能硬件中必须面对的重大课题。业界在低功耗的设计方面有许多优秀的实践案例,值得我们借鉴和学习,本文总结了一些经典的低功耗设计方法,同时也会详细阐述AliOS Things在IPC中采用的低功耗方案。
低功耗技术在智能硬件上的应用