// Cloned by Rajesh Dande on 26 Oct 2020 from World "Binary tree" by "Coding Train" project
// Please leave this clone trail here.
// canvas size
const cw = 950;
const ch = 650;
const root_x = cw / 2;
const root_y = ch / 10;
const ellipse_size = cw / 25;
// range of numbers
const MAX = 450;
// how many nodes
const NONODES = MAX / 3;
// console log how we build the tree or not
const SHOWBUILD = true;
// Binary tree
var tree;
var img;
var angle = 0;
function preload()
{
img = loadImage ('/uploads/rajeshdande9/1603557962.png');
}
function setup()
{
createCanvas(cw,ch,WEBGL);
$.getScript ( "/uploads/rajeshdande9/node_sample.js", function() {
// console.log ("Got node");
$.getScript ( "/uploads/rajeshdande9/tree_sample.js", function() {
// console.log ("Got tree");
// 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);
}
//background("lightblue");
// Traverse the tree
/*tree.traverse();*/
// Search the tree for random number
var x = floor(random(0, MAX));
AB.msg( "Welcome to the binary tree search ! <br> <br>" +
"Random range of numbers used : 0 to " + MAX + "<br>" +
"Total nodes constructed for Binary tree : " + NONODES + "<br> <br>" +
"<p style=\"background-color:Orange;\"> Console log shows how we search a sorted tree quickly. <br> <br>" +
"Searched tree for " + x + "." + "<br> <br> " );
console.log ( "============ Search tree for " + x + " ===================");
var result = tree.search(x);
if (result === null) AB.msg("Hard luck ! Could not found "+ x + "." + "<br> <br>", 2);
else AB.msg("Good news ! Found " + x + "." + "<br> <br>", 2);
AB.msg("Please click Reload button to try again ! <br> " +
"Alternately, type tree.search(x); in console to search the number of your choice. Example: tree.search(46); <br> <br>" +
"Enjoy the scenic view on the left side !! <br>", 3);
} );
} );
}
function draw()
{
AB.headerRHS();
AB.headerWidth (400);
// background("black");
background("white");
texture(img);
rotateY(angle);
box(425,250,250);
angle = angle + 0.001 ;
}