Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testBinNodeUtils.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 <cstdlib>
33# include <ctime>
34# include <iostream>
35# include <string>
36# include <tpl_dynArray.H>
37# include <tpl_binTree.H>
38# include <tpl_binNodeUtils.H>
39
40
41# include <cassert>
42using namespace std;
43# include <cassert>
44using namespace Aleph;
45
46
47static void printNode(BinTree<int>::Node* node, int, int)
48{
49 cout << node->get_key() << " ";
50}
51
52static void print_node(BinTree<int>::Node* node, int, int)
53{
54 cout << node->get_key() << " ";
55}
56
57static void print_node(BinTree<int>::Node* node, int, bool)
58{
59 cout << node->get_key() << " ";
60}
61
62
64
65static void fill_preorder(BinTree<int>::Node* node, int, int pos)
66{
67 preorder[pos] = node->get_key();
68}
69
70
72
73static void fill_inorder(BinTree<int>::Node* node, int, int pos)
74{
75 inorder[pos] = node->get_key();
76}
77
78
80
81static void fill_postorder(BinTree<int>::Node* node, int, int pos)
82{
83 postorder[pos] = node->get_key();
84}
85
86
87struct Clave
88{
90 {
91 return p->get_key();
92 }
93};
94
95
97{
98 void operator () (BinNode<int> * p, istream & input) const
99 {
100 input >> p->get_key();
101 }
102};
103
104
105int main(int argc, char *argv[])
106{
107 int n = 1000;
108 unsigned int t = std::time(0);
109 int value;
110
111 try
112 {
113 if (argc > 1)
114 n = std::stoi(argv[1]);
115
116 if (argc > 2)
117 t = std::stoi(argv[2]);
118 }
119 catch (...)
120 {
121 // ignore
122 }
123
124 if (n <= 0)
125 {
126 cout << "n must be positive" << endl;
127 return 1;
128 }
129
130 srand(t);
131
132 cout << "testBinNodeUtils " << n << " " << t << endl;
133
134 BinTree<int> tree;
135 BinTree<int>::Node *node;
136 int i;
137
138 int insCount = 0, delCount = 0;
139
140 for (i = 0; i < n; i++)
141 {
142 value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
143 node = tree.search(value);
144 if (node == NULL)
145 {
146 cout << value << " ";
147 insCount++;
148 node = new BinTree<int>::Node (value);
149 tree.insert(node);
150 }
151 }
152
153 cout << endl << insCount << " Items inserted" << endl;
154
155 /* Fills the prefix array */
156 cout << "Prefix :";
159 cout << endl;
160
163
165
167
168 /* Fills the infix array */
170 cout << "Infix: ";
171 int n_inorder = inOrderRec(tree.getRoot(), print_node);
172 cout << endl;
173
174 /* Fills the suffix array */
175 cout << "Suffix: ";
178 cout << endl;
179
183
184 try /* Copy binary tree */
185 {
186 BinNode<int> * aux = copyRec(tree.getRoot());
187
188 assert( areEquivalents(tree.getRoot(), aux) );
189
190 destroyRec(aux);
191 }
192 catch (exception & e) { cout << e.what() << endl; }
193 catch (...) { cout << "Unknown exception" << endl; }
194
195 cout << "Level traverse" << endl;
196 level_traverse(tree.getRoot(), [] (BinNode<int> * p)
197 {
198 cout << p->get_key() << " ";
199 return true;
200 });
201 cout << endl << endl;
202
203 /* build a tree from prefix and infix traverses */
205 (preorder, 0, n_preorder - 1, inorder, 0, n_inorder - 1);
206
208
210 (postorder, 0, n_postorder - 1, inorder, 0, n_inorder - 1);
211
213
214 /* Randomly remove items */
215 for (i = 0; i < n; i++)
216 {
217 value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
218 node = tree.remove(value);
219 if (node not_eq NULL)
220 {
221 delCount++;
222 delete node;
223 }
224 }
225
226 cout << delCount << " Items removed" << endl;
227
228 /* Free memory of the original tree */
229 try { destroyRec(tree.getRoot()); }
230 catch (exception & e) { cout << e.what() << endl; }
231 catch (...) { cout << "Unknown exception" << endl; }
232
234
235 /* Search for a number not in the tree for partitioning */
236 while (true)
237 {
238 value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
240 break;
241 }
242
243 cout << "Tree ";
245 cout << endl
246 << "will be recursively partitioned according to key " << value << endl;
247
248 /* recursively partition */
250
252
254 cout << "|" << value << "| ";
256 cout << endl << endl;
257
258 /* build a tree from prefix and infix traverses */
260 (preorder, 0, n_preorder - 1, inorder, 0, n_inorder - 1);
261
262 cout << "Tree ";
264 cout << endl
265 << "will be iteratively partitioned according to key " << value << endl;
266
267 /* recursively partition */
269
271 cout << "|" << value << "| ";
273 cout << endl << endl;
274
276 AH_ERROR("Left sides of partitions are not equal");
277
279 AH_ERROR("Right sides of partitions are not equal");
280
281 cout <<
282 "Recursive partition result is identical to iterative partition"
283 << endl;
284
285 if (t1_rec not_eq NULL)
287 if (t2_rec not_eq NULL)
289
290 if (t1_it not_eq NULL)
292 if (t2_it not_eq NULL)
294
295 {
297 DynArray<int> values(n);
298 cout << "Recursive insertion of " << n << " nodes at root ..." << endl;
299
300 for (i = 0; i < n; i++)
301 {
302 values[i] = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
303 node = searchInBinTree(t_rot, values[i]);
304 if (node == NULL)
305 {
306 cout << values[i] << " ";
307 node = new BinNode<int> (values[i]);
308 t_rot = insert_root(t_rot, node);
309 }
310 }
311
313
314 cout << endl << "Finished" << endl;
315
317 cout << "Iterative insertion of " << n << " nodes at root ..." << endl;
318
319 for (i = 0; i < n; i++)
320 {
321 node = searchInBinTree(t_it, values[i]);
322 if (node == NULL)
323 {
324 cout << values[i] << " ";
325 node = new BinNode<int> (values[i]);
326 t_it = insert_root(t_it, node);
327 }
328 }
329
331
332 cout << endl << "Finished" << endl;
333
334 cout << "Comparing recursive result with iterative ... " << endl;
336 cout << "Resulting trees are equal" << endl;
337 else
338 cout << "Resulting trees are different" << endl;
339
340 {
341 ofstream out("bintree.tree");
343 }
344
345 ifstream input("bintree.tree");
347
348 cout << "Comparing loaded tree with iterative ... " << endl;
350 cout << "Resulting trees are equal" << endl;
351 else
352 cout << "Resulting trees are different" << endl;
353
356 }
357}
#define AH_ERROR(...)
Print an error message (always enabled).
Definition ahDefs.H:270
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
Node for binary search tree.
Key & get_key() noexcept
size_t size() const noexcept
Return the current dimension of array.
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 * remove(const Key &key) noexcept
Remove a key from the 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 postOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in postorder a binary tree.
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
bool level_traverse(Node *root, Operation &operation)
Level traverse a tree and execute an operation.
bool split_key_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept
Split recursively according to a key.
Node * copyRec(Node *root)
Copy recursively a tree.
void save_tree(Node *root, std::ostream &output)
Store a binary tree in a stream.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively inorder a binary tree.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Search a key in a binary search tree.
Node * insert_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert the node p as root of a binary search tree.
void split_key(Node *&root, const Key &key, Node *&l, Node *&r, const Compare &cmp=Compare()) noexcept
Split a binary search tree according to a key.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool areEquivalents(Node *t1, Node *t2, Equal &op) noexcept
Return true if trees are equivalents.
STL namespace.
Binary search tree with nodes without virtual destructors,.
void operator()(BinNode< int > *p, istream &input) const
Key structure for DFS tree nodes.
int operator()(BinNode< int > *p) const
static void fill_postorder(BinTree< int >::Node *node, int, int pos)
DynArray< int > preorder
static void fill_preorder(BinTree< int >::Node *node, int, int pos)
DynArray< int > postorder
static void fill_inorder(BinTree< int >::Node *node, int, int pos)
DynArray< int > inorder
static void printNode(BinTree< int >::Node *node, int, int)
static void print_node(BinTree< int >::Node *node, int, int)
Utility functions for binary tree operations.
Generic unbalanced binary search tree.
Lazy and scalable dynamic array implementation.