0

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. 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);

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;
}
1
  • RBTNode* newNode = new BstNode(); Commented Dec 7, 2014 at 15:29

1 Answer 1

2

Your Insert method takes 2 arguments (the root to insert into and the data to be inserted); in main, you are calling it with just 1 (the data).

Sign up to request clarification or add additional context in comments.

7 Comments

What would I put into the method call from main? RBTNode* root or just root? I've tried both and getting the error message "error C2664: 'void RBT<std::string>::Insert (RBT<std::string>::RBTNode*,T)' : cannot convert argument 1 from 'RBT<std::string>' to 'RBT<std::string>::RBTNode *'
RBTNode is internal to your class, so it sounds like you need an extra version of Insert which can be called from outside the class which can then, in turn, call your current Insert with the appropriate RBTNode.
Now I am getting "error C224: 'RBT <T>::Insert': unable to match function definition to an existing declaration" Any ideas? I am trying to research that problem and see what it gives me
Given that you have supplied no new code to comment on: no new ideas.
I updated the code up top. I changed the issue that schwer pointed out and then made changes with the Insert function and under the public: area in class RBT.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.