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 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 <cassert>
35# include <charconv>
36# include <ctime>
37# include <gsl/gsl_rng.h>
38# include <fstream>
39# include <iostream>
40# include <string_view>
41# include <ah-errors.H>
42# include <aleph.H>
43# include <tpl_treap.H>
44# include <gmpfrxx.h>
45# include <tpl_binNodeUtils.H>
46
47
48using namespace std;
49using namespace Aleph;
50
52{
53 return (long) (100.0*rand()/(RAND_MAX+1.0));
54}
55
56ofstream output;
57fstream fig_file;
58fstream tex_file;
59
61
62
63gsl_rng * rand_gen = nullptr;
64
65void print_pair(Treap<int>::Node *node, int, int)
66{
67 mpz_class pri =
68 mpz_class(gsl_rng_max(rand_gen)) + mpz_class(node->getPriority()) ;
69
70 pri = pri / 10000000;
71
72 tex_file << "$(" << node->get_key() << "," << pri.get_si();
73
74 if (node != last_node)
75 tex_file << ")$, ";
76 else
77 tex_file << ")$ ";
78}
79
80void print_key(Treap<int>::Node *node, int, int)
81{
82 fig_file << node->get_key() << " ";
83}
84
85void print_treap(Treap<int>::Node *node, int, int)
86{
87 output << node->get_key() << " ";
88}
89
90
91
92void print_prio(Treap<int>::Node *node, int, int)
93{
94 mpz_class pri =
95 mpz_class(gsl_rng_max(rand_gen)) + mpz_class(node->getPriority()) ;
96
97 pri = pri / 10000000;
98
99 fig_file << pri.get_si() << " ";
100}
101
102
103int main(int argc, char *argv[])
104{
105 int n = 10;
106 unsigned int t = std::time(0);
107 int value;
108
109 if (argc > 1)
110 {
111 const std::string_view arg = argv[1];
112 auto [ptr, ec] = std::from_chars(arg.data(), arg.data() + arg.size(), n);
113 ah_invalid_argument_if(ec != std::errc{} or ptr != arg.data() + arg.size() or n < 0)
114 << "Invalid or out-of-range value for n";
115 }
116
117 if (n <= 0)
118 {
119 cerr << "n must be positive" << endl;
120 return 1;
121 }
122
123 if (argc > 2)
124 {
125 const std::string_view arg = argv[2];
126 auto [ptr, ec] = std::from_chars(arg.data(), arg.data() + arg.size(), t);
127 ah_invalid_argument_if(ec != std::errc{} or ptr != arg.data() + arg.size())
128 << "Invalid or out-of-range value for t";
129 }
130
131 srand(t);
132
133 output.open("treap-aux.Tree", ios::out | ios::trunc);
135 << "Could not open treap-aux.Tree";
136 fig_file.open("bal-04-aux.Tree", ios::out | ios::trunc);
138 << "Could not open bal-04-aux.Tree";
139 tex_file.open("treap-aux.tex", ios::out | ios::trunc);
141 << "Could not open treap-aux.tex";
142
143 cout << "writeTreap " << n << " " << t << endl;
144
145 Treap<int> tree;
146 tree.set_seed(t);
147 Treap<int>::Node *node;
148 int i;
149
150 cout << "Inserting " << n << " random values in tree ...\n";
151
152 for (i = 0; i < n; i++)
153 {
154 while (true)
155 {
156 value = 1 + static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
157 node = tree.search(value);
158 if (node == nullptr)
159 break;
160 }
161 node = new Treap<int>::Node (value);
162 tree.insert(node);
163 }
164
165 assert(is_treap(tree.getRoot()));
166
167 rand_gen = tree.gsl_rng_object();
168
169 cout << "Min = " << gsl_rng_min(rand_gen) << endl
170 << "Max = " << gsl_rng_max(rand_gen) << endl;
171
172 preOrderRec(tree.getRoot(), print_key);
173 fig_file << endl << "START-AUX " << endl;
174 inOrderRec(tree.getRoot(), print_prio);
175
176 last_node = find_max(tree.getRoot());
177
178 tex_file << "~\\ ";
179 inOrderRec(tree.getRoot(), print_pair);
180 tex_file << "~\\ ";
181
182 // destroyRec() leaves the root as Node::NullPtr, the treap's sentinel.
183 // Do not reset it to nullptr: the tree would no longer be empty but broken.
184 destroyRec(tree.getRoot());
185 tree.set_seed(t);
186
187 for (i = 0; i < n; i++)
188 {
189 value = static_cast<int>(n * 10.0 * rand() / (RAND_MAX + 1.0));
190 node = tree.search(value);
191 if (node == nullptr)
192 {
193 node = new Treap<int>::Node (value);
194 tree.insert(node);
195 }
196 }
197
198 assert(is_treap(tree.getRoot()));
199
201}
202
203
204
205
206
207
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:271
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:644
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
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:2045
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
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
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:80
long randomLong()
Definition writeTreap.C:51
Treap< int >::Node * last_node
Definition writeTreap.C:60
void print_treap(Treap< int >::Node *node, int, int)
Definition writeTreap.C:85
ofstream output
Definition writeTreap.C:56
fstream fig_file
Definition writeTreap.C:57
void print_prio(Treap< int >::Node *node, int, int)
Definition writeTreap.C:92
fstream tex_file
Definition writeTreap.C:58
void print_pair(Treap< int >::Node *node, int, int)
Definition writeTreap.C:65