Friday, 11 November 2016

Custom similar tree implementaition : Tree Data Structure in OOPS Java

class TOne{

public int m;

public TOne tLeft;

public TOne tRight;

public TOne(){

}


public void transverse(){
System.out.println("value "+m);

if(tLeft!=null){
System.out.println("Left tree ");
tLeft.transverse();
}


if(tRight!=null){

System.out.println("Right tree ");

tRight.transverse();

}
}
public boolean insertValue(int vale){



if(m==0){

m=vale;
return true;

}else if(m!=0){
if(vale<m){

if(tLeft==null){
tLeft=new TOne();
}

return tLeft.insertValue(vale);

}else if(vale>m){

if(tRight==null){
tRight=new TOne();
}

return tRight.insertValue(vale);

}else {
System.out.println(" value equal.");
return false;
}
}

return false;
}

public boolean search(int val){
if(m==val){
return true;
}else if(tLeft!=null){
return tLeft.search(val);
}else if(tRight!=null){
return tRight.search(val);
}else{
return false;
}
}

public static void main(String arr[]){
TOne one=new TOne();

System.out.println(""+one.insertValue(10));


System.out.println(""+one.insertValue(20));


System.out.println(""+one.insertValue(16));

System.out.println(""+one.insertValue(12));

System.out.println("");

one.transverse();

System.out.println("search 10 : "+one.search(10));


System.out.println("search 20 : "+one.search(20));


System.out.println("search 16 : "+one.search(16));


System.out.println("search 4 : "+one.search(4));

System.out.println("search 4 : "+one.size());

}

public int size(){

int size=1;



if(tLeft!=null && tRight!=null){

int leftSize=tLeft.getSize(size);
int rightSize=tRight.getSize(size);

if(leftSize>rightSize){
return leftSize;
}else if(leftSize<rightSize){
return rightSize;
}else if(leftSize==rightSize){
return leftSize;
}

return leftSize;

}else if(tLeft!=null && tRight==null){
return tLeft.getSize(size);
}else if(tLeft==null && tRight!=null){
return tRight.getSize(size);
}else{
return size;
}


}

public int getSize(int sizze){
if(tLeft==null && tRight == null){
sizze+=1;
return sizze;
}else if(tLeft!=null && tRight==null){
sizze+=1;
return tLeft.getSize(sizze);
}else   if(tLeft==null && tRight!=null){
sizze+=1;
return tRight.getSize(sizze);
}else{

sizze+=1;

int leftSize=tLeft.getSize(sizze);
int rightSize=tRight.getSize(sizze);

if(leftSize>rightSize){
return leftSize;
}else if(leftSize<rightSize){
return rightSize;
}else if(leftSize==rightSize){
return leftSize;
}

return leftSize;
}

}


}

Sunday, 6 November 2016

Java - dead lock on String

class DeadLockString{

private static final String text1="this is text1";

private static final String text2="this is text2";

public static void main(String arr[]){

DeadLockString obj=new DeadLockString();

ThreadModelOne one=new ThreadModelOne();


ThreadModelTwo two=new ThreadModelTwo();

one.start();

two.start();
}

static class ThreadModelOne extends Thread{

public void run(){
synchronized(text1){
System.out.println("Lock aquired by thread model one in text1 ");
try{
Thread.sleep(2000);
}catch(Exception e){
System.out.println(e);
}

System.out.println("Waiting for lock on text2");

synchronized(text2){
System.out.println(" Locked aquired in text 1 and text 2 by thread model one.");
}
}
}
}


static class ThreadModelTwo extends Thread{

public void run(){
synchronized(text2){
System.out.println("Lock aquired by thread model one in text2 ");
try{
Thread.sleep(2000);
}catch(Exception e){
System.out.println(e);
}

System.out.println("Waiting for lock on text1");

synchronized(text1){
System.out.println(" Locked aquired in text 1 and text 2 by thread model two.");
}
}
}

}
}