Code viewer for World: Binary tree (clone by Clem)

// Cloned by Clem on 3 Oct 2023 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

// canvas size 
const cw = 1200;
const ch = 650;
   
const root_x = cw / 2;
const root_y = ch / 10;
const ellipse_size = cw / 30;

// range of numbers
const MAX = 5000;

// how many nodes 
const NONODES = 20;

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


// Binary tree
var tree;


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


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

  // Nodes in tree
  var nodeList = [];

  // Get random Number
  function rando() 
  {
      return floor(random(0, MAX));
  }

  console.log ("=== build tree =================");
  // Add random values
  for (var i = 0; i < NONODES; i++) 
  {
      var n = rando();
      // If node in list -> Try again
      while(nodeList.includes(n)) 
      {
          n = rando();
      }
      // Add node to list
      nodeList.push(n);
      // console.log ("adding node: " + n);
      tree.addValue(n);
  }

  background("lightgreen");


  // 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  + " ===================");
   
  // Time at beginning of search
  var t = new Date().getTime(); 	 // number of milliseconds since 1970 
  
  var result = tree.search(x);
  if (result == null)  
  {
    AB.msg('not found', 2);
  }
  else 
  {
    AB.msg('found', 2);
  }
  //time at end
  var end_t = new Date().getTime(); 	 // number of milliseconds since 1970 
  console.log('Time to search tree: ' + end_t - t + ' milliseconds');

   
  
  
} );
} );

}