View Single Post
  #1 (permalink)  
Old 03-12-2008, 09:40 PM
danylo danylo is offline
Member
 
Join Date: Mar 2008
Posts: 1
danylo is on a distinguished road
Can anybody help with cuncurrent binary search tree guys)
Good afternoon, I'v got a problem with concurency in java program, wonder wether somebody can give me a couple of hints on how to improve the program sothat it could be cuncurrent and run cuncurrent processes.
Briefly about the task- i have to construct a binary search tree and implement it using cuncurency. I managed to write program in a slightly usual way, and could not do much about ncurrent bit. So thats the shorten code so far

class Node {
Object data;
Node left, right;

public Node (Object d, Node Lef, Node Rig) {

data = d;
left = Lef;
right = Rig;
}
public synchronized void setLeft(Node obj) { left = obj;}
setRight()
getLeft() - other methods in my Node class
getData()
setData()


}

public class BinarySearchTree {
private Node root;
public static void main(String arg[]){

}

BinarySearchTree(){
root = null;

}

boolean isEmpty(){
if(root==null) return true;
return false;
}

boolean search(Object x) {
return search(root, x);
}

boolean search(Node T, Object x) {
if (T == null) return false;
if (T.getData().equals(x)) return true;
if (((Comparable)x).compareTo(T.getData()) < 0) {
return search(T.left, x);
}
else {
return search(T.right, x);
}
}


boolean insert(Object x) {
if(search(x)) return false;
if (isEmpty()) {
root = new Node(x, null, null);
return true;
}
else insert(root, x);
return true;
}

boolean insert(Node t , Object x) {
if( t == null )
t = new Node( x , null , null );
else if(((Comparable)x).compareTo(t.getData()) < 0) {
if(t.getLeft() == null ) {t.left = new Node(x , null , t);}
else {insert(t.getLeft() , x);} }
else if(((Comparable)x).compareTo(t.getData()) > 0){
if(t.getRight() == null ) {t.right = new Node(x , t , null);}
else {insert(t.getRight(), x);}

} return true;
}


public boolean delete(Object x) {

delete(root, x);
return true;
}

private Node delete(Node T, Object x) {
if (T == null) return null;
if (x.equals(T.getData())) {
if (T.left == null && T.right == null) return null;
if (T.left == null) return T.right;
if (T.right == null) return T.left;

Node tmp = smallest (T.right);
T.setData( tmp.getData() );

T.right = (delete(T.right, tmp.getData()) );
return T;

}
else if (((Comparable)x).compareTo(T.getData()) < 0) {
T.left = delete(T.left, x);
return T;
}
else {
T.right = delete(T.right, x);
return T;
}
}
private static Node smallest(Node T)
{
if (T.left == null) return T;
else return smallest(T.left);
}
}

The program complis and runs and pretty much does the job. And i dont kno how to upgrade it and there quite a number of cuncurrency methods at which im not good at all. Can somebody give me some hins on how to start and which concurny method is more appropriate for the task

Thank You
Reply With Quote
Sponsored Links