Thursday 27 June 2013

What is Race Condition in java? (or) How does Race condition occur in java?


  • Race condition in java is a type of concurrency issue caused in our program because of parallel execution of multiple threads.
  • A race condition occurs when two or more threads can access shared data and they try to change it at the same time i.e. both threads are "racing" to access/change the data.
  • Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. 
  • Since Java is a multi-threaded programming language hence risk of Race condition is higher in Java which demands clear understanding of what causes a race condition and how to avoid that.

Wednesday 26 June 2013

Code for Binary Search Tree

public class BinaryTree {

public int var;
public BinaryTree rightNode;
public BinaryTree leftNode;

public BinaryTree(){

}
public BinaryTree(int var){
this.var=var;
this.rightNode=null;
this.leftNode=null;
}

public void insert(BinaryTree tree, BinaryTree node){
int temp=tree.var;
int temp1=0;
if(node!=null){
temp1=node.var;
}
if(node == null){
//First Node Do nothing
}else if(temp < temp1 && tree.rightNode==null){
tree.rightNode = node;
}else if(temp > temp1 && tree.leftNode == null){
tree.leftNode = node;
}else if(temp < temp1 && tree.rightNode != null){
insert(tree.rightNode,node);
}else if(temp > temp1 && tree.leftNode != null){
insert(tree.leftNode,node);
}
}

public void display(BinaryTree tree){
if(tree == null){
System.out.println("No nodes to display");
}else if(tree.leftNode==null && tree.rightNode == null){
System.out.println(tree.var);
}else if(tree.leftNode==null && tree.rightNode != null){
System.out.println(tree.var);
display(tree.rightNode);
}else if(tree.leftNode != null && tree.rightNode == null){
display(tree.leftNode);
System.out.println(tree.var);
}else if(tree.leftNode != null && tree.rightNode != null){
display(tree.leftNode);
System.out.println(tree.var);
display(tree.rightNode);
}
}

//Main method can be written in your own way. I have used a unique object which i have not used. But this is not the correct approch.
public static void main(String args[]){
BinaryTree obj=new BinaryTree();
BinaryTree bt=new BinaryTree(8);
obj.insert(bt, null);
BinaryTree bt1=new BinaryTree(10);
obj.insert(bt, bt1);
BinaryTree bt2=new BinaryTree(5);
obj.insert(bt, bt2);
BinaryTree bt3=new BinaryTree(7);
obj.insert(bt, bt3);
BinaryTree bt4=new BinaryTree(4);
obj.insert(bt, bt4);
obj.display(bt);
obj.linkedListConversion(bt);
}
}

Algorithm for constructing a BinarySearchTree

Step 1: Creating Varibales
1.1: Create a integer variable to store the value
1.2: Create two references for the same class to point to leftNode and rightNode
Step 2: Initializing constructor
2.1: Create a constructor which sets the value for a node and make rightNode as null and leftNode as
null
Step 3: Mapping the node
3.1: If this the the first node then mapping is not required
3.2: If already node is available get value of the currentNode in the tree and compare with the value of
the new node
3.3: If the value is less than the currentNode check whether the leftNode is null or has any other node
3.3.1: If the leftNode has a value then perform 3.2 again
3.3.2: Else if the leftNode is null then insert the node here by pointing the null value to the
newNode
3.4: Else if the value is greater than the currentNode check whether the rightNode is null or has any
other node
3.4.1: If the rightNode has a value then perform 3.2 again
3.4.2: Else if the rightNode is null then insert the node here by pointing the null value to the
newNode