Results 1 to 2 of 2
  1. #1
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Default [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);
    		    }
    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:
     /**
         * 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++;
        }
    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 ();
        }
    Does anyone have any idea why the search function can not find the key but the insert function does find the key?
    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...

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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

  1. Binary Search searching news Article
    By peterdfl in forum New To Java
    Replies: 0
    Last Post: 09-25-2008, 11:57 PM
  2. Can anybody help with cuncurrent binary search tree guys)
    By danylo in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 06:22 PM
  3. Binary Search in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:43 PM
  4. binary search
    By tranceluv in forum New To Java
    Replies: 10
    Last Post: 01-14-2008, 07:13 PM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •