Skip to content

Commit 64fdc51

Browse files
committed
fin
1 parent 9fb640d commit 64fdc51

17 files changed

+326
-54
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 avltree/Display.cpp avltree/Display.hpp Binary-Tree-RecursiveFun.cpp)
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 avltree/Menu.cpp avltree/Menu.hpp)

IMPORT-DATA0.txt

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

IMPORT-DATA6.txt

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

IMPORT-DATA_SINGLE.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5

IMPORT-DATA_Subtree2false.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
16
2+
4
3+
3
4+
31
5+
18
6+
19
7+
495

IMPORT-DATA_Subtree2true.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
16
2+
4
3+
3
4+
31
5+
18
6+
19

IMPORT-DATA_unbalanced1.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
15
2+
5
3+
6
4+
20
5+
18
6+
94
7+
98
8+
29
9+
28
10+
78
11+
23
12+
47
13+
81
14+
40
15+
83

IMPORT-DATA_unbalanced2.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
60
2+
97
3+
88
4+
67
5+
35
6+
40
7+
2
8+
26
9+
22
10+
27
11+
6

avltree/AVLManager.cpp

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
#include "AVLManager.hpp"
22

3-
AVLManager::AVLManager() {
4-
setupTree();
5-
}
3+
AVLManager::AVLManager() {}
64

7-
void AVLManager::setupTree() {
8-
feedNewTree();
9-
display = new Display();
10-
display->printTree(holders.at(0));
11-
display->printDisplays();
12-
delete display;
13-
holders.clear();
14-
setupTree();
5+
// try to load tree from save, by index. make a new tree, if not saved yet
6+
Holder* AVLManager::getTree(string command) {
7+
try {
8+
int index = stoi(command);
9+
if (command.length() == 1 && (holders.size()-1) >= index) {
10+
return holders.at(stoi(command));
11+
}
12+
} catch (...) {}
13+
int res = feedNewTree(command);
14+
if (res != -1) return holders.at(res);
15+
else return nullptr;
1516
}
1617

17-
void AVLManager::feedNewTree() {
18-
string path;
19-
// path = "/home/domain/Nextcloud/Documents/Technikum/2_Semester/ALGOS/AVLTree/IMPORT-DATA5.txt";
20-
cout << "Feed the tree! (Drag&Drop): ";
21-
cin >> path;
22-
18+
// feed a tree with data
19+
int AVLManager::feedNewTree(string path) {
2320
ifstream inFile(path, ios::in);
2421
if( inFile.fail( ) ) {
2522
cerr << "Error: Datei nicht gefunden oder kann nicht geöffnet werden:\n->" << path <<endl;
26-
feedNewTree(); // Error;
23+
return -1; // Error;
2724
} else {
2825
Holder* holder = new Holder();
29-
3026
string lineStr;
3127
while (getline(inFile, lineStr)) {
3228
int error = holder->insertNode(stoi(lineStr));
3329
if (error == 0) continue;
3430
else printError(error);
3531
}
3632
holders.push_back(holder);
33+
holder->getAVLCondition(holder->getHead());
34+
std::cout << "\nTree successfully added and saved at " << holders.size()-1 << ".\n\n";
35+
return (holders.size()-1);
3736
}
3837
}
3938

39+
// some error prints
4040
void AVLManager::printError(int error) {
4141
switch (error) {
4242
case 1: std::cout << "Error 1: Duplicate Numbers >:(" << endl; break;

avltree/AVLManager.hpp

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

1111
using namespace std;
1212

1313
class AVLManager {
1414
public:
1515
AVLManager();
16-
void feedNewTree();
17-
void printError(int error);
16+
Holder* getTree(string command);
17+
1818

1919
private:
20-
void setupTree();
20+
int feedNewTree(string path);
21+
void printError(int error);
22+
2123
std::vector<Holder*> holders;
22-
Display* display;
24+
2325
};
2426

2527

avltree/Display.cpp

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
#include "Display.hpp"
22
//#define clearScreen() (std::cout << "\033c")
3+
#define WAIT_TIME 300000 // 500000 = 0,5sek
34

5+
Display::Display(Holder* holder) {
6+
printTree(holder);
7+
}
8+
9+
// display preps, prints and self-destruct.
410
void Display::printTree(Holder* holder) {
511
int depth = holder->getDepth(holder->getHead());
6-
int maxNumDigits = getDigits(holder->getMaxNum(holder->getHead()));
12+
int maxNumDigits = getDigits(holder->getMinMaxNum(holder->getHead(), true));
713

814
for (int i = 0; i < depth+1; ++i) {
915
displays.push_back("");
1016
}
1117
calcEmptySpaces(depth, maxNumDigits);
1218
recursiveDraw(depth, maxNumDigits, holder->getHead());
13-
// printDisplays();
19+
20+
usleep(WAIT_TIME);
21+
printDisplays();
22+
delete this;
1423
}
1524

25+
// draw one node after another
1626
void Display::recursiveDraw(int depth, int maxNumDigits, Node* node) {
17-
usleep(500000);
27+
usleep(WAIT_TIME);
1828
printDisplays();
1929
if (depth < 0) return;
2030
int emptySpace = getEmptySpace(depth);
@@ -38,6 +48,8 @@ void Display::recursiveDraw(int depth, int maxNumDigits, Node* node) {
3848
if (maxNumDigits == 1 || maxNumDigits == 3)
3949
displays.at(depth).append(" ");
4050
}
51+
52+
// draw empty space, where nodes should've been
4153
void Display::recursiveDraw(int depth, int maxNumDigits) {
4254
int emptySpace = getEmptySpace(depth);
4355
displays.at(depth).append(emptySpace, ' ');
@@ -50,6 +62,7 @@ void Display::recursiveDraw(int depth, int maxNumDigits) {
5062
displays.at(depth).append(emptySpace, ' ');
5163
}
5264

65+
// Node: (1) (2) ..
5366
std::string Display::drawNode(Node* node, int maxNumDigits) {
5467
int digits = getDigits(node->num);
5568
if (maxNumDigits == digits)
@@ -65,13 +78,16 @@ std::string Display::drawNode(Node* node, int maxNumDigits) {
6578
return " ";
6679
}
6780

81+
// check how much digits the largest num in tree has
6882
int Display::getDigits(int num) {
6983
if (num >= 1000) return 4;
7084
if (num >= 100) return 3;
7185
if (num >= 10) return 2;
7286
return 1;
7387
}
7488

89+
// based on digit-amount of largest num in tree,
90+
// decide which number-sequence to take for [empty space]-calculations
7591
void Display::calcEmptySpaces(int depth, int maxNumDigits) {
7692
emptySpaces.clear();
7793

@@ -100,6 +116,8 @@ void Display::calcEmptySpaces(int depth, int maxNumDigits) {
100116
}
101117
}
102118

119+
// 3 formulas for number sequences to calculate spaces for given tree-depth
120+
// n = (n*2)+2
103121
int Display::recursiveSingleSpace(int depth, int maxDepth, int num) {
104122
if (depth < maxDepth) {
105123
return recursiveSingleSpace(depth+1, maxDepth, (num*2)+2);
@@ -108,6 +126,7 @@ int Display::recursiveSingleSpace(int depth, int maxDepth, int num) {
108126
}
109127
}
110128

129+
// n = n+3*2^d
111130
int Display::recursiveDoubTripSpace(int depth, int maxDepth, int num) {
112131
if (depth < maxDepth) {
113132
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) {
116135
}
117136
}
118137

138+
// n = (2*n)+3
119139
int Display::recursiveQuadSpace(int depth, int maxDepth, int num) {
120140
if (depth < maxDepth) {
121141
return recursiveQuadSpace(depth+1, maxDepth, ((2*num)+3));
@@ -124,11 +144,11 @@ int Display::recursiveQuadSpace(int depth, int maxDepth, int num) {
124144
}
125145
}
126146

147+
// return number of spaces for given depth
127148
int Display::getEmptySpace(int depth) {
128149
return emptySpaces.at(depth);
129150
}
130151

131-
132152
void Display::printDisplays() {
133153
std::cout << "\033c";
134154
for (int i = 0; i < displays.size(); ++i) {

avltree/Display.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
class Display {
1313
public:
14+
Display(Holder* holder);
15+
private:
1416
void printTree(Holder* holder);
1517
void recursiveDraw(int depth, int maxNumDigits, Node* node);
1618
void recursiveDraw(int depth, int maxNumDigits);

0 commit comments

Comments
 (0)