Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeAllTrees.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
33# include <cstdlib>
34# include <ctime>
35# include <iostream>
36# include <fstream>
37# include <string>
38# include <aleph.H>
39# include <tpl_splay_tree.H>
40# include <tpl_avl.H>
41# include <tpl_binTree.H>
42# include <tpl_rb_tree.H>
43# include <tpl_rand_tree.H>
44# include <tpl_treap.H>
45# include <tpl_binNodeUtils.H>
46
47using namespace Aleph;
48using namespace std;
49
50
51ofstream output_splay("splay.Tree", ios::out);
52
54{
55 output_splay << p->get_key() << " ";
56}
57
58
59ofstream output_bin("bin.Tree", ios::out);
60
61void print_bin(BinTree<int>::Node * p, int, int)
62{
63 output_bin << p->get_key() << " ";
64}
65
66
67ofstream output_rb("rb.Tree", ios::out);
68
69void print_rb(Rb_Tree<int>::Node * p, int, int)
70{
71 output_rb << p->get_key() << " ";
72}
73
74
75ofstream output_avl("avl.Tree", ios::out);
76
77void print_avl(Avl_Tree<int>::Node * p, int, int)
78{
79 output_avl << p->get_key() << " ";
80}
81
82
83ofstream output_rand("rand.Tree", ios::out);
84
86{
87 output_rand << p->get_key() << " ";
88}
89
90
91ofstream output_treap("treap.Tree", ios::out);
92
93void print_treap(Treap<int>::Node * p, int, int)
94{
95 output_treap << p->get_key() << " ";
96}
97
98
99
100int main(int argc, char *argv[])
101{
102 int n = 512;
103 unsigned int t = std::time(0);
104 int value;
105
106 if (argc > 1)
107 {
108 try
109 {
110 n = stoi(argv[1]);
111 }
112 catch (...)
113 {
114 n = 512;
115 }
116 }
117
118 if (n <= 0)
119 {
120 cerr << "Error: n must be a positive integer." << endl;
121 return 1;
122 }
123
124 if (argc > 2)
125 {
126 try
127 {
128 t = stoi(argv[2]);
129 }
130 catch (...)
131 {
132 t = std::time(0);
133 }
134 }
135
136 srand(t);
137
138 cout << "writeAllTrees " << n << " " << t << endl;
139
140 {
141 srand(t);
142 Splay_Tree<int> tree;
144 int i;
145
146 cout << "Inserting " << n << " random values in treee ...\n";
147
148 for (i = 0; i < n; i++)
149 {
150 while (true)
151 {
152 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
153 node = tree.search(value);
154 if (node == NULL)
155 break;
156 }
157 node = new Splay_Tree<int>::Node (value);
158 tree.insert(node);
159 }
160
162
163 destroyRec(tree.getRoot());
164 }
165
166 {
167 srand(t);
168 Rand_Tree<int> tree;
170 int i;
171
172 cout << "Inserting " << n << " random values in treee ...\n";
173
174 for (i = 0; i < n; i++)
175 {
176 while (true)
177 {
178 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
179 node = tree.search(value);
180 if (node == NULL)
181 break;
182 }
183 node = new Rand_Tree<int>::Node (value);
184 tree.insert(node);
185 }
186
188
189 destroyRec(tree.getRoot());
190 }
191
192 {
193 srand(t);
194 Rb_Tree<int> tree;
195 Rb_Tree<int>::Node *node;
196 int i;
197
198 cout << "Inserting " << n << " random values in treee ...\n";
199
200 for (i = 0; i < n; i++)
201 {
202 while (true)
203 {
204 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
205 node = tree.search(value);
206 if (node == NULL)
207 break;
208 }
209 node = new Rb_Tree<int>::Node (value);
210 tree.insert(node);
211 }
212
214
215 destroyRec(tree.getRoot());
216 }
217
218 {
219 srand(t);
220 BinTree<int> tree;
221 BinTree<int>::Node *node;
222 int i;
223
224 cout << "Inserting " << n << " random values in treee ...\n";
225
226 for (i = 0; i < n; i++)
227 {
228 while (true)
229 {
230 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
231 node = tree.search(value);
232 if (node == NULL)
233 break;
234 }
235 node = new BinTree<int>::Node (value);
236 tree.insert(node);
237 }
238
240
241 destroyRec(tree.getRoot());
242 }
243
244 {
245 srand(t);
246 Avl_Tree<int> tree;
248 int i;
249
250 cout << "Inserting " << n << " random values in treee ...\n";
251
252 for (i = 0; i < n; i++)
253 {
254 while (true)
255 {
256 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
257 node = tree.search(value);
258 if (node == NULL)
259 break;
260 }
261 node = new Avl_Tree<int>::Node (value);
262 tree.insert(node);
263 }
264
266
267 destroyRec(tree.getRoot());
268 }
269
270 {
271 srand(t);
272 Treap<int> tree;
273 Treap<int>::Node *node;
274 int i;
275
276 cout << "Inserting " << n << " random values in treee ...\n";
277
278 for (i = 0; i < n; i++)
279 {
280 while (true)
281 {
282 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
283 node = tree.search(value);
284 if (node == NULL)
285 break;
286 }
287 node = new Treap<int>::Node (value);
288 tree.insert(node);
289 }
290
292
293 destroyRec(tree.getRoot());
294 }
295}
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Node *& getRoot() noexcept
Return the root of tree.
Node * insert(Node *p) noexcept
Insert a node in the tree.
Node * search(const Key &key) const noexcept
Search a key.
Node * search(const Key &key) const noexcept
Search a node containing key; if found, then a pointer to the node containing it is returned; otherwi...
Definition tpl_avl.H:541
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Definition tpl_avl.H:528
Node * insert(Node *p) noexcept
Insert the node pointed by p in the tree.
Definition tpl_avl.H:569
Node * insert(Node *p) noexcept
Insert a node in the tree.
Node *& getRoot() noexcept
Return the tree's root.
Node * search(const Key &key) const noexcept
Search a key.
Node * insert(Node *p) noexcept
Insert a node into the tree.
Node * search(const Key &key) const noexcept
Search for a key in the tree.
Node *& getRoot() noexcept
Get reference to root pointer.
Node * search(const Key &key) const noexcept
Search a key in a treap.
Definition tpl_treap.H:222
Node *& getRoot() noexcept
Return the tree's root.
Definition tpl_treap.H:214
Node * insert(Node *root, Node *p) noexcept
Definition tpl_treap.H:229
NodeType< Key > Node
Node *& getRoot() noexcept
Get the top-down splay tree's root.
Node * insert(Node *p) noexcept
Inserts a node in a top-down splay tree.
Node * search(const Key &key) noexcept
Searches a key in a top-down splay tree.
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
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
STL namespace.
AVL binary search tree with nodes without a virtual destructor.
Definition tpl_avl.H:743
Binary search tree with nodes without virtual destructors,.
Randomized binary search tree.
Red-black tree with nodes without virtual destructor.
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:614
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.
Generic unbalanced binary search tree.
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.
void print_avl(Avl_Tree< int >::Node *p, int, int)
ofstream output_treap("treap.Tree", ios::out)
ofstream output_bin("bin.Tree", ios::out)
void print_rb(Rb_Tree< int >::Node *p, int, int)
void print_treap(Treap< int >::Node *p, int, int)
void print_splay(Splay_Tree< int >::Node *p, int, int)
void print_bin(BinTree< int >::Node *p, int, int)
ofstream output_rand("rand.Tree", ios::out)
ofstream output_splay("splay.Tree", ios::out)
void print_rand(Rand_Tree< int >::Node *p, int, int)
ofstream output_rb("rb.Tree", ios::out)
ofstream output_avl("avl.Tree", ios::out)