Question

everything in this program works, except the "search" function, which returns true or false depending on whether or not a word is in a binary search tree. I have an if statement in "main" that tells the user whether the word is in the tree or not. As I step through the debugger, it is saying the function returns true when it should, but it is skipping straight to the else part of the if statement in "main". Why is this? It seems to work perfectly, except when it is "viewed" by the if statement. For example, "test.txt" contains the words "This Program Works" with each word on a separate line. I know it is adding them to the tree because when i "printAll" those words come out...

BinarySearchTree

#include "BinarySearchTree.h"
#include <iostream>
using namespace std;

bool BinarySearchTree::search(string target) {

    if(target == data) {
        return true;
    }
    else if(target < data) {
        if(left == nullptr) {
            return false;
        }
        else {
            left->search(target);
        }
    }
    else if(target > data) {
        if(right == nullptr) {
            return false;
        }
        else {
            right->search(target);
        }
    }
}

main

#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include "BinarySearchTree.h"

using namespace std;

int main(int argc, char** argv) {
    //pull in text from a .txt file, one word at a time, add each to the      BinarySearchTree.
    //loop and let the user enter a word, let the user know if the word was in the file or not.  Have the user type 0 to quit.
    cout<<"Search: ";
    string target;
    getline(cin,target);

    while(target != "0") {
    ///////////////////MAIN ISSUE HERE/////////////////////////////////
        if(tree.search(target) == true) {
            cout<<"\""<<target<<"\""<<" found in file."<<endl;
        }

        else {
            cout<<"\""<<target<<"\""<<" not found in file."<<endl;
        }
  ////////////////////////////////////////////////////////////////////
        cout<<"Search: ";
        getline(cin,target);

    }
    //now that the user is content, print all words that were in the text file, in lexicographical order.
    tree.printAll();
    cout<<endl;

    return 0;
}

I know for a fact everything else works well.

Was it helpful?

Solution

You're not returning anything here:

    else {
        left->search(target);
    }

it should be:

    else {
        return left->search(target);
    }

You have the same error with right->search(target).

OTHER TIPS

When you call search() recursively, you're never returning the value. Therefore most of your results are simply being thrown away, and often your function will return an undefined value when it simply "falls off the end."

You are missing return statements in your search function:

bool BinarySearchTree::search(string target) {

    if(target == data) {
        return true;
    }
    else if(target < data) {
        if(left == nullptr) {
            return false;
        }
        else {
            return left->search(target);
        }
    }
    else if(target > data) {
        if(right == nullptr) {
            return false;
        }
        else {
            return right->search(target);
        }
    }
}

Turning on/listening to compiler warnings would catch this sort of thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top