-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.h
More file actions
177 lines (148 loc) · 5.56 KB
/
BinaryTree.h
File metadata and controls
177 lines (148 loc) · 5.56 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#pragma once
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
template <typename T>
class BinaryTree
{
private:
int _depth;
std::vector<std::vector<T>> _tree;
public:
// Constructor
BinaryTree(int depth)
{
_depth = depth;
_tree.resize(_depth + 1); // Initialize the full size of the tree
for (int i = 0; i < _tree.size(); i++)
_tree[i].resize(i + 1); // Initialize the size of each branch
}
// Empty Constructor
BinaryTree() : _depth(0){}
//Destructor
~BinaryTree(){}
// Return the depth of the tree
int getDepth() { return _depth; }
// Set depth of the tree
void setDepth(int depth)
{
if (_depth > depth) // If initial depth is greater than input depth, shrink the tree
{
_depth = depth;
_tree.resize(_depth + 1);
}
else { // Otherwise, create new branches
_tree.resize(depth + 1);
for (int i = _depth; i < _tree.size(); i++)
_tree[i].resize(i + 1);
_depth = depth;
}
}
// Return the length of a specified branch
long getLengthVec(int index) { return _tree[index].size(); }
// Assign a value to a specified node
void setNode(int index1, int index2, T value) { _tree[index1][index2] = value; }
// Retrieve the value of a specified node
T getNode(int index1, int index2) { return _tree[index1][index2]; }
// Display the tree
void display()
{
for (int i = 0; i < _depth + 1; i++)
{
for (int j = 0; j <= i; j++)
{
if (j != i)
{
std::cout << _tree[i][j] << "\t";
}
else
{
std::cout << _tree[i][j] << std::endl;
}
}
}
std::cout << std::endl;
for (int i = 0; i < _tree.size(); i++)
{
int space = _depth * 3 + 3 - 3 * i;
if (i == _tree.size() - 1)
{
for (int k = 0; k < space; k++)
{
std::cout << " ";
}
for (int j = 0; j < _tree[i].size(); j++)
{
long value = 0;
if (int(_tree[i][j]) == _tree[i][j])
value = std::to_string(int(_tree[i][j])).length();
else
value = std::to_string(int(_tree[i][j])*100).length() + 1;
switch (value)
{
case 1:
std::cout << " " << _tree[i][j] << " ";
break;
case 2:
std::cout << " " << _tree[i][j] << " ";
break;
case 3:
std::cout << " " << _tree[i][j] << " ";
break;
case 4:
std::cout << _tree[i][j] << " ";
break;
default:
std::cout << "Erreur" << " ";
}
}
std::cout << std::endl << std::endl;
}
else
{
for (int k = 0; k < space; k++)
{
std::cout << " ";
}
for (int j = 0; j < _tree[i].size(); j++)
{
long value = 0;
if (int(_tree[i][j]) == _tree[i][j])
value = std::to_string(int(_tree[i][j])).length();
else
value = std::to_string(int(_tree[i][j])*100).length() + 1;
switch (value)
{
case 1:
std::cout << " " << _tree[i][j] << " ";
break;
case 2:
std::cout << " " << _tree[i][j] << " ";
break;
case 3:
std::cout << " " << _tree[i][j] << " ";
break;
case 4:
std::cout << _tree[i][j] << " ";
break;
default:
std::cout << "Erreur" << " ";
}
}
std::cout << std::endl;
for (int k = 0; k < space; k++)
{
std::cout << " ";
}
for (int j = 0; j < _tree[i].size(); j++)
{
std::cout << "/ \\ ";
}
std::cout << std::endl;
}
}
std::cout << std::endl << std::endl;
}
};