Skip to content

TreeRandomizer

afaji edited this page Mar 20, 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_count(int N) - generated tree will have N nodes
  • depth(int D) - generated tree will have depth D
  • leaf_count(int L ) - generated tree will have L leaves
  • child_count(int Cmin , int Cmax) - Every internal node will have has Cmin to Cmax childs
  • index_base(int b) - set your index base to b. Default is 0
  • root(int r) - set your tree root to r. Default is your index base

You can chain and combine all parameters above as you like, for example:

tree_rand.node_count(500).leaf_count(42);
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_count() : int - this will return an integer N, the number of nodes in your tree. Note that the number of edge is always N - 1.
  • edges() : pair<vector,vector>- this will return your tree's edges
  • root() : int - this will return your tree root.

Example

Below is the basic usage of Tree.

	TreeRandomizer tree_rand;
	int N = 25;
	Tree t = tree_rand.node_count(N).depth(5).index_base(1).root(10).next();


	
	vector<int> p1, p2;
	p1 = t.edges().first;
	p2 = t.edges().second;
	
	for (int i=0;i<N-1;i++){
		cout<<p1[i] <<" "<<p2[i]<<endl;
	}

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

4 5
24 8
7 15
20 18
11 12
6 8
17 18
1 9
13 11
9 6
6 4
11 5
25 19
7 14
21 20
22 20
23 8
10 2
6 7
3 10
4 3
7 16
19 18
17 2

Clone this wiki locally