哈夫曼树编码选择函数代码c++
时间: 2023-12-14 13:05:06 浏览: 76
以下是C++实现的哈夫曼树编码选择函数代码:
```c++
void select(HuffmanTree HT, int n, int &s1, int &s2) {
int i;
int min1, min2;
min1 = min2 = MAXVALUE;
for (i = 1; i <= n; i++) {
if (HT[i].parent == 0) {
if (HT[i].weight < min1) {
min2 = min1;
s2 = s1;
min1 = HT[i].weight;
s1 = i;
} else if (HT[i].weight < min2) {
min2 = HT[i].weight;
s2 = i;
}
}
}
}
```
该函数的作用是在哈夫曼树中选择权值最小的两个节点。其中,HT是哈夫曼树,n是叶子节点的个数,s1和s2是指向权值最小的两个节点的指针。该函数的实现过程是遍历哈夫曼树中所有未被标记的节点,找到其中权值最小的两个节点,并将它们的下标分别赋值给s1和s2。
相关问题
根据哈夫曼树求哈夫曼编码的C++代码
下面是一个根据哈夫曼树求哈夫曼编码的C代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 100
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode **array;
};
struct MinHeapNode *newNode(char data, unsigned freq) {
struct MinHeapNode *temp = (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
struct MinHeap *createMinHeap(unsigned capacity) {
struct MinHeap *minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode**)malloc(minHeap->capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b) {
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
void minHeapify(struct MinHeap* minHeap, int idx) {
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
int isSizeOne(struct MinHeap *minHeap) {
return (minHeap->size == 1);
}
struct MinHeapNode *extractMin(struct MinHeap *minHeap) {
struct MinHeapNode *temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct MinHeap *minHeap, struct MinHeapNode *minHeapNode) {
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
void buildMinHeap(struct MinHeap *minHeap) {
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
int isLeaf(struct MinHeapNode *root) {
return !(root->left) && !(root->right);
}
struct MinHeap *createAndBuildMinHeap(char data[], int freq[], int size) {
struct MinHeap *minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
struct MinHeapNode *buildHuffmanTree(char data[], int freq[], int size) {
struct MinHeapNode *left, *right, *top;
struct MinHeap *minHeap = createAndBuildMinHeap(data, freq, size);
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
void printCodes(struct MinHeapNode *root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
printf("%c: ", root->data);
for (int i = 0; i < top; ++i)
printf("%d", arr[i]);
printf("\n");
}
}
void HuffmanCodes(char data[], int freq[], int size) {
struct MinHeapNode *root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
}
int main() {
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};
int freq[] = {5, 9, 12, 13, 16, 45};
int size = sizeof(arr) / sizeof(arr[0]);
HuffmanCodes(arr, freq, size);
return 0;
}
```
这段代码实现了根据给定字符集的频率构建哈夫曼树,并输出每个字符的哈夫曼编码。其中,`buildHuffmanTree()`函数用于构建哈夫曼树,`printCodes()`函数用于打印每个字符的哈夫曼编码,`main()`函数中给出了一个示例,用于演示如何使用该代码实现哈夫曼编码。
哈夫曼树的设计源代码 C++
以下是哈夫曼树的设计源代码,使用了C++语言:
```cpp
#include <iostream>
#include <queue>
using namespace std;
// 定义哈夫曼树节点结构体
struct HuffmanNode {
int weight; // 权值
HuffmanNode *leftChild; // 左子节点指针
HuffmanNode *rightChild; // 右子节点指针
// 构造函数
HuffmanNode(int w) {
weight = w;
leftChild = rightChild = nullptr;
}
};
// 定义哈夫曼树类
class HuffmanTree {
private:
HuffmanNode *root; // 根节点指针
// 比较函数对象,用于优先队列排序
struct cmp {
bool operator()(HuffmanNode *a, HuffmanNode *b) {
return a->weight > b->weight;
}
};
public:
// 构造函数
HuffmanTree(int weights[], int n) {
// 创建优先队列
priority_queue<HuffmanNode*, vector<HuffmanNode*>, cmp> pq;
// 将每个权值转换为节点,并加入优先队列
for (int i = 0; i < n; i++) {
pq.push(new HuffmanNode(weights[i]));
}
// 不断取出两个最小的节点,合并为一个新节点,直到队列中只剩一个节点,即为根节点
while (pq.size() > 1) {
HuffmanNode *left = pq.top();
pq.pop();
HuffmanNode *right = pq.top();
pq.pop();
HuffmanNode *newNode = new HuffmanNode(left->weight + right->weight);
newNode->leftChild = left;
newNode->rightChild = right;
pq.push(newNode);
}
root = pq.top();
}
// 析构函数
~HuffmanTree() {
destroy(root);
}
// 销毁哈夫曼树
void destroy(HuffmanNode *node) {
if (node != nullptr) {
destroy(node->leftChild);
destroy(node->rightChild);
delete node;
}
}
// 获取哈夫曼编码
void getHuffmanCode(HuffmanNode *node, string code, string codes[]) {
if (node->leftChild == nullptr && node->rightChild == nullptr) {
codes[node->weight] = code;
return;
}
getHuffmanCode(node->leftChild, code + "0", codes);
getHuffmanCode(node->rightChild, code + "1", codes);
}
// 输出哈夫曼编码表
void printHuffmanCode(string codes[], int n) {
for (int i = 0; i < n; i++) {
cout << "字符 " << char(i) << " 的哈夫曼编码为:" << codes[i] << endl;
}
}
};
int main() {
const int n = 6;
int weights[n] = {2, 3, 7, 10, 4, 2};
string codes[n];
HuffmanTree tree(weights, n);
tree.getHuffmanCode(tree.root, "", codes);
tree.printHuffmanCode(codes, n);
return 0;
}
```
这个代码实现了哈夫曼树的创建、销毁、获取哈夫曼编码以及输出哈夫曼编码表的功能。
阅读全文