JSFiddle - React, Tailwind, and code Playground
by leiperte
HTML
Enter a value for x <input type = "textbox" id = "x"/><br/>
Enter a value for y <input type = "textbox" id = "y"/><br/>
<input type = "button" id = "solve" value = "Solve" onClick = "solve()"/>
<p id="output"></p>
JavaScript
//Assignment 12
var TNode = function(_content) {
this.parent = null;
this.left = null;
this.right = null;
this.content = _content;
}
TNode.prototype.addNode = function(_content) {
var _node = new TNode(_content);
// if no left - add to left
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.numberOfChildren();
var rightN = this.right.numberOfChildren();
if (leftN < rightN) {
this.left.addNode(_content);
} else {
this.right.addNode(_content);
}
return this;
}
TNode.prototype.numberOfChildren = function() {
var n = 0;
if (this.left != null) {
n = 1 + this.left.numberOfChildren();
}
if (this.right != null) {
n = n + 1 + this.right.numberOfChildren();
}
return n;
}
TNode.prototype.print = function() {
var s = this.printNode();
if (this.left != null) {
s += this.left.print();
}
if (this.right != null) {
s += this.right.print();
}
return s;
}
TNode.prototype.printNode = function() {
var s = "Node: " + this.content;
if (this.left != null) {
s += " Left: " + this.left.content;
}
if (this.right != null) {
s = s + " Right: " + this.right.content;
}
s += "<br />";
return s;
}
var BinaryTree = function() {
this.top = null;
this.addNode = function(_content) {
// If no top node - add to top
if (this.top == null) {
var _node = new TNode(_content);
this.top = _node;
return this;
}
// otherwise let node select
this.top.addNode(_content);
return this;
}
}
BinaryTree.prototype.print = function() {
if (this.top != null) return this.top.print();
}
function createTree() {
var tree = new BinaryTree();
tree.addNode(3);
tree.addNode('*');
tree.addNode('x');
tree.addNode('+');
tree.addNode(5);
tree.addNode('*');
tree.addNode('y');
...