1
1
#include " Display.hpp"
2
2
// #define clearScreen() (std::cout << "\033c")
3
+ #define WAIT_TIME 300000 // 500000 = 0,5sek
3
4
5
+ Display::Display (Holder* holder) {
6
+ printTree (holder);
7
+ }
8
+
9
+ // display preps, prints and self-destruct.
4
10
void Display::printTree (Holder* holder) {
5
11
int depth = holder->getDepth (holder->getHead ());
6
- int maxNumDigits = getDigits (holder->getMaxNum (holder->getHead ()));
12
+ int maxNumDigits = getDigits (holder->getMinMaxNum (holder->getHead (), true ));
7
13
8
14
for (int i = 0 ; i < depth+1 ; ++i) {
9
15
displays.push_back (" " );
10
16
}
11
17
calcEmptySpaces (depth, maxNumDigits);
12
18
recursiveDraw (depth, maxNumDigits, holder->getHead ());
13
- // printDisplays();
19
+
20
+ usleep (WAIT_TIME);
21
+ printDisplays ();
22
+ delete this ;
14
23
}
15
24
25
+ // draw one node after another
16
26
void Display::recursiveDraw (int depth, int maxNumDigits, Node* node) {
17
- usleep (500000 );
27
+ usleep (WAIT_TIME );
18
28
printDisplays ();
19
29
if (depth < 0 ) return ;
20
30
int emptySpace = getEmptySpace (depth);
@@ -38,6 +48,8 @@ void Display::recursiveDraw(int depth, int maxNumDigits, Node* node) {
38
48
if (maxNumDigits == 1 || maxNumDigits == 3 )
39
49
displays.at (depth).append (" " );
40
50
}
51
+
52
+ // draw empty space, where nodes should've been
41
53
void Display::recursiveDraw (int depth, int maxNumDigits) {
42
54
int emptySpace = getEmptySpace (depth);
43
55
displays.at (depth).append (emptySpace, ' ' );
@@ -50,6 +62,7 @@ void Display::recursiveDraw(int depth, int maxNumDigits) {
50
62
displays.at (depth).append (emptySpace, ' ' );
51
63
}
52
64
65
+ // Node: (1) (2) ..
53
66
std::string Display::drawNode (Node* node, int maxNumDigits) {
54
67
int digits = getDigits (node->num );
55
68
if (maxNumDigits == digits)
@@ -65,13 +78,16 @@ std::string Display::drawNode(Node* node, int maxNumDigits) {
65
78
return " " ;
66
79
}
67
80
81
+ // check how much digits the largest num in tree has
68
82
int Display::getDigits (int num) {
69
83
if (num >= 1000 ) return 4 ;
70
84
if (num >= 100 ) return 3 ;
71
85
if (num >= 10 ) return 2 ;
72
86
return 1 ;
73
87
}
74
88
89
+ // based on digit-amount of largest num in tree,
90
+ // decide which number-sequence to take for [empty space]-calculations
75
91
void Display::calcEmptySpaces (int depth, int maxNumDigits) {
76
92
emptySpaces.clear ();
77
93
@@ -100,6 +116,8 @@ void Display::calcEmptySpaces(int depth, int maxNumDigits) {
100
116
}
101
117
}
102
118
119
+ // 3 formulas for number sequences to calculate spaces for given tree-depth
120
+ // n = (n*2)+2
103
121
int Display::recursiveSingleSpace (int depth, int maxDepth, int num) {
104
122
if (depth < maxDepth) {
105
123
return recursiveSingleSpace (depth+1 , maxDepth, (num*2 )+2 );
@@ -108,6 +126,7 @@ int Display::recursiveSingleSpace(int depth, int maxDepth, int num) {
108
126
}
109
127
}
110
128
129
+ // n = n+3*2^d
111
130
int Display::recursiveDoubTripSpace (int depth, int maxDepth, int num) {
112
131
if (depth < maxDepth) {
113
132
return recursiveDoubTripSpace (depth+1 , maxDepth, (num+3 *(int )std::pow (2 ,depth)));
@@ -116,6 +135,7 @@ int Display::recursiveDoubTripSpace(int depth, int maxDepth, int num) {
116
135
}
117
136
}
118
137
138
+ // n = (2*n)+3
119
139
int Display::recursiveQuadSpace (int depth, int maxDepth, int num) {
120
140
if (depth < maxDepth) {
121
141
return recursiveQuadSpace (depth+1 , maxDepth, ((2 *num)+3 ));
@@ -124,11 +144,11 @@ int Display::recursiveQuadSpace(int depth, int maxDepth, int num) {
124
144
}
125
145
}
126
146
147
+ // return number of spaces for given depth
127
148
int Display::getEmptySpace (int depth) {
128
149
return emptySpaces.at (depth);
129
150
}
130
151
131
-
132
152
void Display::printDisplays () {
133
153
std::cout << " \033 c" ;
134
154
for (int i = 0 ; i < displays.size (); ++i) {
0 commit comments