Visualize a tree: Difference between revisions

Content deleted Content added
adding maxima
Line 4: Line 4:
'''Task''': Write a program to produce a visual representation of some tree. The content of the tree doesn't matter, nor does the output format, the only requirement being that the output is human friendly. Make do with the vague term "friendly" the best you can.
'''Task''': Write a program to produce a visual representation of some tree. The content of the tree doesn't matter, nor does the output format, the only requirement being that the output is human friendly. Make do with the vague term "friendly" the best you can.


=={{header|C}}==
Print a simple tree to standard output:
<lang c>#include <stdio.h>
#include <stdlib.h>

typedef struct stem_t *stem;
struct stem_t { char *str; stem next; };

void tree(int root, stem head)
{
static char *sdown = " |", *slast = " `", *snone = " ";
struct stem_t col = {0, 0}, *tail;

for (tail = head; tail; tail = tail->next) {
printf("%s", tail->str);
if (!tail->next) break;
}

printf("--%d\n", root);

if (root <= 1) return;

if (tail && tail->str == slast)
tail->str = snone;

if (!tail) tail = head = &col;
else tail->next = &col;

while (root) { // make a tree by doing something random
int r = 1 + (rand() % root);
root -= r;
col.str = root ? sdown : slast;

tree(r, head);
}

tail->next = 0;
}

int main(int c, char**v)
{
int n;
if (c < 2 || (n = atoi(v[1])) < 0) n = 8;

tree(n, 0);
return 0;
}</lang>
{{out}}
<pre>
--8
`--8
|--7
| |--3
| | |--2
| | | `--2
| | | `--2
| | | |--1
| | | `--1
| | `--1
| |--2
| | |--1
| | `--1
| |--1
| `--1
`--1
</pre>
=={{header|Maxima}}==
=={{header|Maxima}}==
<lang maxima>load(graphs)$
<lang maxima>load(graphs)$