Skip to content

Commit 0e28462

Browse files
committed
Display! :D
added Display for: 1, 2, 3, 4 - digits
1 parent 12b4ae5 commit 0e28462

12 files changed

+340
-12
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ project(AVLTree)
33

44
set(CMAKE_CXX_STANDARD 23)
55

6-
add_executable(AVLTree avltree/main.cpp avltree/Holder.cpp avltree/Holder.hpp avltree/Node.cpp avltree/Node.hpp avltree/AVLManager.cpp avltree/AVLManager.hpp)
6+
add_executable(AVLTree avltree/main.cpp avltree/Holder.cpp avltree/Holder.hpp avltree/Node.cpp avltree/Node.hpp avltree/AVLManager.cpp avltree/AVLManager.hpp avltree/Display.cpp avltree/Display.hpp Binary-Tree-RecursiveFun.cpp)

IMPORT-DATA1.txt

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
5
2-
3
3-
17
2+
7
43
9
5-
23
6-
54
7-
11
8-
79
9-
30
10-
12
4+
6
5+
3
6+
1
7+
4

IMPORT-DATA2.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
16
2+
8
3+
4
4+
2
5+
1
6+
3
7+
6
8+
5
9+
7
10+
12
11+
10
12+
9
13+
11
14+
14
15+
13
16+
15
17+
31
18+
20
19+
18
20+
17
21+
19
22+
22
23+
21
24+
23
25+
36
26+
34
27+
38
28+
33
29+
35
30+
37
31+
39

IMPORT-DATA3.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
16
2+
8
3+
4
4+
2
5+
1
6+
3
7+
6
8+
5
9+
7
10+
12
11+
10
12+
9
13+
11
14+
14
15+
13
16+
15
17+
31
18+
20
19+
18
20+
17
21+
19
22+
22
23+
21
24+
23
25+
36
26+
34
27+
38
28+
33
29+
35
30+
37
31+
390

IMPORT-DATA4.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
16
2+
8
3+
4
4+
2
5+
1
6+
3
7+
6
8+
5
9+
7
10+
12
11+
10
12+
9
13+
11
14+
14
15+
13
16+
15
17+
31
18+
20
19+
18
20+
17
21+
19
22+
22
23+
21
24+
23
25+
36
26+
34
27+
38
28+
33
29+
35
30+
37
31+
3900

IMPORT-DATA5.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
16
2+
8
3+
4
4+
2
5+
1
6+
3
7+
12
8+
10
9+
9
10+
11
11+
14
12+
13
13+
31
14+
20
15+
18
16+
17
17+
19
18+
22
19+
21
20+
23
21+
36
22+
34
23+
38
24+
33
25+
35
26+
37
27+
39

avltree/AVLManager.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
#include "AVLManager.hpp"
22

33
AVLManager::AVLManager() {
4+
setupTree();
5+
}
6+
7+
void AVLManager::setupTree() {
48
feedNewTree();
9+
display = new Display();
10+
display->printTree(holders.at(0));
11+
delete display;
12+
holders.clear();
13+
setupTree();
514
}
615

716
void AVLManager::feedNewTree() {
817
string path;
9-
path = "/home/domain/Nextcloud/Documents/Technikum/2_Semester/ALGOS/AVLTree/IMPORT-DATA1.txt";
18+
path;// = "/home/domain/Nextcloud/Documents/Technikum/2_Semester/ALGOS/AVLTree/IMPORT-DATA5.txt";
1019
cout << "Feed the tree! (Drag&Drop): ";
11-
// cin >> path;
20+
cin >> path;
1221

1322
ifstream inFile(path, ios::in);
1423
if( inFile.fail( ) ) {

avltree/AVLManager.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <fstream>
77
#include <iostream>
88
#include "Holder.hpp"
9+
#include "Display.hpp"
910

1011
using namespace std;
1112

@@ -16,7 +17,9 @@ class AVLManager {
1617
void printError(int error);
1718

1819
private:
20+
void setupTree();
1921
std::vector<Holder*> holders;
22+
Display* display;
2023
};
2124

2225

avltree/Display.cpp

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}

avltree/Display.hpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef AVLTREE_DISPLAY_HPP
2+
#define AVLTREE_DISPLAY_HPP
3+
4+
#include <string>
5+
#include <vector>
6+
#include <cmath>
7+
#include <iostream>
8+
#include "Holder.hpp"
9+
10+
class Display {
11+
public:
12+
void printTree(Holder* holder);
13+
void recursiveDraw(int depth, int maxNumDigits, Node* node);
14+
void recursiveDraw(int depth, int maxNumDigits);
15+
void printDisplays();
16+
std::string drawNode(Node* node, int maxNumDigits);
17+
int getDigits(int num);
18+
std::vector<std::string> displays;
19+
int getEmptySpace(int depth);
20+
void calcEmptySpaces(int depth, int maxNumDigits);
21+
int recursiveSingleSpace(int depth, int maxDepth, int num);
22+
int recursiveDoubTripSpace(int depth, int maxDepth, int num);
23+
int recursiveQuadSpace(int depth, int maxDepth, int num);
24+
std::vector<int>emptySpaces;
25+
26+
};
27+
28+
29+
#endif //AVLTREE_DISPLAY_HPP

avltree/Holder.cpp

+30-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,33 @@ Node* Holder::searchNode(int num) {
4444

4545
void Holder::deleteNode(int num) {
4646
//to do
47-
}
47+
}
48+
49+
int Holder::getDepth(Node* node) {
50+
int depth = 0, maxDepth = 0;
51+
recursiveDive(depth, maxDepth, node);
52+
return maxDepth;
53+
}
54+
55+
// pass ints by reference to save some precious bits
56+
void Holder::recursiveDive(int &depth, int &maxDepth, Node* node) {
57+
if (node == nullptr) return;
58+
if (depth > maxDepth) maxDepth = depth;
59+
recursiveDive(++depth, maxDepth, node->left);
60+
--depth;
61+
recursiveDive(++depth, maxDepth, node->right);
62+
--depth;
63+
}
64+
65+
int Holder::getMaxNum(Node* node) {
66+
int maxNum = 0;
67+
recursiveDive(maxNum, node);
68+
return maxNum;
69+
}
70+
71+
void Holder::recursiveDive(int &maxNum, Node* node) {
72+
if (node == nullptr) return;
73+
if (node->num > maxNum) maxNum = node->num;
74+
recursiveDive(maxNum, node->left);
75+
recursiveDive(maxNum, node->right);
76+
}

0 commit comments

Comments
 (0)