This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I am having a bit of trouble with a do while loop. I either can't get it to stop, or it doesn't do exactly as I need it to.
I am using a array called nums to insert random numbers into a binary search tree. Once the tree has been created I need to create a method to search the nums array looking for the node I have asked the user to find in the tree. Once it finds it I need it to output the nodes in that order into a vector.
Sometimes I ask for a node which is not in the tree - this is represented by the outCome. 0 if it is in the tree and 1 if it is not.
I'm attempting to do the first part, if the node is in the tree and have tried creating the following method. Currently it just loops. Can someone tell me how to tell it to stop?
Also, could you help me with telling it to only add nodes until it reaches the node they are searching for, represented by searchValue.
hope someone can help me with this, its driving me and the program loopy
For each value of i from 0 to amount-1, we'll start this do-loop. We'll add an item to optimalSearch, print a message, and then go back to the top of the do-while loop, if i isn't equal to the searchValue. Do you see the problem? The do-while is an infinite loop, because neither searchValue nor i changes during its execution. It will loop forever, and nothing will ever change.
I honestly can't tell what you intend, exactly, so I'm not quite sure what to recommend instead. But I really only see one loop variable here, so it seems like you only need one loop (the for loop), not both.
Thanks for the response. I have tried using the for loop only but it then displays the whole array.
What I am trying to do is, if the nums array contained [15,17,16,25,22] and the user was looking for . I would like to search the array and then stop at 16 and add all those previous numbers to the vector, so it would contain:
including the one they were looking for.
Does that make more sense?
Its just I need to be able to inform the user the best search root if they didn't take it.