The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

binarysearchtree traversing

Hi, I'm trying to figure out how to do this contains method. everything that's in the method is my code. My thinking is: start at root and go in Inorder traversal and use .equals(). Should I even do that? is there a way I can compare the data, since BST will have lower value to left and higher in right child? I'm lost.

/**
Searches for a given element in the binary search tree
@param someElement element to be searched
@return true - if someElement is found in the tree; false otherwise
*/// Complexity: O(h) - where h is the height of the tree. In the worst case it could be O(n). But on average// we can expect a complexity of O(log n)publicboolean contains( E someElement){
Node node1 = root;if(root == someElement)returntrue;if(node1.left!=null){boolean result = someElement.equals(node1.left);if(result ==false){
contains((E) node1);}}}