// Cloned by Josh Casey 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 =900;const ch =600;const root_x = cw /2;const root_y = ch /100;const ellipse_size = cw /50;// range of numbersconst MAX =100;// how many nodes const NONODES = MAX;// console log how we build the tree or not const SHOWBUILD =true;// Binary treevar tree;function setup(){
createCanvas(cw *2,ch *2);
$.getScript ("/uploads/joshcasey/node.js",function(){
console.log ("Got node");
$.getScript ("/uploads/joshcasey/tree.js",function(){
console.log ("Got tree");// New tree
tree =newTree();
tree2 =newTree();
console.log ("=== build tree =================");// Add random valuesfor(var i =0; i < NONODES; i++){var n = floor(random(0, MAX));
console.log ("adding node: "+ n);
tree.addValue(n);
tree2.addValue(n);}
background("blue");// Traverse the tree
tree.traverse();
tree2.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);var result2 = tree2.search(x);if(result2 ==null) AB.msg('not found',2);else AB.msg('found',2);});});}