123

https://blog.csdn.net/liufeng_king/article/details/8720896
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10;
template<class Type>class Huffman;

template<class Type>class BinaryTree;
template<class Type>
BinaryTree<int>HuffmanTree(Type f[],int n);


template <class T>
struct BTnode{
	T data;
	BTnode<T>*lchild;
	BTnode<T>*rchild;
	BTnode(){
		lchild = rchild = NULL;
	}
	BTnode(const T&val,BTnode<T>*LCHILD,BTnode<T>*RCHILD){
		data = val;
		lchild = LCHILD;
		rchild = RCHILD;
		
	}
	BTnode<T>*CopyTree(){
		BTnode<T> *nl,*nr,*nt;
		if(&data == NULL)
			return NULL;
		nl = lchild->CopyTree() ;
		nr = rchild->CopyTree();
		
		nt = new BTnode<T>(data,nl,nr);
		return nt;
	}
};
template<class T>
class BinaryTree{
	public:
		BTnode<T> *root;
		BinaryTree();
		void MakeTree(T pData,BinaryTree<T>ltree,BinaryTree<T>rtree){
			root = new BTnode<T>();
			root->data = pData;
			root->lchild = ltree.root;
			root->rchild = rtree.root;
		}
		
};
template <typename T>
class MinHeap{
	public:
		MinHeap(int n);
		T RemoveMin();
	
		bool insert(T );
		bool createMinHeap(T *a,int length);
		void filterup(int index);
		void filterdown(int start,int end);
		
		int size;
	private:
	 	int capacity;
	 	T *heap;
 
		
};
template <typename T>
MinHeap<T>::MinHeap(int n):capacity(n),size(0),heap(NULL){
	heap = new T[capacity];
};


template <typename T>
bool MinHeap<T>::createMinHeap(T *a,int length){
	if(length > capacity)
		return false;
	for(int i = 0; i < length;i++)
		 heap[i] = a[i];
	size = length;
	int currentPos = (size-2)/2;
	while(currentPos >= 0){
		filterdown(currentPos,size-1);
		currentPos--;
	}
	return true;
}
template <typename T>
void MinHeap<T>::filterup(int start){
	int j = start;
	if(j == 0)
		return;
	int i = (start-1)/2;
	T temp = heap[start];
	while( j > 0){
		if(heap[i]>= temp){
			heap[j] = heap[i];
			j = i;
			i = (i-1)/2;
		}else
		   break;
	}
	heap[j] = temp;
}
template<typename T>
void MinHeap<T>::filterdown(int start,int end){
	int i = start;
	int j = i*2 + 1;
	T temp = heap[i];
	if(j > size-1)
		return;
	while( i <= end){
	 	if(j > size-1)
	 		break;
	 	if(j + 1 <=size-1 && heap[j] > heap[j+1])
			j++;
		if(temp < heap[j])
			break;
		else{
			heap[i] = heap[j];
			i = j;
			j = 2*i+1;
		}
		 	
	}
	heap[i] = temp;
}

template <typename T>
bool MinHeap<T>::insert(T value){
	if(size == capacity){
		cout<<"Heap full"<<endl;
		return false;
	}
		
	heap[size] = value;
	filterup(size);
	size++;
	return true;
}

template <typename T>
T MinHeap<T>::RemoveMin(){
	T x = heap[0];
	bool judge = false;
	heap[0] = heap[size-1];
	size--;
	filterdown(0,size);
	return x;
}

template <typename Type>
class Huffman{
	friend BinaryTree<int>HuffmanTree(Type [],int);
	public:
		operator Type()const{
			return weight;
		}

	//private:
		BinaryTree<Type>tree;
		Type weight;
};
template<class Type>
BinaryTree<int> HuffmanTree(Type f[],int n){
	Huffman<Type>*w = new Huffman<Type>[n+1];
	BinaryTree<int>z,zero;
	for(int i = 1;i <= n;i++){
		z.MakeTree(i,zero,zero);
		w[i].weight = f[i];
		w[i].tree = z;
	}
	MinHeap<Huffman<Type> >Q(100);
	
	Huffman<Type>x,y;
	for(int i = 1; i < n;i++){
		x = Q.RemoveMin();
		y = Q.RemoveMin();
		z.MakeTree(0,x.tree,y.tree);
		x.weight +=y.weight;
		x .tree = z;
		Q.insert(x);
	}
	return x.tree;
}
int main(){
	int f[10] = { 1,2,4,3,8,6,7,5,9,11};
	BinaryTree<int> t = HuffmanTree(f,10);
	return 0;
}

 

 


#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10;
template<typename Type>class Huffman;

template<typename Type>class BinaryTree;
 
BinaryTree<int> HuffmanTree(int f[],int n);


template <typename T>
struct BTnode{
	T data;
	BTnode<T>*lchild;
	BTnode<T>*rchild;
	BTnode(){
		lchild = rchild = NULL;
	}
	
	BTnode(const T&val,BTnode<T>*LCHILD,BTnode<T>*RCHILD){
		data = val;
		lchild = LCHILD;
		rchild = RCHILD;
		
	}
	BTnode<T>*CopyTree(){
		
		BTnode<T> *nl,*nr,*nt;
		if(&data == NULL)
			return NULL;
		nl = lchild->CopyTree() ;
		nr = rchild->CopyTree();
		
		nt = new BTnode<T>(data,nl,nr);
		return nt;
	}
};
template<typename T>
class BinaryTree{
	public:
		BTnode<T> *root;
		BinaryTree();
		void MakeTree(T pData,BinaryTree<T>ltree,BinaryTree<T>rtree){
			root = new BTnode<T>();
			root->data = pData;
			root->lchild = ltree.root;
			root->rchild = rtree.root;
		}
		
};
template <typename T>
class MinHeap{
	public:
		MinHeap(int n);
		T RemoveMin();
	
		bool insert(T );
		bool createMinHeap(T *a,int length);
		void filterup(int index);
		void filterdown(int start,int end);
		
		int size;
	private:
	 	int capacity;
	 	T *heap;
 
		
};
template <typename T>
MinHeap<T>::MinHeap(int n):capacity(n),size(0),heap(NULL){
	heap = new T[capacity];
};


template <typename T>
bool MinHeap<T>::createMinHeap(T *a,int length){
	if(length > capacity)
		return false;
	for(int i = 0; i < length;i++)
		 heap[i] = a[i];
	size = length;
	int currentPos = (size-2)/2;
	while(currentPos >= 0){
		filterdown(currentPos,size-1);
		currentPos--;
	}
	return true;
}
template <typename T>
void MinHeap<T>::filterup(int start){
	int j = start;
	if(j == 0)
		return;
	int i = (start-1)/2;
	T temp = heap[start];
	while( j > 0){
		if(heap[i]>= temp){
			heap[j] = heap[i];
			j = i;
			i = (i-1)/2;
		}else
		   break;
	}
	heap[j] = temp;
}
template<typename T>
void MinHeap<T>::filterdown(int start,int end){
	int i = start;
	int j = i*2 + 1;
	T temp = heap[i];
	if(j > size-1)
		return;
	while( i <= end){
	 	if(j > size-1)
	 		break;
	 	if(j + 1 <=size-1 && heap[j] > heap[j+1])
			j++;
		if(temp < heap[j])
			break;
		else{
			heap[i] = heap[j];
			i = j;
			j = 2*i+1;
		}
		 	
	}
	heap[i] = temp;
}

template <typename T>
bool MinHeap<T>::insert(T value){
	if(size == capacity){
		cout<<"Heap full"<<endl;
		return false;
	}
		
	heap[size] = value;
	filterup(size);
	size++;
	return true;
}

template <typename T>
T MinHeap<T>::RemoveMin(){
	T x = heap[0];
	bool judge = false;
	heap[0] = heap[size-1];
	size--;
	filterdown(0,size);
	return x;
}

template <typename Type>
class Huffman{
	 
	friend BinaryTree<int> HuffmanTree(int f[],int n);
	public:
		operator Type()const{
			return weight;
		}

	//private:
		BinaryTree<Type>tree;
		Type weight;
};

BinaryTree<int> HuffmanTree(int f[],int n){
	Huffman<int>*w = new Huffman<int>[n+1];
	BinaryTree<int>z,zero;
	for(int i = 1;i <= n;i++){
		z.MakeTree(i,zero,zero);
		w[i].weight = f[i];
		w[i].tree = z;
	}
	
	MinHeap<Huffman<int> >Q(100);
	
	Huffman<int>x,y;
	for(int i = 1; i < n;i++){
		x = Q.RemoveMin();
		y = Q.RemoveMin();
		z.MakeTree(0,x.tree,y.tree);
		x.weight +=y.weight;
		x .tree = z;
		Q.insert(x);
	}
	return x.tree;

	} 

int main(){
	int f[10] = { 1,2,4,3,8,6,7,5,9,11};
	BinaryTree<int> t = HuffmanTree(f,10);
	return 0;
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值