ืืœื’ื•ืจื™ืชื ื“ื—ื™ืกื” ืฉืœ ื”ืืคืžืŸ

ืœืคื ื™ ืชื—ื™ืœืช ื”ืงื•ืจืก "ืืœื’ื•ืจื™ืชืžื™ื ืœืžืคืชื—ื™ื" ื”ื›ื™ื ื• ืขื‘ื•ืจื›ื ืชืจื’ื•ื ืฉืœ ื—ื•ืžืจ ืฉื™ืžื•ืฉื™ ืื—ืจ.

ืงื™ื“ื•ื“ ื”ืืคืžืŸ ื”ื•ื ืืœื’ื•ืจื™ืชื ื“ื—ื™ืกืช ื ืชื•ื ื™ื ื”ืžื ืกื— ืืช ื”ืจืขื™ื•ืŸ ื”ื‘ืกื™ืกื™ ืฉืœ ื“ื—ื™ืกืช ืงื‘ืฆื™ื. ื‘ืžืืžืจ ื–ื” ื ื“ื‘ืจ ืขืœ ืงื™ื“ื•ื“ ื‘ืื•ืจืš ืงื‘ื•ืข ื•ืžืฉืชื ื”, ืงื•ื“ื™ื ื”ื ื™ืชื ื™ื ืœืคืขื ื•ื— ื™ื™ื—ื•ื“ื™, ื›ืœืœื™ ืงื™ื“ื•ืžืช ื•ื‘ื ื™ื™ืช ืขืฅ ื”ืืคืžืŸ.

ืื ื• ื™ื•ื“ืขื™ื ืฉื›ืœ ืชื• ืžืื•ื—ืกืŸ ื›ืจืฆืฃ ืฉืœ 0 ื•-1 ื•ืชื•ืคืก 8 ื‘ื™ื˜ื™ื. ื–ื” ื ืงืจื ืงื™ื“ื•ื“ ื‘ืื•ืจืš ืงื‘ื•ืข ืžื›ื™ื•ื•ืŸ ืฉื›ืœ ืชื• ืžืฉืชืžืฉ ื‘ืื•ืชื• ืžืกืคืจ ืงื‘ื•ืข ืฉืœ ื‘ื™ื˜ื™ื ืœืื—ืกื•ืŸ.

ื ื ื™ื— ืฉืงื™ื‘ืœื ื• ื˜ืงืกื˜. ื›ื™ืฆื“ ื ื•ื›ืœ ืœืฆืžืฆื ืืช ื›ืžื•ืช ื”ืฉื˜ื— ื”ื ื“ืจืฉ ืœืื—ืกื•ืŸ ืชื• ื‘ื•ื“ื“?

ื”ืจืขื™ื•ืŸ ื”ืžืจื›ื–ื™ ื”ื•ื ืงื™ื“ื•ื“ ื‘ืื•ืจืš ืžืฉืชื ื”. ืื ื• ื™ื›ื•ืœื™ื ืœื”ืฉืชืžืฉ ื‘ืขื•ื‘ื“ื” ืฉื—ืœืง ืžื”ืชื•ื•ื™ื ื‘ื˜ืงืกื˜ ืžื•ืคื™ืขื™ื ืœืขืชื™ื ืงืจื•ื‘ื•ืช ื™ื•ืชืจ ืžืื—ืจื•ืช (ืจืื” ื›ืืŸ) ืœืคืชื— ืืœื’ื•ืจื™ืชื ืฉื™ื™ืฆื’ ืืช ืื•ืชื• ืจืฆืฃ ืฉืœ ืชื•ื•ื™ื ื‘ืคื—ื•ืช ื‘ื™ื˜ื™ื. ื‘ืงื™ื“ื•ื“ ื‘ืื•ืจืš ืžืฉืชื ื”, ืื ื• ืžืงืฆื™ื ืœืชื•ื•ื™ื ืžืกืคืจ ืžืฉืชื ื” ืฉืœ ืกื™ื‘ื™ื•ืช, ื‘ื”ืชืื ืœืชื“ื™ืจื•ืช ืฉื‘ื” ื”ื ืžื•ืคื™ืขื™ื ื‘ื˜ืงืกื˜ ื ืชื•ืŸ. ื‘ืกื•ืคื• ืฉืœ ื“ื‘ืจ, ืชื•ื•ื™ื ืžืกื•ื™ืžื™ื ืขืฉื•ื™ื™ื ืœืงื—ืช ืžืขื˜ ื›ืžื• ื‘ื™ื˜ ืื—ื“, ื‘ืขื•ื“ ืฉืื—ืจื™ื ืขืฉื•ื™ื™ื ืœืงื—ืช 1 ืกื™ื‘ื™ื•ืช, 2 ืื• ื™ื•ืชืจ. ื”ื‘ืขื™ื” ืขื ืงื™ื“ื•ื“ ื‘ืื•ืจืš ืžืฉืชื ื” ื”ื™ื ืจืง ื”ืคืขื ื•ื— ื”ื‘ื ืฉืœ ื”ืจืฆืฃ.

ื›ื™ืฆื“, ืœื“ืขืช ืืช ืจืฆืฃ ื”ื‘ื™ื˜ื™ื, ืœืคืขื ื— ืื•ืชื• ื‘ืื•ืคืŸ ื—ื“ ืžืฉืžืขื™?

ืงื—ื• ื‘ื—ืฉื‘ื•ืŸ ืืช ื”ืงื• "ืื‘ืงื“ืื‘". ื™ืฉ ืœื• 8 ืชื•ื•ื™ื, ื•ื›ืืฉืจ ืžืงื•ื“ื“ื™ื ืื•ืจืš ืงื‘ื•ืข, ื”ื•ื ื™ืฆื˜ืจืš 64 ืกื™ื‘ื™ื•ืช ื›ื“ื™ ืœืื—ืกืŸ ืื•ืชื•. ืฉื™ื ืœื‘ ื›ื™ ืชื“ื™ืจื•ืช ื”ืกืžืœ "ื ื‘ ื’" ะธ "D" ืฉื•ื•ื” ืœ-4, 2, 1, 1 ื‘ื”ืชืืžื”. ื‘ื•ืื• ื ื ืกื” ืœื“ืžื™ื™ืŸ "ืื‘ืงื“ืื‘" ืคื—ื•ืช ื‘ื™ื˜ื™ื, ืชื•ืš ืฉื™ืžื•ืฉ ื‘ืขื•ื‘ื“ื” ืฉ "ืœ" ืžืชืจื—ืฉืช ื‘ืชื“ื™ืจื•ืช ื’ื‘ื•ื”ื” ื™ื•ืชืจ ืžืืฉืจ "ื‘"ื• - "ื‘" ืžืชืจื—ืฉืช ื‘ืชื“ื™ืจื•ืช ื’ื‘ื•ื”ื” ื™ื•ืชืจ ืžืืฉืจ "ื’" ะธ "D". ื ืชื—ื™ืœ ื‘ืงื™ื“ื•ื“ "ืœ" ืขื ื‘ื™ื˜ ืื—ื“ ืฉื•ื•ื” ืœ-0, "ื‘" ื ืงืฆื” ืงื•ื“ ืฉืœ ืฉื ื™ ืกื™ื‘ื™ื•ืช 11, ื•ื‘ืืžืฆืขื•ืช ืฉืœื•ืฉ ืกื™ื‘ื™ื•ืช 100 ื•-011 ื ืงื•ื“ื“ "ื’" ะธ "D".

ื›ืชื•ืฆืื” ืžื›ืš ื ืงื‘ืœ:

a
0

b
11

c
100

d
011

ืื– ื”ืงื• "ืื‘ืงื“ืื‘" ื ืงื•ื“ื“ ื› 00110100011011 (0|0|11|0|100|011|0|11)ื‘ืืžืฆืขื•ืช ื”ืงื•ื“ื™ื ืฉืœืžืขืœื”. ืขื ื–ืืช, ื”ื‘ืขื™ื” ื”ืขื™ืงืจื™ืช ืชื”ื™ื” ื‘ืคืขื ื•ื—. ื›ืืฉืจ ืื ื• ืžื ืกื™ื ืœืคืขื ื— ืืช ื”ืžื—ืจื•ื–ืช 00110100011011, ื ืงื‘ืœ ืชื•ืฆืื” ืžืขื•ืจืคืœืช, ืฉื›ืŸ ื ื™ืชืŸ ืœื™ื™ืฆื’ ืื•ืชื” ื›ืš:

0|011|0|100|011|0|11    adacdab
0|0|11|0|100|0|11|011   aabacabd
0|011|0|100|0|11|0|11   adacabab 

...
ื•ื›ื• '

ื›ื“ื™ ืœืžื ื•ืข ืื™ ื‘ื”ื™ืจื•ืช ื–ื•, ืขืœื™ื ื• ืœื•ื•ื“ื ืฉื”ืงื™ื“ื•ื“ ืฉืœื ื• ืขื•ื ื” ืขืœ ืžื•ืฉื’ ื›ืžื• ื›ืœืœ ืงื™ื“ื•ืžืช, ืืฉืจ ื‘ืชื•ืจื• ืžืจืžื– ืฉื ื™ืชืŸ ืœืคืขื ื— ืืช ื”ืงื•ื“ื™ื ืจืง ื‘ื“ืจืš ื™ื™ื—ื•ื“ื™ืช ืื—ืช. ื›ืœืœ ื”ืงื™ื“ื•ืžืช ืžื‘ื˜ื™ื— ืฉืืฃ ืงื•ื“ ืื™ื ื• ืงื™ื“ื•ืžืช ืฉืœ ืื—ืจ. ื‘ืงื•ื“, ืื ื• ืžืชื›ื•ื•ื ื™ื ืœื‘ื™ื˜ื™ื ื”ืžืฉืžืฉื™ื ืœื™ื™ืฆื•ื’ ืชื• ืžืกื•ื™ื. ื‘ื“ื•ื’ืžื” ืœืžืขืœื” 0 ื”ื•ื ืงื™ื“ื•ืžืช 011, ืžื” ืฉืžืคืจ ืืช ื›ืœืœ ื”ืงื™ื“ื•ืžืช. ืœื›ืŸ, ืื ื”ืงื•ื“ื™ื ืฉืœื ื• ืขื•ืžื“ื™ื ื‘ื—ื•ืง ื”ืงื™ื“ื•ืžืช, ืื– ื ื•ื›ืœ ืœืคืขื ื— ื‘ืื•ืคืŸ ื™ื™ื—ื•ื“ื™ (ื•ืœื”ื™ืคืš).

ื‘ื•ืื• ื ื—ื–ื•ืจ ืขืœ ื”ื“ื•ื’ืžื” ืฉืœืžืขืœื”. ื”ืคืขื ื ืงืฆื” ืœืกืžืœื™ื "ื ื‘ ื’" ะธ "D" ืงื•ื“ื™ื ื”ืขื•ื ื™ื ืขืœ ื›ืœืœ ื”ืงื™ื“ื•ืžืช.

a
0

b
10

c
110

d
111

ืขื ื”ืงื™ื“ื•ื“ ื”ื–ื”, ื”ืžื—ืจื•ื–ืช "ืื‘ืงื“ืื‘" ื™ืงื•ื“ื“ ื› 00100100011010 (0|0|10|0|100|011|0|10). ืื‘ืœ 00100100011010 ื›ื‘ืจ ื ื•ื›ืœ ืœืคืขื ื— ื‘ืื•ืคืŸ ื—ื“ ืžืฉืžืขื™ ื•ืœื—ื–ื•ืจ ืœืžื—ืจื•ื–ืช ื”ืžืงื•ืจื™ืช ืฉืœื ื• "ืื‘ืงื“ืื‘".

ืงื™ื“ื•ื“ ื”ืืคืžืŸ

ื›ืขืช, ืœืื—ืจ ืฉืขืกืงื ื• ื‘ืงื™ื“ื•ื“ ื‘ืื•ืจืš ืžืฉืชื ื” ื•ื‘ื›ืœืœ ื”ืงื™ื“ื•ืžืช, ื‘ื•ืื• ื ื“ื‘ืจ ืขืœ ืงื™ื“ื•ื“ ื”ืืคืžืŸ.

ื”ืฉื™ื˜ื” ืžื‘ื•ืกืกืช ืขืœ ื™ืฆื™ืจืช ืขืฆื™ื ื‘ื™ื ืืจื™ื™ื. ื‘ื•, ื”ืฆื•ืžืช ื™ื›ื•ืœ ืœื”ื™ื•ืช ืกื•ืคื™ ืื• ืคื ื™ืžื™. ื‘ืชื—ื™ืœื”, ื›ืœ ื”ืฆืžืชื™ื ื ื—ืฉื‘ื™ื ืœืขืœื™ื (ื˜ืจืžื™ื ืœื™ื), ื”ืžื™ื™ืฆื’ื™ื ืืช ื”ืกืžืœ ืขืฆืžื• ื•ืืช ืžืฉืงืœื• (ื›ืœื•ืžืจ, ืชื“ื™ืจื•ืช ื”ื”ืชืจื—ืฉื•ืช). ื”ืฆืžืชื™ื ื”ืคื ื™ืžื™ื™ื ืžื›ื™ืœื™ื ืืช ืžืฉืงืœ ื”ืชื• ื•ืžืชื™ื™ื—ืกื™ื ืœืฉื ื™ ืฆืžืชื™ื ืฆืืฆืื™ื. ื‘ื”ืกื›ืžื” ื›ืœืœื™ืช, ืงืฆืช ยซ0ยป ืžื™ื™ืฆื’ ื‘ืขืงื‘ื•ืช ื”ืขื ืฃ ื”ืฉืžืืœื™, ื• ยซ1ยป - ื‘ืฆื“ ื™ืžื™ืŸ. ื‘ืขืฅ ืžืœื N ืขืœื™ื ื• N-1 ืฆืžืชื™ื ืคื ื™ืžื™ื™ื. ืžื•ืžืœืฅ ื›ื™ ื‘ืขืช ื‘ื ื™ื™ืช ืขืฅ ื”ืืคืžืŸ, ื™ืฉ ืœื”ืฉืœื™ืš ืกืžืœื™ื ืฉืื™ื ื ื‘ืฉื™ืžื•ืฉ ื›ื“ื™ ืœืงื‘ืœ ืงื•ื“ื™ ืื•ืจืš ืื•ืคื˜ื™ืžืœื™ื™ื.

ื ืฉืชืžืฉ ื‘ืชื•ืจ ืขื“ื™ืคื•ืช ืœื‘ื ื™ื™ืช ืขืฅ ื”ืืคืžืŸ, ื›ืืฉืจ ื”ืฆื•ืžืช ื‘ืขืœ ื”ืชื“ืจ ื”ื ืžื•ืš ื‘ื™ื•ืชืจ ื™ืงื‘ืœ ืืช ื”ืขื“ื™ืคื•ืช ื”ื’ื‘ื•ื”ื” ื‘ื™ื•ืชืจ. ืฉืœื‘ื™ ื”ื‘ื ื™ื™ื” ืžืชื•ืืจื™ื ืœื”ืœืŸ:

  1. ืฆื•ืจ ืฆื•ืžืช ืขืœื™ื ืขื‘ื•ืจ ื›ืœ ืชื• ื•ื”ื•ืกืฃ ืื•ืชื ืœืชื•ืจ ื”ืขื“ื™ืคื•ืช.
  2. ื‘ืขื•ื“ ืฉื™ืฉ ื™ื•ืชืจ ืžื’ื™ืœื™ื•ืŸ ืื—ื“ ื‘ืชื•ืจ, ื‘ืฆืข ืืช ื”ืคืขื•ืœื•ืช ื”ื‘ืื•ืช:
    • ื”ืกืจ ืืช ืฉื ื™ ื”ืฆืžืชื™ื ืขื ื”ืขื“ื™ืคื•ืช ื”ื’ื‘ื•ื”ื” ื‘ื™ื•ืชืจ (ื”ืชื“ื™ืจื•ืช ื”ื ืžื•ื›ื” ื‘ื™ื•ืชืจ) ืžื”ืชื•ืจ;
    • ืฆื•ืจ ืฆื•ืžืช ืคื ื™ืžื™ ื—ื“ืฉ, ืฉื‘ื• ืฉื ื™ ื”ืฆืžืชื™ื ื”ืœืœื• ื™ื”ื™ื• ื™ืœื“ื™ื, ื•ืชื“ื™ืจื•ืช ื”ื”ืชืจื—ืฉื•ืช ืชื”ื™ื” ืฉื•ื•ื” ืœืกื›ื•ื ื”ืชื“ืจื™ื ืฉืœ ืฉื ื™ ื”ืฆืžืชื™ื ื”ืœืœื•.
    • ื”ื•ืกืฃ ืฆื•ืžืช ื—ื“ืฉ ืœืชื•ืจ ื”ืขื“ื™ืคื•ืช.
  3. ื”ืฆื•ืžืช ื”ื™ื—ื™ื“ ืฉื ื•ืชืจ ื™ื”ื™ื” ื”ืฉื•ืจืฉ, ื•ื–ื” ื™ืฉืœื™ื ืืช ื‘ื ื™ื™ืช ื”ืขืฅ.

ืชืืจื• ืœืขืฆืžื›ื ืฉื™ืฉ ืœื ื• ื˜ืงืกื˜ ืฉืžื•ืจื›ื‘ ืจืง ืžืชื•ื•ื™ื "ื ื‘ ื’ ื“" ะธ "ื•", ื•ืชื“ืจื™ ื”ืžื•ืคืข ืฉืœื”ื ื”ื 15, 7, 6, 6 ื•-5, ื‘ื”ืชืืžื”. ืœื”ืœืŸ ืื™ื•ืจื™ื ื”ืžืฉืงืคื™ื ืืช ืฉืœื‘ื™ ื”ืืœื’ื•ืจื™ืชื.

ืืœื’ื•ืจื™ืชื ื“ื—ื™ืกื” ืฉืœ ื”ืืคืžืŸ

ืืœื’ื•ืจื™ืชื ื“ื—ื™ืกื” ืฉืœ ื”ืืคืžืŸ

ืืœื’ื•ืจื™ืชื ื“ื—ื™ืกื” ืฉืœ ื”ืืคืžืŸ

ืืœื’ื•ืจื™ืชื ื“ื—ื™ืกื” ืฉืœ ื”ืืคืžืŸ

ืืœื’ื•ืจื™ืชื ื“ื—ื™ืกื” ืฉืœ ื”ืืคืžืŸ

ื ืชื™ื‘ ืžื”ืฉื•ืจืฉ ืœื›ืœ ืฆื•ืžืช ืงืฆื” ื™ืื—ืกืŸ ืืช ืงื•ื“ ื”ืงื™ื“ื•ืžืช ื”ืื•ืคื˜ื™ืžืœื™ (ื”ื™ื“ื•ืข ื’ื ื›ืงื•ื“ Huffman) ื”ืชื•ืื ืœืชื• ื”ืžืฉื•ื™ืš ืœืื•ืชื• ืฆื•ืžืช ืงืฆื”.

ืืœื’ื•ืจื™ืชื ื“ื—ื™ืกื” ืฉืœ ื”ืืคืžืŸ
ืขืฅ ื”ืืคืžืŸ

ืœื”ืœืŸ ืชืžืฆืื• ืืช ื”ื™ื™ืฉื•ื ืฉืœ ืืœื’ื•ืจื™ืชื ื”ื“ื—ื™ืกื” ืฉืœ Huffman ื‘-C++ ื•ื‘-Java:

#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>
using namespace std;

// A Tree node
struct Node
{
	char ch;
	int freq;
	Node *left, *right;
};

// Function to allocate a new tree node
Node* getNode(char ch, int freq, Node* left, Node* right)
{
	Node* node = new Node();

	node->ch = ch;
	node->freq = freq;
	node->left = left;
	node->right = right;

	return node;
}

// Comparison object to be used to order the heap
struct comp
{
	bool operator()(Node* l, Node* r)
	{
		// highest priority item has lowest frequency
		return l->freq > r->freq;
	}
};

// traverse the Huffman Tree and store Huffman Codes
// in a map.
void encode(Node* root, string str,
			unordered_map<char, string> &huffmanCode)
{
	if (root == nullptr)
		return;

	// found a leaf node
	if (!root->left && !root->right) {
		huffmanCode[root->ch] = str;
	}

	encode(root->left, str + "0", huffmanCode);
	encode(root->right, str + "1", huffmanCode);
}

// traverse the Huffman Tree and decode the encoded string
void decode(Node* root, int &index, string str)
{
	if (root == nullptr) {
		return;
	}

	// found a leaf node
	if (!root->left && !root->right)
	{
		cout << root->ch;
		return;
	}

	index++;

	if (str[index] =='0')
		decode(root->left, index, str);
	else
		decode(root->right, index, str);
}

// Builds Huffman Tree and decode given input text
void buildHuffmanTree(string text)
{
	// count frequency of appearance of each character
	// and store it in a map
	unordered_map<char, int> freq;
	for (char ch: text) {
		freq[ch]++;
	}

	// Create a priority queue to store live nodes of
	// Huffman tree;
	priority_queue<Node*, vector<Node*>, comp> pq;

	// Create a leaf node for each character and add it
	// to the priority queue.
	for (auto pair: freq) {
		pq.push(getNode(pair.first, pair.second, nullptr, nullptr));
	}

	// do till there is more than one node in the queue
	while (pq.size() != 1)
	{
		// Remove the two nodes of highest priority
		// (lowest frequency) from the queue
		Node *left = pq.top(); pq.pop();
		Node *right = pq.top();	pq.pop();

		// Create a new internal node with these two nodes
		// as children and with frequency equal to the sum
		// of the two nodes' frequencies. Add the new node
		// to the priority queue.
		int sum = left->freq + right->freq;
		pq.push(getNode('', sum, left, right));
	}

	// root stores pointer to root of Huffman Tree
	Node* root = pq.top();

	// traverse the Huffman Tree and store Huffman Codes
	// in a map. Also prints them
	unordered_map<char, string> huffmanCode;
	encode(root, "", huffmanCode);

	cout << "Huffman Codes are :n" << 'n';
	for (auto pair: huffmanCode) {
		cout << pair.first << " " << pair.second << 'n';
	}

	cout << "nOriginal string was :n" << text << 'n';

	// print encoded string
	string str = "";
	for (char ch: text) {
		str += huffmanCode[ch];
	}

	cout << "nEncoded string is :n" << str << 'n';

	// traverse the Huffman Tree again and this time
	// decode the encoded string
	int index = -1;
	cout << "nDecoded string is: n";
	while (index < (int)str.size() - 2) {
		decode(root, index, str);
	}
}

// Huffman coding algorithm
int main()
{
	string text = "Huffman coding is a data compression algorithm.";

	buildHuffmanTree(text);

	return 0;
}

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

// A Tree node
class Node
{
	char ch;
	int freq;
	Node left = null, right = null;

	Node(char ch, int freq)
	{
		this.ch = ch;
		this.freq = freq;
	}

	public Node(char ch, int freq, Node left, Node right) {
		this.ch = ch;
		this.freq = freq;
		this.left = left;
		this.right = right;
	}
};

class Huffman
{
	// traverse the Huffman Tree and store Huffman Codes
	// in a map.
	public static void encode(Node root, String str,
							  Map<Character, String> huffmanCode)
	{
		if (root == null)
			return;

		// found a leaf node
		if (root.left == null && root.right == null) {
			huffmanCode.put(root.ch, str);
		}


		encode(root.left, str + "0", huffmanCode);
		encode(root.right, str + "1", huffmanCode);
	}

	// traverse the Huffman Tree and decode the encoded string
	public static int decode(Node root, int index, StringBuilder sb)
	{
		if (root == null)
			return index;

		// found a leaf node
		if (root.left == null && root.right == null)
		{
			System.out.print(root.ch);
			return index;
		}

		index++;

		if (sb.charAt(index) == '0')
			index = decode(root.left, index, sb);
		else
			index = decode(root.right, index, sb);

		return index;
	}

	// Builds Huffman Tree and huffmanCode and decode given input text
	public static void buildHuffmanTree(String text)
	{
		// count frequency of appearance of each character
		// and store it in a map
		Map<Character, Integer> freq = new HashMap<>();
		for (int i = 0 ; i < text.length(); i++) {
			if (!freq.containsKey(text.charAt(i))) {
				freq.put(text.charAt(i), 0);
			}
			freq.put(text.charAt(i), freq.get(text.charAt(i)) + 1);
		}

		// Create a priority queue to store live nodes of Huffman tree
		// Notice that highest priority item has lowest frequency
		PriorityQueue<Node> pq = new PriorityQueue<>(
										(l, r) -> l.freq - r.freq);

		// Create a leaf node for each character and add it
		// to the priority queue.
		for (Map.Entry<Character, Integer> entry : freq.entrySet()) {
			pq.add(new Node(entry.getKey(), entry.getValue()));
		}

		// do till there is more than one node in the queue
		while (pq.size() != 1)
		{
			// Remove the two nodes of highest priority
			// (lowest frequency) from the queue
			Node left = pq.poll();
			Node right = pq.poll();

			// Create a new internal node with these two nodes as children 
			// and with frequency equal to the sum of the two nodes
			// frequencies. Add the new node to the priority queue.
			int sum = left.freq + right.freq;
			pq.add(new Node('', sum, left, right));
		}

		// root stores pointer to root of Huffman Tree
		Node root = pq.peek();

		// traverse the Huffman tree and store the Huffman codes in a map
		Map<Character, String> huffmanCode = new HashMap<>();
		encode(root, "", huffmanCode);

		// print the Huffman codes
		System.out.println("Huffman Codes are :n");
		for (Map.Entry<Character, String> entry : huffmanCode.entrySet()) {
			System.out.println(entry.getKey() + " " + entry.getValue());
		}

		System.out.println("nOriginal string was :n" + text);

		// print encoded string
		StringBuilder sb = new StringBuilder();
		for (int i = 0 ; i < text.length(); i++) {
			sb.append(huffmanCode.get(text.charAt(i)));
		}

		System.out.println("nEncoded string is :n" + sb);

		// traverse the Huffman Tree again and this time
		// decode the encoded string
		int index = -1;
		System.out.println("nDecoded string is: n");
		while (index < sb.length() - 2) {
			index = decode(root, index, sb);
		}
	}

	public static void main(String[] args)
	{
		String text = "Huffman coding is a data compression algorithm.";

		buildHuffmanTree(text);
	}
}

ื”ืขืจื”: ื”ื–ื™ื›ืจื•ืŸ ื”ืžืฉืžืฉ ืืช ืžื—ืจื•ื–ืช ื”ืงืœื˜ ื”ื•ื 47*8 = 376 ืกื™ื‘ื™ื•ืช ื•ื”ืžื—ืจื•ื–ืช ื”ืžืงื•ื“ื“ืช ื”ื™ื ืจืง 194 ืกื™ื‘ื™ื•ืช, ื›ืœื•ืžืจ. ื”ื ืชื•ื ื™ื ื ื“ื—ืกื™ื ื‘ื›-48%. ื‘ืชื•ื›ื ื™ืช C++ ืฉืœืžืขืœื”, ืื ื• ืžืฉืชืžืฉื™ื ื‘ืžื—ืœืงื” string ื›ื“ื™ ืœืื—ืกืŸ ืืช ื”ืžื—ืจื•ื–ืช ื”ืžืงื•ื“ื“ืช ื›ื“ื™ ืœื”ืคื•ืš ืืช ื”ืชื•ื›ื ื™ืช ืœืงืจื™ืื”.

ืžื›ื™ื•ื•ืŸ ืฉืžื‘ื ื™ ื ืชื•ื ื™ื ื™ืขื™ืœื™ื ืฉืœ ืชื•ืจ ืขื“ื™ืคื•ืช ื“ื•ืจืฉื™ื ื›ืœ ื”ื›ื ืกื” O(log(N)) ื–ืžืŸ, ืื‘ืœ ื‘ืขืฅ ื‘ื™ื ืืจื™ ืฉืœื ืขื N ืขืœื™ื ื ื•ื›ื—ื™ื 2N-1 ืฆืžืชื™ื, ื•ืขืฅ ื”ื”ืืคืžืŸ ื”ื•ื ืขืฅ ื‘ื™ื ืืจื™ ืฉืœื, ืื– ื”ืืœื’ื•ืจื™ืชื ื ื›ื ืก ืคื ื™ืžื” O(Nlog(N)) ื–ืžืŸ, ืื™ืคื” N - ื“ืžื•ื™ื•ืช.

ืžืงื•ืจื•ืช:

en.wikipedia.org/wiki/Huffman_coding
en.wikipedia.org/wiki/Variable-length_code
www.youtube.com/watch?v=5wRPin4oxCo

ืœืžื™ื“ืข ื ื•ืกืฃ ืขืœ ื”ืงื•ืจืก.

ืžืงื•ืจ: www.habr.com

ื”ื•ืกืคืช ืชื’ื•ื‘ื”