简体   繁体   中英

Binary Search Tree Display issue

I have a lab assignment that I have been working on for the last couple of weeks and I am stuck and desperately need help as this will be about 50% of the final project. The assignment is to create a Binary Search Tree in C++. We have to read in the words from the Declaration of Independence into a Binary Search Tree. I have my search and insert methods "working properly" meaning that they aren't throwing any errors. The problem I am having is displaying the BST to figure out if everything is working properly. The display method should be called by the overloaded operator >> to display the tree. The errors I keep getting are:

"error C3867: 'BST::display': function call missing argument list; use '&BST::display' to create a pointer to member"

and the other one is

"error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)."

Last time I rebuilt the program it shows "ItelliSense: a pointer to a bound function may only be used to call the function."

#include "stdafx.h"
#include <string> 
#include <iostream> 
#include <fstream> 
using namespace std; 

template <typename T> 
class BST{
private:
    struct node {
        T data; 
        struct node* left; 
        struct node* right; 
    };
    node* root; 

public: 
    BST()
    {
        root = NULL; 
    }
    bool isEmpty() const { return root == NULL; }
    ~BST(); 

template <typename T> 
void insert(T d)
{
    node* t = new node; 
    node* parent; 
    t->data = d; 
    t->left = NULL; 
    t->right = NULL; 
    parent = NULL; 

    if (isEmpty()) root = t; 
    else {
        node* current; 
        current = root; 
        while (current)
        {
            parent = current; 
            if (t->data > current->data) current = current->right; 
            else current = current->left; 
        }
        if (t->data < parent->data)
            parent->left = t;
        else
            parent->right = t; 
    }
}


template<typename T>
bool search(T d)
{
    if (root == NULL)
        return false;
    else if (d == root->item) {
        return true;
    }
    else if (d < root->item) {
        return search(root->left, d);
    }
    else {
        return search(root->right, d);
    }
}


template<typename T>
void display(node *p, std::ostream& os)
{
    if (p != NULL)
    {
        if (p->left) display(p->left); 
        os << " " << p->data << " "; 
        if (p->right) display(p->right); 
    }   
} 

template<typename T> friend std::ostream& operator<<(std::ostream& os, const BST<T>& obj)
{
    obj.display(os, obj.root);
}


};

int main( )
{
    BST<string> s; 
    ifstream inFile; 
    string word, tmp; 
    string filename = "Independence.txt"; 


    ifstream fstr(filename.c_str()); 
    while (inFile >> word) {
        s.insert(word); 
    }
    inFile.close(); 

    cout << s << std::endl; 

    cout << "Search for: ";
    cin.ignore(1);
    cin >> tmp;
    s.search(tmp);


    return 0;
};
template<typename T>
void display(node *p)

This takes a parameter with a node pointer.

Then you call it later with:

BST<string> s;
cout << s.display << endl; 

But you don't actually pass it any parameters here. The compiler then complains it can't figure out how to call that function. Because that function has a return type of void it can't figure out how to print it as you aren't actually returning anything for cout to print. You will want to fix both these problems before you move on, given that you said it's an assignment I'll leave you to figure out how to do that :).

There seem to be a number of problems with your code. I suspect display and search should not be separately templated - this would actually be a template member function inside a template class and I don't think that's what you intended. Also, the search function refers to node::item , but the declaration of the node type has node::data . Finally, BST::display is written to be a void function taking a node in a way that it could be declared static but your usage is as if you expect it to work like a member function. It doesn't return anything so it certainly can't be passed to iostream::operator<<. Better would be to have display take the iostream as input and then call it as either root->display(cout) or display(cout, root) depending on whether you want it to be a member function or a static function.

you have a couple of concept errors, the display method returns void so you cannot pass it to cout that expect something to show. So, the easy change for you will be to add a new method called display_tree like this

void display_tree()
{
    display(root);
}

and in your main only call the method

s.display_tree();

not like this

cout << s.display << std::endl; //this is wrong cause this is not even calling a method

another option is to override the << operator, but editing your diplay method like this

template<typename T> void display(node *p, std::ostream& os)
{
    if (p != NULL)
    {
        if (p->left) display(p->left);
        os << " " << p->data << " ";
        if (p->right) display(p->right);
    }       

}

and then outside your class

template<typename T> friend std::ostream& operator<<(std::ostream& os, const BST<T>& obj)
{
    obj.display(obj.root);
}

and call it in your programa like this

cout << s << std::endl;

in order to this to work, the T needs to have the operator << overloaded (which is the case of the string in your case)

try with this instead -- new version compiling

void display(node *p, std::ostream& os) const
{
    if (p != NULL)
    {
        if (p->left) display(p->left,os);
        os << " " << p->data << " ";
        if (p->right) display(p->right,os);
    }
}

template<typename T> friend std::ostream& operator<<(std::ostream& os, const BST<T>& obj)
{
    obj.display(obj.root,os);
    return os;
}

Here is your code compiling and fixed the search

#include "stdafx.h"
#include <string> 
#include <iostream> 
#include <fstream> 
using namespace std;

template <typename T>
class BST{
private:
    struct node {
        T data;
        struct node* left;
        struct node* right;
    };
    node* root;

    template<typename T> bool search(node* p, T d)
    {
        if (!p) {
            return false;
        }
        if (d == p->data) {
            return true;
        }
        else if (d < p->data) {
            return search(root->left, d);
        }
        else {
            return search(root->right, d);
        }
    }

public:
    BST()
    {
        root = NULL;
    }
    bool isEmpty() const { return root == NULL; }
    ~BST();

    template <typename T>
    void insert(T d)
    {
        node* t = new node;
        node* parent;
        t->data = d;
        t->left = NULL;
        t->right = NULL;
        parent = NULL;

        if (isEmpty()) root = t;
        else {
            node* current;
            current = root;
            while (current)
            {
                parent = current;
                if (t->data > current->data) current = current->right;
                else current = current->left;
            }
            if (t->data < parent->data)
                parent->left = t;
            else
                parent->right = t;
        }
    }

    template<typename T> bool search(T d)
    {
        if (root == NULL)
            return false;
        else if (d == root->data) {
            return true;
        }
        else if (d < root->data) {
            return search(root->left, d);
        }
        else {
            return search(root->right, d);
        }
    }




    void display(node *p, std::ostream& os) const
    {
        if (p != NULL)
        {
            if (p->left) display(p->left,os);
            os << " " << p->data << " ";
            if (p->right) display(p->right,os);
        }
    }

    template<typename T> friend std::ostream& operator<<(std::ostream& os, const BST<T>& obj)
    {
        obj.display(obj.root,os);
        return os;
    }


};

int main()
{
    BST<string> s;
    ifstream inFile;
    string word, tmp;
    string filename = "Independence.txt";


    ifstream fstr(filename.c_str());
    while (inFile >> word) {
        s.insert(word);
    }
    inFile.close();

    cout << s << std::endl;

    cout << "Search for: ";
    cin.ignore(1);
    cin >> tmp;
    s.search(tmp);


    return 0;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM