Saturday, September 8, 2012

Basic Binary Tree Operations

package com.abhi.Tree;


class Node {
   
    int data;
    Node left;
    Node right;
}
public class createABinaryTree {

    static Node root = null;
    public static Node addNode(int data, Node root){
        if(root == null){
           
            Node t = new Node();
            t.data = data;
            //root = t;
            return t;
        }
        if(root.data > data)
                root.left = addNode(data,root.left);
        else
            root.right = addNode(data,root.right);
       
        return root;
       
    }
   
   
    public static void main(String st[]){
        Node root = addNode(6, null);
        addNode(3, root);
        addNode(10, root);
        addNode(11,root);
        addNode(12,root);

        //addNode(5,root);
        //addNode(13,root);
        //addNode(-1,root);
        System.out.println(root);
        System.out.println("Number of Nodes"+numberOfNodes(root));
        System.out.println("Max Depth"+maxDepth(root));
        inOrder(root);
        pathSum(root, 0);
       
        doubleTree(root);
        System.out.println("-----");
        inOrder(root);
    }
   
     static int numberOfNodes(Node n){
       
        if(n== null)
            return  0;
        return(1+numberOfNodes(n.left)+numberOfNodes(n.right));
       
    }
   
     static int maxDepth(Node n){
         if(n == null)
             return 0;
         int leftLen = maxDepth(n.left);
         int rightLen = maxDepth(n.right);
       
         if(leftLen > rightLen){
             return leftLen+1;
         }else
             return rightLen+1;
     }
   
 static void  inOrder(Node n){
     if(n== null)
         return;
     inOrder(n.left);
     System.out.print(n.data+" ");
     inOrder(n.right);
 }
 static void pathSum(Node n , int sum){
   
   
     if(n== null)
         return;
     if(n.left == null && n.right == null ){
         System.out.println(sum+n.data);
         return;
     }
    // if(n.left!=null)
         pathSum(n.left, sum+n.data);
     //if(n.right!=null)
     pathSum(n.right, sum+n.data);
 }

 static void doubleTree(Node n){
   
     if(n == null )
         return;
   
   
     doubleTree(n.left);
     Node temp = new Node();
     temp.data = n.data;
     Node tt = n.left;
     temp.left = tt;
     n.left = temp;
   
   
 }


}

Wednesday, September 5, 2012

Rope Puzzle

You are given two ropes and a lighter. This is the only equipment you can use. You are told that each of the two ropes has the following property: if you light one end of the rope, it will take exactly one hour to burn all the way to the other end. But it doesn't have to burn at a uniform rate. In other words, half the rope may burn in the first five minutes, and then the other half would take 55 minutes. The rate at which the two ropes burn is not necessarily the same, so the second rope will also take an hour to burn from one end to the other, but may do it at some varying rate, which is not necessarily the same as the one for the first rope. Now you are asked to measure a period of 45 minutes. How will you do it?






Ans :Light both ends of rope A and one end of rope B. After 30 minutes, rope A will be completely burned up and there will be 30 minutes of rope B left. Light the other end of rope B; it will burn up in 15 minutes. Total time elapsed since starting the ropes on fire: 45 minutes.

Sunday, August 26, 2012

How to Reverse a String


public class ReverseString {
   
   
    public static void main(String st[]){
       
        String s = "12345678";
        char a[] = s.toCharArray();
        int l = s.length()-1;
        char temp;
        int i = 0;
        while(i <= l/2){
           
            temp = a[i];
            a[i] = a[l-i];
            a[l-i] = temp;
            i++;
           
        }
        System.out.println(a);
    }

}

Friday, August 24, 2012

how to convert decimal numbers to roman numerals


public class LetterToRoman {

    private static final String[] RCODE = {"M", "CM", "D", "CD", "C", "XC", "L",
        "XL", "X", "IX", "V", "IV", "I"};
private static final int[]    BVAL  = {1000, 900, 500, 400,  100,   90,  50,
        40,   10,    9,   5,   4,    1};

public static void main(String st[]){
   
    int num = 11;
    int i = 0;
    int gnum = num;
    while (gnum!=0){
       
        while(!((BVAL[i]<=gnum) )){
            i++;
        }
            System.out.print(RCODE[i]);
            gnum = gnum - BVAL[i];
       
    }
}
}

Wednesday, August 22, 2012

Write a program which returns true if the given string contains the consecutive repeated substring .Ex-adabcabcd here abc is consecutive repeated substring.

import java.util.ArrayList;
import java.util.List;


public class Consecutiverepeat {

  
    static String s = "daradarabcece";
    static char[] ch = s.toCharArray();
  
    public static void main(String str[]){
      
        int i=1;
        while(i<ch.length){
            List<Integer> ls = isInPastArray(i);
            if(ls.size()!=0){
                for(Integer intg: ls){
                    int leg = i-intg;
                    System.out.println(leg);
                  
                    if((i+leg-1 <ch.length )&&(ch[i-1]==ch[i+leg-1])){
                        if(compare(i, leg)){
                            System.out.println("true");
                            return;
                        }
                    }                  
                }
            }
            i++;
        }
    }
    static boolean compare(int i,int len){
      
        int s1 = i;
        int s2=i-len;
        while(len>=1){
            System.out.println("ch["+s1+"]"+ch[s1]+"  ch["+s2+"]"+ch[s2]);
            if(ch[s1] != ch[s2])
                return false;
        s1++;
        s2++;
        len--;
        }
      
      
        return true;
      
    }
  
    static List<Integer> isInPastArray(int p){
      
        char ch1 = ch[p];
        List <Integer> list = new ArrayList<Integer>();
        int i =0;
        int pi = 0;
        while(i<p){
            if(ch[i] == ch1){
                list.add(i);
            }
            i++;
        }
      
        return list;
    }
}

Tuesday, July 10, 2012

What is the usage of the field serialVersionUID (Java Serial Version Id) in Java ?


Java Serial Unique Identifier (or serialVersionUID) is used for versioning of Java Classes. Whenever you perform validation it is recommended that you declare value of serialVersionUID manually (Otherwise Java creates it on it's own while compiling). This ensures that if we make any changes in the Java class, we could avoid a "ClassNotCompatibleError" during runtime while de-serializing the class.

Monday, July 9, 2012

Initialization-on-demand

public class Something {
        private Something() {
        }
 
        private static class LazyHolder {
                public static final Something INSTANCE = new Something();
        }
 
        public static Something getInstance() {
                return LazyHolder.INSTANCE;
        }
}