213# include <tclap/CmdLine.h>
220using namespace Aleph;
239 output1 << p->get_key() <<
" ";
244 output2 << p->get_key() <<
" ";
252 "Demonstrate BST join operation.\n"
253 "Creates two BSTs and joins them into one, generating visualization files.",
256 TCLAP::ValueArg<int>
nArg(
"n",
"count",
257 "Number of elements per tree (total = 2n)",
261 TCLAP::ValueArg<unsigned int>
seedArg(
"s",
"seed",
262 "Random seed (0 = use time)",
263 false, 0,
"unsigned int");
268 int n =
nArg.getValue();
269 unsigned int t =
seedArg.getValue();
276 cout <<
"=== BST Join Operation Demo ===" <<
endl;
277 cout <<
"Elements per tree: " << n <<
", Seed: " << t <<
endl <<
endl;
280 output.open(
"join-aux.Tree", ios::out);
281 output1.open(
"join-1-aux.Tree", ios::out);
282 output2.open(
"join-2-aux.Tree", ios::out);
286 cerr <<
"Error: cannot open output files" <<
endl;
294 cout <<
"Building first tree with " << n <<
" elements..." <<
endl;
295 for (
int i = 0; i < n; i++)
299 value =
rand() % (n * 100) + 1;
311 cout <<
" Nodes: " << n1
317 cout <<
"Building second tree with " << n <<
" elements..." <<
endl;
318 for (
int i = 0; i < n; i++)
322 value =
rand() % (n * 100) + 1;
335 cout <<
" Nodes: " << n2
339 cout <<
endl <<
"Joining trees..." <<
endl;
345 cout <<
"Warning: duplicates found (unexpected)" <<
endl;
352 cout <<
"Resulting tree:" <<
endl;
362 cout <<
endl <<
"Generated files:" <<
endl;
363 cout <<
" - join-1-aux.Tree (first tree)" <<
endl;
364 cout <<
" - join-2-aux.Tree (second tree)" <<
endl;
365 cout <<
" - join-aux.Tree (joined result)" <<
endl;
367 catch (TCLAP::ArgException& e)
369 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() <<
endl;
Core header for the Aleph-w library.
WeightedDigraph::Node Node
Node for binary search tree.
static BinNode *const NullPtr
__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)
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
Node * insert_in_bst(Node *&r, Node *p, const Compare &cmp=Compare()) noexcept
Insert a node p in 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.
Node * insert_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert the node p as root of a binary search tree.
size_t computeHeightRec(Node *root) noexcept
Compute recursively the height of root
Main namespace for Aleph-w library functions.
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.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
Utility functions for binary tree operations.
Extended binary node with subtree count.
void print_key(Node *p, int, int)
void print_key1(Node *p, int, int)
void print_key2(Node *p, int, int)