Skip to content

Instantly share code, notes, and snippets.

View alexandervasyuk's full-sized avatar

alexandervasyuk

View GitHub Profile
@alexandervasyuk
alexandervasyuk / infixToBinaryTreeSimple
Last active August 29, 2015 14:07
infixToBinaryTreeSimple
function Stack() {
this.stack = new Array();
}
Stack.prototype = {
isEmpty: function() {
return this.stack.length == 0;
},
pop: function() {
var evaluate = function(node) {
var token = node.data;
if (token instanceof Operator) {
return token.applyFunction(evaluate(node.left), evaluate(node.right));
} else {
return node.data;
}
}
@alexandervasyuk
alexandervasyuk / parenthesesConditions
Created October 7, 2014 19:45
parentheses conditions
if the token is a left parenthesis, then push it onto the stack.
If the token is a right parenthesis:
Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output stack.
Pop the left parenthesis from the stack, but not onto the output queue.
@alexandervasyuk
alexandervasyuk / infixToBinaryTreeUtils
Created October 8, 2014 22:54
infixToBinaryTreeUtils
function Stack() {
this.stack = new Array();
}
Stack.prototype = {
isEmpty: function() {
return this.stack.length == 0;
},
pop: function() {
@alexandervasyuk
alexandervasyuk / infixToBinaryParsing
Created October 8, 2014 22:58
infixToBinary Parsing
for (var s = 0; s < input.length; ) {
var token;
if (isNaN(input[s])) {
token = input[s];
s+=1;
} else {
var i = s + 1;
while(i < input.length && !isNaN(input[i])) {
i+=1;
}
@alexandervasyuk
alexandervasyuk / infixBinaryTreeLogic
Last active August 29, 2015 14:07
infixBinaryTreeLogic
...
if (token.length == 1 && isNaN(token)) {
var operator = createOperator(token);
if (!operatorStack.isEmpty() && operator.lessOrEqualInPrecedenceTo(operatorStack.peek())) {
var subtree = createSubtree(operatorStack.pop(), null);
outputStack.push(subtree);
}
operatorStack.push(operator);
} else {
@alexandervasyuk
alexandervasyuk / infixToBinaryCreateSubtree
Created October 8, 2014 23:16
infixToBinaryCreateSubtree
var createSubtree = function createSubtree(operator, tree) {
if (tree == null) {
var right = outputStack.pop(),
left = outputStack.pop();
tree = new BinaryTreeNode(operator);
tree.right = right instanceof BinaryTreeNode ? right : new BinaryTreeNode(right);
tree.left = left instanceof BinaryTreeNode ? left : new BinaryTreeNode(left);
} else {
var subtree = tree,
left = outputStack.pop();
@alexandervasyuk
alexandervasyuk / infixToBinaryUpdateTree
Created October 8, 2014 23:18
infixToBinaryUpdateTree
var updateTree = function() {
var operator = operatorStack.pop(),
output = outputStack.pop();
if (head == null) {
head = new BinaryTreeNode(operator);
left = outputStack.pop();
head.left = left instanceof BinaryTreeNode ? left : new BinaryTreeNode(left);
head.right = output instanceof BinaryTreeNode ? output : new BinaryTreeNode(output);
} else {
var subtree = head;
@alexandervasyuk
alexandervasyuk / infixBinaryTreeParentheseFunctionality
Created October 8, 2014 23:55
parenthesis functionality infix binary tree
if (token == '(') {
operatorStack.push(token);
} else if (token == ')') {
while(operatorStack.peek() != '(') {
var subtree = createSubtree(operatorStack.pop(), null);
outputStack.push(subtree);
}
operatorStack.pop();
}
@alexandervasyuk
alexandervasyuk / infixBinaryTreePart2
Created October 8, 2014 23:57
infixBinaryTreePart2
function Stack() {
this.stack = new Array();
}
Stack.prototype = {
isEmpty: function() {
return this.stack.length == 0;
},
pop: function() {