Algoritmos y Estructuras de Datos

Anuncio
Algoritmos y Estructuras de Datos
Dr. Rodrigo Paz, Dr. Mario Storti, Dr. Lisandro Dalcin
CONICET
Centro Internacional de Métodos Computacionales en IngenierÃa
September 18, 2012
Contents
1
Correspondencias con listas STL ordenadas
2
2
Árbol Ordenado Orientado: interfaz tipo STL
4
1
1
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
Correspondencias con listas STL ordenadas
#ifndef AED_MAP_H
#define AED_MAP_H
#include <list>
using namespace std;
namespace aed {
/*
/*
/*
/*
/*
/*
template<typename first_t,typename second_t> */
class pair { */
public: */
first_t first; */
second_t second; */
}; */
template<typename domain_t,typename range_t>
class map {
private:
typedef pair<domain_t,range_t> pair_t;
typedef list<pair_t> list_t;
list_t l;
/* iterator para map va a ser el mismo que para listas. */
public:
typedef typename list_t::iterator iterator;
private:
iterator lower_bound(domain_t key) {
iterator p = l.begin();
while (p!=l.end()) {
domain_t dom = p->first;
if (dom >= key) return p;
p++;
}
return l.end();
}
public:
map()
{ }
iterator find(domain_t key) {
iterator p = l.lower_bound(key);
if (p!=l.end() && p->first == key)
return p;
else return l.end();
}
2
48
49
50
51
52
53
54
55
56
57
58
59
60
range_t & operator[](domain_t key) {
iterator q = find(key);
if (q==end()) q=l.insert(key,range_t());
return q->second;
}
bool empty() { return l.begin()==l.end(); }
void erase(iterator p) { l.erase(p); }
iterator begin() { return l.begin(); }
iterator end() { return l.end(); }
void clear() { l.erase(l.begin(),l.end()); }
};
}
#endif
Listing 1: correspondencia con listas ordenadas
3
2 Árbol Ordenado Orientado: interfaz tipo STL
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
#ifndef AED_TREE_H
#define AED_TREE_H
#include <iostream>
#include <cstddef>
#include <cstdlib>
#include <cassert>
using namespace std;
namespace aed {
template<class T>
class tree {
public:
class iterator;
private:
// ----------------------------------------------------------------// -------Begin Class cell-------------------------------------class cell {
friend class tree;
friend class iterator;
T t;
cell *right, *left_child;
cell() : right(NULL), left_child(NULL) {}
};
// -------End Class cell -------------------------------------// ----------------------------------------------------------------cell *header;
// ----------------------------------------------------------------iterator tree_copy_aux(iterator nq,
tree<T> &TT,iterator nt) {
nq = insert(nq,*nt);
iterator
ct = nt.lchild(),
cq = nq.lchild();
while (ct!=TT.end()) {
cq = tree_copy_aux(cq,TT,ct);
ct = ct.right();
cq = cq.right();
}
return nq;
}
// ----------------------------------------------------------------public:
static int cell_count_m;
static int cell_count() { return cell_count_m; }
// -----------------------------------------------------------------
4
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
// -------Begin Class iterator-------------------------------------class iterator {
private:
friend class tree;
cell *ptr,*prev,*father;
iterator(cell *p,cell *prev_a,cell *f_a) : ptr(p),
prev(prev_a), father(f_a) { }
public:
iterator(const iterator &q) {
ptr = q.ptr;
prev = q.prev;
father = q.father;
}
T &operator*() { return ptr->t; }
T *operator->() { return &ptr->t; }
bool operator!=(iterator q) { return ptr!=q.ptr; }
bool operator==(iterator q) { return ptr==q.ptr; }
iterator() : ptr(NULL), prev(NULL), father(NULL) { }
iterator lchild() { return iterator(ptr->left_child,NULL,ptr); }
iterator right() { return iterator(ptr->right,ptr,father); }
// Prefix:
iterator operator++() {
*this = right();
return *this;
}
// Postfix:
iterator operator++(int) {
iterator q = *this;
*this = right();
return q;
}
};
// -------End Class iterator-------------------------------------// ----------------------------------------------------------------// ------------------------------------------------------------// constructor por defecto tree
tree() {
header = new cell;
cell_count_m++;
header->right = NULL;
header->left_child = NULL;
}
// ------------------------------------------------------------// constructor copia tree
tree<T>(const tree<T> &TT) {
if (&TT != this) {
5
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
header = new cell;
cell_count_m++;
header->right = NULL;
header->left_child = NULL;
tree<T> &TTT = (tree<T> &) TT;
if (TTT.begin()!=TTT.end())
tree_copy_aux(begin(),TTT,TTT.begin());
}
}
// ------------------------------------------------------------// destructor tree
˜tree() { clear(); delete header; cell_count_m--; }
// ------------------------------------------------------------tree<T>& operator=(const tree<T>& Q) {
clear();
tree<T> &QQ = (tree<T> &) Q;
if (QQ.begin()!=QQ.end())
tree_copy_aux(begin(),QQ,QQ.begin());
}
// ------------------------------------------------------------iterator insert(iterator p,T t) {
assert(!(p.father==header && p.ptr));
cell *c = new cell;
cell_count_m++;
c->right = p.ptr;
c->t = t;
p.ptr = c;
if (p.prev) p.prev->right = c;
else p.father->left_child = c;
return p;
}
// ------------------------------------------------------------iterator erase(iterator p) {
if(p==end()) return p;
iterator c = p.lchild();
while (c!=end()) c = erase(c);
cell *q = p.ptr;
p.ptr = p.ptr->right;
if (p.prev) p.prev->right = p.ptr;
else p.father->left_child = p.ptr;
delete q;
cell_count_m--;
return p;
}
// ------------------------------------------------------------iterator splice(iterator to,iterator from) {
assert(!(to.father==header && to.ptr));
cell *c = from.ptr;
if (from.prev) from.prev->right = c->right;
6
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
else from.father->left_child = c->right;
c->right = to.ptr;
to.ptr = c;
if (to.prev) to.prev->right = c;
else to.father->left_child = c;
return to;
}
// ------------------------------------------------------------iterator find(T t) { return find(t,begin()); }
// ------------------------------------------------------------iterator find(T t,iterator p) {
if(p==end() || p.ptr->t == t) return p;
iterator q,c = p.lchild();
while (c!=end()) {
q = find(t,c);
if (q!=end()) return q;
else c++;
}
return iterator();
}
// ------------------------------------------------------------void clear() { erase(begin()); }
// ------------------------------------------------------------iterator begin() { return iterator(header->left_child,NULL,header); }
// ------------------------------------------------------------iterator end() { return iterator(); }
// ------------------------------------------------------------void print_prev(iterator p) {
if (p==end()) return;
cout << "(" << p.ptr << "," << p.ptr->t << ")" << endl;
iterator c = p.lchild();
while (c!=end()) print_prev(c++);
}
// ------------------------------------------------------------void print_prev() { print_prev(begin()); }
// ------------------------------------------------------------void print_post(iterator p) {
if (p==end()) return;
iterator c = p.lchild();
while (c!=end()) print_post(c++);
cout << "(" << p.ptr << "," << p.ptr->t << ")" << endl;
}
// ------------------------------------------------------------void print_post() { print_post(begin()); }
// ------------------------------------------------------------void lisp_print(iterator n) {
if (n==end()) return;
iterator c = n.lchild();
bool is_leaf = c==end();
7
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
if (is_leaf) cout << *n;
else {
cout << "(" << *n;
while (c!=end()) {
cout << " ";
lisp_print(c++);
}
cout << ")";
}
}
// ------------------------------------------------------------void lisp_print() { lisp_print(begin()); }
};
// --------------------------------------------------------------template<class T>
int tree<T>::cell_count_m = 0;
// ----------------------------------------------------------------}
#endif
// -----------------------------------------------------------------
Listing 2: Árbol Ordenado Orientado
8
Descargar