This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Stack() { | |
| this.stack = new Array(); | |
| } | |
| Stack.prototype = { | |
| isEmpty: function() { | |
| return this.stack.length == 0; | |
| }, | |
| pop: function() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Stack() { | |
| this.stack = new Array(); | |
| } | |
| Stack.prototype = { | |
| isEmpty: function() { | |
| return this.stack.length == 0; | |
| }, | |
| pop: function() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (token == '(') { | |
| operatorStack.push(token); | |
| } else if (token == ')') { | |
| while(operatorStack.peek() != '(') { | |
| var subtree = createSubtree(operatorStack.pop(), null); | |
| outputStack.push(subtree); | |
| } | |
| operatorStack.pop(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Stack() { | |
| this.stack = new Array(); | |
| } | |
| Stack.prototype = { | |
| isEmpty: function() { | |
| return this.stack.length == 0; | |
| }, | |
| pop: function() { |