2014-12-07 63 views
0

我有一个周一晚上到期的项目。该项目将实施一个红黑树,将独立宣言写入“Independence.txt”,并将这些字符串放入红黑树中。我试图将它作为一个二进制搜索树实现,然后将添加颜色和旋转,因为我已经有了代码。在字符串中读取二叉搜索树? C++

我面临的问题是,现在我不断收到以下错误:“错误C2660”'RBT :: Insert':函数不带1个参数“和”IntelliSense:非合适的转换函数from“std:字符串“到”RBT :: RBTNode *“存在”,然后IntelliSense:函数调用中的参数太少,指向行root.Insert(myString);

我还需要帮助,在文件中读入二进制搜索树。我认为我的insert方法是正确的,问题出在main方法。

感谢您的帮助,因为我卡住了。

#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

template<typename T> 
class RBT 
{ 
    struct RBTNode { 
     T data; 
     RBTNode* left; 
     RBTNode* right; 
    }; 

public: 
    RBT(); 
    ~RBT(); 
    void GetNewNode(RBTNode* root, T data); 
    void Insert(RBTNode* root, T data); 
    bool Search(); 
    void Display(); 

}; 


template <typename T> 
void RBT<T>::GetNewNode(RBTNode* root, T data) { 
    RBTNode* newNode = new RBTNode(); 
    newNode->data = data; 
    newNode->left = newNode->right = NULL; 
    return newNode; 
} 

template <typename T> 
void RBT<T>::Insert(RBTNode* root, T data) { 
    if (root == NULL) { 
     root = GetNewNode(data); 
    } 
    else if (data <= root->data) { 
     root->left = Insert(root->left, data); 
    } 
    else { 
     root->right = Insert(root->right, data); 
    } 
    return root; 
} 

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

template<typename T> 
void RBT<T>::Display() { 
    if (root->left != NULL) 
     display(root->left); 
    cout << root->left << endl; 

    if (root->right != NULL) 
     Display(root->right); 
    cout << root->right << endl; 
} 



int main() 
{ 
    RBT<string> root; 
    string myString; 
    ifstream infile; 
    infile.open("Independence.txt"); 
    while (infile) 
    { 
     infile >> myString; 
     root.Insert(myString); 
    } 

    cin.ignore(); 
    return 0; 
} 
+0

RBTNode * newNode = new BstNode(); – schwer 2014-12-07 15:29:57

回答

2

您的Insert方法需要2个参数(插入的根和要插入的数据);在main中,您只用1(数据)调用它。

+0

我会从main调用什么方法调用? RBTNode * root还是只是root?我已经尝试了两种错误消息“错误C2664:'void RBT :: Insert(RBT :: RBTNode *,T)':无法将参数1从'RBT '转换为'RBT :: RBTNode * ' – user2288502 2014-12-07 16:01:52

+0

'RBTNode'对于你的类是内部的,所以它听起来像你需要一个额外版本的'Insert',它可以从类外调用,然后可以调用你当前的'Insert'和相应的'RBTNode ' – 2014-12-07 16:24:07

+0

现在我得到了“错误C224:'RBT ::插入':无法匹配函数定义到现有的声明”任何想法?我试图研究这个问题,看看它给了我什么 – user2288502 2014-12-07 17:59:47