-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cc
More file actions
75 lines (65 loc) · 1.79 KB
/
tree.cc
File metadata and controls
75 lines (65 loc) · 1.79 KB
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
#include "storage.h"
Simple_Tree::Simple_Tree(): root{nullptr}, ite{nullptr} {
nodes.reserve(512);
}
void Simple_Tree::load_node() {
nodes.emplace_back(make_unique<Shrunk_Byte>(0,0));
Shrunk_Byte * right = _stack.top();
_stack.pop();
Shrunk_Byte * left = _stack.top();
_stack.pop();
nodes.back()->assign_left(left);
nodes.back()->assign_right(right);
_stack.push(nodes.back().get());
}
void Simple_Tree::load_node(unsigned char _byte) {
nodes.emplace_back(make_unique<Shrunk_Byte>(_byte,0));
_stack.push(nodes.back().get());
}
void Simple_Tree::move_left() {
if (ite->left == nullptr) throw Null_Leaf();
ite = ite->left;
}
void Simple_Tree::move_right() {
if (ite->right == nullptr) throw Null_Leaf();
ite = ite->right;
}
unsigned char Simple_Tree::get_byte() {
unsigned char _byte = ite->byte;
ite = root;
return _byte;
}
bool Simple_Tree::at_leaf() {
return ite->left == nullptr && ite->right == nullptr;
}
void Simple_Tree::clear_up() {
nodes.shrink_to_fit();
root = nodes.back().get();
ite = root;
}
void Simple_Tree::print_tree (ostream & os) {
for (auto & i: nodes) { i->visited = false; }
unsigned short i;
Shrunk_Byte * cur = root;
while (cur != nullptr) {
if (cur->left == nullptr && cur->right == nullptr) {
cur->visited = true;
i = cur->byte;
os << i << ' ';
cur = cur->parent;
}
else if (!(cur->left->visited)) {
cur = cur->left;
}
else if (!(cur->right->visited)) {
cur = cur->right;
}
else {
if (cur->parent) { os << "256 ";}
else { os << "256"; }
cur->visited = true;
cur = cur->parent;
}
}
os << std::endl;
}