207# include <tclap/CmdLine.h>
217using namespace Aleph;
226int main(
int argc,
char* argv[])
231 "Demonstrate root insertion in BST.\n"
232 "Shows how root insertion maintains recently inserted elements near the root.",
235 TCLAP::ValueArg<int> nArg(
"n",
"count",
236 "Number of elements",
240 TCLAP::ValueArg<unsigned int> seedArg(
"s",
"seed",
241 "Random seed (0 = use time)",
242 false, 0,
"unsigned int");
245 cmd.parse(argc, argv);
247 int n = nArg.getValue();
248 unsigned int t = seedArg.getValue();
255 cout <<
"=== Root Insertion Demo ===" << endl;
256 cout <<
"Elements: " << n <<
", Seed: " << t << endl << endl;
259 output.open(
"insert_root-aux.Tree", ios::out);
262 cerr <<
"Error: cannot open output file" << endl;
267 int inserted_count = 0;
268 int last_inserted = -1;
270 cout <<
"Inserting values: ";
271 for (
int i = 0; i < n; i++)
273 int value =
static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
280 KEY(p) =
static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
283 cout <<
KEY(p) <<
" ";
284 last_inserted =
KEY(p);
287 cout << endl << endl;
291 cout <<
"Statistics:" << endl;
292 cout <<
" Items inserted: " << inserted_count << endl;
294 cout <<
" Last inserted (should be root): " << last_inserted << endl;
295 cout <<
" Actual root: " <<
KEY(
root) << endl;
297 if (
KEY(
root) == last_inserted)
298 cout <<
" [OK] Root is the last inserted element" << endl;
300 cout <<
" [Note] Root may have changed due to rotations" << endl;
307 cout << endl <<
"Generated file:" << endl;
308 cout <<
" - insert_root-aux.Tree" << endl;
312 catch (TCLAP::ArgException& e)
314 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() << endl;
Core header for the Aleph-w library.
size_t size_t int32_t value
Node for binary search tree.
__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_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
Node * insert_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert the node p as root of a binary search tree.
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
Main namespace for Aleph-w library functions.
Utility functions for binary tree operations.
Basic binary tree node definitions.
static void printNode(BinNode< int > *node, int, int)