<h1>Bianary Tress</h1>
<b>Input values for X and Y to be solved in the following equation: <br>
3*(X + 5*Y)</b>
<br>
<br>
X input :<input type = text id = xInput size = 5><br>
Y input :<input type = text id = yInput size = 5><br>
<br>
<button id = runTree onclick = createTree()>Create tree</button>
<p id = 'output'><b>
Binary Tree Formation: <br>
*
<br>
/ \
<br>
* 3
<br>
/ \ / \
<br>
5 y x +
<br>
</b>
------------------------------------------------------------------------------------------
</p>
<p id = 'output1'></p>
<p id = 'output2'></p>
<p id = 'output3'>Order pushed to stack:</p>
<p id = 'output4'></p>
JavaScript
///////////////////////////////////////////////////////////////////////////////////////////////////
document.getElementById('xInput').focus();
var TNode = function (_c)
{
this.parent = null
this.left = null;
this.right = null;
this.content = _c;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
TNode.prototype.addNode = function (_c)
{
var node = new TNode(_c);
if (this.left == null)
{
node.parent = this;
this.left = node;
return this;
}
if (this.right == null)
{
node.parent = this;
this.right = node;
return this;
}
var leftNode = this.left.numberOfChildren();
var rightNode = this.right.numberOfChildren();
if (leftNode < rightNode)
{
this.left.addNode(_c);
} else {
this.right.addNode(_c);
}
return this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
TNode.prototype.numberOfChildren = function()
{
var n = 0;
if (this.left != null)
{
n = 1 + this.left.numberOfChildren();
}
if (this.right != null)
{
n = n + 1 + this.right.numberOfChildren();
}
return n;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
TNode.prototype.print = function()
{
var s = this.printNode();
if (this.left != null)
{
s += this.left.print();
}
if (this.right != null)
{
s += this.right.print();
}
return s;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
TNode.prototype.printNode = function()
{
var s = ' ' + this.content;
if (this.left != null)
{
s += ' Left: ' + this.left.content;
}
if (this.right != null)
{
s = s + ' Right: ' + this.right.content;
}
s += '<br> ' ;
return...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.