-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.h
82 lines (66 loc) · 1.96 KB
/
tree.h
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
/*
Filename: tree.h
Description: Declaration of the class Tree to represent the binary Huffman Tree
Author: Rami Isaac
Date: 06/12/2020
Course: Data Structures II
*/
#ifndef HUFFMAN_RAMIISAAC_TREE_H
#define HUFFMAN_RAMIISAAC_TREE_H
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <fstream>
#include <queue>
#include <iomanip>
using namespace std;
class Tree{
private:
struct Node{
char _data;
size_t _frequency;
string _code;
bool _isLeaf;
Node* _left = nullptr;
Node* _right = nullptr;
// Leaf constructor
Node(char data, size_t frequency, bool isLeaf) :
_data(data), _frequency(frequency),
_left(nullptr), _right(nullptr),
_isLeaf(isLeaf){}
// Internal Node Constructor
Node(size_t frequency, Node* left, Node* right) :
_frequency(frequency),
_left(left), _right(right),
_isLeaf(false) {}
~Node() {
delete _left;
delete _right;
_left = _right = nullptr;
}
};
struct least{
bool operator()(Node* l, Node* r){
return (l->_frequency > r->_frequency || l->_frequency == r->_frequency && int(l->_data) > int(r->_data));
}
};
struct reverse{
bool operator()(Node* l, Node* r){
return (l->_frequency < r->_frequency || l->_frequency == r->_frequency && int(l->_data) < int(r->_data));
}
};
Node* _root;
vector <Node*> leafs;
unordered_map<char, string> encodingTable;
public:
Tree();
~Tree();
void setRoot(Node* root);
void handleAction(const string& inFile, const string& outFile, const string& action);
static string fileToString(const string& file);
void decode(Node* root, int& index, string str);
void encode(Node* root, const string& str);
};
#endif //HUFFMAN_RAMIISAAC_TREE_H