find the number of nodes in a binary tree greater than x

If you’re walking the tree from the root down, you don’t need to propagate ‘counts’ downward at all–the fact that you are is resulting in repeated counting of nodes as they contribute to both count, countLeft, and countRight, since you increment count before passing it to nodesGreaterThanX for the children. public static int nodesGreaterThanX(BinaryTreeNode<Integer> node, … Read more

Java permutation algorithm

Your question is of mathematics nature – combinations and permutations. You are actually asking the number of possible permutation for string length 7. The formula is factorial(numOfchar). In this case 7! = 7x6x5x4x3x2x1 = 5040. public static void main(String[] args) { String str = “ABCDEFH”; System.out.println(“Number of permutations for ” + str + ” is … Read more

Algorithm to detect when and where a point will exit a rectangle area

You need to find what edge is intersected first. Make equations for moving along both coordinates and calculate the first time of intersection. Note that for geographic coordinates you might need more complex calculations because “rectangle” defined by Lat/Lon coordinates is really curvy trapezoid on the Earth surface. Look at “Intersection of two paths given … Read more