Code viewer for World: Binary Tree World
/* Initialise any constants. */
const ch = 400;
const cw = 500;
const ellipse_size = cw / 50;
const root_x = cw / 2;
const root_y = ch / 10;
const MAX = 25;
const NONODES = MAX / 2;
const SHOWBUILD = true;

/* Initialise any variables. */
var tree;

function setup() {
    createCanvas(cw, ch);
  
    $.getScript ('uploads/drummk2/binary_tree_node.js', function() {
        $.getScript ('uploads/drummk2/binary_tree.js', function() {
            /* Initialise a new tree. */
            tree = new Tree();
          
            /* Populate the tree with random values. */
            for (var i = 0; i < NONODES; i++) {
                var n = floor(random(0, MAX));
                tree.addValue(n);
            }
          
            /* Set the background colour for this world. */
            background('lightgreen');
        
            /* Record the start time (pre-traversal). */
            var startTime = new Date().getTime();
        
            /* Traverse the tree. */
            /* This can be toggled to determine whether or not the tree is drawn. */
            tree.traverse();
        
            /* Search the tree for a random number. */
            var randomNum = floor(random(0, MAX));
            AB.msg('Searching for ' + randomNum + ': ');
            var result = tree.search(randomNum);
            if (!result) {
                AB.msg('NOT FOUND', 2);
            } else {
                AB.msg('FOUND', 2);
            }
            
            /* Record the end time (post-traversal). */
            var endTime = new Date().getTime();
            
            /* Output the time taken. */
            console.log(endTime - startTime);
        });
    });
}