【问题标题】:Binary Search Tree Display issue二叉搜索树显示问题
【发布时间】:2023-03-14 02:00:01
【问题描述】:

我有一个实验室任务,我在过去几周一直在努力,我陷入困境并且迫切需要帮助,因为这将是最终项目的 50%。 任务是在 C++ 中创建一个二叉搜索树。我们必须将独立宣言中的文字读入二叉搜索树。我的搜索和插入方法“正常工作”,这意味着它们不会引发任何错误。我遇到的问题是显示 BST 以确定一切是否正常。 display 方法应该被重载的操作符 >> 调用来显示树。 我不断收到的错误是:

“错误 C3867: 'BST::display': 缺少函数调用 参数列表;使用 '&BST::display' 创建指向 会员”

另一个是

"error C2679: binary '

上次我重建程序时,它显示“ItelliSense:指向绑定函数的指针只能用于调用该函数。”

#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;
};

【问题讨论】:

    标签: c++ templates binary-search-tree


    【解决方案1】:
    template<typename T>
    void display(node *p)
    

    这需要一个带有节点指针的参数。

    然后你稍后调用它:

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

    但是你实际上并没有在这里传递任何参数。然后编译器抱怨它无法弄清楚如何调用该函数。因为该函数的返回类型为void,所以它无法弄清楚如何打印它,因为您实际上并没有返回任何东西供cout 打印。在你继续之前,你会想要解决这两个问题,因为你说这是一项任务,我会让你弄清楚如何做到这一点:)。

    【讨论】:

      【解决方案2】:

      您的代码似乎存在许多问题。我怀疑 displaysearch 不应该单独模板化 - 这实际上是模板类中的模板成员函数,我认为这不是你想要的。另外,搜索函数引用node::item,但节点类型的声明有node::data。最后,BST::display 被写成一个 void 函数,它以可以声明为静态的方式获取节点,但您的用法就像您希望它像成员函数一样工作。它不返回任何东西,所以它当然不能传递给 iostream::operatoriostream 作为输入,然后根据您希望它是成员函数还是静态函数,将其调用为 root-&gt;display(cout)display(cout, root)

      【讨论】:

        【解决方案3】:

        你有几个概念错误, display 方法返回 void 所以你不能将它传递给 cout 期望显示的东西。 因此,对您来说最简单的更改是添加一个名为 display_tree 的新方法,如下所示

        void display_tree()
        {
            display(root);
        }
        

        并且在你的 main 中只调用方法

        s.display_tree();
        

        不是这样的

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

        另一种选择是覆盖

        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(obj.root);
        }
        

        并像这样在你的程序中调用它

        cout << s << std::endl;
        

        为了使其工作,T 需要让运算符

        试试这个——新版本编译

        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;
        }
        

        【讨论】:

        • dariogriffo,我尝试按照您的建议进行双向操作,但我无法让任何一个工作。当我使用覆盖选项时,我不断收到“错误 C2783: 'void BST<:string>::display(BST<:string>::node*,std::ostream &)' :可以不推断“T”的模板参数。此外,当我尝试将朋友部分放在课堂之外时,它会抛出一个错误,说朋友不能在课堂外。有什么建议吗?我几乎尝试了所有东西,但不能把这件事弄清楚,明天晚上就到期了。
        • 你能用朋友选项的代码更新问题吗?
        • 更新了我的答案,检查底部。您还有其他关于搜索功能的编译问题
        【解决方案4】:

        这是您的代码编译并修复了搜索

        #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;
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-18
          • 2016-02-03
          • 1970-01-01
          • 1970-01-01
          • 2012-08-04
          • 1970-01-01
          相关资源
          最近更新 更多