Assignment 12 - binary tree

by sheila massey

HTML

<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...