-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTree.cpp
55 lines (53 loc) · 1.12 KB
/
Tree.cpp
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
#include <iostream>
#include <functional>
using namespace std;
template<class G>
class Tree {
template<class T>
class Node {//inner class(composicion)
public:
Node<T>* l, * r;
T data;
Node(T d) :data(d) { l = r = nullptr; }
};
Node<G>* root;
public:
Tree() { root = nullptr; }
void insert(G e,Node<G>*& tmp ) {
if (tmp == nullptr)
tmp = new Node<G>(e);
else if (e >= tmp->data)//insero a la derecha
insert(e, tmp->r);
else insert(e, tmp->l);
}
void insert(G e) {
insert(e, root);
}
void print() { print(root); }
void print(Node<G>* tmp) {//enOrden
if (tmp == nullptr)return;
print(tmp->l);
cout << tmp->data << " ";
print(tmp->r);
}
void preOrden(Node<G>* tmp) {//preOrden
if (tmp == nullptr)return;
cout << tmp->data << " ";
preOrden(tmp->l);
preOrden(tmp->r);
}
void postOrden(Node<G>* tmp) {//postOrden
if (tmp == nullptr)return;
postOrden(tmp->l);
postOrden(tmp->r);
cout << tmp->data << " ";
}
};
int main() {
Tree<int> enteros;
for (size_t i = 0;i < 1000;++i) {
enteros.insert(rand() % 100000);
}
enteros.print();
return 0;
}