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 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
28# include <cstdlib>
29# include <ctime>
30# include <iostream>
31# include <fstream>
32# include <string>
33# include <aleph.H>
34# include <tpl_splay_tree.H>
35# include <tpl_avl.H>
36# include <tpl_binTree.H>
37# include <tpl_rb_tree.H>
38# include <tpl_rand_tree.H>
39# include <tpl_treap.H>
40# include <tpl_binNodeUtils.H>
41
42using namespace Aleph;
43using namespace std;
44
45
46ofstream output_splay("splay.Tree", ios::out);
47
49{
50 output_splay << p->get_key() << " ";
51}
52
53
54ofstream output_bin("bin.Tree", ios::out);
55
56void print_bin(BinTree<int>::Node * p, int, int)
57{
58 output_bin << p->get_key() << " ";
59}
60
61
62ofstream output_rb("rb.Tree", ios::out);
63
64void print_rb(Rb_Tree<int>::Node * p, int, int)
65{
66 output_rb << p->get_key() << " ";
67}
68
69
70ofstream output_avl("avl.Tree", ios::out);
71
72void print_avl(Avl_Tree<int>::Node * p, int, int)
73{
74 output_avl << p->get_key() << " ";
75}
76
77
78ofstream output_rand("rand.Tree", ios::out);
79
81{
82 output_rand << p->get_key() << " ";
83}
84
85
86ofstream output_treap("treap.Tree", ios::out);
87
88void print_treap(Treap<int>::Node * p, int, int)
89{
90 output_treap << p->get_key() << " ";
91}
92
93
94
95int main(int argc, char *argv[])
96{
97 int n = 512;
98 unsigned int t = std::time(0);
99 int value;
100
101 if (argc > 1)
102 {
103 try
104 {
105 n = stoi(argv[1]);
106 }
107 catch (...)
108 {
109 n = 512;
110 }
111 }
112
113 if (n <= 0)
114 {
115 cerr << "Error: n must be a positive integer." << endl;
116 return 1;
117 }
118
119 if (argc > 2)
120 {
121 try
122 {
123 t = stoi(argv[2]);
124 }
125 catch (...)
126 {
127 t = std::time(0);
128 }
129 }
130
131 srand(t);
132
133 cout << "writeAllTrees " << n << " " << t << endl;
134
135 {
136 srand(t);
137 Splay_Tree<int> tree;
139 int i;
140
141 cout << "Inserting " << n << " random values in treee ...\n";
142
143 for (i = 0; i < n; i++)
144 {
145 while (true)
146 {
147 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
148 node = tree.search(value);
149 if (node == NULL)
150 break;
151 }
152 node = new Splay_Tree<int>::Node (value);
153 tree.insert(node);
154 }
155
157
158 destroyRec(tree.getRoot());
159 }
160
161 {
162 srand(t);
163 Rand_Tree<int> tree;
165 int i;
166
167 cout << "Inserting " << n << " random values in treee ...\n";
168
169 for (i = 0; i < n; i++)
170 {
171 while (true)
172 {
173 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
174 node = tree.search(value);
175 if (node == NULL)
176 break;
177 }
178 node = new Rand_Tree<int>::Node (value);
179 tree.insert(node);
180 }
181
183
184 destroyRec(tree.getRoot());
185 }
186
187 {
188 srand(t);
189 Rb_Tree<int> tree;
190 Rb_Tree<int>::Node *node;
191 int i;
192
193 cout << "Inserting " << n << " random values in treee ...\n";
194
195 for (i = 0; i < n; i++)
196 {
197 while (true)
198 {
199 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
200 node = tree.search(value);
201 if (node == NULL)
202 break;
203 }
204 node = new Rb_Tree<int>::Node (value);
205 tree.insert(node);
206 }
207
209
210 destroyRec(tree.getRoot());
211 }
212
213 {
214 srand(t);
215 BinTree<int> tree;
216 BinTree<int>::Node *node;
217 int i;
218
219 cout << "Inserting " << n << " random values in treee ...\n";
220
221 for (i = 0; i < n; i++)
222 {
223 while (true)
224 {
225 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
226 node = tree.search(value);
227 if (node == NULL)
228 break;
229 }
230 node = new BinTree<int>::Node (value);
231 tree.insert(node);
232 }
233
235
236 destroyRec(tree.getRoot());
237 }
238
239 {
240 srand(t);
241 Avl_Tree<int> tree;
243 int i;
244
245 cout << "Inserting " << n << " random values in treee ...\n";
246
247 for (i = 0; i < n; i++)
248 {
249 while (true)
250 {
251 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
252 node = tree.search(value);
253 if (node == NULL)
254 break;
255 }
256 node = new Avl_Tree<int>::Node (value);
257 tree.insert(node);
258 }
259
261
262 destroyRec(tree.getRoot());
263 }
264
265 {
266 srand(t);
267 Treap<int> tree;
268 Treap<int>::Node *node;
269 int i;
270
271 cout << "Inserting " << n << " random values in treee ...\n";
272
273 for (i = 0; i < n; i++)
274 {
275 while (true)
276 {
277 value = 1+(int) (n*1.0*rand()/(RAND_MAX+1.0));
278 node = tree.search(value);
279 if (node == NULL)
280 break;
281 }
282 node = new Treap<int>::Node (value);
283 tree.insert(node);
284 }
285
287
288 destroyRec(tree.getRoot());
289 }
290}
Core header for the Aleph-w library.
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:533
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Definition tpl_avl.H:526
Node * insert(Node *p) noexcept
Insert the node pointed by p in the tree.
Definition tpl_avl.H:561
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.
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
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.
AVL binary search tree with nodes without virtual destructor.
Definition tpl_avl.H:737
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)