Frog Archiver 1.0
Frog Archiver is compression tools for any files you have.
Loading...
Searching...
No Matches
huffmantree.h
1#pragma once
2
3#include <iostream>
4#include <stdio.h>
5#include <string>
6
7using namespace std;
8
9class Tree {
10private:
11 class Node {
12 public:
13 unsigned int freq;
14 unsigned char ch;
15 Node *left, *right;
16
17 Node() {
18 freq = 0;
19 ch = '\0';
20 left = NULL;
21 right = NULL;
22 }
23 };
24
25 Node *root;
26
27public:
28 Tree() {
29 Node *N = new Node;
30 root = N;
31 } // ctor
32 // Tree(const Tree&); //cctor
33 ~Tree() { destroy(root); } // dtor
34 const Tree &operator=(const Tree &);
35
36 friend void decoder();
37 friend void encoder();
38
39 void destroy(Node *N) {
40 if (N) {
41 destroy(N->left);
42 destroy(N->right);
43
44 delete N;
45 }
46 } // destroy the tree
47
48 unsigned int get_freq() const { return root->freq; }
49
50 unsigned char get_char() const { return root->ch; }
51
52 void set_freq(unsigned int f) { root->freq = f; }
53
54 void set_char(unsigned char c) { root->ch = c; }
55
59 Node *get_root() const { return root; }
60
64 Node *get_left() const { return root->left; }
65
69 Node *get_right() const { return root->right; }
70
76 void set_left(Node *N) { root->left = N; }
77
83 void set_right(Node *N) { root->right = N; }
84
85 // operator overloading
86
91 bool operator==(const Tree &T) const { return (root->freq == T.root->freq); }
92
97 bool operator!=(const Tree &T) const { return (root->freq != T.root->freq); }
98
103 bool operator<(const Tree &T) const { return (root->freq < T.root->freq); }
104
109 bool operator<=(const Tree &T) const { return (root->freq <= T.root->freq); }
114 bool operator>(const Tree &T) const { return (root->freq > T.root->freq); }
115
120 bool operator>=(const Tree &T) const { return (root->freq >= T.root->freq); }
121
129 void huffman(Node *N, unsigned char c, string str, string &s) const {
130 if (N) {
131 if (!N->left && !N->right && N->ch == c) {
132 s = str;
133 } else {
134 huffman(N->left, c, str + "0", s);
135 huffman(N->right, c, str + "1", s);
136 }
137 }
138 }
139
146 void huffman_list(Node *N, string str) const {
147 if (N) {
148 if (!N->left && !N->right) { // daun
149 cout << print_char(N) << " : " << N->freq << " : " << str
150 << endl; // basis
151 } else {
152 // rekursif
153 huffman_list(N->left, str + "0");
154 huffman_list(N->right, str + "1");
155 }
156 }
157 }
158
166 bool get_huf_char(string str, unsigned char &c) const {
167 Node *curr = root;
168
169 for (unsigned i = 0; i < str.size(); ++i) {
170 if (str[i] == '0') {
171 curr = curr->left;
172 }
173 if (str[i] == '1') {
174 curr = curr->right;
175 }
176 }
177
178 bool found = false;
179
180 if (!curr->left && !curr->right) { // daun
181 found = true;
182 c = curr->ch;
183 }
184
185 return found;
186 }
187
188 // prints chars
194 string print_char(Node *N) const {
195 string str = "";
196
197 if (!N->left && !N->right) {
198 unsigned char c = N->ch;
199
200 if (iscntrl(c) || c == 32) {
201 char *temp_char = new char;
202 for (int i = 0; i < 3; ++i) {
203 sprintf(temp_char, "%i", c % 8);
204 c = c - c % 8;
205 c = c / 8;
206 str = (*temp_char) + str;
207 }
208 str = '/' + str;
209 } else {
210 str = c;
211 }
212 }
213
214 return str;
215 }
216};
Definition huffmantree.h:9
bool get_huf_char(string str, unsigned char &c) const
Definition huffmantree.h:166
void set_left(Node *N)
Definition huffmantree.h:76
Node * get_right() const
Definition huffmantree.h:69
void set_right(Node *N)
Definition huffmantree.h:83
bool operator==(const Tree &T) const
Definition huffmantree.h:91
bool operator>=(const Tree &T) const
Definition huffmantree.h:120
bool operator<(const Tree &T) const
Definition huffmantree.h:103
Node * get_root() const
Definition huffmantree.h:59
void huffman_list(Node *N, string str) const
Definition huffmantree.h:146
bool operator<=(const Tree &T) const
Definition huffmantree.h:109
void huffman(Node *N, unsigned char c, string str, string &s) const
Definition huffmantree.h:129
Node * get_left() const
Definition huffmantree.h:64
string print_char(Node *N) const
Definition huffmantree.h:194
bool operator!=(const Tree &T) const
Definition huffmantree.h:97
bool operator>(const Tree &T) const
Definition huffmantree.h:114