118# include <tclap/CmdLine.h>
127using namespace Aleph;
143 output << p->getCount() <<
" ";
148 output <<
"tag " << pos <<
" " << pos <<
" N -15 35" << endl;
151int main(
int argc,
char* argv[])
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");
170 cmd.parse(argc, argv);
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 <<
" ";
210 cout << endl << endl;
216 cout <<
"Tree statistics:" << endl;
217 cout <<
" Total nodes: " <<
root->getCount() << endl;
219 cout <<
" Root key: " <<
KEY(
root) << 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;
234 output << endl << endl <<
"START-AUX ";
238 output << endl << endl <<
"% Position tags (infix order)" << endl << 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
size_t size_t int32_t value
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.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended 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_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 all the memory occupied by the tree root
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Search a key in a binary search tree using optimistic search.
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
Main namespace for Aleph-w library functions.
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)