This is a JSFiddle assignment and must be completed at jsfiddle.net and include
ID: 3777521 • Letter: T
Question
This is a JSFiddle assignment and must be completed at jsfiddle.net and include both the HTML and Java sections
A common use for trees is the Expression Tree. This is a specific case of a binary tree. When you write an equation, the computer stores the equation in a tree - which stores both the operations and the expression order.
We will give an example 2 - 3 * 4 + 5
The expression tree for this is;
If we traverse the tree using left - first traversal - the first dead end node is 2, then traverse back up to - and down to * and then down again to 3, then up to * and back down to 4 - so the traversal order without intermediate points is
2, 3, 4, *, - 5, +
The logical execution order is
3, 4, * = result
2, result, - = result
result, 5, + = result
or if you were to put it in logical order 2 - 3*4 + 5 , our original equation. For this assignment you will create a binary tree representation of the equation
So output should be (if I enter 1,1)
Input X : [ 1 ]
Input Y: [ 1 ]
X = 1, Y=1, Output = 18
2 ( 4 5 3Explanation / Answer
page.html
Please Enter values:<br/>
A: <input type="text" id="a" /> <br/>
B: <input type="text" id="b" /> <br/>
<input type='button' value='Calculate'/> <br/><br/>
<span id='output' />
page.js
window.calculate = function()
{
var output = document.getElementById('output');
// take values of a and b
var a = document.getElementById('a').value.trim();
var b = document.getElementById('b').value.trim();
// change to numbers
a = parseFloat(a);
b = parseFloat(b);
if (isNaN(x) || isNaN(y)) {
output.innerHTML = "Error detected: a or b is not a number.";
return;
}
// build the true for expression
// 3 * (a + 5 * b)
// leaf nodes
var leaf3 = new leaf(3);
var leafa = new leaf("a");
var leaf5 = new leaf(5);
var leafa = new leaf("b");
// Intermediate nodes
var mult1 = new node("*", leaf5, leafb); // 5 * b
var add1 = new node("+", leafa, mult1); // a + 5 * b
var mult2 = new node("*", leaf3, add1); // 3 * (a + 5 * b)
// evaluate or estimate the tree with the values
var values = {};
values["a"] = a;
values["b"] = b;
var result = mult2.traverse(values);
// result will be display
output.innerHTML = "A = " + a + ", B = " + b + ", Output = " + result;
};
// take leaf node as a operands
function leaf(value) {
this.value = value;
}
// build a intermediate node as operators
function node(symbol, left, right) {
this.symbol = symbol;
this.left = left;
this.right = right;
}
// traverse the tree
leaf.prototype.traverse = function(values) {
if (isNaN(this.value)) { // variable
return values[this.value];
}
// constant
return this.value;
};
node.prototype.traverse = function(values) {
// get or take the value of left and right children
var left = this.left.traverse(values);
var right = this.right.traverse(values);
// evalulate the result
var symbol = this.symbol;
if (symbol == '+') {
return left + right;
}
if (symbol == "-") {
return left - right;
}
if (symbol == '*') {
return left * right;
}
if (symbol == '/') {
return left / right;
}
// operator didnt defined
return 0;
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.