我有一个周一晚上到期的项目。该项目将实施一个红黑树,将独立宣言写入“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;
}
RBTNode * newNode = new BstNode(); – schwer 2014-12-07 15:29:57