I'm trying to make a JavaScript function that returns the HTML for a full binary up to a certain number of bits.
For instance, full_bintree(1)
would give the HTML for
/\
1 0
and full_bintree(2) would give the HTML for
/\
/ \
1 0
/ \ / \
1 01 0
and full_bintree(3) would give the HTML for
/\
/ \
/ \
/ \
/ \
1 0
/ \ / \
/ \ / \
1 0 1 0
/ \ / \ / \ / \
1 0 1 01 0 1 0
and so forth.
I started to make a function but it's looking horrible and I keep realizing problems that I need to go back and fix.
function full_bintree(var b)
{
var retstr = "";
var nr = (2 << b) + (b - 1); // number of rows
for (var i = 0, j = nr - 1, k = 0; i < nr; ++i, --j, k += 2)
{
retstr += "<p>";
// Add leading spaces on line
for (var m = 0; m < j; ++m)
retstr += " ";
// Add backslashes or
// Add middle spaces
for (var m = 0; m < k; ++m)
retstring += " ";
// Add forward slashes
retstr += "</p>";
}
}
varkeyword from that function definition -full_bintree(b)nrwould appear to always be equal tob + 1(you're telling it to draw that many rows plus the root). You're also not checking forb < 1which you may want to do.retstr += " ";isn't going to give the desired effect in HTML.