Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 二叉搜索树显示问题_C++_Templates_Binary Search Tree - Fatal编程技术网

C++ 二叉搜索树显示问题

C++ 二叉搜索树显示问题,c++,templates,binary-search-tree,C++,Templates,Binary Search Tree,我有一个在过去几周里一直在做的实验任务,我陷入困境,急需帮助,因为这将是最终项目的50%。 任务是在C++中创建二叉搜索树。我们必须把《独立宣言》中的文字读入二叉搜索树。我的搜索和插入方法“工作正常”,这意味着它们不会抛出任何错误。我遇到的问题是显示BST以确定是否一切正常。重载运算符>>应调用display方法来显示树。 我经常遇到的错误有: 错误C3867:'BST::display':缺少函数调用 参数列表;使用“&BST::display”创建指向的指针 成员“ 另一个是 错误C2679

我有一个在过去几周里一直在做的实验任务,我陷入困境,急需帮助,因为这将是最终项目的50%。 任务是在C++中创建二叉搜索树。我们必须把《独立宣言》中的文字读入二叉搜索树。我的搜索和插入方法“工作正常”,这意味着它们不会抛出任何错误。我遇到的问题是显示BST以确定是否一切正常。重载运算符>>应调用display方法来显示树。 我经常遇到的错误有:

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

另一个是

错误C2679:二进制'
模板
无效显示(节点*p)
这将获取带有节点指针的参数

然后,您稍后将其命名为:

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

cout您的代码似乎有很多问题。我怀疑
display
search
不应该单独模板化-这实际上是模板类中的模板成员函数,我认为这不是您想要的。此外,搜索函数引用
node::item
,但声明函数节点类型的函数具有
node::data
。最后,
BST::display
被写入一个void函数,它以一种可以声明为静态的方式接收节点,但您的使用就好像您期望它像一个成员函数一样工作。它不返回任何内容,因此肯定不能传递给iostream::operator您有两个c概念错误, display方法返回void,因此不能将其传递给希望显示内容的cout。 因此,对您来说最简单的更改是添加一个名为display_tree的新方法,如下所示

void display_tree()
{
    display(root);
}
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);
    }       

}
cout << s << std::endl;
而在你的主程序中,只调用这个方法

s.display_tree();
不是这样的

void display_tree()
{
    display(root);
}
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);
    }       

}
cout << s << std::endl;

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

#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;
};
#包括“stdafx.h”
#包括
#包括
#包括
使用名称空间std;
模板
BST级{
私人:
结构节点{
T数据;
结构节点*左;
结构节点*右;
};
节点*根;
模板布尔搜索(节点*p,td)
{
如果(!p){
返回false;
}
如果(d==p->数据){
返回true;
}
否则如果(d数据){
返回搜索(根->左,d);
}
否则{
返回搜索(根->右,d);
}
}
公众:
BST()
{
root=NULL;
}
bool isEmpty()常量{return root==NULL;}
~BST();
模板
空白插入(T d)
{
node*t=新节点;
节点*父节点;
t->data=d;
t->left=NULL;
t->right=NULL;
parent=NULL;
如果(isEmpty())root=t;
否则{
节点*电流;
电流=根;
while(当前)
{
父项=当前;
如果(t->data>current->data)current=current->right;
否则当前=当前->左侧;
}
if(t->datadata)
父->左=t;
其他的
父->右=t;
}
}
模板布尔搜索(TD)
{
if(root==NULL)
返回false;
else if(d==根->数据){
返回true;
}
else if(ddata){
返回搜索(根->左,d);
}
否则{
返回搜索(根->右,d);
}
}
无效显示(节点*p,标准::ostream&os)常数
{
如果(p!=NULL)
{
如果(p->left)显示(p->left,操作系统);
操作系统对,操作系统);
}
}
模板friend std::ostream和运算符(word){
s、 插入(字);
}
infle.close();

dariogriffo,我试着按照你的建议两个方向都做了,但两个方向都不行。当我使用覆盖选项时,我一直得到“错误C2783:'void BST::display(BST::node*,std::ostream&”)“:无法推断“T”的模板参数。此外,当我尝试将friend部分放在类外时,它会抛出一个错误,表示friend不能在类外。有什么建议吗?我已经尝试了几乎所有的方法,但无法解决此问题,它将于明晚到期。你能用friend opt的代码更新de question吗ion?更新了我的答案,请检查底部。另外,您在搜索功能的编译方面还有其他问题