Posts

Showing posts from April, 2014

Is Binary Search Tree - Java

public class IsBST { public static boolean isBST(Tree treeNode) { return isBST(treeNode,Integer.MIN_VALUE,Integer.MAX_VALUE); } private static boolean isBST(Tree treeNode, int minValue, int maxValue) { if(treeNode == null) return true; if(treeNode.data < minValue || treeNode.data > maxValue) return false; if(!isBST(treeNode.left,minValue,treeNode.data) && !isBST(treeNode.right,treeNode.data,maxValue)) return false; return true; } public static void main(String[] args) { Tree myTree = new Tree(4); myTree.left = new Tree(2); myTree.right = new Tree(6); myTree.left.left = new Tree(1); myTree.left.right = new Tree(3); myTree.right.left = new Tree(7); System.out.println(Boolean.toString(isBST(myTree))); } } class Tree{ int data; Tree left; Tree right; public Tree(int data) { this.data = data; this.left = this.right = null; } }

Binary Search in sorted array - Java

public class BinarySearch { public static void main(String[] args) { int[] nums = {1,2,3,4,5,6,7,8}; int num = 6;         binSearch(nums, num); } public static void binSearch(int[] nums,int num){ int start = 0; int end = nums.length - 1; while(end > start){ int mid = (start+end)/2; if(nums[mid] == num){ System.out.println("Number found" +num); break; } else if(nums[mid] > num) end = mid-1; else if(nums[mid] < num) start = mid+1; } } }

Reverse Linked List - Java

package excel; public class ReverseLinkedList { public static class List{ int value; List next; public List(int value){ this.value = value; } public String toString(){ List l = this; String listInString = ""; while(l != null){ listInString += l.value + "-->"; l = l.next; } return listInString + "tail"; } } public static void main(String args[]) { List l = new List(1); l.next = new List(2); l.next.next = new List(3); l.next.next.next = new List(4); System.out.println(l.toString()); System.out.println(reverse(l).toString()); } public static List reverse(List l){ if(l == null || l.next == null) return l; List remainingReverseList = reverse(l.next); List cur = remainingReverseList ; while(cur.next != null) cur = cur.next; cur.next = l; l.next = null; return remainingReverseList ; } } input: 1-->2-->3-->4--...

All permutations of String - Java

public class StringPermutation { public static void main(String[] args) { permutation("abc"); } private static void permutation(String str){ permutation(" ",str); } private static void permutation(String prefix, String str) { int len = str.length(); if(len == 0){ System.out.println(prefix); } else{ for (int i = 0; i < len; i++) { permutation(prefix+str.charAt(i),str.substring(0,i)+str.substring(i+1,len)); } } } } Output:  abc  acb  bac  bca  cab  cba Explanation: permutation(a,bc) -> permutation(ab,c) -> permutation(abc, '"') -> "abc"                             ->permutation(ac,b) -> permutation(acb, '"') -> "acb" permutation(b,ac) -> permutation(ba,c) -> permutation(bac, '"') -> "bac"                           ...

Square root using binary search method - Java

public class SquareRoot { public static void main(String[] args) { double squareRoot = sqRoot(6.25); System.out.println(squareRoot); } public static double sqRoot(double d) { double start = 0; double end = d; while(end > start){ double mid = (start + end)/2; if(mid * mid == d) return mid; else if(mid * mid > d){ end = mid; }else if (mid * mid < d){ start = mid; } } return d; } }