Aleph-w 7.10.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 <ah-random.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
63void print_pair(Treap<int>::Node *node, int, int)
64{
65 mpz_class pri =
66 mpz_class(static_cast<unsigned long>(Gsl_Compatible_Rng::max())) + mpz_class(node->getPriority()) ;
67
68 pri = pri / 10000000;
69
70 tex_file << "$(" << node->get_key() << "," << pri.get_si();
71
72 if (node != last_node)
73 tex_file << ")$, ";
74 else
75 tex_file << ")$ ";
76}
77
78void print_key(Treap<int>::Node *node, int, int)
79{
80 fig_file << node->get_key() << " ";
81}
82
83void print_treap(Treap<int>::Node *node, int, int)
84{
85 output << node->get_key() << " ";
86}
87
88
89
90void print_prio(Treap<int>::Node *node, int, int)
91{
92 mpz_class pri =
93 mpz_class(static_cast<unsigned long>(Gsl_Compatible_Rng::max())) + mpz_class(node->getPriority()) ;
94
95 pri = pri / 10000000;
96
97 fig_file << pri.get_si() << " ";
98}
99
100
101int main(int argc, char *argv[])
102{
103 int n = 10;
104 unsigned int t = std::time(0);
105 int value;
106
107 if (argc > 1)
108 {
109 const std::string_view arg = argv[1];
110 auto [ptr, ec] = std::from_chars(arg.data(), arg.data() + arg.size(), n);
111 ah_invalid_argument_if(ec != std::errc{} or ptr != arg.data() + arg.size() or n < 0)
112 << "Invalid or out-of-range value for n";
113 }
114
115 if (n <= 0)
116 {
117 cerr << "n must be positive" << endl;
118 return 1;
119 }
120
121 if (argc > 2)
122 {
123 const std::string_view arg = argv[2];
124 auto [ptr, ec] = std::from_chars(arg.data(), arg.data() + arg.size(), t);
125 ah_invalid_argument_if(ec != std::errc{} or ptr != arg.data() + arg.size())
126 << "Invalid or out-of-range value for t";
127 }
128
129 srand(t);
130
131 output.open("treap-aux.Tree", ios::out | ios::trunc);
132 ah_runtime_error_if(not output.is_open())
133 << "Could not open treap-aux.Tree";
134 fig_file.open("bal-04-aux.Tree", ios::out | ios::trunc);
135 ah_runtime_error_if(not fig_file.is_open())
136 << "Could not open bal-04-aux.Tree";
137 tex_file.open("treap-aux.tex", ios::out | ios::trunc);
138 ah_runtime_error_if(not tex_file.is_open())
139 << "Could not open treap-aux.tex";
140
141 cout << "writeTreap " << n << " " << t << endl;
142
143 Treap<int> tree;
144 tree.set_seed(t);
145 Treap<int>::Node *node;
146 int i;
147
148 cout << "Inserting " << n << " random values in tree ...\n";
149
150 for (i = 0; i < n; i++)
151 {
152 while (true)
153 {
154 value = 1 + static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
155 node = tree.search(value);
156 if (node == nullptr)
157 break;
158 }
159 node = new Treap<int>::Node (value);
160 tree.insert(node);
161 }
162
163 assert(is_treap(tree.getRoot()));
164
165 cout << "Min = " << Gsl_Compatible_Rng::min() << endl
166 << "Max = " << Gsl_Compatible_Rng::max() << endl;
167
168 preOrderRec(tree.getRoot(), print_key);
169 fig_file << endl << "START-AUX " << endl;
170 inOrderRec(tree.getRoot(), print_prio);
171
172 last_node = find_max(tree.getRoot());
173
174 tex_file << "~\\ ";
175 inOrderRec(tree.getRoot(), print_pair);
176 tex_file << "~\\ ";
177
178 // destroyRec() leaves the root as Node::NullPtr, the treap's sentinel.
179 // Do not reset it to nullptr: the tree would no longer be empty but broken.
180 destroyRec(tree.getRoot());
181 tree.set_seed(t);
182
183 for (i = 0; i < n; i++)
184 {
185 value = static_cast<int>(n * 10.0 * rand() / (RAND_MAX + 1.0));
186 node = tree.search(value);
187 if (node == nullptr)
188 {
189 node = new Treap<int>::Node (value);
190 tree.insert(node);
191 }
192 }
193
194 assert(is_treap(tree.getRoot()));
195
197}
198
199
200
201
202
203
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:415
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
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 tree's root.
Definition tpl_treap.H:338
void set_seed(const unsigned long seed)
Seeds the Mersenne Twister of the treap, as gsl_rng_set() did: the priorities are from then on those ...
Definition tpl_treap.H:186
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a key in a treap.
Definition tpl_treap.H:350
Node * insert(Node *root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_treap.H:360
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
static constexpr result_type min() noexcept
The least number of operator()().
Definition ah-random.H:256
signed long int get_si() const
Definition gmpfrxx.h:2034
__gmp_expr< mpz_t, mpz_t > mpz_class
Definition gmpfrxx.h:2068
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 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.
bool is_treap(Node *root) noexcept
Validate that a tree satisfies treap (heap) property.
Definition treapNode.H:144
STL namespace.
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:797
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:78
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:83
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:90
fstream tex_file
Definition writeTreap.C:58
void print_pair(Treap< int >::Node *node, int, int)
Definition writeTreap.C:63