ouuan's blog

By ouuan, history, 21 month(s) ago, In English

Want to generate different kinds of trees with simple functions or even a single string? The Tree Generator may be a good choice for you.

You can generate a tree by a single string:

Tree t("ch9,0st10,9"); // a tree consisting of a 10-node chain and a 10-node star at the bottom of it

You can add nodes by simple functions:

t.binary(10, 3); // add a 10-node binary tree with node 3 as its parent

You can shuffle the nodes and edges and output the tree:

cout << t;

You can customize the random function and the output function so that you can use it with testlib.h or print the edges with weights:

#include "testlib.h"

int myRandInt(int l, int r)
    return rnd.next(l, r);
randint = myRandInt;

void myOutputEdge(ostream& os, int u, int pa)
    os << u + 1 << ' ' << pa + 1 << ' ' << randint(1, 10) << endl;
cout << t;

Why don't you try it? Just visit https://github.com/ouuan/Tree-Generator to clone/copy the codes, and read the GUIDEBOOK to learn how to use it.

However, it hasn't been well-tested, so bug reports are welcomed. My English is not very good, so any opinions for guidebook improvement are welcomed, too.

  • Vote: I like it
  • +107
  • Vote: I do not like it

21 month(s) ago, # |
  Vote: I like it +14 Vote: I do not like it

Use Prufer Codes for truly random trees.

And I strongly recommend a function gen(int n, double low, double high) that will choose parent of $$$i$$$ from interval $$$[i \cdot low, i \cdot high]$$$. It's simple and still powerful.