MOD 10 - BINARY TREE

by SHELDON PASCIAK

HTML

Calculates the result of
(3*(X+(5*Y)))<br />

<div id="theE" name="theE" ></div>
X:<input type="number" id="xx"  value=1 />
Y:<input type="number" id="yy"  value=1 />
<br />
<input type="button" id="calc" value="Calculate" />
<br /><br />
<p id="output"> </p>
<input HIDDEN type="text" id="userValue"  placeholder="USER INPUT" />
<input HIDDEN type="button" id="add" value="add" />

<p id="output"></p>

<!--

Module 10 - Binary Trees
 
Introduction
 
Tree (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 - Tree Data Structures
 
Assignment
 
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
 
(SEE LINK: https://cop3530.pbworks.com/w/page/97959507/Module%2010%20-%20Binary%20Trees )
 
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)

examples:  x=5, y=3, answer = 60 .... good!

1..2  = 33
1..1  = 18
 
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...

JavaScript

// MOD 10 - PROGRAM 1 - SHELDON PASCIAK

var binaryTree = function () {
    this.root = null;
    var binaryTreeNode = function (content) {
        this.left = null;
        this.right = null;
        this.content = content;
    };

    this.createEquationTree = function() {        
        this.root = null;
        // create equation tree for (3*(x + 5*y))
        // 5*Y + x * 3
        //                           3                    
        //                          /           
        //                         *           
        //                        /             
        //                       +
        //                      / \              
        //                    *     X         
        //                   / \                
        //                  5   Y                    
        //                                                                

        var tempNode;        
        tempNode = this.add("3");                
        tempNode = this.addLeft(tempNode,"*");        
        tempNode = this.addLeft(tempNode,"+");
        this.addRight(tempNode,"X");
        tempNode = this.addLeft(tempNode,"*");
        this.addLeft(tempNode,"5");
        this.addRight(tempNode,"Y");        
    };

    //using example start provided from link on assignment page
    this.traverse = function (process) {
        //helper function
        function inOrder(node) {
            if (node) {
                //traverse the left subtree
                if (node.left !== null) {
                    inOrder(node.left);
                }
                //call the process method on this node
                process.call(this, node);
                //traverse the right subtree
                if (node.right !== null) {
                    inOrder(node.right);
                }
            }
        }
        //start with the root
        inOrder(this.root);
    };

    //USED to manual build expression tree
    this.addLeft = function (node,...