-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.cpp
More file actions
99 lines (74 loc) · 1.61 KB
/
Copy pathTree.cpp
File metadata and controls
99 lines (74 loc) · 1.61 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
#include <iostream>
using namespace std;
template <typename T> class Node {
private:
T key;
Node* sx;
Node* dx;
public:
Node(const T& key) : key(key) {
sx = nullptr;
dx = nullptr;
}
Node* get_sx() const {
return sx;
}
Node* get_dx() const {
return dx;
}
const T& get_key() const {
return key;
}
void set_key(const T& key) {
this->key = key;
}
void set_sx(Node* sx){
this->sx = sx;
}
void set_dx(Node* dx){
this->dx = dx;
}
//distruttore
};
template < typename T> class BST {
private:
Node<T>* rad;
// h altezza
void add(Node<T>* nodo, const T& key){
if(nodo != nullptr && key <= nodo->get_key()){
if(nodo->get_sx() == nullptr){
nodo->set_sx(new Node<T>(key));
return;
}else{
add(nodo->get_sx(), key);
}
}else{
if(nodo != nullptr && key > nodo->get_key())
if(nodo->get_dx() == nullptr){
nodo->set_dx(new Node<T>(key));
return;
}else{
add(nodo->get_dx(), key);
}
}
}
public:
BST() : rad(nullptr) {}
BST(const T& key) {
rad = new Node<T>(key);
}
//inserimento
void add(const T& key){
if(rad == nullptr){
rad = new Node<T>(key);
return;
}
add(rad, key);
}
};
int main() {
BST<int>* tree = new BST<int>();
tree->add(10);
tree->add(7);
tree->add(3);
}