Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testBinTree.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 <ctime>
33# include <cstdlib>
34# include <cassert>
35# include <iostream>
36# include <vector>
37# include <algorithm>
38# include <random>
39# include <memory>
40
41# include <aleph.H>
42# include <tpl_binTree.H>
43# include <tpl_tree_node.H>
44# include <generate_tree.H>
45
46# include <ah-random.H>
47
48using namespace std;
49using namespace Aleph;
50
51static void printNode(BinTree<int>::Node* node, int, int)
52{
53 cout << node->get_key() << " ";
54}
55
56
57struct Write
58{
60 {
61 return to_string(p->get_key());
62 }
63};
64
66{
67 cout << "(" << p->get_key();
68 p->for_each_child([](auto c) { print_forest_par(c); });
69 cout << ")";
70}
71
72void print_forest_deway(Tree_Node<int> * p, const string & idx)
73{
74 cout << "(" << idx << ":" << p->get_key() << ")";
75 size_t counter = 1;
76 p->for_each_child([&idx, &counter](auto c) {
77 stringstream s;
78 s << idx << '.' << counter++;
79 print_forest_deway(c, s.str());
80 });
81}
82
83int main(int argc, char *argv[])
84{
85 int n = argc > 1 ? stoi(argv[1]) : 1000;
86
87 unsigned int t = std::time(0);
88
89 if (argc > 2)
90 t = stoi(argv[2]);
91
93
94 cout << argv[0] << " " << n << " " << t << endl;
95
96 BinTree<int> tree;
97
98 int ins_count = 0;
99
100 // Validate input to prevent infinite loops
101 if (n > 1000)
102 {
103 cerr << "Error: n must be <= 1000 to avoid infinite loops with current RNG range." << endl;
104 return 1;
105 }
106
107 cout << "Inserting " << n << " random values in tree ...\n";
108
109 for (int i = 0; i < n; i++)
110 {
111 int value;
112 BinTree<int>::Node *node;
113 do
114 {
115 value = rng.uniform_int(1000);
116 node = tree.search(value);
117 }
118 while (node not_eq nullptr);
119
120 node = new BinTree<int>::Node (value);
121 tree.insert(node);
122 ins_count++;
123
124 cout << value << " ";
125 }
126 cout << endl << endl;
127
128 Tree_Node<int> * ttree =
129 bin_to_forest<Tree_Node<int>, BinTree<int>::Node>(tree.getRoot());
130
131 // Check if forest conversion succeeded before proceeding
132 if (ttree == nullptr)
133 {
134 cout << "Warning: Empty tree, no forest to process." << endl;
135 return 0;
136 }
137
138 generate_forest<Tree_Node<int>, Write> (ttree, std::cout);
139
140 cout << endl << "Secuencia paréntesis: ";
141 print_forest_par(ttree);
142 auto rc = ttree->get_right_sibling();
143 while (rc != nullptr)
144 {
146 rc = rc->get_right_sibling();
147 }
148 cout << endl << endl;
149
150 cout << "Secuencia en notación Deway: ";
151 print_forest_deway(ttree, "1");
152 rc = ttree->get_right_sibling();
153 size_t counter = 2;
154 while (rc != nullptr)
155 {
156 stringstream s;
157 s << counter++;
158 print_forest_deway(rc, s.str());
159 rc = rc->get_right_sibling();
160 }
161 cout << endl;
162
163 destroy_forest(ttree);
164
165 assert(tree.verifyBin());
166 cout << endl << ins_count << " insertions" << endl
167 << "prefijo: " << endl;
169 cout << endl << endl;
170
171 cout << "sufijo: " << endl;
173 cout << endl << endl;
174
175 cout << "infijo: " << endl;
177 cout << endl << endl;
178
179 cout << "Code = " << code(tree.getRoot()) << endl;
180
181 int ipl = internal_path_length(tree.getRoot());
182
183 cout << "IPL = " << ipl << endl
184 << "EPL = " << ipl + 2*n << endl;
185
186 BinTree<int>::Node * t1 = nullptr, * t2 = nullptr;
187
188 BinTree<int>::Node * aux;
189 aux = copyRec(tree.getRoot());
190
191 split_key(aux, 487, t1, t2);
192 cout << "t1: ";
193 preOrderRec(t1, printNode); cout << endl << endl;
194 cout << "t2: ";
195 preOrderRec(t2, printNode); cout << endl << endl;
196
197 int del_count = 0;
198
199 cout << "Removing " << n/4 << " keys" << endl;
200
201 std::vector<int> keys;
202 Aleph::for_each_in_order<BinTree<int>::Node>(tree.getRoot(), [&keys](auto node) {
203 keys.push_back(node->get_key());
204 });
205 std::shuffle(keys.begin(), keys.end(), std::mt19937(t));
206
207 for (size_t i = 0; i < (size_t)(n/4) && i < keys.size(); i++)
208 {
209 int value = keys[i];
210 BinTree<int>::Node * node = tree.remove(value);
211 if (node != nullptr)
212 {
213 del_count++;
214 cout << node->get_key() << " ";
215 delete node;
216 }
217 }
218
219 cout << endl << del_count << " deletions" << endl
220 << "prefijo: ";
222 cout << endl;
223 cout << endl;
224
225 assert(tree.verifyBin());
226
227 destroyRec(tree.getRoot());
228 destroyRec(t1);
229 destroyRec(t2);
230
231 cout << argv[0] << " " << n << " " << t << endl;
232 }
233
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Node * insert(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Insert a node in the tree.
Node *& getRoot() noexcept
Return the root of tree.
Node * remove(const Key &key) noexcept(nothrow_compare_copy_v< Compare, Key >)
Remove a key from the tree.
Node * search(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Search a key.
bool verifyBin() const
See verify().
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
Forward declaration used by CRTP helpers before the full node definition.
Tree_Node * get_right_sibling() const noexcept
Returns the right sibling of this.
void for_each_child(Operation &op) const
Visits each child of this and executes the operation on the child node.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
static mt19937 rng
Tree visualization and output generation.
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.
Node * copyRec(Node *root)
Copy a tree.
size_t internal_path_length(Node *p) noexcept
Compute the internal path length.
void destroy_forest(Node *root)
Destroys (frees memory) the forest whose first tree is root.
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively inorder a binary tree.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
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.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
STL namespace.
Binary search tree with nodes without virtual destructors,.
string operator()(Tree_Node< int > *p)
Definition testBinTree.C:59
static long counter
Definition test-splice.C:40
int keys[]
void print_forest_par(Tree_Node< int > *p)
Definition testBinTree.C:65
void print_forest_deway(Tree_Node< int > *p, const string &idx)
Definition testBinTree.C:72
static void printNode(BinTree< int >::Node *node, int, int)
Definition testBinTree.C:51
Generic unbalanced binary search tree.
General tree (n-ary tree) node.