230# include <tclap/CmdLine.h>
241using namespace Aleph;
255 while (values.size() < n)
257 int val = 1 + rand() % (n * 10);
258 if (used.find(val) == used.end())
261 values.push_back(val);
290 cerr <<
"Error: cannot open " <<
prefix <<
"avl.Tree" << endl;
296 for (
int val : values)
298 if (tree.
search(val) ==
nullptr)
310 <<
", Nodes: " <<
count << endl;
311 cout <<
" Written: " <<
prefix <<
"avl.Tree" << endl;
340 cerr <<
"Error: cannot open " <<
prefix <<
"rb.Tree" << endl;
346 for (
int val : values)
348 if (tree.
search(val) ==
nullptr)
361 output << endl <<
"START-SHADOW ";
367 <<
", Nodes: " <<
count << endl;
368 cout <<
" Written: " <<
prefix <<
"rb.Tree (with color info)" << endl;
389 cerr <<
"Error: cannot open " <<
prefix <<
"splay.Tree" << endl;
395 for (
int val : values)
397 if (tree.
search(val) ==
nullptr)
409 <<
", Nodes: " <<
count << endl;
410 cout <<
" Written: " <<
prefix <<
"splay.Tree" << endl;
431 cerr <<
"Error: cannot open " <<
prefix <<
"treap.Tree" << endl;
437 for (
int val : values)
439 if (tree.
search(val) ==
nullptr)
451 <<
", Nodes: " <<
count << endl;
452 cout <<
" Written: " <<
prefix <<
"treap.Tree" << endl;
473 cerr <<
"Error: cannot open " <<
prefix <<
"rand.Tree" << endl;
479 for (
int val : values)
481 if (tree.
search(val) ==
nullptr)
493 <<
", Nodes: " << tree.
size() << endl;
494 cout <<
" Written: " <<
prefix <<
"rand.Tree" << endl;
515 cerr <<
"Error: cannot open " <<
prefix <<
"bin.Tree" << endl;
521 for (
int val : values)
523 if (tree.
search(val) ==
nullptr)
535 <<
", Nodes: " <<
count << endl;
536 cout <<
" Written: " <<
prefix <<
"bin.Tree" << endl;
545int main(
int argc,
char* argv[])
550 "Generate tree structure files for visualization.\n"
551 "Creates .Tree files with preorder traversal for btreepic.",
554 TCLAP::ValueArg<size_t> nArg(
"n",
"count",
555 "Number of elements",
556 false, 30,
"size_t");
559 TCLAP::ValueArg<unsigned int> seedArg(
"s",
"seed",
560 "Random seed (0 = use time)",
561 false, 0,
"unsigned int");
564 vector<string> allowedTypes = {
"all",
"avl",
"rb",
"splay",
"treap",
"rand",
"bin"};
565 TCLAP::ValuesConstraint<string> typeConstraint(allowedTypes);
566 TCLAP::ValueArg<string> typeArg(
"t",
"type",
567 "Tree type to generate",
568 false,
"all", &typeConstraint);
571 TCLAP::ValueArg<string> prefixArg(
"o",
"output",
572 "Output file prefix",
573 false,
"",
"prefix");
576 cmd.parse(argc, argv);
578 size_t n = nArg.getValue();
579 unsigned int seed = seedArg.getValue();
580 string type = typeArg.getValue();
581 string prefix = prefixArg.getValue();
584 seed = time(
nullptr);
586 cout <<
"=== Tree Structure Generator ===" << endl;
587 cout <<
"Elements: " << n <<
", Seed: " <<
seed << endl;
588 cout <<
"Type: " << type << endl << endl;
593 cout <<
"Generated " << values.size() <<
" unique values" << endl << endl;
596 if (type ==
"all" || type ==
"avl")
598 cout <<
"AVL Tree:" << endl;
602 if (type ==
"all" || type ==
"rb")
604 cout <<
"Red-Black Tree:" << endl;
608 if (type ==
"all" || type ==
"splay")
610 cout <<
"Splay Tree:" << endl;
614 if (type ==
"all" || type ==
"treap")
616 cout <<
"Treap:" << endl;
620 if (type ==
"all" || type ==
"rand")
622 cout <<
"Rand Tree:" << endl;
626 if (type ==
"all" || type ==
"bin")
628 cout <<
"Binary Tree (unbalanced):" << endl;
632 cout << endl <<
"Done." << endl;
634 catch (TCLAP::ArgException& e)
636 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() << endl;
Core header for the Aleph-w library.
WeightedDigraph::Node Node
Node * insert(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Insert a node in the tree.
Node *& getRoot() noexcept
Return the root of tree.
Node * search(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Search a key.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node pointed by p in the tree.
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a node containing key.
size_t size() const noexcept
Return the number of nodes that have the tree.
Node *& getRoot() noexcept
Return the tree's root.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a key.
Node * insert(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Insert a node in the tree.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search for a key in the tree.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node into the tree.
Node *& getRoot() noexcept
Get reference to root pointer.
Node *& getRoot() noexcept
Return the tree's root.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a key in a treap.
Node * insert(Node *root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Node *& getRoot() noexcept
Get the top-down splay tree's root.
Node * insert(Node *p) noexcept
Inserts a node in a top-down splay tree.
Node * search(const Key &key) noexcept
Searches a key in a top-down splay tree.
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively inorder a binary tree.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
Main namespace for Aleph-w library functions.
static void prefix(Node *root, DynList< Node * > &acc)
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
#define RED
Red color constant (newly inserted nodes are red)
AVL binary search tree with nodes without a virtual destructor.
Binary search tree with nodes without virtual destructors,.
Randomized binary search tree.
Red-black tree with nodes without virtual destructor.
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.
Generic unbalanced binary search tree.
Randomized binary search tree.
Red-Black tree implementation (bottom-up balancing).
Top-down splay tree implementation (without rank support).
Treap: randomized BST combining tree and heap properties.
void write_bin(const vector< int > &values, const string &prefix)
void write_rand(const vector< int > &values, const string &prefix)
void write_avl(const vector< int > &values, const string &prefix)
static void bin_print_key(BinTree< int >::Node *p, int, int)
vector< int > generate_random_values(size_t n, unsigned int seed)
static void avl_print_key(Avl_Tree< int >::Node *p, int, int)
void write_treap(const vector< int > &values, const string &prefix)
static void treap_print_key(Treap< int >::Node *p, int, int)
void write_splay(const vector< int > &values, const string &prefix)
static void rb_write_colors(Rb_Tree< int >::Node *p, int, int)
static void splay_print_key(Splay_Tree< int >::Node *p, int, int)
void write_rb(const vector< int > &values, const string &prefix)
static void rand_print_key(Rand_Tree< int >::Node *p, int, int)
static ofstream * current_output
static void rb_print_key(Rb_Tree< int >::Node *p, int, int)