-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
120 lines (113 loc) · 2.77 KB
/
Main.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
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
#include "BTree.h"
#include <stdlib.h>
#include <time.h>
class Wrapper{
public:
int data;
Wrapper(int d = -1){
data = d;
}
~Wrapper(){
data = 0;
}
bool operator>(const Wrapper t) const {
if (data == -1 or t.data == -1) {
cerr << "called > uninitialized!" << endl;
}
return (data > t.data);
}
bool operator==(const Wrapper t) const {
if (data == -1 or t.data == -1) {
cerr << "called == uninitialized!" << endl;
exit(EXIT_FAILURE);
}
return (data == t.data);
}
};
template <typename E>
class PrintN : public Functor<E> {
std::ostream& o;
mutable int n;
public:
explicit PrintN( int n = 0, std::ostream& o = std::cout ) : o( o ), n( n ) { }
explicit PrintN( std::ostream& o ) : o( o ), n( 0 ) { }
bool operator()( const E& e ) const {
return n <= 0 || --n;
}
};
ostream& operator<< (ostream &out, const Wrapper &w) {
out << w.data;
return out;
}
int main(){
srand(42);
for (int r = 0; r < 1000; r++) {
int add[100000];
for (int i = 0; i < 100000; i++) {
add[i] = rand() % 1000;
}
BTree<Wrapper> *bt = new BTree<Wrapper>(4);
for (int i = 0; i < 1000; i++) {
cout << i << endl << endl;
Wrapper *wr = new Wrapper(add[i]);
bt->add(*wr);
//bt->root->print();
//cout << "member " << add << "?\n" << endl;
//cout << r << endl;
//if (bt->member(*wr)){
////cout << "yes" << endl;
//} else {
//bt->print(cerr);
////cout << "no" << endl;
//return -1;
//}
//if (!bt->validate(bt->root)) {
//cerr << "validation failed" << endl;
//bt->print(cerr);
//return -1;
//} else {
//cerr << "valid." << endl;
//}
//cerr << "min: " << bt->min() << endl;
//cerr << "max: " << bt->max() << endl;
//cerr << "size: " << bt->size() << endl;
}
//bt->print(cerr);
cout << "size: " << bt->size() << endl;
cout << "descending:" << endl;
size_t rc = bt->apply( PrintN<Wrapper>( 600 ), descending );
cout << rc << " == 100?" << endl;
cout << "ascending:" << endl;
rc = bt->apply( PrintN<Wrapper>( 10 ), ascending );
for (int i = 0; i < 1000; i++) {
cerr << "############### removing " << add[i] << endl;
Wrapper *wr = new Wrapper(add[i]);
bt->remove(*wr);
//bt->root->print();
//cout << "member " << add << "?\n" << endl;
//cout << r << endl;
//if (!bt->member(*wr)){
////cout << "yes" << endl;
//} else {
////cout << "no" << endl;
//return -1;
//}
//if(!bt->validate(bt->root)) {
//bt->print(cerr);
//cerr << "INVALID!" << endl;
//return -1;
//}
}
//delete[] add;
//bt->print(cerr);
//bt->print(cerr);
//cout << bt->size() << endl;
delete bt;
cout << endl << "############################################" << endl << endl;
bt = 0;
cout << r << endl;
}
//bt.add(54);
//cout << "test" << endl;
return 0;
}