JSFiddle - React, Tailwind, and code Playground
by wooozy
HTML
<h1>
Assignment 11
</h1>
<input type="button" value="Calculate" onclick="buildTree()" /><br>
Enter x:<input type="text" id="x" /><br>
Enter y:<input type="text" id="y" /><br>
<p id="value1"></p>
<p id="value2"></p>
<p id="value3"></p>
<p id="value4"></p>
JavaScript
var str = "";
var buildNode = function(_content,l,r){
this.parent = null;
this.left = null;
this.right = null;
this.content = _content;
}
buildNode.prototype.nodeL = function (_content){
var n = new buildNode(_content);
n.parent = this;
this.left = n;
return n;
}
buildNode.prototype.nodeR = function (_content){
var n = new buildNode(_content);
n.parent = this;
this.right = n;
return n;
}
function displayOrder(n){
if (n !== null){
displayOrder(n.left);
displayOrder(n.right);
str = str + n.content;
}
return str;
}
function fixedEvaluator(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();
}
buildNode.prototype.addNode = function(_content) {
var _node = new buildNode(_content);
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.numofChildren();
var rightN = this.right.numChildren();
if (leftN < rightN) {
this.left.addNode(_content);
} else {
this.right.addNode(_content);
}
return this;
}
buildNode.prototype.numChildren = function() {
var n = 0;
if (this.left != null) {
n = 1 + this.left.numChildren();
}
if (this.right != null) {
n = n + 1 + this.right.numChildren();
}
return n;
}
buildNode.prototype.print = function() {
var s = this.printNodeoutput();
if (this.left != null) {
s += this.left.print();
}
if (this.right != null) {
s += this.right.print();
}
...