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.

Binary Tree Search[HELP]

hello everyone!
I'm have created a binary tree program where the tree is balanced(but not sorted). The Tree hold unique strings in each nodes. if the user enters a non unique string, the node frequency (a field of the TreeNode class) should be increased by one, but no new node is created.Also, there is an option for searching for a string and getting the frequency if the string is found. In order to do this, I need to use recursion to create a search function in the Tree class (the search should use preorder (compare items in the left subtree, then right))
My CODE below:

Re: Binary Tree Search[HELP]

I noticed when searching for Items in the left subtree (A,B,D,H) the search function works, but for(F,C,E,G) the search doesn't work. Could Anyone please tell me how to fix this logic error?

Think hard about what the search method posted actually does. It will search (and return the result of) the left branch if that child is not null...in affect, never searching the right child of a node if the left is not null. Think about your algorithm, write it out on paper, then translate that to code. One way would be to search the left child, if the value is found return the result, otherwise search the right.

Re: Binary Tree Search[HELP]

Sorry, but this didn't really help. I understand where is my error, I just have no idea how to fix it. I tried making it look like the preorderPrint class, but it doesn't work because I have to return the found TreeNode which makes things complicated.
I tired this, but with "missing return statement" error.

Re: Binary Tree Search[HELP]

I understand what you guys are saying, its just that putting it in code is is a bit tough for me
I have to search in preorder using recursion to compare the strings, and if a match is found, I have to return its corresponding TreeNode.

This is my last try, and it still doesn't work *surprise* . I think the problem is the "return null" in the else statement in "search", but removing it creates this error --> "missing return statement"