Code viewer for World: Binary tree (clone by Thom...

// Cloned by Thomas Mc Cann on 20 Oct 2019 from World "Binary tree" by "Coding Train" project 
// Please leave this clone trail here.
 

// Modified port of "01_binary_tree_viz" from AI course by Daniel Shiffman
// https://github.com/nature-of-code/NOC-S17-2-Intelligence-Learning/tree/master/week1-graphs

// Daniel Shiffman
// Nature of Code: Intelligence and Learning
// https://github.com/shiffman/NOC-S17-2-Intelligence-Learning


//
var drawTree = false;

// canvas size 
const cw = 900;
const ch = 600;
   
const root_x = cw / 2;
const root_y = ch / 10;
const ellipse_size = cw / 25;

// range of numbers
const MAX = 5000;

// how many nodes 
const NONODES = MAX / 2;

// console log how we build the tree or not 
const SHOWBUILD = true;


// Binary tree
var tree;


function setup() 
{
  createCanvas(cw,ch);


$.getScript ( "/uploads/midnightsky/node.js", function() {  //  /uploads/codingtrain/node.js
   // console.log ("Got node");
    
$.getScript ( "/uploads/midnightsky/tree.js", function() { //   /uploads/codingtrain/tree.js
      //  console.log ("Got tree");
        
        
  // New tree
  tree = new Tree();

  console.log ("=== build tree =================");
  // Add random values
  for (var i = 0; i < NONODES; i++) 
  {
      var n = floor(random(0, MAX));
      // console.log ("adding node: " + n);
      tree.addValue(n);
  }

  background("cornflowerblue");
  showMessage("Click to show Tree with " + MAX + " nodes.");

  // Traverse the tree
  tree.traverse();

  // Search the tree for random number 
  var x = floor(random(0, MAX));
  AB.msg( "console log shows how we search a sorted tree quickly <br> search tree for " + x + "<br>" );
  console.log ( "=== search tree for " + x  + " ===================");
   
  var result = tree.search(x);
  if (result === null)   AB.msg('not found', 2);
  else                  AB.msg('found', 2);
  
  
} );
} );

}

window.onclick = function(e){
    
   if(drawTree === false){
        
        drawTree = true;
        drawCanvas();
        showMessage("Showing the Tree.");
    }
    else{
        
        drawTree = false;
        drawCanvas();
        showMessage("Click to show Tree.");
    }
    
    tree.traverse();
    
};

function showMessage(message){
    
        fill(255);
        stroke("magenta");
        textSize(40);
       
        text(message, 20, ch/12);
}

function drawCanvas(){
    
        createCanvas(cw,ch);
        background("cornflowerblue");
}