1

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>";
    }
}
2
  • Remove the var keyword from that function definition - full_bintree(b) Commented Mar 26, 2014 at 21:40
  • nr would appear to always be equal to b + 1 (you're telling it to draw that many rows plus the root). You're also not checking for b < 1 which you may want to do. retstr += " "; isn't going to give the desired effect in HTML. Commented Mar 26, 2014 at 23:27

1 Answer 1

4

Here's your function:

var logtree = function(A){
    for(var i = 0; i < A.length; i++)
        A[i] = A[i].join("");
    A = A.join("\n");
    if(typeof $ != "undefined")
        $("#a").html(A);
    console.log(A);
    return A;
},
bintree = function(n, A, offset, lvl){
    if(typeof offset == "undefined") offset = 0;
    if(typeof lvl == "undefined") lvl = 0;
    var width = 3 * Math.pow(2, n) - 1,
        height = Math.ceil(width / 2),
        mid = Math.floor(width / 2),
        half = Math.ceil(height / 2),
        nsub = Math.pow(2, lvl),
        lim = offset + half - 1,
        l = "/",
        r = "\\";
    if(typeof A == "undefined"){
        A = [];
        for(var i = 0; i < height; i++){
            a = [];
            for(var j = 0; j < width; j++) a.push(" ");
            A[i] = a;
        }
    }
    for(var i = offset, inc = 0; i <= lim; i++){
        if(i == lim){
            l = 1;
            r = 0;
        }
        var a = 0, j = 0;
        for(var j = 0; j < nsub; j++){
            A[i][mid - inc + a - 1] = l;
            A[i][mid + inc + a + 1] = r;
            a += width + 1;
        }
        inc++;
    }
    if(n > 1)
        bintree(n - 1, A, offset + height / 2, ++lvl);
    return A;
};
logtree(bintree(4));

and here's the fiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.