Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testAllTree.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32# include <iostream>
33# include <aleph.H>
34# include <tpl_binTree.H>
35# include <tpl_avl.H>
36# include <tpl_treap.H>
37# include <tpl_splay_tree.H>
38# include <tpl_rb_tree.H>
39# include <tpl_rand_tree.H>
40# include <tpl_dynMapTree.H>
41# include <ran_array.h>
42# include <ctime>
43# include <gsl/gsl_rng.h>
44# include <argp.h>
45
46using namespace std;
47
48 template <class Node>
49static void printNode(Node* node, int, int)
50{
51 cout << "(" << node->get_key() << "," << node->get_data() << ")";
52}
53
54
55 template <
56 template <typename /* key */, class /* Compare */>
57 class TreeType>
59{
60 unsigned long max = 100*n;
61 size_t i;
62 int value;
64
65 for (i = 0; i < n; i++)
66 {
68 tree.insert(value, i);
69 }
70
71 cout << "Reading test ... " << endl;
72
73 int content;
74
75 for (i = 0; i < n; i++)
76 {
78 try
79 {
80 content = tree.find(value);
81 // cout << "(" << value << "," << content << ")";
82 }
83 catch (...)
84 {
85 ;//cout << i << ".";
86 }
87 }
88
89 cout << endl;
90
91 cout << "Writing test ... " << endl;
92
93 for (i = 0; i < n; i++)
94 {
96 auto val = tree.insert(value, i);
97 cout << "(" << val << "," << i << ")";
98 }
99
100 cout << endl;
101
102 cout << "The path length is " << tree.internal_path_length() << endl;
103
104 cout << "The height is " << tree.height() << endl;
105
106 unsigned int insCount = tree.size();
107 cout << insCount << " Items inserted" << endl;
108
109 for (i = 0; i < n; i++)
110 {
112 tree.remove(value);
113 }
114
115 cout << insCount - tree.size() << " Items removed" << endl;
116}
117
122
124{
125 int n;
126 int seed;
128 char *str;
129
131 : n(_n), seed(_seed), type(INVALID), str(NULL)
132 {
133 // Empty
134 }
135};
136
137
138const char *argp_program_version = "testAllTree 0.0";
139const char *argp_program_bug_address = "aleph-bugs@aleph.ula.ve";
140
141static char doc[] = "testAllTree -- A tester for all binary trees";
142static char argDoc[] = "-n num_nodes -m seed_for_random -<tree type>\n"
143;
144
145static struct argp_option options [] = {
146 { "bin", 'b', 0, OPTION_ARG_OPTIONAL, "pure binary tree" , 0},
147 { "avl", 'a', 0, OPTION_ARG_OPTIONAL, "avl tree" , 0},
148 { "splay", 's', 0, OPTION_ARG_OPTIONAL, "splay tree" , 0},
149 { "redblack", 'r', 0, OPTION_ARG_OPTIONAL, "red black tree" , 0},
150 { "rand", 'd', 0, OPTION_ARG_OPTIONAL, "randomized tree" , 0},
151 { "treap", 'p', 0, OPTION_ARG_OPTIONAL, "treap tree" , 0},
152 { "nodes", 'n', "num_nodes", OPTION_ARG_OPTIONAL,
153 "Specify the number of nodes to be generated", 0 },
154 { "seed", 'm', "seed_for_random", OPTION_ARG_OPTIONAL,
155 "Specify the seed for randon number generator", 0},
156 { 0, 0, 0, 0, 0, 0 }
157};
158
159static error_t parser_opt(int key, char *, struct argp_state *state)
160{
161 Parameters *parsPtr = static_cast<Parameters*>(state->input);
162
163 switch (key)
164 {
165 case ARGP_KEY_END:
166 if (parsPtr->type == INVALID)
167 argp_usage(state);
168 break;
169 case 'n':
170 char *end;
171 if (state->argv[state->next] == NULL)
172 argp_usage(state);
173 parsPtr->n = strtol(state->argv[state->next], &end, 10);
174 if (*end != '\0' && *end != '\n')
175 argp_usage(state);
176 state->next++;
177 break;
178 case 'b':
179 parsPtr->type = BIN;
180 parsPtr->str = "BinTree";
181 break;
182 case 'a':
183 parsPtr->type = AVL;
184 parsPtr->str = "AvlTree";
185 break;
186 case 'r':
187 parsPtr->type = RB;
188 parsPtr->str = "RbTree";
189 break;
190 case 's':
191 parsPtr->type = SPLAY;
192 parsPtr->str = "SplayTree";
193 break;
194 case 'p':
195 parsPtr->type = TREAP;
196 parsPtr->str = "Treap";
197 break;
198 case 'd':
199 parsPtr->type = RAND;
200 parsPtr->str = "Randomized";
201 break;
202 case 'm':
203 if (state->argv[state->next] == NULL)
204 argp_usage(state);
205 parsPtr->seed = strtol(state->argv[state->next], &end, 10);
206 if (*end != '\0' && *end != '\n')
207 argp_usage(state);
208 state->next++;
209 break;
210 case ARGP_KEY_ARG:
211 default: return ARGP_ERR_UNKNOWN;
212 }
213 return 0;
214}
215
216static struct argp argDefs = { options, parser_opt, argDoc, doc, 0, 0, 0 };
217
218int main(int argc, char *argv[])
219{
220 if (argc == 1)
221 {
222 cout << "testAllTree -- stress-test for Aleph-w binary search trees\n"
223 << "\n"
224 << "Inserts, searches, and removes random keys in a DynMapTree backed by\n"
225 << "the chosen tree type, then reports path length, height, and counts.\n"
226 << "\n"
227 << "Usage:\n"
228 << " " << argv[0] << " -<tree> [-n num_nodes] [-m seed]\n"
229 << "\n"
230 << "Tree type (required; last wins if repeated):\n"
231 << " -b, --bin Pure (unbalanced) binary tree\n"
232 << " -a, --avl AVL tree\n"
233 << " -s, --splay Splay tree\n"
234 << " -r, --redblack Red-black tree\n"
235 << " -p, --treap Treap (randomized BST with priorities)\n"
236 << " -d, --rand Randomized tree\n"
237 << "\n"
238 << "Options:\n"
239 << " -n num_nodes Number of keys to insert (default: 1000)\n"
240 << " -m seed Seed for the random number generator (default: current time)\n"
241 << "\n"
242 << "Examples:\n"
243 << " " << argv[0] << " -a -n 5000 # AVL tree with 5000 nodes\n"
244 << " " << argv[0] << " -p -n 10000 -m 42 # Treap, 10000 nodes, seed 42\n";
245 return 0;
246 }
247
248 Parameters pars(1000, std::time(0));
249
250 error_t status = argp_parse(&argDefs, argc, argv, 0, 0, &pars);
251
252 if (status != 0)
253 AH_ERROR( ("Internal error") );
254
255 if (pars.type == INVALID)
256 AH_ERROR( ("Invalid tree type" ) );
257
258 unsigned long n = pars.n;
259
261 gsl_rng_set(r, pars.seed % gsl_rng_max(r));
262
263 cout << "testAllTree<" << pars.str << "> " << n << " " << pars.seed
264 << endl;
265
266 try
267 {
268 switch (pars.type)
269 {
270 case BIN:
271 test<BinTree>(n, r);
272 break;
273 case AVL:
274 test<Avl_Tree>(n, r);
275 break;
276 case TREAP:
277 test<Treap>(n, r);
278 break;
279 case RAND:
280 test<Rand_Tree>(n, r);
281 break;
282 case SPLAY:
283 test<Splay_Tree>(n, r);
284 break;
285 case RB:
286 test<Rb_Tree>(n, r);
287 break;
288 case INVALID:
289 default: AH_ERROR("Invalid tree type %d", pars.type);
290 }
291
292 cout << "testAllTree<" << pars.str << "> " << n << " " << pars.seed
293 << endl;
294 }
295 catch (exception & e)
296 {
297 cout << "**** Exception: " << e.what() << endl;
298 }
299
301}
#define AH_ERROR(...)
Print an error message (always enabled).
Definition ahDefs.H:270
Core header for the Aleph-w library.
WeightedDigraph::Node Node
int main()
@ INVALID
Definition btreepic.C:181
size_t size_t int32_t value
Definition ca-c-api.h:116
Generic key-value map implemented on top of a binary search tree.
Data remove(const Key &key)
Deletes the pair key,data
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
size_t height() const
Calculates and returns the height of the binary search tree.
const size_t & size() const
Returns the cardinality of the set.
size_t internal_path_length() const
Calculates and returns the length of the internal path of the tree search binary.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4121
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
STL namespace.
TreeType type
Parameters(int _n, int _seed)
void test()
Definition test-comb.C:40
static char argDoc[]
static void printNode(Node *node, int, int)
Definition testAllTree.C:49
const char * argp_program_version
static error_t parser_opt(int key, char *, struct argp_state *state)
const char * argp_program_bug_address
static struct argp_option options[]
TreeType
@ BIN
@ RAND
@ TREAP
@ RB
@ AVL
@ SPLAY
@ INVALID
static struct argp argDefs
static char doc[]
gsl_rng * r
TreeType
AVL tree implementation (height-balanced BST).
Generic unbalanced binary search tree.
Dynamic key-value map based on balanced binary search trees.
Randomized binary search tree.
Red-Black tree implementation (bottom-up balancing).
Top-down splay tree implementation (without rank support).
Treap: randomized BST combining tree and heap properties.