1
+ #include " Display.hpp"
2
+
3
+ void Display::printTree (Holder* holder) {
4
+ int depth = holder->getDepth (holder->getHead ());
5
+ int maxNumDigits = getDigits (holder->getMaxNum (holder->getHead ()));
6
+
7
+ for (int i = 0 ; i < depth+1 ; ++i) {
8
+ displays.push_back (" " );
9
+ }
10
+ calcEmptySpaces (depth, maxNumDigits);
11
+ recursiveDraw (depth, maxNumDigits, holder->getHead ());
12
+ printDisplays ();
13
+ }
14
+
15
+ void Display::recursiveDraw (int depth, int maxNumDigits, Node* node) {
16
+ if (depth < 0 ) return ;
17
+ int emptySpace = getEmptySpace (depth);
18
+ displays.at (depth).append (emptySpace, ' ' );
19
+ displays.at (depth).append (drawNode (node, maxNumDigits));
20
+ if (node->left == nullptr ) {
21
+ if (depth != 0 ) {
22
+ recursiveDraw (depth-1 , maxNumDigits);
23
+ }
24
+ } else {
25
+ recursiveDraw (depth-1 , maxNumDigits, node->left );
26
+ }
27
+ if (node->right == nullptr ) {
28
+ if (depth != 0 ) {
29
+ recursiveDraw (depth-1 , maxNumDigits);
30
+ }
31
+ } else {
32
+ recursiveDraw (depth-1 , maxNumDigits, node->right );
33
+ }
34
+ displays.at (depth).append (emptySpace, ' ' );
35
+ if (maxNumDigits == 1 || maxNumDigits == 3 )
36
+ displays.at (depth).append (" " );
37
+ }
38
+ void Display::recursiveDraw (int depth, int maxNumDigits) {
39
+ int emptySpace = getEmptySpace (depth);
40
+ displays.at (depth).append (emptySpace, ' ' );
41
+ displays.at (depth).append (maxNumDigits+2 , ' ' );
42
+ if (depth != 0 ) {
43
+ recursiveDraw (depth-1 , maxNumDigits);
44
+ recursiveDraw (depth-1 , maxNumDigits);
45
+
46
+ }
47
+ displays.at (depth).append (emptySpace, ' ' );
48
+ }
49
+
50
+ std::string Display::drawNode (Node* node, int maxNumDigits) {
51
+ int digits = getDigits (node->num );
52
+ if (maxNumDigits == digits)
53
+ return " (" + std::to_string (node->num ) + " )" ;
54
+ if (maxNumDigits-1 == digits)
55
+ return " ( " + std::to_string (node->num ) + " )" ;
56
+ if (maxNumDigits-2 == digits)
57
+ return " ( " + std::to_string (node->num ) + " )" ;
58
+ if (maxNumDigits-3 == digits)
59
+ return " ( " + std::to_string (node->num ) + " )" ;
60
+ if (maxNumDigits-4 == digits)
61
+ return " ( " + std::to_string (node->num ) + " )" ;
62
+ return " " ;
63
+ }
64
+
65
+ int Display::getDigits (int num) {
66
+ if (num >= 1000 ) return 4 ;
67
+ if (num >= 100 ) return 3 ;
68
+ if (num >= 10 ) return 2 ;
69
+ return 1 ;
70
+ }
71
+
72
+ void Display::calcEmptySpaces (int depth, int maxNumDigits) {
73
+ emptySpaces.clear ();
74
+
75
+ switch (maxNumDigits) {
76
+ case 1 :
77
+ emptySpaces.push_back (0 );
78
+ for (int i = 0 ; i < depth; ++i) {
79
+ emptySpaces.push_back (recursiveSingleSpace (0 , i, 0 ));
80
+ } break ;
81
+ case 2 :
82
+ emptySpaces.push_back (1 );
83
+ for (int i = 0 ; i < depth; ++i) {
84
+ emptySpaces.push_back (recursiveDoubTripSpace (0 , i, 0 )+1 );
85
+ } break ;
86
+ case 3 :
87
+ emptySpaces.push_back (0 );
88
+ for (int i = 0 ; i < depth; ++i) {
89
+ emptySpaces.push_back (recursiveDoubTripSpace (0 , i, 0 ));
90
+ } break ;
91
+ case 4 :
92
+ emptySpaces.push_back (1 );
93
+ for (int i = 0 ; i < depth; ++i) {
94
+ emptySpaces.push_back (recursiveQuadSpace (0 , i, 1 ));
95
+ } break ;
96
+ default : break ;
97
+ }
98
+ }
99
+
100
+ int Display::recursiveSingleSpace (int depth, int maxDepth, int num) {
101
+ if (depth < maxDepth) {
102
+ return recursiveSingleSpace (depth+1 , maxDepth, (num*2 )+2 );
103
+ } else {
104
+ return (num*2 )+2 ;
105
+ }
106
+ }
107
+
108
+ int Display::recursiveDoubTripSpace (int depth, int maxDepth, int num) {
109
+ if (depth < maxDepth) {
110
+ return recursiveDoubTripSpace (depth+1 , maxDepth, (num+3 *(int )std::pow (2 ,depth)));
111
+ } else {
112
+ return (num+3 *(int )std::pow (2 ,depth));
113
+ }
114
+ }
115
+
116
+ int Display::recursiveQuadSpace (int depth, int maxDepth, int num) {
117
+ if (depth < maxDepth) {
118
+ return recursiveQuadSpace (depth+1 , maxDepth, ((2 *num)+3 ));
119
+ } else {
120
+ return ((2 *num)+3 );
121
+ }
122
+ }
123
+
124
+ int Display::getEmptySpace (int depth) {
125
+ return emptySpaces.at (depth);
126
+ }
127
+
128
+
129
+ void Display::printDisplays () {
130
+ std::cout << " \n " ;
131
+ for (int i = 0 ; i < displays.size (); ++i) {
132
+ std::cout << displays.at (i) << " \n " ;
133
+ std::cout << " -\n " ;
134
+ }
135
+ }
0 commit comments