179# include <tclap/CmdLine.h>
186using namespace Aleph;
204 outputa << p->get_key() <<
" ";
212 "Demonstrate BST balancing (DSW algorithm).\n"
213 "Creates an unbalanced tree and shows the result of balancing.",
216 TCLAP::ValueArg<int>
nArg(
"n",
"count",
217 "Number of elements",
221 TCLAP::ValueArg<unsigned int>
seedArg(
"s",
"seed",
222 "Random seed (0 = use time)",
223 false, 0,
"unsigned int");
228 int n =
nArg.getValue();
229 unsigned int t =
seedArg.getValue();
233 cerr <<
"Error: number of elements must be positive\n";
242 cout <<
"=== BST Balancing Demo (median rotations) ===" <<
endl;
243 cout <<
"Elements: " << n <<
", Seed: " << t <<
endl <<
endl;
246 outputb.open(
"balance-before-aux.Tree", ios::out);
247 outputa.open(
"balance-after-aux.Tree", ios::out);
251 cerr <<
"Error: cannot open output files" <<
endl;
259 cout <<
"Building unbalanced BST with " << n <<
" elements..." <<
endl;
260 for (
int i = 0; i < n; i++)
264 value =
static_cast<int>(100.0 * n *
rand() / (
RAND_MAX + 1.0));
275 int node_count =
root->getCount();
276 cout <<
"Before balancing:" <<
endl;
278 cout <<
" Approx. optimal height would be: " << (
static_cast<int>(
log2(node_count)) + 1) <<
endl;
285 cout <<
endl <<
"Balancing tree by median selection + rotations..." <<
endl;
291 cout <<
"After balancing:" <<
endl;
304 cout <<
endl <<
"Generated files:" <<
endl;
305 cout <<
" - balance-before-aux.Tree (original unbalanced)" <<
endl;
306 cout <<
" - balance-after-aux.Tree (after balancing)" <<
endl;
308 catch (TCLAP::ArgException& e)
310 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_unary_expr< __gmp_expr< T, U >, __gmp_log2_function > > log2(const __gmp_expr< T, U > &expr)
__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 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.
Node * balance_tree(Node *root)
Reequilibra un árbol binario de búsqueda.
Tree balancing with extended nodes.
Lazy and scalable dynamic array implementation.
void print_keya(Node *p, int, int)
void print_keyb(Node *p, int, int)