Results 1 to 2 of 2
Thread: Help. Binary Search Problem
 11032008, 06:39 AM #1Member
 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:
Java Code:try { Integer freq = M.search(word); M.modify(word,freq+1); } catch(KeyNotFoundException e) { M.insert(word,1); }
Java Code:/** * Inserts a keyvalue 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++; }
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; 11032008 at 07:21 AM. Reason: Solved...
 11032008, 07:19 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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: 09262008, 12:57 AM 
Can anybody help with cuncurrent binary search tree guys)
By danylo in forum Threads and SynchronizationReplies: 1Last Post: 04232008, 07:22 PM 
Binary Search in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04152008, 08:43 PM 
binary search
By tranceluv in forum New To JavaReplies: 10Last Post: 01142008, 08:13 PM 
problem with recursive binary search program
By imran_khan in forum New To JavaReplies: 3Last Post: 08022007, 04:08 PM
Bookmarks