我有一個週一晚上到期的項目。該項目將實施一個紅黑樹,將獨立宣言寫入「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