Aleph-w 3.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
71#ifndef RANDOM_TREE_H
72#define RANDOM_TREE_H
73
74# include <gsl/gsl_rng.h>
75# include <memory>
76# include <tpl_tree_node.H>
77# include <tpl_binNodeUtils.H>
78
79# include <ah-errors.H>
80
81using namespace Aleph;
82
111template <typename T>
113{
114 gsl_rng * r = nullptr;
115
122 BinNode<T> * random(unsigned long n)
123 {
124 if (n == 0)
125 return nullptr;
126
127 // inorder position of the root (random choice)
128 unsigned long i = gsl_rng_uniform_int(r, n);
129 BinNode<T> * root = new BinNode<T>();
130 LLINK(root) = random(i);
131 RLINK(root) = random(n - i - 1);
132 return root;
133 }
134
135public:
136
143 RandTree(unsigned long seed)
144 {
146 if (r == nullptr)
147 ah_bad_alloc_if(true);
148 gsl_rng_set(r, seed % gsl_rng_max(r));
149 }
150
157
162 {
163 if (r != nullptr)
165 }
166
185 {
186 assert(n != 0);
187 BinNode<T> * l = random(n - 1); // only left side
189 broot.getL() = l;
192 destroyRec(l);
193 return root;
194 }
195};
196
197
198
199
200#endif
Exception handling system with formatted messages for Aleph-w.
#define ah_bad_alloc_if(C)
Throws std::bad_alloc if condition holds.
Definition ah-errors.H:429
Node for binary search tree.
Generic m-ary trees.
Generator for uniformly random trees.
Tree_Node< T > * operator()(size_t n)
Generate a random tree with n nodes.
gsl_rng * r
GSL random number generator.
RandTree()
Construct generator with time-based seed.
RandTree(unsigned long seed)
Construct generator with specified seed.
BinNode< T > * random(unsigned long n)
Recursively generate random binary tree.
~RandTree()
Destructor releases GSL resources.
__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:4060
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
static void bin_to_tree(BNode *broot, TNode *troot)
DynList< T > maps(const C &c, Op op)
Classic map operation.
Utility functions for binary tree operations.
General tree (n-ary tree) node.
DynList< int > l