// 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 = 30;
// how many nodes
const NONODES = MAX / 2;
// console log how we build the tree or not
const SHOWBUILD = false;
// Binary tree
var tree;
function setup() {
createCanvas(cw, ch);
background(51, 150, 220);
$.getScript ( "/uploads/fordea23/tree.js", function() {
$.getScript ( "/uploads/fordea23/node.js", function() {
// New tree
tree = new Tree();
console.log ("=== build tree =================");
// Add random values
for (var i = 1; i <= NONODES; i++)
{
var n = floor(random(1, MAX+1));
console.log ("adding node: " + n);
tree.addValue(n);
}
// for (i = 1; i < 10; i++) {
// tree.addValue(floor(random(0, 20)));
// }
// tree.addValue(5);
// tree.addValue(4);
// tree.addValue(7);
// tree.addValue(6);
// tree.addValue(16);
// tree.addValue(26);
// tree.addValue(8);
// tree.addValue(96);
// tree.addValue(65);
//console.log(tree);
tree.traverse();
// var varSearch = 16;
var varSearch = floor(random(1, MAX+1));
var result = tree.search(varSearch);
var resultMesage = "";
console.log("result -- " + result);
if (result == null){
console.log("result -- not found");
resultMessage = "but not found.";
}
else {
console.log("result -- found " + result);
resultMessage = "and found it!";
}
textAlign(LEFT);
textSize(20);
text("Max number of nodes: " + NONODES, 10, ch - 60);
text("Range of search values: 1 to " + MAX, 10, ch - 35);
text("Searching for " + varSearch + " " + resultMessage, 10, ch - 10);
} );
} );
}