Skip to content

TreeRandomizer

afaji edited this page Mar 10, 2017 · 7 revisions

TreeRandomizer

TreeRandomizer randomly generates a Tree.

To begin, you have to include

#include "tcrand/tree.hpp"

This will enable TreeRandomizer and Tree class. Next, you can create your randomizer object as follow:

tcrand::TreeRandomizer tree_rand;

Setting Parameters

You may set some parameters for your TreeRandomizer, by calling methods below:

  • node(N) - generated tree will have N nodes
  • depth(D) - generated tree will have depth D
  • leaf(Lmin , Lmax ) - generated tree will have Lmin to Lmax leaves
  • child(Cmin , Cmax) - Every internal node will have has Cmin to Cmax childs You can chain and combine all parameters above as you like, for example:
tree_rand.leaf(100, 150).node(500);
Tree t = tree_rand.next();

Setting Property

You may additionally set a property of your tree. Different from the parameter, you can only set at maximum one property. You may combine property with some parameters, but be cautious as some properties will ignore certain parameter(s).

  • chain() - this will generate a chain tree. i.e, the tree's depth is equal to the tree's node. This property ignores leaf, depth and child configurations
  • star() - this will generate a star tree. This property ignores leaf, depth and child configurations
  • caterpillar() - this will generate a [caterpillar tree] (https://en.wikipedia.org/wiki/Caterpillar_tree).

Generate a random Tree

After giving the configurations above as needed, you have to call .next() to create a random Tree object. This tree will be a rooted tree with N nodes, numbered 0 to N-1 where 0 is the root.

Tree

Tree is a special class that holds the result of TreeRandomizer. You may simply print your result to file by calling print(file = stdout) method. This will print a tree based on this format:

  • First line is an integer N, the number of nodes in the tree
  • The next N-1 lines consist of 2 variable path_from[i] and path_to[i], where node path_from[i] and path_to[i] is directly connected.

Additionally, you may call methods below:

  • node() - this will return an integer N, the number of nodes in your tree. Note that the number of edge is always N - 1.
  • path() - this will return a tuple<vector<int> , vector<int> > that desribe the tree's path in path_from and path_to.
  • pathTo() - this will return a vector<int> that describe vector path_from
  • pathFrom() - this will return a vector<int> that describe vector path_to
  • shufflePath() - this will shuffle the path order
  • shuffleDirection() - this will shuffle the direction order
  • shuffle() - shuffles both path and direction

Example

Below is the basic usage of Tree.

	TreeRandomizer tree_rand;
	Tree t = tree_rand.node(6).next();
	cout<<"I am printing the tree (result may vary)"<<endl;
	t.print();

	cout<<"Obtaining the path simultaneously via .path()"<<endl;
	vector<int> p1, p2;
	tie(p1, p2) = t.path();
	cout<<"vector p1 : "; for (int v: p1) cout<< v <<" "; cout<<endl;
	cout<<"vector p2 : "; for (int v: p2) cout<< v <<" "; cout<<endl;

	cout<<"Obtaining the path individually via .pathFrom() and .pathTo()"<<endl;
	vector<int> p3, p4;
	p3 = t.pathFrom();
	p4 = t.pathTo();
	cout<<"vector p3 : "; for (int v: p3) cout<< v <<" "; cout<<endl;
	cout<<"vector p4 : "; for (int v: p4) cout<< v <<" "; cout<<endl;

the code above will print to stdout (might be different due to randomness):

I am printing the tree (result may vary)
6
0 1
0 2
2 3
2 4
4 5
Obtaining the path simultaneously via .path()
vector p1 : 0 0 2 2 4 
vector p2 : 1 2 3 4 5 
Obtaining the path individually via .pathFrom() and .pathTo()
vector p3 : 0 0 2 2 4 
vector p4 : 1 2 3 4 5 

Below is the example in case you want to shuffle the tree. Note that shuffling the tree won't change the tree. It will be the same tree with different persentation. This might be useful in certain cases.

	cout<<"Shuffling the path order"<<endl;
	t.shuffleOrder().print();
	cout<<"Shuffling the direction"<<endl;
	t.shuffleDirection().print();

The output will be:

Shuffling the path order
6
0 2
0 1
4 5
2 3
2 4
Shuffling the direction
6
2 0
1 0
5 4
2 3
2 4

Clone this wiki locally