Skip to content

Instantly share code, notes, and snippets.

View alexandervasyuk's full-sized avatar

alexandervasyuk

View GitHub Profile
@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 / 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.
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 / 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() {
@alexandervasyuk
alexandervasyuk / simpleinfixToBinaryTreePseudocode
Last active August 29, 2015 14:07
Simple infixToBinaryTree Pseudocode
Declare BinaryTreeNode result;
While there are tokens to be read:
Read a token.
If the token is a number, then add it to the output stack.
If the token is an operator, o1, then:
while there is an operator token, o2, at the top of the operator stack, and
o1 has precedence less than or equal to that of o2,
operator = pop o2 off the operator stack;
if result is null:
set result's data to operator
@alexandervasyuk
alexandervasyuk / simpleShuntingYardPseudo
Last active August 29, 2015 14:07
simpleShuntingYardPseudo
While there are tokens to be read:
Read a token.
If the token is a number, then add it to the output queue.
If the token is an operator, o1, then:
while there is an operator token, o2, at the top of the operator stack, and
o1 has precedence less than or equal to that of o2,
pop o2 off the stack, onto the output queue;
push o1 onto the stack.
When there are no more tokens to read:
While there are still operator tokens in the stack:
@alexandervasyuk
alexandervasyuk / deepCompare
Created October 4, 2014 20:57
Deep Compare
var deepCompare = function(a,b) {
for (var field in b) {
if (a.hasOwnProperty(field)){
if (a[field] instanceof Object) {
return deepCompare(a[field], b[field]);
} else if (a[field] !== b[field]){
return false
}
} else {
return false;
@alexandervasyuk
alexandervasyuk / merge
Created October 4, 2014 04:30
Merge Two Objects
/*
Write a function that will recursively merge two objects with the following conditions:
1.) If a[field] is an array, and b[field] is defined and is not an array, add b[field] to the array
2.) If a[field] is an array an b[field] exists but is undefined or null, set a[field] to an empty array
3.) If a[filed] is an array and b[field] is an array, set a[field] to b[field]
4.) If a[field] exists and b[field] exists but is undefined, delete a[field]
5.) If b[field] is a non-complex type (number, string, boolean, et cetera), copy to a[field]
*/
var a = {
@alexandervasyuk
alexandervasyuk / largestContinuousSum
Last active August 29, 2015 14:07
largestContinuousSum
public static LinkedList<Integer> largestContinuousSum(int[] array) {
if (array.length == 0) return null;
LinkedList<Integer> current_path, max_path;
int current_sum, max_sum;
max_path = current_path = new LinkedList<Integer>();
current_sum = max_sum = Integer.MIN_VALUE;
@alexandervasyuk
alexandervasyuk / matrixRegionSum
Created October 2, 2014 22:23
matrixRegionSum
public class SummableMatrix {
private HashMap<Coordinate, Integer> cache;
public SummableMatrix(int[][] matrix) {
cache = preprocess(matrix);
}
public static HashMap<Coordinate, Integer> preprocess(int[][] matrix) {
HashMap<Coordinate, Integer> result = new HashMap<Coordinate, Integer>();