Thread: search binary tree

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    search binary tree

    It has been a while since I built a binary tree from scratch so I decided to do it. Everything works fine but this one function. When I enter a number to search it just keeps running and allowing me to keep enter numbers.

    Code:
    void tree::search(int key,Node* leaf)
    {
        
        if (leaf == NULL)
        {
            std::cout<<"The tree is empty\n";
        }
        else if (key==leaf->num)
        {
            std::cout<<"The number "<<leaf->num<<" was found\n";
        }
        else if(key < leaf->num)
        {
             search(key, leaf->left);
        }
        else if(key > leaf->num)
        {
             search(key, leaf->right);
        }
        else
        {
            std::cout<<"The number "<<key<<"was not found\n";
            
        }
    
    }
    
    
    void tree::search(int key)
    {
        Node* current;
        
        current=root;
        
        search(key, current);
        
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    The way you describe it the problem sounds like it could easily be elsewhere. Your search method is a little odd. Is there really a difference between if (leaf == NULL) and a failed search, and is the else ever going to be reached? I don't think so. Besides that being strange, there's nothing wrong with what you posted.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    It sounds like you aren't entering the function. The function itself seems correct (so long as you have the orientation of the > < correct), except for the last else like WhiteFlags said. You can just move the failed search message into the first if though.

    Check to see if you are entering the function. Then if not check the conditions on whatever loop you are using for input to see if they are correct.
    Last edited by Alpo; 08-12-2014 at 05:21 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Lines 20 to 24 are unreachable here. There are no options remaining besides key being either equal to, less than, or greater than leaf->num.
    The solution is to realise that the case actually belongs in lines 4-7, and the test for an empty tree cannot be done properly from the recursive function, nor need it be done so.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. binary search tree
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2002, 07:50 AM