49 cout << node->get_key() <<
" ";
54 cout << node->get_key() <<
" ";
59 cout << node->get_key() <<
" ";
105int main(
int argc,
char *argv[])
108 unsigned int t = std::time(0);
114 n = std::stoi(argv[1]);
117 t = std::stoi(argv[2]);
126 cout <<
"n must be positive" << endl;
132 cout <<
"testBinNodeUtils " << n <<
" " << t << endl;
138 int insCount = 0, delCount = 0;
140 for (i = 0; i < n; i++)
142 value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
146 cout <<
value <<
" ";
153 cout << endl << insCount <<
" Items inserted" << endl;
180 assert(n_preorder == insCount);
181 assert(n_inorder == insCount);
182 assert(n_postorder == insCount);
192 catch (exception & e) { cout << e.what() << endl; }
193 catch (...) { cout <<
"Unknown exception" << endl; }
195 cout <<
"Level traverse" << endl;
198 cout << p->get_key() <<
" ";
201 cout << endl << endl;
215 for (i = 0; i < n; i++)
217 value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
219 if (node not_eq NULL)
226 cout << delCount <<
" Items removed" << endl;
230 catch (exception & e) { cout << e.what() << endl; }
231 catch (...) { cout <<
"Unknown exception" << endl; }
238 value = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
246 <<
"will be recursively partitioned according to key " <<
value << endl;
254 cout <<
"|" <<
value <<
"| ";
256 cout << endl << endl;
259 new_root = build_tree<BinNode, int>
265 <<
"will be iteratively partitioned according to key " <<
value << endl;
271 cout <<
"|" <<
value <<
"| ";
273 cout << endl << endl;
276 AH_ERROR(
"Left sides of partitions are not equal");
279 AH_ERROR(
"Right sides of partitions are not equal");
282 "Recursive partition result is identical to iterative partition"
285 if (t1_rec not_eq NULL)
287 if (t2_rec not_eq NULL)
290 if (t1_it not_eq NULL)
292 if (t2_it not_eq NULL)
298 cout <<
"Recursive insertion of " << n <<
" nodes at root ..." << endl;
300 for (i = 0; i < n; i++)
302 values[i] = 1+(int) (n*100.0*rand()/(RAND_MAX+1.0));
306 cout << values[i] <<
" ";
314 cout << endl <<
"Finished" << endl;
317 cout <<
"Iterative insertion of " << n <<
" nodes at root ..." << endl;
319 for (i = 0; i < n; i++)
324 cout << values[i] <<
" ";
332 cout << endl <<
"Finished" << endl;
334 cout <<
"Comparing recursive result with iterative ... " << endl;
336 cout <<
"Resulting trees are equal" << endl;
338 cout <<
"Resulting trees are different" << endl;
341 ofstream
out(
"bintree.tree");
345 ifstream input(
"bintree.tree");
348 cout <<
"Comparing loaded tree with iterative ... " << endl;
350 cout <<
"Resulting trees are equal" << endl;
352 cout <<
"Resulting trees are different" << endl;
#define AH_ERROR(...)
Print an error message (always enabled).
size_t size_t int32_t value
size_t size_t int32_t * out
Node for binary search tree.
size_t size() const noexcept
Return the current dimension of array.
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 * remove(const Key &key) noexcept(nothrow_compare_copy_v< Compare, Key >)
Remove a key from the tree.
Node * search(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Search a key.
int postOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in postorder a binary tree.
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
bool split_key_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Split recursively according to a key.
bool level_traverse(Node *root, Operation &operation)
Level traverse a tree and execute an operation.
Node * copyRec(Node *root)
Copy a tree.
void save_tree(Node *root, std::ostream &output)
Store a binary tree in a stream.
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.
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.
Node * insert_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert the node p as root of a binary search tree.
void split_key(Node *&root, const Key &key, Node *&l, Node *&r, const Compare &cmp=Compare()) noexcept
Split a binary search tree according to a key.
Main namespace for Aleph-w library functions.
bool areEquivalents(Node *t1, Node *t2, Equal &op) noexcept
Return true if trees are equivalents.
Binary search tree with nodes without virtual destructors,.
void operator()(BinNode< int > *p, istream &input) const
int operator()(BinNode< int > *p) const
static void fill_postorder(BinTree< int >::Node *node, int, int pos)
static void fill_preorder(BinTree< int >::Node *node, int, int pos)
DynArray< int > postorder
static void fill_inorder(BinTree< int >::Node *node, int, int pos)
static void printNode(BinTree< int >::Node *node, int, int)
static void print_node(BinTree< int >::Node *node, int, int)
Utility functions for binary tree operations.
Generic unbalanced binary search tree.
Lazy and scalable dynamic array implementation.