Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeBinTree.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
33# include <cstdlib>
34# include <ctime>
35# include <iostream>
36# include <fstream>
37# include <random>
38# include <ah-errors.H>
39# include <aleph.H>
40# include <tpl_binTree.H>
41# include <tpl_binNodeUtils.H>
42
43using namespace std;
44using namespace Aleph;
45
46static void print_key(BinTree<int>::Node *p, int, int)
47{
48 static ofstream output("bin-tree-aux.Tree", ios::out);
49 if (not output.is_open())
50 ah_runtime_error() << "Could not open bin-tree-aux.Tree";
51 output << p->get_key() << " ";
52}
53
54
55static void print_ex(BinTree<int>::Node *p, int, int)
56{
57 static ofstream example("example-34-ar-aux.Tree", ios::out);
58 if (not example.is_open())
59 ah_runtime_error() << "Could not open example-34-ar-aux.Tree";
60 example << p->get_key() << " ";
61}
62
63static void print_tex(BinTree<int>::Node *p, int, int)
64{
65 static ofstream tex("example-34-aux.tex", ios::out);
66 if (not tex.is_open())
67 ah_runtime_error() << "Could not open example-34-aux.tex";
68 tex << " $" << p->get_key() << "\\ $";
69}
70
71
72int main(int argc, char *argv[])
73{
74 int n = 1000;
75 if (argc > 1)
76 {
77 try { n = stoi(argv[1]); }
78 catch (...) { n = 1000; }
79 }
80
81 unsigned int t = std::time(0);
82 if (argc > 2)
83 {
84 try { t = static_cast<unsigned int>(stoul(argv[2])); }
85 catch (...) { t = std::time(0); }
86 }
87
88 std::mt19937 rng(t);
89
90 cout << "writeBinTree " << n << " " << t << endl;
91
92 BinTree<int> tree;
94
95 for (int i = 0; i < 30; i++)
96 {
97 int value;
98 do
99 {
100 value = std::uniform_int_distribution<int>(0, 499)(rng);
101 node = tree.search(value);
102 } while (node not_eq NULL);
103 node = new BinTree<int>::Node (value);
104 tree.insert(node);
105 }
106
109 destroyRec(tree.getRoot());
110 tree.getRoot() = nullptr; // Reset tree state after destruction
111
112 for (int i = 0; i < n; i++)
113 {
114 int value;
115 do
116 {
117 value = std::uniform_int_distribution<int>(0, n * 10 - 1)(rng);
118 node = tree.search(value);
119 } while (node not_eq NULL);
120 node = new BinTree<int>::Node (value);
121 tree.insert(node);
122 }
123
125
126 destroyRec(tree.getRoot());
127 tree.getRoot() = nullptr;
128}
129
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error()
Throws std::runtime_error unconditionally.
Definition ah-errors.H:287
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Node *& getRoot() noexcept
Return the root of tree.
Node * insert(Node *p) noexcept
Insert a node in the tree.
Node * search(const Key &key) const noexcept
Search a key.
static mt19937 rng
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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 recursively all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
STL namespace.
Binary search tree with nodes without virtual destructors,.
Utility functions for binary tree operations.
Generic unbalanced binary search tree.
static void print_key(BinTree< int >::Node *p, int, int)
static void print_tex(BinTree< int >::Node *p, int, int)
static void print_ex(BinTree< int >::Node *p, int, int)
ofstream output
Definition writeHeap.C:215