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 <iostream>
217# include <fstream>
218# include <tclap/CmdLine.h>
219# include <aleph.H>
220# include <tpl_dynArray.H>
221# include <tpl_binNodeXt.H>
222# include <tpl_binNodeUtils.H>
223# include <tpl_binTree.H>
224
225using namespace Aleph;
226using namespace std;
227
229
230// Output streams
231ofstream output;
232ofstream output_1;
233ofstream output_2;
234
235void print_key(Node* p, int, int)
236{
237 output << p->get_key() << " ";
238}
239
240void print_key1(Node* p, int, int)
241{
242 output_1 << p->get_key() << " ";
243}
244
245void print_key2(Node* p, int, int)
246{
247 output_2 << p->get_key() << " ";
248}
249
250int main(int argc, char* argv[])
251{
252 try
253 {
254 TCLAP::CmdLine cmd(
255 "Demonstrate BST split by position operation.\n"
256 "Creates a tree and splits it at a given position.",
257 ' ', "1.0");
258
259 TCLAP::ValueArg<int> nArg("n", "count",
260 "Number of elements",
261 false, 10, "int");
262 cmd.add(nArg);
263
264 TCLAP::ValueArg<unsigned int> seedArg("s", "seed",
265 "Random seed (0 = use time)",
266 false, 0, "unsigned int");
267 cmd.add(seedArg);
268
269 TCLAP::ValueArg<int> posArg("p", "position",
270 "Split position (-1 = middle)",
271 false, -1, "int");
272 cmd.add(posArg);
273
274 cmd.parse(argc, argv);
275
276 int n = nArg.getValue();
277 unsigned int t = seedArg.getValue();
278 int split_pos = posArg.getValue();
279
280 if (n <= 0)
281 {
282 cerr << "Error: number of elements must be positive\n";
283 return 1;
284 }
285
286 if (t == 0)
287 t = time(nullptr);
288
289 srand(t);
290
291 cout << "=== BST Split by Position Demo ===" << endl;
292 cout << "Elements: " << n << ", Seed: " << t << endl << endl;
293
294 // Open output files
295 output.open("split-before-aux.Tree", ios::out);
296 output_1.open("split-1-aux.Tree", ios::out);
297 output_2.open("split-2-aux.Tree", ios::out);
298
299 if (!output.is_open() || !output_1.is_open() || !output_2.is_open())
300 {
301 cerr << "Error: cannot open output files" << endl;
302 return 1;
303 }
304
305 // Build ranked tree
307 int value;
308
309 cout << "Building ranked BST with " << n << " elements..." << endl;
310 for (int i = 0; i < n; i++)
311 {
312 while (true)
313 {
314 value = static_cast<int>(1.0 * n * rand() / (RAND_MAX + 1.0));
315 if (searchInBinTree(root, value) == Node::NullPtr)
316 break;
317 }
318 Node* p = new Node(value);
320 }
321
322 int num_nodes = root->getCount();
323 cout << " Nodes: " << num_nodes
324 << ", Height: " << computeHeightRec(root) << endl;
325
328
329 // Write original tree
331
332 // Determine split position
334 split_pos = num_nodes / 2;
335
336 cout << endl << "Splitting at position " << split_pos
337 << " (of " << num_nodes << ")..." << endl;
338
339 // Add SPLIT directive for visualization
340 output << "SPLIT " << split_pos << " \"\" \"\"" << endl;
341
342 // Perform split
343 Node* l;
344 Node* r;
346 cout << " ...done" << endl;
347
348 // Verify and write left subtree
349 if (l != Node::NullPtr)
350 {
354 output_1 << endl;
355 cout << endl << "Left subtree:" << endl;
356 cout << " Nodes: " << (l ? l->getCount() : 0)
357 << ", Height: " << computeHeightRec(l) << endl;
358 }
359 else
360 {
361 cout << endl << "Left subtree: empty" << endl;
362 }
363
364 // Verify and write right subtree
365 if (r != Node::NullPtr)
366 {
368 assert(check_bst(r));
370 output_2 << endl;
371 cout << "Right subtree:" << endl;
372 cout << " Nodes: " << (r ? r->getCount() : 0)
373 << ", Height: " << computeHeightRec(r) << endl;
374 }
375 else
376 {
377 cout << "Right subtree: empty" << endl;
378 }
379
380 // Clean up
381 output.close();
382 output_1.close();
383 output_2.close();
384 destroyRec(l);
385 destroyRec(r);
386
387 cout << endl << "Generated files:" << endl;
388 cout << " - split-before-aux.Tree (original with split directive)" << endl;
389 cout << " - split-1-aux.Tree (left subtree)" << endl;
390 cout << " - split-2-aux.Tree (right subtree)" << endl;
391 }
392 catch (TCLAP::ArgException& e)
393 {
394 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
395 return 1;
396 }
397
398 return 0;
399}
int main()
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
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:233
void print_key(Node *p, int, int)
Definition writeSplit.C:235
ofstream output
Definition writeSplit.C:231
ofstream output_1
Definition writeSplit.C:232
void print_key1(Node *p, int, int)
Definition writeSplit.C:240
BinNodeXt< int > Node
Definition writeSplit.C:228
void print_key2(Node *p, int, int)
Definition writeSplit.C:245