-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffmanTreeFiner.cpp
84 lines (78 loc) · 2.15 KB
/
huffmanTreeFiner.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
#include <iostream>
#include <vector>
#include <iomanip>
#include <fstream>
#include "binaryTree.h"
#include <string>
void Fig::autoFill() {
while (pos.row >= fig.size()) {
fig.push_back(std::vector<Item>({}));
}
while (pos.col > fig[pos.row].size()) {
fig[pos.row].push_back({"",""});
}
while (pos.col < fig[pos.row].size()) {
pos.col++;
}
}
Fig::Fig(): pos({0,0}) {
autoFill();
}
void Fig::print() {
int depth = fig.size();
for (auto row : fig) {
std::cout << std::setw(2*depth) << "";
for (auto str : row) {
std::cout << std::setw(4) << str.str;
}
std::cout << std::endl;
std::cout << std::setw(2*depth+1) << "";
for (auto str : row) {
std::cout << std::setw(4) << str.leaf;
}
std::cout << std::endl;
depth--;
}
}
void Fig::operator<= (Item item) {fig[pos.row].push_back(item); }
void Fig::downToLeft() {pos.row++; autoFill(); }
void Fig::downToRight() {pos.row++; pos.col++; autoFill(); }
void Fig::UpFromLeft() {pos.row--; autoFill(); }
void Fig::UpFromRight() {pos.row--; pos.col--; autoFill(); }
bool isPrintableChar(char ch) {
return ch > 32;
}
void printTree_finer_helper(Fig &fig, const Node *n) {
if (n->l) {
fig.downToLeft();
printTree_finer_helper(fig, n->l);
fig.UpFromLeft();
}
if (n->r) {
fig.downToRight();
printTree_finer_helper(fig, n->r);
fig.UpFromRight();
}
std::string sym;
if (!n->l || !n->r) {
sym = "";
if (isPrintableChar(n->c)) {
char str[] = " ";
str[0] = n->c;
fig <= Item({std::string(str), sym});
}
else {
fig <= Item({std::to_string(n->c), sym});
}
} else {
if (n->l) sym = "/ ";
if (n->r) sym = "\\";
if (n->r && n->l) sym = "/ \\";
fig <= Item({std::to_string(n->n), sym});
}
}
void printTree_finer(const Node *n) {
Fig fig;
printTree_finer_helper(fig, n);
fig.print();
}