A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d5/d8e/huffman_8cpp_source.html below:

TheAlgorithms/C++: greedy_algorithms/huffman.cpp Source File

15

MinHeapNode *left, *right;

17

MinHeapNode(

char

data,

unsigned

freq)

28

deleteAll(root->left);

29

deleteAll(root->right);

39 return

l->freq > r->freq;

45void

printCodes(

struct MinHeapNode

* root,

const string

& str) {

49 if

(root->data !=

'$'

)

50

cout << root->data <<

": "

<< str <<

"\n"

;

52

printCodes(root->left, str +

"0"

);

53

printCodes(root->right, str +

"1"

);

58void

HuffmanCodes(

const char data

[],

const int

freq[],

int

size) {

62

priority_queue<MinHeapNode*, vector<MinHeapNode*>,

compare

> minHeap;

64 for

(

int

i = 0; i < size; ++i)

68 while

(minHeap.size() != 1) {

74

right = minHeap.top();

84 auto

*

const

top =

new MinHeapNode

(

'$'

, left->freq + right->freq);

94

printCodes(minHeap.top(),

""

);

95

deleteAll(minHeap.top());

100 char

arr[] = {

'a'

,

'b'

,

'c'

,

'd'

,

'e'

,

'f'

};

101 int

freq[] = {5, 9, 12, 13, 16, 45};

103 int

size =

sizeof

(arr) /

sizeof

(arr[0]);

105

HuffmanCodes(arr, freq, size);

int main()

Main function.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4