// Cloned by Tony Forde on 7 Oct 2021 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 = 800;
const root_x = cw / 2;
const root_y = ch / 10;
const ellipse_size = cw / 25;
// range of numbers
const MAX = 20
// 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/codingtrain/node.js", function() {
// console.log ("Got node");
$.getScript ( "/uploads/codingtrain/tree.js", function() {
// console.log ("Got tree");
// MAX = Range of allowed numbers
// NONODES = Number of times Add Node will be called.
// EXAMPLE:
// MAX = 20
// NONODES = MAX / 2 = 10
// But it may add the same number more than once. As it doesn't duplicate existing node, this means why you often get less than NONODES.
// E.g. Add nodes for {7, 17, 4, 1, 4, 1, 17, 1, 17, 9} will only give you 5 nodes, i.e. for: 1, 4, 7, 9, 17. Because some numbers were repeated:
// Ordered list: {1, 1, 1, 4, 4, 7, 9, 17, 17, 17}
// 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);
}
// Comment out these next few lines if you don't want to draw the tree
//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 + " ===================");
var result = tree.search(x);
if (result == null) AB.msg('not found', 2);
else AB.msg('found', 2);
} );
} );
}