Pages

Sunday, 5 June 2016

Find the Maximum Depth or Height of a Tree

Call the depth function recursively
1. if Sub tree/tree is empty then return 0
2. else get maximum depth in left sub tree and right sub tree + 1.

class Tree {
   int value;
   Tree left;
   Tree right;
   public Tree (int value) {
      this.value = value;
   }
}

public classFindMaxDepth {

   private static int depth(Tree tree) {
      if(tree==null) {
         return 0;
      } else {
         return Math.max(depth(tree.left), depth(tree.right)) + 1;
      }
   }

   public static voidmain(String[] args) {
      Tree root = new Tree(1);
      root.left = new Tree(2);
      root.right = new Tree(3);
      root.left.left = new Tree(7);
      root.left.right = new Tree(6);
      root.right.left = new Tree(5);
      root.right.right = new Tree(4);
      root.left.left.left = new Tree(8);
      root.left.left.right = new Tree(9);
      root.left.left.left.left = new Tree(11);
      root.left.left.left.right = new Tree(10);

      System.out.println(depth(root));
   }
}

Level order traversal in spiral form

1. Use two stacks.
      One stack for print the elements from left to right,
      Other stack to print the elements from right to left.

2. In every iteration, we have nodes of one level,
   We will print the nodes and keep pushing the child nodes in another stack.

import java.util.Stack;
class Tree {
   int value;
   Tree left;
   Tree right;
   public Tree (int value) {
      this.value = value;
   }
}

public class PrintSpriralTree {
   public static void main(String[] args) {
      Tree root = new Tree(1);
      root.left = new Tree(2);
      root.right = new Tree(3);
      root.left.left = new Tree(7);
      root.left.right = new Tree(6);
      root.right.left = new Tree(5);
      root.right.right = new Tree(4);
      root.left.left.left = new Tree(8);
      root.left.left.right = new Tree(9);
      root.left.left.left.left = new Tree(11);
      root.left.left.left.right = new Tree(10);

      printSpiral(root);
   }

   private static void printSpiral(Tree root) {

      Stack<Tree> s1 = new Stack<Tree>();
      Stack<Tree> s2 = new Stack<Tree>();
      s1.add(root);
      while(!s1.isEmpty() || !s2.isEmpty()) {
        
         while(!s1.isEmpty()) {
            Tree top = s1.pop();
            System.out.print(top.value+" ");
           
            if(top.right!=null) {
               s2.add(top.right);
            }
           
            if(top.left!=null) {
               s2.add(top.left);
            }
         }
        
         while(!s2.isEmpty()) {
            Tree top = s2.pop();
            System.out.print(top.value + " ");
           
            if(top.left!=null) {
               s1.add(top.left);
            }
           
            if(top.right!=null) {
               s1.add(top.right);
            }           
         }
      }
   }
}

Spiral order traversal will take O(n) time and O(n) extra space.


Saturday, 4 June 2016

Find the maximum repeating number in O(n) time and O(1) extra space

public classFindMaxRepeat {

   private static int maxRptElmt(int[] array, int k) {
      for(int i=0;i<array.length;i++) {
         array[array[i]%k] += k;
      }

      int max = Integer.MIN_VALUE;
      int index = -1;

      for(int i=0;i<array.length;i++) {
         if(max<array[i]) {
            max = array[i];
            index = i;
         }
      }

      /* restore the original array. */
      for (int i = 0; i< array.length; i++) {
         array[i] = array[i]%k;
      }
      return index;
   }
  
   public static voidmain(String[] args) {
      int[] array = {1,2,3,4,5,6,7,1,2,3,4,5,6,9,1,2,2};
      int k = array.length;
      int maxRpt = maxRptElmt(array, k);
      System.out.println(maxRpt);
   }
}

Wednesday, 1 June 2016

Element : load-on-startup


     <servlet>
           <servlet-name>rest</servlet-name>
           <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
           </servlet-class>
           <load-on-startup>1</load-on-startup>
     </servlet>

Data Type: integer

Value >= 0: servlet is loaded when the web-app is deployed or when the server starts.
Value < 0: servlet is loaded whenever the container feels like.

The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application.

The optional contents of these elements must be an integer indicating the order in which the servlet should be loaded.

If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses.

If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.