// Cloned by Paul R on 24 Sep 2022 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
// range of numbers
const MAX = 10000000;
//const MAX = 10000;
// how many nodes
const NONODES = MAX ;
// console log how we build the tree or not
const SHOWBUILD = false;
const SHOW_SEARCH = false;
// Binary tree
var tree;
function setup()
{
$.getScript ( "/uploads/paul79/node.js", function() {
$.getScript ( "/uploads/paul79/tree.js", function() {
// New tree
tree = new Tree();
AB.msg("Building tree. Max nodes = " + NONODES + ". Please wait...");
console.log ("=== build tree =================");
// Add random values
let start = Date.now();
for (var i = 0; i < NONODES; i++)
{
var n = floor(random(0, MAX));
tree.addValue(n);
}
var delta = Date.now() - start;
// Search the tree for random number
var x = floor(random(0, MAX));
AB.msg("Number of nodes added = " + actualNodes, 2);
AB.msg("<br> Time to build tree = " + delta + " milliseconds", 3);
AB.msg("<br> Search tree for = " + x, 4);
console.log ( "=== search tree for " + x + " ===================");
start = Date.now();
var result = tree.search(x);
delta = Date.now() - start;
if (result === null) AB.msg('not found', 5);
else AB.msg('found', 5);
AB.msg("<br>Search time: " + delta + " milliseconds", 6);
} );
} );
}