Aleph-w 7.10.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 <ctime>
42# include <ah-random.H>
43# include <argp.h>
44
45using namespace std;
46
47 template <class Node>
48static void printNode(Node* node, int, int)
49{
50 cout << "(" << node->get_key() << "," << node->get_data() << ")";
51}
52
53
54 template <
55 template <typename /* key */, class /* Compare */>
56 class TreeType>
57void test(unsigned long n, Gsl_Compatible_Rng & r)
58{
59 unsigned long max = 100*n;
60 size_t i;
61 int value;
63
64 for (i = 0; i < n; i++)
65 {
67 tree.insert(value, i);
68 }
69
70 cout << "Reading test ... " << endl;
71
72 int content;
73
74 for (i = 0; i < n; i++)
75 {
77 try
78 {
79 content = tree.find(value);
80 // cout << "(" << value << "," << content << ")";
81 }
82 catch (...)
83 {
84 ;//cout << i << ".";
85 }
86 }
87
88 cout << endl;
89
90 cout << "Writing test ... " << endl;
91
92 for (i = 0; i < n; i++)
93 {
95 auto val = tree.insert(value, i);
96 cout << "(" << val << "," << i << ")";
97 }
98
99 cout << endl;
100
101 cout << "The path length is " << tree.internal_path_length() << endl;
102
103 cout << "The height is " << tree.height() << endl;
104
105 unsigned int insCount = tree.size();
106 cout << insCount << " Items inserted" << endl;
107
108 for (i = 0; i < n; i++)
109 {
111 tree.remove(value);
112 }
113
114 cout << insCount - tree.size() << " Items removed" << endl;
115}
116
121
123{
124 int n;
125 int seed;
127 char *str;
128
129 Parameters(int _n, int _seed)
130 : n(_n), seed(_seed), type(INVALID), str(NULL)
131 {
132 // Empty
133 }
134};
135
136
137const char *argp_program_version = "testAllTree 0.0";
138const char *argp_program_bug_address = "aleph-bugs@aleph.ula.ve";
139
140static char doc[] = "testAllTree -- A tester for all binary trees";
141static char argDoc[] = "-n num_nodes -m seed_for_random -<tree type>\n"
142;
143
144static struct argp_option options [] = {
145 { "bin", 'b', 0, OPTION_ARG_OPTIONAL, "pure binary tree" , 0},
146 { "avl", 'a', 0, OPTION_ARG_OPTIONAL, "avl tree" , 0},
147 { "splay", 's', 0, OPTION_ARG_OPTIONAL, "splay tree" , 0},
148 { "redblack", 'r', 0, OPTION_ARG_OPTIONAL, "red black tree" , 0},
149 { "rand", 'd', 0, OPTION_ARG_OPTIONAL, "randomized tree" , 0},
150 { "treap", 'p', 0, OPTION_ARG_OPTIONAL, "treap tree" , 0},
151 { "nodes", 'n', "num_nodes", OPTION_ARG_OPTIONAL,
152 "Specify the number of nodes to be generated", 0 },
153 { "seed", 'm', "seed_for_random", OPTION_ARG_OPTIONAL,
154 "Specify the seed for randon number generator", 0},
155 { 0, 0, 0, 0, 0, 0 }
156};
157
158static error_t parser_opt(int key, char *, struct argp_state *state)
159{
160 Parameters *parsPtr = static_cast<Parameters*>(state->input);
161
162 switch (key)
163 {
164 case ARGP_KEY_END:
165 if (parsPtr->type == INVALID)
166 argp_usage(state);
167 break;
168 case 'n':
169 char *end;
170 if (state->argv[state->next] == NULL)
171 argp_usage(state);
172 parsPtr->n = strtol(state->argv[state->next], &end, 10);
173 if (*end != '\0' && *end != '\n')
174 argp_usage(state);
175 state->next++;
176 break;
177 case 'b':
178 parsPtr->type = BIN;
179 parsPtr->str = "BinTree";
180 break;
181 case 'a':
182 parsPtr->type = AVL;
183 parsPtr->str = "AvlTree";
184 break;
185 case 'r':
186 parsPtr->type = RB;
187 parsPtr->str = "RbTree";
188 break;
189 case 's':
190 parsPtr->type = SPLAY;
191 parsPtr->str = "SplayTree";
192 break;
193 case 'p':
194 parsPtr->type = TREAP;
195 parsPtr->str = "Treap";
196 break;
197 case 'd':
198 parsPtr->type = RAND;
199 parsPtr->str = "Randomized";
200 break;
201 case 'm':
202 if (state->argv[state->next] == NULL)
203 argp_usage(state);
204 parsPtr->seed = strtol(state->argv[state->next], &end, 10);
205 if (*end != '\0' && *end != '\n')
206 argp_usage(state);
207 state->next++;
208 break;
209 case ARGP_KEY_ARG:
210 default: return ARGP_ERR_UNKNOWN;
211 }
212 return 0;
213}
214
215static struct argp argDefs = { options, parser_opt, argDoc, doc, 0, 0, 0 };
216
217int main(int argc, char *argv[])
218{
219 if (argc == 1)
220 {
221 cout << "testAllTree -- stress-test for Aleph-w binary search trees\n"
222 << "\n"
223 << "Inserts, searches, and removes random keys in a DynMapTree backed by\n"
224 << "the chosen tree type, then reports path length, height, and counts.\n"
225 << "\n"
226 << "Usage:\n"
227 << " " << argv[0] << " -<tree> [-n num_nodes] [-m seed]\n"
228 << "\n"
229 << "Tree type (required; last wins if repeated):\n"
230 << " -b, --bin Pure (unbalanced) binary tree\n"
231 << " -a, --avl AVL tree\n"
232 << " -s, --splay Splay tree\n"
233 << " -r, --redblack Red-black tree\n"
234 << " -p, --treap Treap (randomized BST with priorities)\n"
235 << " -d, --rand Randomized tree\n"
236 << "\n"
237 << "Options:\n"
238 << " -n num_nodes Number of keys to insert (default: 1000)\n"
239 << " -m seed Seed for the random number generator (default: current time)\n"
240 << "\n"
241 << "Examples:\n"
242 << " " << argv[0] << " -a -n 5000 # AVL tree with 5000 nodes\n"
243 << " " << argv[0] << " -p -n 10000 -m 42 # Treap, 10000 nodes, seed 42\n";
244 return 0;
245 }
246
247 Parameters pars(1000, std::time(0));
248
249 error_t status = argp_parse(&argDefs, argc, argv, 0, 0, &pars);
250
251 if (status != 0)
252 AH_ERROR( ("Internal error") );
253
254 if (pars.type == INVALID)
255 AH_ERROR( ("Invalid tree type" ) );
256
257 unsigned long n = pars.n;
258
260
261 cout << "testAllTree<" << pars.str << "> " << n << " " << pars.seed
262 << endl;
263
264 try
265 {
266 switch (pars.type)
267 {
268 case BIN:
269 test<BinTree>(n, r);
270 break;
271 case AVL:
272 test<Avl_Tree>(n, r);
273 break;
274 case TREAP:
275 test<Treap>(n, r);
276 break;
277 case RAND:
278 test<Rand_Tree>(n, r);
279 break;
280 case SPLAY:
281 test<Splay_Tree>(n, r);
282 break;
283 case RB:
284 test<Rb_Tree>(n, r);
285 break;
286 case INVALID:
287 default: AH_ERROR("Invalid tree type %d", pars.type);
288 }
289
290 cout << "testAllTree<" << pars.str << "> " << n << " " << pars.seed
291 << endl;
292 }
293 catch (exception & e)
294 {
295 cout << "**** Exception: " << e.what() << endl;
296 }
297
298}
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
#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.
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
Definition ah-random.H:302
__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:4188
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:48
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_Compatible_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.