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 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27# include <iostream>
28# include <aleph.H>
29# include <tpl_binTree.H>
30# include <tpl_avl.H>
31# include <tpl_treap.H>
32# include <tpl_splay_tree.H>
33# include <tpl_rb_tree.H>
34# include <tpl_rand_tree.H>
35# include <tpl_dynMapTree.H>
36# include <ran_array.h>
37# include <ctime>
38# include <gsl/gsl_rng.h>
39# include <argp.h>
40
41using namespace std;
42
43 template <class Node>
44static void printNode(Node* node, int, int)
45{
46 cout << "(" << node->get_key() << "," << node->get_data() << ")";
47}
48
49
50 template <
51 template <typename /* key */, class /* Compare */>
52 class TreeType>
54{
55 unsigned long max = 100*n;
56 size_t i;
57 int value;
59
60 for (i = 0; i < n; i++)
61 {
62 value = gsl_rng_uniform_int(r, max);
63 tree.insert(value, i);
64 }
65
66 cout << "Reading test ... " << endl;
67
68 int content;
69
70 for (i = 0; i < n; i++)
71 {
72 value = gsl_rng_uniform_int(r, max);
73 try
74 {
75 content = tree.find(value);
76 // cout << "(" << value << "," << content << ")";
77 }
78 catch (...)
79 {
80 ;//cout << i << ".";
81 }
82 }
83
84 cout << endl;
85
86 cout << "Writing test ... " << endl;
87
88 for (i = 0; i < n; i++)
89 {
90 value = gsl_rng_uniform_int(r, max);
91 auto val = tree.insert(value, i);
92 cout << "(" << val << "," << i << ")";
93 }
94
95 cout << endl;
96
97 cout << "The path length is " << tree.internal_path_length() << endl;
98
99 cout << "The height is " << tree.height() << endl;
100
101 unsigned int insCount = tree.size();
102 cout << insCount << " Items inserted" << endl;
103
104 for (i = 0; i < n; i++)
105 {
106 value = gsl_rng_uniform_int(r, max);
107 tree.remove(value);
108 }
109
110 cout << insCount - tree.size() << " Items removed" << endl;
111}
112
117
119{
120 int n;
121 int seed;
123 char *str;
124
126 : n(_n), seed(_seed), type(INVALID), str(NULL)
127 {
128 // Empty
129 }
130};
131
132
133const char *argp_program_version = "testAllTree 0.0";
134const char *argp_program_bug_address = "aleph-bugs@aleph.ula.ve";
135
136static char doc[] = "testAllTree -- A tester for all binary trees";
137static char argDoc[] = "-n num_nodes -m seed_for_random -<tree type>\n"
138;
139
140static struct argp_option options [] = {
141 { "bin", 'b', 0, OPTION_ARG_OPTIONAL, "pure binary tree" , 0},
142 { "avl", 'a', 0, OPTION_ARG_OPTIONAL, "avl tree" , 0},
143 { "splay", 's', 0, OPTION_ARG_OPTIONAL, "splay tree" , 0},
144 { "redblack", 'r', 0, OPTION_ARG_OPTIONAL, "red black tree" , 0},
145 { "rand", 'd', 0, OPTION_ARG_OPTIONAL, "randomized tree" , 0},
146 { "treap", 'p', 0, OPTION_ARG_OPTIONAL, "treap tree" , 0},
147 { "nodes", 'n', "num_nodes", OPTION_ARG_OPTIONAL,
148 "Specify the number of nodes to be generated", 0 },
149 { "seed", 'm', "seed_for_random", OPTION_ARG_OPTIONAL,
150 "Specify the seed for randon number generator", 0},
151 { 0, 0, 0, 0, 0, 0 }
152};
153
154static error_t parser_opt(int key, char *, struct argp_state *state)
155{
156 Parameters *parsPtr = static_cast<Parameters*>(state->input);
157
158 switch (key)
159 {
160 case ARGP_KEY_END:
161 if (parsPtr->type == INVALID)
162 argp_usage(state);
163 break;
164 case 'n':
165 char *end;
166 if (state->argv[state->next] == NULL)
167 argp_usage(state);
168 parsPtr->n = strtol(state->argv[state->next], &end, 10);
169 if (*end != '\0' && *end != '\n')
170 argp_usage(state);
171 state->next++;
172 break;
173 case 'b':
174 parsPtr->type = BIN;
175 parsPtr->str = "BinTree";
176 break;
177 case 'a':
178 parsPtr->type = AVL;
179 parsPtr->str = "AvlTree";
180 break;
181 case 'r':
182 parsPtr->type = RB;
183 parsPtr->str = "RbTree";
184 break;
185 case 's':
186 parsPtr->type = SPLAY;
187 parsPtr->str = "SplayTree";
188 break;
189 case 'p':
190 parsPtr->type = TREAP;
191 parsPtr->str = "Treap";
192 break;
193 case 'd':
194 parsPtr->type = RAND;
195 parsPtr->str = "Randomized";
196 break;
197 case 'm':
198 if (state->argv[state->next] == NULL)
199 argp_usage(state);
200 parsPtr->seed = strtol(state->argv[state->next], &end, 10);
201 if (*end != '\0' && *end != '\n')
202 argp_usage(state);
203 state->next++;
204 break;
205 case ARGP_KEY_ARG:
206 default: return ARGP_ERR_UNKNOWN;
207 }
208 return 0;
209}
210
211static struct argp argDefs = { options, parser_opt, argDoc, doc, 0, 0, 0 };
212
213int main(int argc, char *argv[])
214{
215 if (argc == 1)
216 {
217 cout << "testAllTree -- stress-test for Aleph-w binary search trees\n"
218 << "\n"
219 << "Inserts, searches, and removes random keys in a DynMapTree backed by\n"
220 << "the chosen tree type, then reports path length, height, and counts.\n"
221 << "\n"
222 << "Usage:\n"
223 << " " << argv[0] << " -<tree> [-n num_nodes] [-m seed]\n"
224 << "\n"
225 << "Tree type (required; last wins if repeated):\n"
226 << " -b, --bin Pure (unbalanced) binary tree\n"
227 << " -a, --avl AVL tree\n"
228 << " -s, --splay Splay tree\n"
229 << " -r, --redblack Red-black tree\n"
230 << " -p, --treap Treap (randomized BST with priorities)\n"
231 << " -d, --rand Randomized tree\n"
232 << "\n"
233 << "Options:\n"
234 << " -n num_nodes Number of keys to insert (default: 1000)\n"
235 << " -m seed Seed for the random number generator (default: current time)\n"
236 << "\n"
237 << "Examples:\n"
238 << " " << argv[0] << " -a -n 5000 # AVL tree with 5000 nodes\n"
239 << " " << argv[0] << " -p -n 10000 -m 42 # Treap, 10000 nodes, seed 42\n";
240 return 0;
241 }
242
243 Parameters pars(1000, std::time(0));
244
245 error_t status = argp_parse(&argDefs, argc, argv, 0, 0, &pars);
246
247 if (status != 0)
248 AH_ERROR( ("Internal error") );
249
250 if (pars.type == INVALID)
251 AH_ERROR( ("Invalid tree type" ) );
252
253 unsigned long n = pars.n;
254
256 gsl_rng_set(r, pars.seed % gsl_rng_max(r));
257
258 cout << "testAllTree<" << pars.str << "> " << n << " " << pars.seed
259 << endl;
260
261 try
262 {
263 switch (pars.type)
264 {
265 case BIN:
266 test<BinTree>(n, r);
267 break;
268 case AVL:
269 test<Avl_Tree>(n, r);
270 break;
271 case TREAP:
272 test<Treap>(n, r);
273 break;
274 case RAND:
275 test<Rand_Tree>(n, r);
276 break;
277 case SPLAY:
278 test<Splay_Tree>(n, r);
279 break;
280 case RB:
281 test<Rb_Tree>(n, r);
282 break;
283 case INVALID:
284 default: AH_ERROR("Invalid tree type %d", pars.type);
285 }
286
287 cout << "testAllTree<" << pars.str << "> " << n << " " << pars.seed
288 << endl;
289 }
290 catch (exception & e)
291 {
292 cout << "**** Exception: " << e.what() << endl;
293 }
294
296}
#define AH_ERROR(format, args...)
Print an error message (always enabled).
Definition ahDefs.H:271
Core header for the Aleph-w library.
WeightedDigraph::Node Node
@ INVALID
Definition btreepic.C:181
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:4110
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
TreeType type
Parameters(int _n, int _seed)
void test()
Definition test-comb.C:35
static char argDoc[]
static void printNode(Node *node, int, int)
Definition testAllTree.C:44
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.