赫夫曼树(一),对字符串中的字符出现频率进行设置权,并且根据权大小进行排序

此程序只是一个demo,有很多bug。

PriorityQueue.h

/***************************
*把字符从小到大排列
* 步骤:1. 传入一个字符串
*      2.全部分割成单个的字符
*      3.查找队列中有没有这个字符 有就加权,没有就 创建加入。
*      4.从对尾部到对头 从小到大排序
*      5.生成哈弗曼树 ->1.根据队列从小到大 两个两个地进行操作
*                      ->最后一个就是赫夫曼树头指针 然后遍历 设置code 左子树为0 右子树为1
******************************/
#ifndef PRIORITYQUEUE_H_
#define PRIORITYQUEUE_H_
/****************************************
*结点储存结构
****************************************/
struct HuffmanNode{
	int weight;
	char data;
	int code;
	HuffmanNode * lChild;
	HuffmanNode * rChild;
};
class PriorityQueue{
private:
	//|最后生成的赫夫曼树
	HuffmanNode * m_pHuffmanTree;
	//|队列尾部指针
	HuffmanNode * m_pQueue;
	/*************************************
	*私有 成员函数 全部由公有成员函数调用
	*
	*************************************/

	//|查找一个值是否存在
	HuffmanNode * find(char data);
	//|加入队列
	void addQueue(char data);
	//|增加权
//	void addWeight(char data);
	
public:
	PriorityQueue();
	//|根据权的大小进行排序(rChild作为后继指针)
	void SortByWeight();
	void SortAddQueue(char * data);
	//|遍历输出队列(用于调试)
	void echoEach();
	//|计数
	int count();
	//|
	HuffmanNode * operator[](int index);
};
#endif
PriorityQueue.cpp

#include"PriorityQueue.h"
#include
using namespace std;
PriorityQueue::PriorityQueue(){

	this->m_pHuffmanTree = 0;
	this->m_pQueue = 0;
}
void PriorityQueue::SortAddQueue(char * data)
{
	/******************************
	*外部传入字符串指针会自动加入\0
	*需要注意的是 char* 字符串指针是常量 如char * p="abcdefg",不在栈内,而是字符串常量区域
	********************************/
	int i = 0;
	//|加入队列或者设置权
	while (data[i] != '\0')
	{
		cout << data[i] << endl;
		/************
		*先查询有的话就增加权
		*没有就直接加入队列
		***********/
		this->addQueue(data[i]);
		i = i + 1;
	}
	//|根据权进行排序(直接选择排序算法)
}
void PriorityQueue::SortByWeight()
{
	HuffmanNode tmp;
	for (int i = 0; i < this->count(); i++)
	{
		for (int j = i; j < this->count(); j++)
		{
			if ((*this)[i]->weight>(*this)[j]->weight)
			{
				//|保存至临时对象
				tmp.data = (*this)[j]->data;
				tmp.weight = (*this)[j]->weight;
				//|替换
				(*this)[j]->data = (*this)[i]->data;
				(*this)[j]->weight = (*this)[i]->weight;

				(*this)[i]->data = tmp.data;
				(*this)[i]->weight = tmp.weight;
			}
		}
	}
}

HuffmanNode * PriorityQueue::find(char data)
{
	HuffmanNode * tmp = this->m_pQueue;
	while (tmp)
	{
		if (tmp->data == data)
			return tmp;
	   //==移到前一个==
		tmp = tmp->rChild;
	}
	return 0;
}
/**********
*
***************/
void PriorityQueue::addQueue(char data)
{
	//|如果没有,就创建 并且加入队列
	if (this->find(data) == 0)
	{
		HuffmanNode * newNode = new HuffmanNode;
		newNode->data = data;
		newNode->rChild = 0;
		newNode->code = 0;
		newNode->weight = 1;
		//|加入队列尾部
		if (this->m_pQueue == 0)
		{
			this->m_pQueue = newNode;
		}
		else{
		newNode->rChild = this->m_pQueue;
		this->m_pQueue = newNode;
		}
	}
	else{
	//|有就 增加权
		HuffmanNode * oldNode = this->find(data);
		oldNode->weight = oldNode->weight + 1;
	}
}
void PriorityQueue::echoEach(){

	HuffmanNode * tmp = this->m_pQueue;
	while (tmp)
	{
		cout << "数据:" << tmp->data << "权:" << tmp->weight << endl;
		tmp = tmp->rChild;
	}
}
int PriorityQueue::count(){

	int amount = 0;
	HuffmanNode * tmp = this->m_pQueue;
	while (tmp)
	{
		amount = amount + 1;
		tmp = tmp->rChild;
	}
	return amount;
}
HuffmanNode * PriorityQueue::operator[](int index)
{
	if (index<0 || index>this->count() - 1)
		return 0;
	//==
	HuffmanNode * tmp = this->m_pQueue;
	int i = 0;
	while (tmp&&irChild;
		i = i + 1;
	}
	return tmp;
}
main.cpp

/*******
*priority 
* Huffman Tree
* Huffman Table
* encode
*decode
******/
#include
#include"PriorityQueue.h"
using namespace std;
int main(int argc, char *argv[])
{
	PriorityQueue * p = new PriorityQueue();
	p->SortAddQueue("abcabcffffggoop-=..ooop8888***");
	p->echoEach();
	cout <<"数量"<< p->count() << endl;
	cout << "重载[]::"<<(*p)[0]->data<<"权"<<(*p)[0]->weight << endl;
	//|排序
	p->SortByWeight();
	//|再次输出
	p->echoEach();
	cin.get();
	return 1;
}

赫夫曼树(一),对字符串中的字符出现频率进行设置权,并且根据权大小进行排序_第1张图片 赫夫曼树(一),对字符串中的字符出现频率进行设置权,并且根据权大小进行排序_第2张图片


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