/* makeRandom: * No. of nodes is largest 2^n - 1 less than or equal to h. */ void makeRandom(int h, int w, edgefn ef) { int i, j, type, size, depth; srand(time(0)); if (rand()%2==1) type = 1; else type = 0; size = depth = 0; while (size <= h) { size += ipow(2,depth); depth++; } depth--; if (size > h) { size -= ipow(2,depth); depth--; } if (type) makeBinaryTree (depth, ef); else makePath (size, ef); for (i=3; i<=size; i++) { for (j=1; j<i-1; j++) { int th = rand()%(size*size); if (((th<=(w*w))&&((i<5)||((i>h-4)&&(j>h-4)))) || (th<=w)) ef(j,i); } } }
void TreeViewer::visit( SyntaxNodeDiv *node ) { makeBinaryTree( node, "/" ); }
void TreeViewer::visit( SyntaxNodeMul *node ) { makeBinaryTree( node, "*" ); }
void TreeViewer::visit( SyntaxNodeSub *node ) { makeBinaryTree( node, "-" ); }
void TreeViewer::visit( SyntaxNodeAdd *node ) { makeBinaryTree( node, "+" ); }
// https://leetcode.com/faq/#different-output void test_makeTree() { auto t1 = makeBinaryTree("1"); auto t2 = makeBinaryTree("1,2,3"); auto t3 = makeBinaryTree("1,null,2,3"); auto t4 = makeBinaryTree("5,4,7,3,null,2,null,-1,null,9"); }
int main(int argc, char *argv[]) { GraphType graphType; edgefn ef; opts.pfx = ""; opts.name = ""; opts.cnt = 1; graphType = init(argc, argv, &opts); if (opts.directed) { fprintf(opts.outfile, "digraph %s{\n", opts.name); ef = dirfn; } else { fprintf(opts.outfile, "graph %s{\n", opts.name); ef = undirfn; } switch (graphType) { case grid: makeSquareGrid(opts.graphSize1, opts.graphSize2, opts.foldVal, opts.isPartial, ef); break; case circle: makeCircle(opts.graphSize1, ef); break; case path: makePath(opts.graphSize1, ef); break; case tree: if (opts.graphSize2 == 2) makeBinaryTree(opts.graphSize1, ef); else makeTree(opts.graphSize1, opts.graphSize2, ef); break; case trimesh: makeTriMesh(opts.graphSize1, ef); break; case ball: makeBall(opts.graphSize1, opts.graphSize2, ef); break; case torus: if ((opts.parm1 == 0) && (opts.parm2 == 0)) makeTorus(opts.graphSize1, opts.graphSize2, ef); else makeTwistedTorus(opts.graphSize1, opts.graphSize2, opts.parm1, opts.parm2, ef); break; case cylinder: makeCylinder(opts.graphSize1, opts.graphSize2, ef); break; case mobius: makeMobius(opts.graphSize1, opts.graphSize2, ef); break; case sierpinski: makeSierpinski(opts.graphSize1, ef); break; case complete: makeComplete(opts.graphSize1, ef); break; case randomg: makeRandom (opts.graphSize1, opts.graphSize2, ef); break; case randomt: { int i; treegen_t* tg = makeTreeGen (opts.graphSize1); for (i = 1; i <= opts.cnt; i++) { makeRandomTree (tg, ef); if (i != opts.cnt) closeOpen (); } freeTreeGen (tg); } makeRandom (opts.graphSize1, opts.graphSize2, ef); break; case completeb: makeCompleteB(opts.graphSize1, opts.graphSize2, ef); break; case hypercube: makeHypercube(opts.graphSize1, ef); break; case star: makeStar(opts.graphSize1, ef); break; case wheel: makeWheel(opts.graphSize1, ef); break; default: /* can't happen */ break; } fprintf(opts.outfile, "}\n"); exit(0); }