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
184# include <cstdlib>
185# include <cassert>
186using namespace Aleph;
187# include <cstdlib>
188# include <cassert>
189using namespace std;
190
192
193// Output streams
194ofstream outputb;
195ofstream outputa;
196
197void print_keyb(Node* p, int, int)
198{
199 outputb << p->get_key() << " ";
200}
201
202void print_keya(Node* p, int, int)
203{
204 outputa << p->get_key() << " ";
205}
206
207int main(int argc, char* argv[])
208{
209 try
210 {
211 TCLAP::CmdLine cmd(
212 "Demonstrate BST balancing (DSW algorithm).\n"
213 "Creates an unbalanced tree and shows the result of balancing.",
214 ' ', "1.0");
215
216 TCLAP::ValueArg<int> nArg("n", "count",
217 "Number of elements",
218 false, 10, "int");
219 cmd.add(nArg);
220
221 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
222 "Random seed (0 = use time)",
223 false, 0, "unsigned int");
224 cmd.add(seedArg);
225
226 cmd.parse(argc, argv);
227
228 int n = nArg.getValue();
229 unsigned int t = seedArg.getValue();
230
231 if (n <= 0)
232 {
233 cerr << "Error: number of elements must be positive\n";
234 return 1;
235 }
236
237 if (t == 0)
238 t = time(nullptr);
239
240 srand(t);
241
242 cout << "=== BST Balancing Demo (median rotations) ===" << endl;
243 cout << "Elements: " << n << ", Seed: " << t << endl << endl;
244
245 // Open output files
246 outputb.open("balance-before-aux.Tree", ios::out);
247 outputa.open("balance-after-aux.Tree", ios::out);
248
249 if (!outputb.is_open() || !outputa.is_open())
250 {
251 cerr << "Error: cannot open output files" << endl;
252 return 1;
253 }
254
255 // Build ranked tree (will be unbalanced due to random insertion)
257 int value;
258
259 cout << "Building unbalanced BST with " << n << " elements..." << endl;
260 for (int i = 0; i < n; i++)
261 {
262 while (true)
263 {
264 value = static_cast<int>(100.0 * n * rand() / (RAND_MAX + 1.0));
265 if (searchInBinTree(root, value) == Node::NullPtr)
266 break;
267 }
268 Node* p = new Node(value);
270 }
271
273
275 int node_count = root->getCount();
276 cout << "Before balancing:" << endl;
277 cout << " Nodes: " << node_count << ", Height: " << height_before << endl;
278 cout << " Approx. optimal height would be: " << (static_cast<int>(log2(node_count)) + 1) << endl;
279
280 // Write tree before balancing
282 outputb << endl;
283
284 // Balance the tree
285 cout << endl << "Balancing tree by median selection + rotations..." << endl;
287
289
291 cout << "After balancing:" << endl;
292 cout << " Nodes: " << root->getCount() << ", Height: " << height_after << endl;
293 cout << " Height reduction: " << (height_before - height_after) << " levels" << endl;
294
295 // Write tree after balancing
297 outputa << endl;
298
299 // Clean up
300 outputb.close();
301 outputa.close();
303
304 cout << endl << "Generated files:" << endl;
305 cout << " - balance-before-aux.Tree (original unbalanced)" << endl;
306 cout << " - balance-after-aux.Tree (after balancing)" << endl;
307 }
308 catch (TCLAP::ArgException& e)
309 {
310 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
311 return 1;
312 }
313
314 return 0;
315}
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
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.
Node * balance_tree(Node *root)
Reequilibra un árbol binario de búsqueda.
STL namespace.
CmdLine cmd
Definition testHash.C:43
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)