Skip to content

Instantly share code, notes, and snippets.

@alexandervasyuk
Created October 8, 2014 22:54
Show Gist options
  • Save alexandervasyuk/8229d7be345ab7341c30 to your computer and use it in GitHub Desktop.
Save alexandervasyuk/8229d7be345ab7341c30 to your computer and use it in GitHub Desktop.
infixToBinaryTreeUtils
function Stack() {
this.stack = new Array();
}
Stack.prototype = {
isEmpty: function() {
return this.stack.length == 0;
},
pop: function() {
return this.stack.pop();
},
peek: function() {
return this.stack[this.stack.length - 1];
},
push: function(o) {
this.stack.push(o);
}
}
function BinaryTreeNode(d) {
this.data = d;
this.left = null;
this.right = null;
}
function Operator(t){
this.sign = t;
return this;
}
Operator.prototype = {
lessOrEqualInPrecedenceTo:function(o) {
return this.value <= o.value;
}
}
var createOperator = function(t) {
switch (t) {
case '+': return new Plus(t);
case '-': return new Minus(t);
case '/': return new Divide(t);
case '*': return new Multiply(t);
default: return null;
}
}
function Plus(t) {
Operator.call(this, t);
this.value = 0;
this.applyFunction = function(arg1,arg2) {
return arg1 + arg2;
}
}
Plus.prototype = Object.create(Operator.prototype);
function Minus(t) {
Operator.call(this, t);
this.value = 0;
this.applyFunction = function(arg1,arg2) {
return arg1 - arg2;
}
}
Minus.prototype = Object.create(Operator.prototype);
function Divide(t) {
Operator.call(this, t);
this.value = 1;
this.applyFunction = function(arg1,arg2) {
return arg1 / arg2;
}
}
Divide.prototype = Object.create(Operator.prototype);
function Multiply(t) {
Operator.call(this, t);
this.value = 1;
this.applyFunction = function(arg1,arg2) {
return arg1 * arg2;
}
}
Multiply.prototype = Object.create(Operator.prototype);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment