Trees (of all sorts) are used throughout programming. You should become familiar
ID: 673796 • Letter: T
Question
Trees (of all sorts) are used throughout programming. You should become familiar with the types of trees and you will do a little bit of the use of trees. Trees are covered in Topic - https://cop3530.pbworks.com/w/page/97278447/Topic%20-%20Tree%20Data%20Structures
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:
3*(x + 5*y)
Hint: there are plenty of javascript code examples of creating a binary tree (http://www.nczonline.net/blog/2009/06/09/computer-science-in-javascript-binary-search-tree-part-1/ )
You will then fill it in and execute by traversing the tree with given values (input by user - you need a GUI to input X and Y), and output a result.
So output should be (if I enter 1,1)
X = 1, Y=1, Output = 18
Please include a screenshot of the Javascript code and / or the template used for the program. Thank you!
Explanation / Answer
Here is your solution
HTML CODE :
<!DOCTYPE html>
<html>
<head title="DashBoard">
<title>DashBoard</title>
<script src="assignment3.js"></script>
</head>
<body>
<div class="main-container">
<div class="container" id="convert">
<form action="">
X:<br>
<input type="text" name="x" id="xvalue">
<br>
Y:<br>
<input type="text" name="y" id="yvalue">
</form>
<button id="b1">Click Here</button>
<p id="print"></p>
</div>
</div>
</body>
</html>
JAVASCRIPT CODE :
var node = {
value: 125,
left: null,
right: null
};
function BinarySearchTree() {
this._root = null;
}
BinarySearchTree.prototype = {
//restore constructor
constructor: BinarySearchTree,
add: function(value){
//create a new item object, place data in
var node = {
value: value,
left: null,
right: null
},
//used to traverse the structure
current;
//special case: no items in the tree yet
if (this._root === null){
this._root = node;
} else {
current = this._root;
while(true){
//if the new value is less than this node's value, go left
if (value < current.value){
//if there's no left, then the new node belongs there
if (current.left === null){
current.left = node;
break;
} else {
current = current.left;
}
//if the new value is greater than this node's value, go right
} else if (value > current.value){
//if there's no right, then the new node belongs there
if (current.right === null){
current.right = node;
break;
} else {
current = current.right;
}
//if the new value is equal to the current one, just ignore
} else {
break;
}
}
}
},
traverse: function(process){
//helper function
function postOrder(node){
if (node){
//traverse the left subtree
if (node.left !== null){
postOrder(node.left);
}
//traverse the right subtree
if (node.right !== null){
postOrder(node.right);
}
//call the process method on this node
process.call(this, node);
}
}
//start with the root
postOrder(this._root);
},
toArray: function(){
var result = [];
this.traverse(function(node){
result.push(node.value);
});
return result;
},
toString: function(){
return this.toArray().toString();
}
};
function convert(element)
{
var x = document.getElementById("xvalue").value;
var y = document.getElementById("yvalue").value;
x = parseInt(x,10);
y = parseInt(y,10);
var out = 3 * (x + 5*y);
//document.write('<div>Print this after the script tag</div>' y);
var el = document.getElementById("print");
el.innerHTML = out;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.