Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeSplit.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
216# include <cstdlib>
217# include <cassert>
218
219# include <iostream>
220# include <fstream>
221# include <tclap/CmdLine.h>
222# include <aleph.H>
223# include <tpl_dynArray.H>
224# include <tpl_binNodeXt.H>
225# include <tpl_binNodeUtils.H>
226# include <tpl_binTree.H>
227
228using namespace Aleph;
229
230using namespace std;
231
233
234// Output streams
235ofstream output;
236ofstream output_1;
237ofstream output_2;
238
239void print_key(Node* p, int, int)
240{
241 output << p->get_key() << " ";
242}
243
244void print_key1(Node* p, int, int)
245{
246 output_1 << p->get_key() << " ";
247}
248
249void print_key2(Node* p, int, int)
250{
251 output_2 << p->get_key() << " ";
252}
253
254int main(int argc, char* argv[])
255{
256 try
257 {
258 TCLAP::CmdLine cmd(
259 "Demonstrate BST split by position operation.\n"
260 "Creates a tree and splits it at a given position.",
261 ' ', "1.0");
262
263 TCLAP::ValueArg<int> nArg("n", "count",
264 "Number of elements",
265 false, 10, "int");
266 cmd.add(nArg);
267
268 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
269 "Random seed (0 = use time)",
270 false, 0, "unsigned int");
271 cmd.add(seedArg);
272
273 TCLAP::ValueArg<int> posArg("p", "position",
274 "Split position (-1 = middle)",
275 false, -1, "int");
276 cmd.add(posArg);
277
278 cmd.parse(argc, argv);
279
280 int n = nArg.getValue();
281 unsigned int t = seedArg.getValue();
282 int split_pos = posArg.getValue();
283
284 if (n <= 0)
285 {
286 cerr << "Error: number of elements must be positive\n";
287 return 1;
288 }
289
290 if (t == 0)
291 t = time(nullptr);
292
293 srand(t);
294
295 cout << "=== BST Split by Position Demo ===" << endl;
296 cout << "Elements: " << n << ", Seed: " << t << endl << endl;
297
298 // Open output files
299 output.open("split-before-aux.Tree", ios::out);
300 output_1.open("split-1-aux.Tree", ios::out);
301 output_2.open("split-2-aux.Tree", ios::out);
302
303 if (!output.is_open() || !output_1.is_open() || !output_2.is_open())
304 {
305 cerr << "Error: cannot open output files" << endl;
306 return 1;
307 }
308
309 // Build ranked tree
311 int value;
312
313 cout << "Building ranked BST with " << n << " elements..." << endl;
314 for (int i = 0; i < n; i++)
315 {
316 while (true)
317 {
318 value = static_cast<int>(1.0 * n * rand() / (RAND_MAX + 1.0));
319 if (searchInBinTree(root, value) == Node::NullPtr)
320 break;
321 }
322 Node* p = new Node(value);
324 }
325
326 int num_nodes = root->getCount();
327 cout << " Nodes: " << num_nodes
328 << ", Height: " << computeHeightRec(root) << endl;
329
332
333 // Write original tree
335
336 // Determine split position
338 split_pos = num_nodes / 2;
339
340 cout << endl << "Splitting at position " << split_pos
341 << " (of " << num_nodes << ")..." << endl;
342
343 // Add SPLIT directive for visualization
344 output << "SPLIT " << split_pos << " \"\" \"\"" << endl;
345
346 // Perform split
347 Node* l;
348 Node* r;
350 cout << " ...done" << endl;
351
352 // Verify and write left subtree
353 if (l != Node::NullPtr)
354 {
358 output_1 << endl;
359 cout << endl << "Left subtree:" << endl;
360 cout << " Nodes: " << (l ? l->getCount() : 0)
361 << ", Height: " << computeHeightRec(l) << endl;
362 }
363 else
364 {
365 cout << endl << "Left subtree: empty" << endl;
366 }
367
368 // Verify and write right subtree
369 if (r != Node::NullPtr)
370 {
374 output_2 << endl;
375 cout << "Right subtree:" << endl;
376 cout << " Nodes: " << (r ? r->getCount() : 0)
377 << ", Height: " << computeHeightRec(r) << endl;
378 }
379 else
380 {
381 cout << "Right subtree: empty" << endl;
382 }
383
384 // Clean up
385 output.close();
386 output_1.close();
387 output_2.close();
388 destroyRec(l);
389 destroyRec(r);
390
391 cout << endl << "Generated files:" << endl;
392 cout << " - split-before-aux.Tree (original with split directive)" << endl;
393 cout << " - split-1-aux.Tree (left subtree)" << endl;
394 cout << " - split-2-aux.Tree (right subtree)" << endl;
395 }
396 catch (TCLAP::ArgException& e)
397 {
398 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
399 return 1;
400 }
401
402 return 0;
403}
Core header for the Aleph-w library.
WeightedDigraph::Node Node
int num_nodes
Definition btreepic.C:410
Node for extended binary search tree.
static BinNodeXt *const NullPtr
Key & get_key() noexcept
__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 split_pos_rec(Node *&r, const size_t i, Node *&ts, Node *&tg)
Split a extended binary tree according to a position.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search 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
std::pair< std::string, std::string > split_pos(const std::string &str, const size_t pos)
Split a std::string at a fixed position.
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.
STL namespace.
CmdLine cmd
Definition testHash.C:43
gsl_rng * r
Utility functions for binary tree operations.
Extended binary node with subtree count.
Generic unbalanced binary search tree.
Lazy and scalable dynamic array implementation.
DynList< int > l
ofstream output_2
Definition writeSplit.C:237
void print_key(Node *p, int, int)
Definition writeSplit.C:239
ofstream output
Definition writeSplit.C:235
ofstream output_1
Definition writeSplit.C:236
void print_key1(Node *p, int, int)
Definition writeSplit.C:244
BinNodeXt< int > Node
Definition writeSplit.C:232
void print_key2(Node *p, int, int)
Definition writeSplit.C:249