Results 1 to 2 of 2
Thread: Help. Binary Search Problem
- 11-03-2008, 05:39 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 43
- Rep Power
- 0
[SOLVED] Help. Binary Search Problem
Hello, I need to make a binary search tree for another program that counts words in documents. When the word counting program gets to the lines:
The search function in my binary tree class throws a Key not found exception so it than tries to add it from scratch but my insert function does find it and throws KeyFound Exception. Here are the functions from my program:Java Code:try { Integer freq = M.search(word); M.modify(word,freq+1); } catch(KeyNotFoundException e) { M.insert(word,1); }
Java Code:/** * Inserts a key-value pair into the map. * @param key The key to be inserted. * @param value Key's corresponding value. * @throws KeyFoundException If a matching key is * already present in the map */ public void insert (K key, V value) throws KeyFoundException { if (isEmpty()) {head = new Node <K, V> (key, value, null, null);} else { Node <K, V> current = head; while (true) { if ((current.key).compareTo(key) == 0) {throw new KeyFoundException ();} else if ((current.key).compareTo(key) > 0) { if (current.rightNode != null) {current = current.rightNode;} else {current.rightNode = new Node <K, V> (key, value, null, null);} } else if ((current.key).compareTo(key) < 0) { if (current.leftNode != null) {current = current.leftNode;} else {current.leftNode = new Node <K, V> (key, value, null, null);} } } } treeSize++; }Does anyone have any idea why the search function can not find the key but the insert function does find the key?Java Code:/** * Returns the value corresponding to key. * @param key The key to search for in the map. * @returns V The value corresponding to key. * @throws KeyNotFoundException If key is not found * in the map. */ public V search (K key) throws KeyNotFoundException { Node <K, V> current = head; while (current != null) { if ((current.key).compareTo(key) == 0) { return current.value;} else if((current.key).compareTo(key) > 0) { current = current.rightNode;} else if((current.key).compareTo(key) < 0) { current = current.leftNode;} } throw new KeyNotFoundException (); }
Thank You for any help.
EDIT: I didnt have a return in my Search function, so it would add it, loop again and find the same entry. Silly mistake.Last edited by Krooger; 11-03-2008 at 06:21 AM. Reason: Solved...
- 11-03-2008, 06:19 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have question here and you solved it your self. It's nice. So you no need to delete it, because you can help to your community on that. Post the way you solved it, then if someone else have the same problem, he/she can refer you thread and solve there questions.
Similar Threads
-
Binary Search searching news Article
By peterdfl in forum New To JavaReplies: 0Last Post: 09-25-2008, 11:57 PM -
Can anybody help with cuncurrent binary search tree guys)
By danylo in forum Threads and SynchronizationReplies: 1Last Post: 04-23-2008, 06:22 PM -
Binary Search in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:43 PM -
binary search
By tranceluv in forum New To JavaReplies: 10Last Post: 01-14-2008, 07:13 PM -
problem with recursive binary search program
By imran_khan in forum New To JavaReplies: 3Last Post: 08-02-2007, 03:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks