简体   繁体   English

二进制搜索树以字符串形式读取? C ++

[英]Binary Search Tree reading in Strings? C++

I have a project that is due Monday night. 我有一个计划在星期一晚上提交。 The project is to implement a Red Black Tree that reads in the declaration of Independence as "Independence.txt" and puts those strings into a red black tree. 该项目将实现一个Red Black Tree,该树在Independence声明中读取为“ Independence.txt”,并将这些字符串放入Red Black树中。 I am trying to implement it as a Binary Search Tree first and then will add the colors and the rotations as I already have that code worked out. 我试图首先将其实现为二进制搜索树,然后将颜色和旋转数添加进去,因为我已经将该代码计算出来了。

The problem I am facing is that right now I keep getting the following errors: "error C2660" 'RBT ::Insert': function does not take 1 arguments" and "IntelliSense: non suitable conversion function from "std:string" to "RBT::RBTNode *" exists" and then IntelliSense: too few arguments in function call which points to the line root.Insert(myString); 我面临的问题是,现在我不断收到以下错误:“错误C2660”'RBT :: Insert':函数不接受1个参数”和“ IntelliSense:从“ std:string”到“ RBT :: RBTNode *“存在”,然后是IntelliSense:函数调用中指向行根的参数太少。Insert(myString);

I also need help with reading in the file into the binary search tree. 在将文件读入二进制搜索树中时,我也需要帮助。 I do think my insert method is correct, and the problem lies in the main method. 我确实认为我的插入方法是正确的,而问题出在主要方法上。

Thanks for any help as I am stuck. 感谢您的帮助,因为我遇到了麻烦。

#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;
}

Your Insert method takes 2 arguments (the root to insert into and the data to be inserted); 您的Insert方法带有2个参数(要插入的根和要插入的数据); in main , you are calling it with just 1 (the data). main ,您仅用1(数据)来调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM