Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-12-2008, 09:40 PM
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-23-2008, 07:22 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 117
danielstoner is on a distinguished road
Synchronize the public methods of the BinarySearchTree class or create a synchronized decorator class ConcurrentBinarySearchTree which keeps inside an instance of BinarySearchTree. It will have the same interface as BinarySearchTree but all the public methods will be synchronized.
__________________
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary Tree Implementation in Java Java Tip java.lang 0 04-16-2008 11:35 PM
Binary Search in Java Java Tip Algorithms 0 04-15-2008 08:43 PM
hi guys!!! surenani Introductions 4 01-23-2008 06:24 AM
binary search tranceluv New To Java 10 01-14-2008 08:13 PM
problem with recursive binary search program imran_khan New To Java 3 08-02-2007 04:08 PM


All times are GMT +3. The time now is 06:16 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org