-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffcode.cpp
161 lines (124 loc) · 3.71 KB
/
huffcode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// huffcode.cpp UNFINISHED
// Glenn G. Chappell
// 29 Nov 2015
//
// For CS 411/611 Fall 2015
// Assignment 6, Exercise A
// Source for class HuffCode
//
// Modified 11/22/17
// Chris Hartman
// For CS 411 Fall 2017
#include "huffcode.hpp" // for class HuffCode declaration
#include <string>
using std::string;
#include <unordered_map>
using std::unordered_map;
#include<memory>
using std::shared_ptr;
using std::make_shared;
#include<queue>
#include<vector>
void generateTree( shared_ptr< Node > & head,
std::priority_queue< shared_ptr< Node >,
std::vector< shared_ptr< Node > >,
Compare > pq ) {
while( pq.size() != 1){
auto first = pq.top();
pq.pop();
auto second = pq.top();
pq.pop();
auto newNode = make_shared< Node >( Node( 0,
first->_weight + second->_weight,
first, second ) );
pq.push( newNode );
}
head = pq.top();
}
void HuffCode::setWeights( const unordered_map< char, int > & theweights ) {
if( theweights.empty() )
return;
for( const auto & value: theweights ){
auto newNode = make_shared< Node >( Node( value.first, value.second ) );
_pqueue.push( newNode );
}
}
string HuffCode::traverse_recursive( const shared_ptr< Node > & head,
const char & data, bool & found ) const {
// BASE CASE
if ( head->_data == data ){
found = true;
return "";
}
string ret = "";
// RECURSIVE CASE
if ( !found && head->_leftChild != nullptr ){
ret.append( "0" );
auto left = traverse_recursive( head->_leftChild, data, found );
if ( found )
ret += left;
}
if ( !found && head->_rightChild != nullptr ){
ret.pop_back();
ret.append( "1" );
auto right = traverse_recursive( head->_rightChild, data, found );
if ( found )
ret += right;
}
if ( !found )
ret.pop_back();
return ret;
}
string HuffCode::encode( const string & text ) const {
if( text.empty() )
return "";
auto head = make_shared< Node >( Node( 0, 0 ) );
generateTree( head, _pqueue );
string total = "";
bool found = false;
std::unordered_map< char, string > memo;
for ( const auto & character : text ) {
auto tempHead(head);
auto memoFind = memo.find( character );
string temp = "";
if ( head->_data == character)
temp.append("0");
else if ( memoFind == memo.end() ){
temp = traverse_recursive( tempHead, character, found );
memo[ character ] = temp;
}
else
temp = memo[ character ];
total += temp;
found = false;
}
return total;
}
string HuffCode::decode( const string & codestr ) const
{
if( codestr.empty() )
return "";
auto head = make_shared< Node >( Node( 0, 0 ) );
generateTree( head, _pqueue );
string ret;
auto tempHead = head;
for ( const char & character : codestr ){
if ( character == '0' ) {
if ( tempHead->_leftChild != nullptr )
tempHead = tempHead->_leftChild;
if ( tempHead->_leftChild == nullptr ) {
ret += tempHead->_data;
tempHead = head;
}
}
else if ( character == '1' ) {
if ( tempHead->_rightChild != nullptr )
tempHead = tempHead->_rightChild;
if ( tempHead->_rightChild == nullptr ) {
ret += tempHead->_data;
tempHead = head;
}
}
}
return ret;
}