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 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 <cstdlib>
28# include <ctime>
29# include <iostream>
30# include <string>
31# include <tpl_dynArray.H>
32# include <tpl_binTree.H>
33# include <tpl_binNodeUtils.H>
34
35
36# include <cassert>
37using namespace std;
38# include <cassert>
39using namespace Aleph;
40
41
42static void printNode(BinTree<int>::Node* node, int, int)
43{
44 cout << node->get_key() << " ";
45}
46
47static void print_node(BinTree<int>::Node* node, int, int)
48{
49 cout << node->get_key() << " ";
50}
51
52static void print_node(BinTree<int>::Node* node, int, bool)
53{
54 cout << node->get_key() << " ";
55}
56
57
59
60static void fill_preorder(BinTree<int>::Node* node, int, int pos)
61{
62 preorder[pos] = node->get_key();
63}
64
65
67
68static void fill_inorder(BinTree<int>::Node* node, int, int pos)
69{
70 inorder[pos] = node->get_key();
71}
72
73
75
76static void fill_postorder(BinTree<int>::Node* node, int, int pos)
77{
78 postorder[pos] = node->get_key();
79}
80
81
82struct Clave
83{
85 {
86 return p->get_key();
87 }
88};
89
90
92{
93 void operator () (BinNode<int> * p, istream & input) const
94 {
95 input >> p->get_key();
96 }
97};
98
99
100int main(int argc, char *argv[])
101{
102 int n = 1000;
103 unsigned int t = std::time(0);
104 int value;
105
106 try
107 {
108 if (argc > 1)
109 n = std::stoi(argv[1]);
110
111 if (argc > 2)
112 t = std::stoi(argv[2]);
113 }
114 catch (...)
115 {
116 // ignore
117 }
118
119 if (n <= 0)
120 {
121 cout << "n must be positive" << endl;
122 return 1;
123 }
124
125 srand(t);
126
127 cout << "testBinNodeUtils " << n << " " << t << endl;
128
129 BinTree<int> tree;
130 BinTree<int>::Node *node;
131 int i;
132
133 int insCount = 0, delCount = 0;
134
135 for (i = 0; i < n; i++)
136 {
137 value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
138 node = tree.search(value);
139 if (node == NULL)
140 {
141 cout << value << " ";
142 insCount++;
143 node = new BinTree<int>::Node (value);
144 tree.insert(node);
145 }
146 }
147
148 cout << endl << insCount << " Items inserted" << endl;
149
150 /* Fills the prefix array */
151 cout << "Prefix :";
154 cout << endl;
155
158
160
162
163 /* Fills the infix array */
165 cout << "Infix: ";
166 int n_inorder = inOrderRec(tree.getRoot(), print_node);
167 cout << endl;
168
169 /* Fills the suffix array */
170 cout << "Suffix: ";
173 cout << endl;
174
178
179 try /* Copy binary tree */
180 {
181 BinNode<int> * aux = copyRec(tree.getRoot());
182
183 assert( areEquivalents(tree.getRoot(), aux) );
184
185 destroyRec(aux);
186 }
187 catch (exception & e) { cout << e.what() << endl; }
188 catch (...) { cout << "Unknown exception" << endl; }
189
190 cout << "Level traverse" << endl;
191 level_traverse(tree.getRoot(), [] (BinNode<int> * p)
192 {
193 cout << p->get_key() << " ";
194 return true;
195 });
196 cout << endl << endl;
197
198 /* build a tree from prefix and infix traverses */
200 (preorder, 0, n_preorder - 1, inorder, 0, n_inorder - 1);
201
203
205 (postorder, 0, n_postorder - 1, inorder, 0, n_inorder - 1);
206
208
209 /* Randomly remove items */
210 for (i = 0; i < n; i++)
211 {
212 value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
213 node = tree.remove(value);
214 if (node not_eq NULL)
215 {
216 delCount++;
217 delete node;
218 }
219 }
220
221 cout << delCount << " Items removed" << endl;
222
223 /* Free memory of the original tree */
224 try { destroyRec(tree.getRoot()); }
225 catch (exception & e) { cout << e.what() << endl; }
226 catch (...) { cout << "Unknown exception" << endl; }
227
229
230 /* Search for a number not in the tree for partitioning */
231 while (true)
232 {
233 value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
234 if (searchInBinTree(new_root, value) == NULL)
235 break;
236 }
237
238 cout << "Tree ";
240 cout << endl
241 << "will be recursively partitioned according to key " << value << endl;
242
243 /* recursively partition */
245
247
249 cout << "|" << value << "| ";
251 cout << endl << endl;
252
253 /* build a tree from prefix and infix traverses */
255 (preorder, 0, n_preorder - 1, inorder, 0, n_inorder - 1);
256
257 cout << "Tree ";
259 cout << endl
260 << "will be iteratively partitioned according to key " << value << endl;
261
262 /* recursively partition */
263 split_key(new_root, value, t1_it, t2_it);
264
266 cout << "|" << value << "| ";
268 cout << endl << endl;
269
271 AH_ERROR("Left sides of partitions are not equal");
272
274 AH_ERROR("Right sides of partitions are not equal");
275
276 cout <<
277 "Recursive partition result is identical to iterative partition"
278 << endl;
279
280 if (t1_rec not_eq NULL)
282 if (t2_rec not_eq NULL)
284
285 if (t1_it not_eq NULL)
287 if (t2_it not_eq NULL)
289
290 {
292 DynArray<int> values(n);
293 cout << "Recursive insertion of " << n << " nodes at root ..." << endl;
294
295 for (i = 0; i < n; i++)
296 {
297 values[i] = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
298 node = searchInBinTree(t_rot, values[i]);
299 if (node == NULL)
300 {
301 cout << values[i] << " ";
302 node = new BinNode<int> (values[i]);
303 t_rot = insert_root(t_rot, node);
304 }
305 }
306
308
309 cout << endl << "Finished" << endl;
310
312 cout << "Iterative insertion of " << n << " nodes at root ..." << endl;
313
314 for (i = 0; i < n; i++)
315 {
316 node = searchInBinTree(t_it, values[i]);
317 if (node == NULL)
318 {
319 cout << values[i] << " ";
320 node = new BinNode<int> (values[i]);
321 t_it = insert_root(t_it, node);
322 }
323 }
324
326
327 cout << endl << "Finished" << endl;
328
329 cout << "Comparing recursive result with iterative ... " << endl;
331 cout << "Resulting trees are equal" << endl;
332 else
333 cout << "Resulting trees are different" << endl;
334
335 {
336 ofstream out("bintree.tree");
338 }
339
340 ifstream input("bintree.tree");
342
343 cout << "Comparing loaded tree with iterative ... " << endl;
345 cout << "Resulting trees are equal" << endl;
346 else
347 cout << "Resulting trees are different" << endl;
348
351 }
352}
#define AH_ERROR(format, args...)
Print an error message (always enabled).
Definition ahDefs.H:271
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.
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
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.
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.