Showing posts with label ava Programming interview Question. Show all posts
Showing posts with label ava Programming interview Question. Show all posts

Saturday, August 18, 2012

How to find element using binary search algorithm


import java.util.Arrays;

public class BinarySearchAlgo {

public static void main(String args[]){
int a[]={1,4,52,5,2,6,9,12};
int sval=4;
Arrays.sort(a);
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println("\n Element location");
System.out.print(binarySearch(a,sval));
}

private static int binarySearch(int[] a, int sval) {
int left=0;
int right=a.length-1;
return bsearch(a,sval,left,right);
}

private static int bsearch(int[] a, int sval, int left, int right) {
if(right<left)
return -1;

int mid=(left+right)/2;
if(sval<a[mid])
return bsearch(a,sval,left,mid-1);
else if(sval>a[mid])
return bsearch(a,sval,mid+1,right);
else
return mid;

}
}

Binary Tree Functions implementation using Java

package com.practice;


public class TreeNode {

TreeNode leftReference;
TreeNode rightReference;
int nodeValue;

TreeNode(int nodeValue){
this.nodeValue=nodeValue;
}

}


package com.practice;

public class BinaryTree {

  public static void main(String args[]){

  // Tree root node.
        int rootValue = 40;
        TreeNode rootnode = new TreeNode(rootValue);
        System.out.println("root node created. " + rootnode.nodeValue);
     
        // Inserting elements into binary tree
        insertion(rootnode, 45);
        insertion(rootnode, 13);
        insertion(rootnode, 78);
        insertion(rootnode, 33);
        insertion(rootnode, 16);
        System.out.println("Binary Tree");

        printTree(rootnode);

        System.out.println("Depth of Tree"+printDepth(rootnode));
     
        printPreOrderTravel(rootnode);
        printInOrderTravel(rootnode);
        printPostOrderTravel(rootnode);
     
        System.out.println("Get number of Nodes"+getNumberOfNodes(rootnode));
    }

   // Get the number of Nodes in binary tree
    private static int getNumberOfNodes(TreeNode rootnode) {
    if(rootnode== null)
    return 0;
    else
    return 1+ getNumberOfNodes(rootnode.leftReference) + getNumberOfNodes(rootnode.rightReference);
    }

//PostOrder Travel
private static void printPostOrderTravel(TreeNode rootnode) {
if(rootnode!=null){
printPreOrderTravel(rootnode.leftReference);
printPreOrderTravel(rootnode.rightReference);
System.out.println(rootnode.nodeValue);
}
}

//PreOrder Travel
  private static void printInOrderTravel(TreeNode rootnode) {
if(rootnode!=null){
printPreOrderTravel(rootnode.leftReference);
System.out.println(rootnode.nodeValue);
printPreOrderTravel(rootnode.rightReference);
}
  }
 
   //PreOrder Travel
  private static void printPreOrderTravel(TreeNode rootnode) {
if(rootnode!=null){
System.out.println(rootnode.nodeValue);
printPreOrderTravel(rootnode.leftReference);
printPreOrderTravel(rootnode.rightReference);
}
  }

  //Finding depth of binary Tree
public static int  printDepth(TreeNode rootnode) {

  if(rootnode==null)
  return 0;
  else
  return 1+ Math.max(printDepth(rootnode.leftReference),printDepth(rootnode.rightReference));
}

//Inserting node into binary Tree
public static void insertion(TreeNode parentNode,int nodeValue){
  if(nodeValue<parentNode.nodeValue){
  if(parentNode.leftReference!=null)
  insertion(parentNode.leftReference,nodeValue);  // Go more depth to right
  else
  parentNode.leftReference=new TreeNode(nodeValue);
  }
  else if(nodeValue >parentNode.nodeValue){
  if(parentNode.rightReference!=null)
  insertion(parentNode.rightReference,nodeValue);  // Go more depth to left
  else
  parentNode.rightReference=new TreeNode(nodeValue);
  }
  }
   
  // Printing Tree contents
    public static void printTree(TreeNode node) {
        if (node != null) {
            printTree(node.leftReference);
            System.out.println("Node" + node.nodeValue);
            printTree(node.rightReference);
        }
    }
}