Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeBalance.C
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
177# include <iostream>
178# include <fstream>
179# include <tclap/CmdLine.h>
180# include <aleph.H>
181# include <tpl_dynArray.H>
182# include <tpl_balanceXt.H>
183
184using namespace Aleph;
185using namespace std;
186
188
189// Output streams
190ofstream outputb;
191ofstream outputa;
192
193void print_keyb(Node* p, int, int)
194{
195 outputb << p->get_key() << " ";
196}
197
198void print_keya(Node* p, int, int)
199{
200 outputa << p->get_key() << " ";
201}
202
203int main(int argc, char* argv[])
204{
205 try
206 {
207 TCLAP::CmdLine cmd(
208 "Demonstrate BST balancing (DSW algorithm).\n"
209 "Creates an unbalanced tree and shows the result of balancing.",
210 ' ', "1.0");
211
212 TCLAP::ValueArg<int> nArg("n", "count",
213 "Number of elements",
214 false, 10, "int");
215 cmd.add(nArg);
216
217 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
218 "Random seed (0 = use time)",
219 false, 0, "unsigned int");
220 cmd.add(seedArg);
221
222 cmd.parse(argc, argv);
223
224 int n = nArg.getValue();
225 unsigned int t = seedArg.getValue();
226
227 if (n <= 0)
228 {
229 cerr << "Error: number of elements must be positive\n";
230 return 1;
231 }
232
233 if (t == 0)
234 t = time(nullptr);
235
236 srand(t);
237
238 cout << "=== BST Balancing Demo (median rotations) ===" << endl;
239 cout << "Elements: " << n << ", Seed: " << t << endl << endl;
240
241 // Open output files
242 outputb.open("balance-before-aux.Tree", ios::out);
243 outputa.open("balance-after-aux.Tree", ios::out);
244
245 if (!outputb.is_open() || !outputa.is_open())
246 {
247 cerr << "Error: cannot open output files" << endl;
248 return 1;
249 }
250
251 // Build ranked tree (will be unbalanced due to random insertion)
253 int value;
254
255 cout << "Building unbalanced BST with " << n << " elements..." << endl;
256 for (int i = 0; i < n; i++)
257 {
258 while (true)
259 {
260 value = static_cast<int>(100.0 * n * rand() / (RAND_MAX + 1.0));
261 if (searchInBinTree(root, value) == Node::NullPtr)
262 break;
263 }
264 Node* p = new Node(value);
266 }
267
269
271 int node_count = root->getCount();
272 cout << "Before balancing:" << endl;
273 cout << " Nodes: " << node_count << ", Height: " << height_before << endl;
274 cout << " Approx. optimal height would be: " << (static_cast<int>(log2(node_count)) + 1) << endl;
275
276 // Write tree before balancing
278 outputb << endl;
279
280 // Balance the tree
281 cout << endl << "Balancing tree by median selection + rotations..." << endl;
283
285
287 cout << "After balancing:" << endl;
288 cout << " Nodes: " << root->getCount() << ", Height: " << height_after << endl;
289 cout << " Height reduction: " << (height_before - height_after) << " levels" << endl;
290
291 // Write tree after balancing
293 outputa << endl;
294
295 // Clean up
296 outputb.close();
297 outputa.close();
299
300 cout << endl << "Generated files:" << endl;
301 cout << " - balance-before-aux.Tree (original unbalanced)" << endl;
302 cout << " - balance-after-aux.Tree (after balancing)" << endl;
303 }
304 catch (TCLAP::ArgException& e)
305 {
306 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
307 return 1;
308 }
309
310 return 0;
311}
int main()
Core header for the Aleph-w library.
WeightedDigraph::Node Node
Node for extended binary search tree.
static BinNodeXt *const NullPtr
Key & get_key() noexcept
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log2_function > > log2(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4064
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4060
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
Node * insert_by_key_xt(Node *&r, Node *p, Compare &cmp) noexcept
Insert a node in an extended binary search tree.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended 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.
size_t computeHeightRec(Node *root) noexcept
Compute recursively the height of root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Node * balance_tree(Node *root)
Reequilibra un árbol binario de búsqueda.
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Tree balancing with extended nodes.
Lazy and scalable dynamic array implementation.
ofstream outputa
void print_keya(Node *p, int, int)
ofstream outputb
BinNodeXt< int > Node
void print_keyb(Node *p, int, int)