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 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27
28# include <cstdlib>
29# include <ctime>
30# include <iostream>
31# include <fstream>
32# include <random>
33# include <ah-errors.H>
34# include <aleph.H>
35# include <tpl_binTree.H>
36# include <tpl_binNodeUtils.H>
37
38using namespace std;
39using namespace Aleph;
40
41static void print_key(BinTree<int>::Node *p, int, int)
42{
43 static ofstream output("bin-tree-aux.Tree", ios::out);
44 if (not output.is_open())
45 ah_runtime_error() << "Could not open bin-tree-aux.Tree";
46 output << p->get_key() << " ";
47}
48
49
50static void print_ex(BinTree<int>::Node *p, int, int)
51{
52 static ofstream example("example-34-ar-aux.Tree", ios::out);
53 if (not example.is_open())
54 ah_runtime_error() << "Could not open example-34-ar-aux.Tree";
55 example << p->get_key() << " ";
56}
57
58static void print_tex(BinTree<int>::Node *p, int, int)
59{
60 static ofstream tex("example-34-aux.tex", ios::out);
61 if (not tex.is_open())
62 ah_runtime_error() << "Could not open example-34-aux.tex";
63 tex << " $" << p->get_key() << "\\ $";
64}
65
66
67int main(int argc, char *argv[])
68{
69 int n = 1000;
70 if (argc > 1)
71 {
72 try { n = stoi(argv[1]); }
73 catch (...) { n = 1000; }
74 }
75
76 unsigned int t = std::time(0);
77 if (argc > 2)
78 {
79 try { t = static_cast<unsigned int>(stoul(argv[2])); }
80 catch (...) { t = std::time(0); }
81 }
82
83 std::mt19937 rng(t);
84
85 cout << "writeBinTree " << n << " " << t << endl;
86
87 BinTree<int> tree;
89
90 for (int i = 0; i < 30; i++)
91 {
92 int value;
93 do
94 {
95 value = std::uniform_int_distribution<int>(0, 499)(rng);
96 node = tree.search(value);
97 } while (node not_eq NULL);
98 node = new BinTree<int>::Node (value);
99 tree.insert(node);
100 }
101
104 destroyRec(tree.getRoot());
105 tree.getRoot() = nullptr; // Reset tree state after destruction
106
107 for (int i = 0; i < n; i++)
108 {
109 int value;
110 do
111 {
112 value = std::uniform_int_distribution<int>(0, n * 10 - 1)(rng);
113 node = tree.search(value);
114 } while (node not_eq NULL);
115 node = new BinTree<int>::Node (value);
116 tree.insert(node);
117 }
118
120
121 destroyRec(tree.getRoot());
122 tree.getRoot() = nullptr;
123}
124
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error()
Throws std::runtime_error unconditionally.
Definition ah-errors.H:282
Core header for the Aleph-w library.
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
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
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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