Assignment 12
by Taylor Zimmerman
HTML
<html>
<body>
<fieldset>
<legend>Enter X and Y Values</legend>
X = <input type="number" id="inputX"><br>
Y = <input type="number" id="inputY"><br>
<input type="button" value="Create Tree" onclick="createTree();">
</fieldset>
<fieldset>
<legend><b><big>Equation: 3*(X + 5*Y)</big></b></legend>
<div class="row">
<div class="column">
<big><b>Node Events</b></big><br><br/>
<div id = "output1">
</div>
</div>
<div class="column">
<big><b>Calculations</b></big><br><br/>
<div id = "output2">
</div>
<div id = "output3">
</div>
</div>
</div>
</fieldset>
</body>
</html>
CSS
.column {
float: left;
width: 50%;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
JavaScript
var a;
var b;
var result;
var TNode = function(_content) {
this.parent = null;
this.left = null;
this.right = null;
this.content = _content;
}
TNode.prototype.addNode = function(_content) {
var _node = new TNode(_content);
// if no left - add to left
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 leftN = this.left.numberOfChildren();
var rightN = this.right.numberOfChildren();
if (leftN < rightN) {
this.left.addNode(_content);
} else {
this.right.addNode(_content);
}
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 = "Node: " + this.content;
if (this.left != null) {
s += " Left: " + this.left.content;
}
if (this.right != null) {
s = s + " Right: " + this.right.content;
}
s += "<br/>";
return s;
}
var BinaryTree = function() {
this.top = null;
this.addNode = function(_content) {
// If no top node - add to top
if (this.top == null) {
var node = new TNode(_content);
this.top = node;
return this;
}
// otherwise let node select
this.top.addNode(_content);
return this;
}
}
BinaryTree.prototype.print = function() {
if (this.top != null) return this.top.print();
}
BinaryTree.prototype._postOrder = function (node) {
if (node == null) { return }
this._postOrder(node.left);
this._postOrder(node.right);
stack.push(node.content);
...