2014-12-07 28 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