Posts

Immutable Class - Java

Class should be made final - avoiding overriding of methods by sub classes All fields should be final and private - avoiding fields access outside the class and avoiding the  changes in the field values No setters Make sure that mutable fields getters will return a defensive copy from their getter method, there by avoiding the state to be mutated public final class Immutable {     private final Integer immutableinteger;     private final String immutableString;     private final Date mutableDate;     public Immutable(Integer immutableinteger, String immutableString, Date mutableDate) {         this.immutableinteger = immutableinteger;         this.immutableString = immutableString;         this.mutableDate = mutableDate;     }     public Integer getImmutableinteger() {         return immutableinteger; ...

Singleton Class - Java

public class Singleton {     private static Singleton singleton;     //1. private prevents the instantiation of this object from other clesses     private Singleton(){     }     // 2. static word makes the method class level     // 3. synchronized avoids the multiple instantiation of object during multiple concurrent calls     public static synchronized Singleton getSingletonInstance(){         if(singleton == null){             singleton = new Singleton();         }         return singleton;     }     //4. avoiding cloning     public Object clone() throws CloneNotSupportedException{         return new CloneNotSupportedException();     } } public class SingletonDemo {     public static void main(String args[]){       ...

Spiral order traversal of a tree - Java

public class TreeTraversal { public static void main(String[] args) { Node node = new Node(40); node.left = new Node(35); node.right = new Node(50); node.left.left = new Node(30); node.left.right = new Node (38); node.left.left.right = new Node(33); node.left.left.left = new Node(28); spiralOrder(node); } public static void spiralOrder(Node root){ if (root == null) return; boolean dir = false; int height = height(root); for (int i = 1; i <= height; i++) { printSpiralLevelOrder(root, i, dir); dir = !dir; } } public static void printSpiralLevelOrder(Node root, int level, boolean dir){ if(root == null) return; if(level == 1) System.out.print(root.value); else if(level > 1){ if(dir){ printSpiralLevelOrder(root.left, level-1, dir); printSpiralLevelOrder(root.right, level-1, dir); } else { printSpiralLevelOrder(root.right, level-1, dir); printSpiralLevelOrder(root.left, level-1, dir); ...

Level Order Traversal using Queue - Java

public class LevelOrderUsingQueue { public static void main(String[] args) { Node node = new Node(40); node.left = new Node(35); node.right = new Node(50); node.left.left = new Node(30); node.left.right = new Node (38); node.left.left.right = new Node(33); node.left.left.left = new Node(28); levelOrderWithQueue(node); } public static void levelOrderWithQueue(Node root){ Node tempNode; Node[] queue = new Node[20]; int front = 0, rear = 0; tempNode = root; while(tempNode != null){ System.out.println(tempNode.value); if(tempNode.left != null){ queue[rear] = tempNode.left; rear++; } if(tempNode.right != null){ queue[rear] = tempNode.right; rear++; } front++; tempNode = queue[front - 1]; } } }

Level Order Traversal of a binary tree - Java

package tree; public class TreeTraversal { public static void main(String[] args) { Node node = new Node(40); node.left = new Node(35); node.right = new Node(50); node.left.left = new Node(30); node.left.right = new Node (38); levelOrder(node); } public static void levelOrder(Node root) { if(root == null) return; int height = height(root); for (int i = 1; i <= height; i++) { printLevelOrder(root, i); } } public static void printLevelOrder(Node node, int level){ if(node == null) return; if(level == 1) System.out.print(node.value); else if(level > 1){ printLevelOrder(node.left, level-1); printLevelOrder(node.right, level-1); } } public static int height(Node node) { if(node == null) return 0; int lHeight = height(node.left); int rHeight = height(node.right); if(lHeight > rHeight) return lHeight + 1; else return rHeight + 1; } } class Node{ Node left; Node righ...

Valid Parentheses - Java

public class PrintParanthesis { public static void main(String[] args) { printParanthesis(3,3,""); } private static void printParanthesis(int leftRemain, int rightRemain, String currentString) { if(rightRemain==0) { System.out.println(currentString); return; } if(leftRemain>0){ printParanthesis(leftRemain-1, rightRemain, currentString+"("); if(leftRemain < rightRemain) printParanthesis(leftRemain, rightRemain-1, currentString+")"); } else printParanthesis(leftRemain, rightRemain-1, currentString+")"); } } Explanation: pP(2,3,"(")--pP(1,3,"((")--pP(0,3,"(((")--pP(0,2,"((()")--pP(0,1,"((())")--pP(0,0,"((()))") ---> "((()))"           |                     |           |                    pP(1,2,"(()")--pP(0,2,"(()(")--pP(0,1,"(()()")--pP(0,0,"(()())") ---...

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; } }