221# include <tclap/CmdLine.h>
228using namespace Aleph;
259 "Demonstrate BST split by position operation.\n"
260 "Creates a tree and splits it at a given position.",
263 TCLAP::ValueArg<int>
nArg(
"n",
"count",
264 "Number of elements",
268 TCLAP::ValueArg<unsigned int>
seedArg(
"s",
"seed",
269 "Random seed (0 = use time)",
270 false, 0,
"unsigned int");
273 TCLAP::ValueArg<int>
posArg(
"p",
"position",
274 "Split position (-1 = middle)",
280 int n =
nArg.getValue();
281 unsigned int t =
seedArg.getValue();
286 cerr <<
"Error: number of elements must be positive\n";
295 cout <<
"=== BST Split by Position Demo ===" <<
endl;
296 cout <<
"Elements: " << n <<
", Seed: " << t <<
endl <<
endl;
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);
305 cerr <<
"Error: cannot open output files" <<
endl;
313 cout <<
"Building ranked BST with " << n <<
" elements..." <<
endl;
314 for (
int i = 0; i < n; i++)
318 value =
static_cast<int>(1.0 * n *
rand() / (
RAND_MAX + 1.0));
350 cout <<
" ...done" <<
endl;
359 cout <<
endl <<
"Left subtree:" <<
endl;
360 cout <<
" Nodes: " << (
l ?
l->getCount() : 0)
365 cout <<
endl <<
"Left subtree: empty" <<
endl;
375 cout <<
"Right subtree:" <<
endl;
376 cout <<
" Nodes: " << (
r ?
r->getCount() : 0)
381 cout <<
"Right subtree: empty" <<
endl;
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;
396 catch (TCLAP::ArgException& e)
398 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() <<
endl;
Core header for the Aleph-w library.
WeightedDigraph::Node Node
Node for extended binary search tree.
static BinNodeXt *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.
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.
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.
Utility functions for binary tree operations.
Extended binary node with subtree count.
Generic unbalanced binary search tree.
Lazy and scalable dynamic array implementation.
void print_key(Node *p, int, int)
void print_key1(Node *p, int, int)
void print_key2(Node *p, int, int)