Posts

Showing posts from October, 2012

Binary tree implementation in java

This post will teach you how to implement a binary tree in java and perform the following functions on it. insert - Inserts the elements to binary tree printInOrder - To print the inorder elements of the binary tree maxDepth and minDepth - To find whether a tree is balanced or not commonAncestor - To find the common ancestor between two nodes findSum - to find all paths whose sum is equal to the given sum addToTree - To insert elements to the tree in such a way that there will be minimum number of leaves. findLevelLinkedList - To find elements in each level The java program is as follows: package com.algorithm; import java.util.ArrayList; import java.util.LinkedList; public class binaryTree { public static void main(String args[]) { new binaryTree().run(); } public static class Node { Node left; Node right; int value; public Node( int value) { this.value = value; } } public void run() { Node ro

Java program to check if a string has unique characters without using additional space

This post is about a java program to check whether a string has unique characters without using additional memory space. The program is implemented with the help of bit vector. The program is as follows: package com.programs; /* * Program to check if the string contains duplicate characters in an efficient way. */ public class strdups { public static void main(String args[]) { String textWithDuplicateCharacters = "aabbcdefghij" ; String textWithUniqueCharacters = "abcdefghijklmnopqrstuvwxyz" ; printOutput(isUnique(textWithDuplicateCharacters)); printOutput(isUniqueEfficient(textWithDuplicateCharacters)); System.out.println(); printOutput(isUnique(textWithUniqueCharacters)); printOutput(isUniqueEfficient(textWithUniqueCharacters)); } public static boolean isUnique(String str) { boolean[] char_set = new boolean[256]; for ( int i = 0; i < str.length(); i++) { int val = str.charAt(i); if (cha

Linked List Implementation in Java

Hi All, for some time we can take a break from Android. Here , I am presenting the implementation of linked list in java, which is very useful in industries as well as helps in programming interview questions. I am including few important methods of linked lists as well - insert - To insert a node to linked list display - to display all the nodes. delete - to delete a node from linked list. NthToLast - to print the nth to last node deletenode - to delete a node from middle by giving the data. deleteDups - to delete duplicate in linked list (using hashtable) deleteDups1 - to delete duplicate in linked list without using additional memory space. addList - to add to separate linked lists node by node FindBeginning - This method will check whether a loop is present and returns the node where the loop starts, else return null. I am publishing the code also in this blog, but I recommend you to use this only as a reference. Happy coding. package com.linkedlist; im

Replicated Key Value storage using Android

Image
This was my fourth project in Android.  T h is p r o j e c t i n vol v es d e sig n i n g a sim p le d i s t ri b u t e d r e p lica t e d - va l u e s t o r a g e b as e d o n D ynam o . It is a sim p l i f ied versi o n of D ynam o , w h i c h i n c l u d es p a r t i t i o n i n g, r e p lica t i n g a n d h a nd l i n g f ail u r es. T h e Si m p le D y n amo is bu i l t o n t op o f th e a n d r o i d c ont e n t p r o v i d e r . System Architecture       Design Overview T h e d esign is p r e t t y si m p le as se e n f r o m t h e a b o v e d iagra m . The D y n a m o ( C o n t e n t P r ov i d e r ) h as a s e rver th r e a d a n d c lie n t t h r e a d s w h i c h c om m un i c a t e b y se n d i n g m e ssages. Qu o r u m ma n ager at t h e c li e n t s i d e w ill c h eck w h eth e r r e a d er qu o r u m s i z e a n d w ri t er q u o r u m si z e is  sa t is f i e d i n t h e r