Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
RandTree< T > Class Template Reference

Generator for uniformly random trees. More...

#include <random_tree.H>

Collaboration diagram for RandTree< T >:
[legend]

Public Member Functions

 RandTree (unsigned long seed)
 Construct generator with specified seed.
 
 RandTree ()
 Construct generator with time-based seed.
 
 RandTree (const RandTree &)=delete
 Not copyable: a copy compiled, and both freed the same generator.
 
RandTree & operator= (const RandTree &)=delete
 Not assignable, as it is not copyable.
 
Tree_Node< T > * operator() (size_t n)
 Generate a random tree with n nodes.
 

Private Member Functions

BinNode< T > * random (unsigned long n)
 Recursively generate random binary tree.
 

Private Attributes

Gsl_Compatible_Rng_Owner r
 The generator, in dynamic memory.
 

Detailed Description

template<typename T>
class RandTree< T >

Generator for uniformly random trees.

Uses a Gsl_Compatible_Rng, the Mersenne Twister (MT19937) with the numbers of GSL, to create uniformly distributed random trees with a specified number of nodes.

Template Parameters
TType of data stored in tree nodes (not used for generation)

Complexity

  • Time: O(n) expected for n nodes
  • Space: O(n) for the tree structure
Example
// Create generator with specific seed for reproducibility
// Generate random tree with 50 nodes
// Tree structure is random but deterministic for same seed
Forward declaration used by CRTP helpers before the full node definition.
Generator for uniformly random trees.
__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
Author
Leandro Rabindranath León

Definition at line 115 of file random_tree.H.

Constructor & Destructor Documentation

◆ RandTree() [1/3]

template<typename T >
RandTree< T >::RandTree ( unsigned long  seed)
inline

Construct generator with specified seed.

Parameters
seedRandom seed for reproducibility
Exceptions
std::bad_allocif there is not enough memory for the generator

Definition at line 146 of file random_tree.H.

◆ RandTree() [2/3]

template<typename T >
RandTree< T >::RandTree ( )
inline

Construct generator with time-based seed.

Uses current time as seed for non-reproducible random trees.

Definition at line 156 of file random_tree.H.

◆ RandTree() [3/3]

template<typename T >
RandTree< T >::RandTree ( const RandTree< T > &  )
delete

Not copyable: a copy compiled, and both freed the same generator.

Member Function Documentation

◆ operator()()

template<typename T >
Tree_Node< T > * RandTree< T >::operator() ( size_t  n)
inline

Generate a random tree with n nodes.

Parameters
nNumber of nodes in the tree (must be > 0)
Returns
Pointer to root of newly allocated tree
Precondition
n > 0
Note
Caller is responsible for freeing the returned tree
Example
RandTree<int> gen(42);
Tree_Node<int>* tree = gen(100);
// ... use tree ...
destroy_tree(tree); // Don't forget to clean up!
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.

Definition at line 184 of file random_tree.H.

References Aleph::bin_to_tree(), Aleph::destroyRec(), Aleph::BinNode< Key >::getL(), l, RandTree< T >::random(), and root().

◆ operator=()

template<typename T >
RandTree & RandTree< T >::operator= ( const RandTree< T > &  )
delete

Not assignable, as it is not copyable.

◆ random()

template<typename T >
BinNode< T > * RandTree< T >::random ( unsigned long  n)
inlineprivate

Recursively generate random binary tree.

Parameters
nNumber of nodes in the subtree
Returns
Root of the generated subtree, or nullptr if n=0

Definition at line 125 of file random_tree.H.

References LLINK, RandTree< T >::r, RandTree< T >::random(), RLINK, root(), and Aleph::Gsl_Compatible_Rng::uniform_int().

Referenced by RandTree< T >::operator()(), and RandTree< T >::random().

Member Data Documentation

◆ r

template<typename T >
Gsl_Compatible_Rng_Owner RandTree< T >::r
private

The generator, in dynamic memory.

Definition at line 117 of file random_tree.H.

Referenced by RandTree< T >::random().


The documentation for this class was generated from the following file: