179# include <tclap/CmdLine.h>
184using namespace Aleph;
200 outputa << p->get_key() <<
" ";
208 "Demonstrate BST balancing (DSW algorithm).\n"
209 "Creates an unbalanced tree and shows the result of balancing.",
212 TCLAP::ValueArg<int>
nArg(
"n",
"count",
213 "Number of elements",
217 TCLAP::ValueArg<unsigned int>
seedArg(
"s",
"seed",
218 "Random seed (0 = use time)",
219 false, 0,
"unsigned int");
224 int n =
nArg.getValue();
225 unsigned int t =
seedArg.getValue();
229 cerr <<
"Error: number of elements must be positive\n";
238 cout <<
"=== BST Balancing Demo (median rotations) ===" <<
endl;
239 cout <<
"Elements: " << n <<
", Seed: " << t <<
endl <<
endl;
242 outputb.open(
"balance-before-aux.Tree", ios::out);
243 outputa.open(
"balance-after-aux.Tree", ios::out);
247 cerr <<
"Error: cannot open output files" <<
endl;
255 cout <<
"Building unbalanced BST with " << n <<
" elements..." <<
endl;
256 for (
int i = 0; i < n; i++)
260 value =
static_cast<int>(100.0 * n *
rand() / (
RAND_MAX + 1.0));
271 int node_count =
root->getCount();
272 cout <<
"Before balancing:" <<
endl;
274 cout <<
" Approx. optimal height would be: " << (
static_cast<int>(
log2(node_count)) + 1) <<
endl;
281 cout <<
endl <<
"Balancing tree by median selection + rotations..." <<
endl;
301 cout <<
" - balance-before-aux.Tree (original unbalanced)" <<
endl;
302 cout <<
" - balance-after-aux.Tree (after balancing)" <<
endl;
304 catch (TCLAP::ArgException& e)
306 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.
Node * balance_tree(Node *root)
Reequilibra un árbol binario de búsqueda.
DynList< T > maps(const C &c, Op op)
Classic map operation.
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)