Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
random_tree.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
74#ifndef RANDOM_TREE_H
75#define RANDOM_TREE_H
76
77# include <memory>
78# include <tpl_tree_node.H>
79# include <tpl_binNodeUtils.H>
80
81# include <ah-errors.H>
82# include <ah-random.H>
83
84using namespace Aleph;
85
114template <typename T>
116{
118
125 BinNode<T> * random(unsigned long n)
126 {
127 if (n == 0)
128 return nullptr;
129
130 // inorder position of the root (random choice)
131 unsigned long i = r->uniform_int(n);
132 BinNode<T> * root = new BinNode<T>();
133 LLINK(root) = random(i);
134 RLINK(root) = random(n - i - 1);
135 return root;
136 }
137
138public:
139
146 RandTree(unsigned long seed) : r(seed % Gsl_Compatible_Rng::max())
147 {
148 // empty
149 }
150
156 RandTree() : RandTree(time(nullptr)) {}
157
162 RandTree(const RandTree &) = delete;
163
165 RandTree & operator = (const RandTree &) = delete;
166
185 {
186 assert(n != 0);
187 BinNode<T> * l = random(n - 1); // only left side
188 BinNode<T> broot;
189 broot.getL() = l;
191 bin_to_tree(&broot, root);
192 destroyRec(l);
193 return root;
194 }
195};
196
197
198
199
200#endif
Exception handling system with formatted messages for Aleph-w.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Node for binary search tree.
BinNode *& getL() noexcept
A Gsl_Compatible_Rng in dynamic memory, so that what holds it grows only by a pointer,...
Definition ah-random.H:494
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
Definition ah-random.H:302
Forward declaration used by CRTP helpers before the full node definition.
Generator for uniformly random trees.
Tree_Node< T > * operator()(size_t n)
Generate a random tree with n nodes.
RandTree()
Construct generator with time-based seed.
Gsl_Compatible_Rng_Owner r
The generator, in dynamic memory.
RandTree & operator=(const RandTree &)=delete
Not assignable, as it is not copyable.
RandTree(unsigned long seed)
Construct generator with specified seed.
BinNode< T > * random(unsigned long n)
Recursively generate random binary tree.
RandTree(const RandTree &)=delete
Not copyable: a copy compiled, and both freed the same generator.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
static void bin_to_tree(BNode *broot, TNode *troot)
#define RLINK(i, n)
#define LLINK(i, n)
ValueArg< size_t > seed
Definition testHash.C:53
Utility functions for binary tree operations.
General tree (n-ary tree) node.
DynList< int > l