Assignment 12
Binary Tree with specific equation.
by Kristy Bond
HTML
<!--
3 * (x + 5 * y)<br>
Enter x:<input type="text" id="x" /><br>
Enter y:<input type="text" id="y" /><br>
<input type="button" value="Create Tree" onclick="createTree()" /><br>
<p id="output1"></p>
<p id="output2"></p>
-->
<p id='reportOut'></p>
JavaScript
var Node = function(_content) {
this.next = null;
this.last = null;
this.content = _content;
}
var Stack = function() {
this.head = null;
this.top = null;
this.push = function(_content) {
if (this.head == null) {
this.head = new Node(_content);
this.top = this.head;
return this;
}
var addedNode = new Node(_content);
addedNode.last = this.top;
this.top.next = addedNode;
this.top = addedNode;
return this;
}
//function Ctree(s){
//var operand = [];
//for (var i = 0, len = s.length; i < len; i++) {
// if(!isNaN(s[i])){
// operand.push(Number(s[i]));
//}
//else {
// var operator = s[i];
// var b = operand.pop();
// var a = operand.pop();
// if (operator === "+"){
// operand.push(a + b);
// }
// else if (operator === "-"){
// operand.push(a - b);
// }
// else if (operator === "*"){
// operand.push(a * b);
// }
// else if (operator === "/"){
// operand.push(a / b);
// }
// }
// }
// return operand.pop();
//}
this.pop = function() {
if (this.head == null) {
return null;
}
var a = this.top.content;
if (this.top == this.head) {
this.head = null;
this.top = null;
return a;
} else {
this.top = this.top.last;
this.top.next = null;
return a;
}
}
this.toString = function() {
var str = "";
var node = this.head;
if (this.head == null) {
str = "Stack is Empty";
} else {
}
while (node != null) {
str += node.content + " ";
node = node.next;
}
return str;
}
this.countElements = function() {
var countX = 0;
var node = this.head;
while (node != null) {
node = node.next;
countX = countX + 1;
}
return countX;
}
}
//put calculator buttons that take user inputed values and run them through the equation.
var buttonRunCalc =...