Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

returning boolean in a recursive method - binary search tree

807606Mar 15 2007 — edited Mar 15 2007
Ok, I know that this won't work as it is because java can't reason about the program properly and doesn't think that this will always return a boolean: it will though! This method is recursing through a binary search tree and it will always either find the "word" it is looking for, else cur will equal null. This means that the method will terminate and return a boolean. However java can't spot this and I wouldn't expect it to tbh. If anyone can think of a way to make this work, then please tell me :D

By the way this is my first post here, and I hope I can start to help others soon!
	
/** Recursively searches the tree to see if the word is present */
	private boolean presentRec(String word, HashTableNode cur) {
		countNodes++; // increase the number of times a node has been compared
		if (cur == null) {
			// if we have reached the bottom of the tree and the word is not
			// found then return false
			return false;
		} else if (cur.getWord().equals(word)) {
			// if we have found the word, then it is in the dictionary so return
			// true
			return true;
		} else if (cur.getWord().compareTo(word) < 0) {
			// if the current node is less than the word, then go to the right
			presentRec(word, cur.right);
		} else {
			// otherwise the current node must be greater than the word so we
			// need to go to the left
			presentRec(word, cur.left);
		}
	}
Message was edited by:
oliverj4455

Message was edited by:
oliverj4455
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 12 2007
Added on Mar 15 2007
7 comments
2,778 views