118# include <tclap/CmdLine.h>
127using namespace Aleph;
143 output << p->getCount() <<
" ";
148 output <<
"tag " << pos <<
" " << pos <<
" N -15 35" <<
endl;
156 "Demonstrate ranked BST with subtree counts.\n"
157 "Creates a tree where each node stores the size of its subtree.",
160 TCLAP::ValueArg<int>
nArg(
"n",
"count",
161 "Number of elements",
165 TCLAP::ValueArg<unsigned int>
seedArg(
"s",
"seed",
166 "Random seed (0 = use time)",
167 false, 0,
"unsigned int");
172 int n =
nArg.getValue();
173 unsigned int t =
seedArg.getValue();
180 cout <<
"=== Ranked BST Demo ===" <<
endl;
181 cout <<
"Elements: " << n <<
", Seed: " << t <<
endl <<
endl;
184 output.open(
"rank-tree-aux.Tree", ios::out);
187 cerr <<
"Error: cannot open output file" <<
endl;
194 cout <<
"Building ranked BST..." <<
endl;
196 for (
int i = 0; i < n; i++)
201 value =
static_cast<int>(10.0 * n *
rand() / (
RAND_MAX + 1.0));
208 cout << value <<
" ";
216 cout <<
"Tree statistics:" <<
endl;
217 cout <<
" Total nodes: " <<
root->getCount() <<
endl;
220 cout <<
" Root count: " <<
root->getCount() <<
endl;
223 cout <<
endl <<
"Order statistics (select):" <<
endl;
224 for (
int i = 0; i <
min(n, 5); i++)
227 cout <<
" Position " << i <<
": " <<
KEY(
sel) <<
endl;
244 cout <<
endl <<
"Generated file:" <<
endl;
245 cout <<
" - rank-tree-aux.Tree (with subtree counts and position tags)" <<
endl;
249 catch (TCLAP::ArgException& e)
251 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< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
__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.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively inorder a binary tree.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
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.
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_tag(Node *, int, int pos)
void print_key(Node *p, int, int)
void print_count(Node *p, int, int)