哈夫曼树(c++)实现,配流程图解释,配合代码阅读效果更好!

哈夫曼树(c++)实现,配流程图解释,配合代码阅读效果更好!_第1张图片
哈夫曼树(c++)实现,配流程图解释,配合代码阅读效果更好!_第2张图片
哈夫曼树(c++)实现,配流程图解释,配合代码阅读效果更好!_第3张图片
哈夫曼树(c++)实现,配流程图解释,配合代码阅读效果更好!_第4张图片
哈夫曼树(c++)实现,配流程图解释,配合代码阅读效果更好!_第5张图片


#include 
using namespace std;
typedef struct TreeNode *Huffman;
typedef struct HeapStruct *MinHeap;
#define Maxsize 1000
#define Mindata -1000
int A[] = { 1,2,3,4,5};
int A_length = 5;
struct TreeNode
{
	int weight;
	Huffman left;
	Huffman right;
};
struct HeapStruct
{
	Huffman *data;
	int size;
	int capacity;

};
Huffman Create()
{
	Huffman h=new TreeNode;
	h->weight = 0;
	h->left = NULL;
	h->right = NULL;
	return h;
}
MinHeap create()
{
	MinHeap H= new HeapStruct;
	H->size = 0;
	H->capacity = Maxsize;
	H->data = (Huffman*)malloc(sizeof(struct TreeNode)*(Maxsize + 1));
   Huffman  h = Create();
   h->weight = Mindata;
   H->data[0] = h;
   return H;
}
void sort(MinHeap H, int i)
{
	int parent, child;
	int temp = H->data[i]->weight;
	for (parent = i; parent * 2 <= H->size; parent = child)
	{
		child = parent * 2;
		if (child != H->size&&H->data[child]->weight > H->data[child + 1]->weight)
			child++;
		if (H->data[child]->weight >= temp)
			break;
		else
			H->data[parent] = H->data[child];
	}
	H->data[parent]->weight = temp;
}


void Adjust(MinHeap H)
{
	for (int i = H->size / 2; i > 0; i--)
	{
		sort(H, i);
	}
}

void Build(MinHeap H)
{
	Huffman h;
	for (int i = 0; i < A_length; i++)
	{
		h = Create();
		h->weight = A[i];
		H->data[++H->size] = h;
	}
	Adjust(H);
}

void Insert(MinHeap H, Huffman h)
{
	int weight = h->weight;
	int i = ++H->size;
	for (; H->data[i / 2]->weight > weight; i /= 2)
		H->data[i] = H->data[i / 2];
	H->data[i] = h;
}

void Pre(Huffman h)
{
	if (h)
		cout << h->weight<<" ";
	if (h->left)
		Pre(h->left);
	if (h->right)
		Pre(h->right);
}
Huffman Del(MinHeap H)
{
	int parent, child;
	Huffman h = H->data[1];
	Huffman temp = H->data[H->size--];
	for (parent = 1; 2 * parent <= H->size; parent = child)
	{
		child = 2 * parent;
		if (child != H->size&&H->data[child]->weight> H->data[child + 1]->weight)
			child++;
		if (H->data[child]->weight >= temp->weight)
			break;
		else
			H->data[parent] = H->data[child];
	}
	H->data[parent] = temp;
	return h;

}
Huffman huffmantree(MinHeap H)
{
	Huffman T;
	Build(H);
	int times = H->size;
	for (int i = 1; i < times; i++)
	{	
		T = new TreeNode;
		T->left = Del(H);
		T->right = Del(H);
		T->weight = T->left->weight + T->right->weight;
		Insert(H, T);
	}
	T = Del(H);
	return T;
}
int main()
{
	MinHeap H;
	Huffman h;
	H = create();
	h = huffmantree(H);
	Pre(h);
	

	system("pause");
	return 0;
	}

哈夫曼树 编码 解码

void Huffmancode(Huffman T, int h)
{	
	static int c[10];
	if (T)
	{
		if ((T->left == NULL) && (T->right == NULL))
		{
			cout << "字符:" << T->ch << "对应的权值为" << T->weight <<"哈夫曼编码为:"<< endl;
			for (int i = 0; i < h; i++)
			{
				cout << c[i] ;

			}
		}
		else
		{
			c[h] = 0;
			Huffmancode(T->left,h+1);

			c[h] = 1;
			Huffmancode(T->right, h + 1);
		}
	}
}

void HuffmanSolve(char ch[], Huffman T)//解码
{
	int count;
	int num[100];
	Huffman temp;
	for (int i = 0; i < strlen(ch); i++)
	{
		if (ch[i] = '0')
		{
			num[i] = 0;
		}
		else
		{
			num[i] = 1;
		}

	}
	if (T)
	{
		count = 0;
		while (count< strlen(ch))
		{
			temp = T;
			while ((temp->right!=NULL)&&(temp->left!=NULL))
			{
				if (num[count] == 0)
				{
					temp = temp->left;
				}
				else
				{
					temp = temp->right;
				}
				count++;
			}
			cout << temp->ch << endl;
		}
		
	}

}


你可能感兴趣的:(数据结构与算法)