-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbintree_eda.h
More file actions
216 lines (182 loc) · 5.56 KB
/
Copy pathbintree_eda.h
File metadata and controls
216 lines (182 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// bintree_eda.h
//
// Implementación de árboles binarios (con compartición de nodos)
//
// Estructuras de Datos y Algoritmos
// Facultad de Informática
// Universidad Complutense de Madrid
//
// Created by Alberto Verdejo on 20/01/17.
// Copyright (c) 2017 Alberto Verdejo. All rights reserved.
//
#ifndef bintree_eda_h
#define bintree_eda_h
#include <iostream>
#include <memory> // shared_ptr, make_shared
#include <stdexcept>
#include <stack>
#include <vector>
#include <queue>
template <class T>
class bintree {
protected:
/*
Nodo que almacena internamente el elemento (de tipo T),
y punteros al hijo izquierdo y derecho, que pueden ser
nullptr si el hijo es vacío (no existe).
*/
struct TreeNode;
using Link = std::shared_ptr<TreeNode>;
struct TreeNode {
TreeNode(Link const& l, T const& e, Link const& r) : left(l), elem(e), right(r) {};
T elem;
Link left, right;
};
// constructora privada
bintree(Link const& r) : raiz(r) {}
// puntero a la raíz del árbol
Link raiz;
public:
// árbol vacío
bintree() : raiz(nullptr) {}
// árbol hoja
bintree(T const& e) :
raiz(std::make_shared<TreeNode>(nullptr, e, nullptr)) {}
// árbol con dos hijos
bintree(bintree<T> const& l, T const& e, bintree<T> const& r) :
raiz(std::make_shared<TreeNode>(l.raiz, e, r.raiz)) {}
// constructora por copia, operador de asignación y destructora por defecto
// consultar si el árbol está vacío
bool empty() const {
return raiz == nullptr;
}
// consultar la raíz
T const& root() const {
if (empty()) throw std::domain_error("El arbol vacio no tiene raiz.");
else return raiz->elem;
}
// consultar el hijo izquierdo
bintree<T> left() const {
if (empty()) throw std::domain_error("El arbol vacio no tiene hijo izquierdo.");
else return bintree<T>(raiz->left);
}
// consultar el hijo derecho
bintree<T> right() const {
if (empty()) throw std::domain_error("El arbol vacio no tiene hijo derecho.");
else return bintree(raiz->right);
}
// recorridos
std::vector<T> preorder() const {
std::vector<T> pre;
preorder(raiz, pre);
return pre;
}
std::vector<T> inorder() const {
std::vector<T> in;
inorder(raiz, in);
return in;
}
std::vector<T> postorder() const {
std::vector<T> post;
postorder(raiz, post);
return post;
}
std::vector<T> levelorder() const {
std::vector<T> levels;
if (!empty()) {
std::queue<Link> pendientes;
pendientes.push(raiz);
while (!pendientes.empty()) {
Link sig = pendientes.front();
pendientes.pop();
levels.push_back(sig->elem);
if (sig->left != nullptr)
pendientes.push(sig->left);
if (sig->right != nullptr)
pendientes.push(sig->right);
}
}
return levels;
}
protected:
static void preorder(Link a, std::vector<T> & pre) {
if (a != nullptr) {
pre.push_back(a->elem);
preorder(a->left, pre);
preorder(a->right, pre);
}
}
static void inorder(Link a, std::vector<T> & in) {
if (a != nullptr) {
inorder(a->left, in);
in.push_back(a->elem);
inorder(a->right, in);
}
}
static void postorder(Link a, std::vector<T> & post) {
if (a != nullptr) {
postorder(a->left, post);
postorder(a->right, post);
post.push_back(a->elem);
}
}
public:
// iterador que recorre el árbol en inorden
class const_iterator {
public:
T const& operator*() const {
if (ptr == nullptr) throw std::out_of_range("fuera del arbol");
return ptr->elem;
}
T const* operator->() const {
return &operator*();
}
bool operator==(const_iterator const& other) const {
return ptr == other.ptr;
}
bool operator!=(const_iterator const& other) const {
return !(*this == other);
}
const_iterator & operator++() { // ++ prefijo
next();
return *this;
}
protected:
friend class bintree;
Link ptr;
std::stack<Link> ancestros;
const_iterator() : ptr(nullptr) {}
const_iterator(Link act) { ptr = first(act); }
Link first(Link act) {
if (act == nullptr) {
return nullptr;
} else {
while (act->left != nullptr) {
ancestros.push(act);
act = act->left;
}
return act;
}
}
void next() {
if (ptr == nullptr) {
throw std::range_error("El iterador no puede avanzar");
} else if (ptr->right != nullptr) {
ptr = first(ptr->right);
} else if (ancestros.empty()) {
ptr = nullptr;
} else {
ptr = ancestros.top();
ancestros.pop();
}
}
};
const_iterator begin() const {
return const_iterator(raiz);
}
const_iterator end() const {
return const_iterator();
}
};
#endif // bintree_eda_h