Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
writeTreap.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 <cassert>
30# include <charconv>
31# include <ctime>
32# include <gsl/gsl_rng.h>
33# include <fstream>
34# include <iostream>
35# include <string_view>
36# include <ah-errors.H>
37# include <aleph.H>
38# include <tpl_treap.H>
39# include <gmpfrxx.h>
40# include <tpl_binNodeUtils.H>
41
42
43using namespace std;
44using namespace Aleph;
45
47{
48 return (long) (100.0*rand()/(RAND_MAX+1.0));
49}
50
51ofstream output;
52fstream fig_file;
53fstream tex_file;
54
56
57
58gsl_rng * rand_gen = nullptr;
59
60void print_pair(Treap<int>::Node *node, int, int)
61{
62 mpz_class pri =
63 mpz_class(gsl_rng_max(rand_gen)) + mpz_class(node->getPriority()) ;
64
65 pri = pri / 10000000;
66
67 tex_file << "$(" << node->get_key() << "," << pri.get_si();
68
69 if (node != last_node)
70 tex_file << ")$, ";
71 else
72 tex_file << ")$ ";
73}
74
75void print_key(Treap<int>::Node *node, int, int)
76{
77 fig_file << node->get_key() << " ";
78}
79
80void print_treap(Treap<int>::Node *node, int, int)
81{
82 output << node->get_key() << " ";
83}
84
85
86
87void print_prio(Treap<int>::Node *node, int, int)
88{
89 mpz_class pri =
90 mpz_class(gsl_rng_max(rand_gen)) + mpz_class(node->getPriority()) ;
91
92 pri = pri / 10000000;
93
94 fig_file << pri.get_si() << " ";
95}
96
97
98int main(int argc, char *argv[])
99{
100 int n = 10;
101 unsigned int t = std::time(0);
102 int value;
103
104 if (argc > 1)
105 {
106 const std::string_view arg = argv[1];
107 auto [ptr, ec] = std::from_chars(arg.data(), arg.data() + arg.size(), n);
108 ah_invalid_argument_if(ec != std::errc{} or ptr != arg.data() + arg.size() or n < 0)
109 << "Invalid or out-of-range value for n";
110 }
111
112 if (n <= 0)
113 {
114 cerr << "n must be positive" << endl;
115 return 1;
116 }
117
118 if (argc > 2)
119 {
120 const std::string_view arg = argv[2];
121 auto [ptr, ec] = std::from_chars(arg.data(), arg.data() + arg.size(), t);
122 ah_invalid_argument_if(ec != std::errc{} or ptr != arg.data() + arg.size())
123 << "Invalid or out-of-range value for t";
124 }
125
126 srand(t);
127
128 output.open("treap-aux.Tree", ios::out | ios::trunc);
130 << "Could not open treap-aux.Tree";
131 fig_file.open("bal-04-aux.Tree", ios::out | ios::trunc);
133 << "Could not open bal-04-aux.Tree";
134 tex_file.open("treap-aux.tex", ios::out | ios::trunc);
136 << "Could not open treap-aux.tex";
137
138 cout << "writeTreap " << n << " " << t << endl;
139
140 Treap<int> tree;
141 tree.set_seed(t);
142 Treap<int>::Node *node;
143 int i;
144
145 cout << "Inserting " << n << " random values in tree ...\n";
146
147 for (i = 0; i < n; i++)
148 {
149 while (true)
150 {
151 value = 1 + (int) (10.0*n*rand()/(RAND_MAX+1.0));
152 node = tree.search(value);
153 if (node == nullptr)
154 break;
155 }
156 node = new Treap<int>::Node (value);
157 tree.insert(node);
158 }
159
160 assert(is_treap(tree.getRoot()));
161
162 rand_gen = tree.gsl_rng_object();
163
164 cout << "Min = " << gsl_rng_min(rand_gen) << endl
165 << "Max = " << gsl_rng_max(rand_gen) << endl;
166
167 preOrderRec(tree.getRoot(), print_key);
168 fig_file << endl << "START-AUX " << endl;
169 inOrderRec(tree.getRoot(), print_prio);
170
171 last_node = find_max(tree.getRoot());
172
173 tex_file << "~\\ ";
174 inOrderRec(tree.getRoot(), print_pair);
175 tex_file << "~\\ ";
176
177 destroyRec(tree.getRoot());
178 tree.getRoot() = nullptr; // Reset tree state after destruction
179 tree.set_seed(t);
180
181 for (i = 0; i < n; i++)
182 {
183 value = (int) (n*10.0*rand()/(RAND_MAX+1.0));
184 node = tree.search(value);
185 if (node == nullptr)
186 {
187 node = new Treap<int>::Node (value);
188 tree.insert(node);
189 }
190 }
191
192 assert(is_treap(tree.getRoot()));
193
195}
196
197
198
199
200
201
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:266
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:639
Core header for the Aleph-w library.
void set_seed(const unsigned long seed) noexcept
Set the random number generator seed.
Definition tpl_treap.H:173
Node * search(const Key &key) const noexcept
Search a key in a treap.
Definition tpl_treap.H:222
Node *& getRoot() noexcept
Return the tree's root.
Definition tpl_treap.H:214
gsl_rng * gsl_rng_object() noexcept
Get a pointer to gsl random number generator.
Definition tpl_treap.H:211
Node * insert(Node *root, Node *p) noexcept
Definition tpl_treap.H:229
__gmp_expr< mpz_t, mpz_t > mpz_class
Definition gmpfrxx.h:2034
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
Node * find_max(Node *root) noexcept
Return the maximum key contained in a binary search tree.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool is_treap(Node *root) noexcept
Validate that a tree satisfies treap (heap) property.
Definition treapNode.H:144
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.
gsl_rng * rand_gen
Internal RNG handle used by the helper functors in this header.
STL namespace.
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:614
Utility functions for binary tree operations.
Treap: randomized BST combining tree and heap properties.
void print_key(Treap< int >::Node *node, int, int)
Definition writeTreap.C:75
long randomLong()
Definition writeTreap.C:46
Treap< int >::Node * last_node
Definition writeTreap.C:55
void print_treap(Treap< int >::Node *node, int, int)
Definition writeTreap.C:80
ofstream output
Definition writeTreap.C:51
fstream fig_file
Definition writeTreap.C:52
void print_prio(Treap< int >::Node *node, int, int)
Definition writeTreap.C:87
fstream tex_file
Definition writeTreap.C:53
void print_pair(Treap< int >::Node *node, int, int)
Definition writeTreap.C:60